Wednesday, July 18, 2018


Correlation - Statistical Analysis!

The most important step in computer vision or machine learning is to understand data well and use that knowledge to make the best design choice.

The open question is
How to understand data well?

The answer is by applying statistical techniques...

Hence the red theme of this tutorial is to understand most important statistical technique i.e Correlation.

The word correlation is used in everyday life to denote some form of association. It is a statistical technique that can show whether and how strongly pairs of variables are related. We might say that we have noticed correlation between student attendance and marks obtained. However, in statistical terms we use correlation to denote association between two quantitative variables. When one variable increases as the other increases the correlation is positive; when one decreases as the other increases it is negative. Fig. 1 depicts the positive, negative and no correlation.

                                              Fig.1 Types of Correlation (Source:Wikipedia)


What is the significance of correlation?

In machine learning before applying any classifier we must find out correlation of intra-intent and inter-intent patterns. The correlation of intra-intent patterns is obviously higher than inter-intent patterns. For example patterns of the same class have more association binding than patterns of different classes. It is good practice to decide hypothesis of your problem statement w.r.t. correlation and check whether really the problem is of pattern classification.


In this tutorial you will learn about
1. Correlation Coefficient
2. Pearson vs. Spearman correlation technique
3. Views from Applied Perspective

1. Correlation coefficient

The degree of association is measured by correlation coefficient. A correlation coefficient is a way to put a value to the relationship. Correlation coefficients have a value of between -1 and 1. A “0” means there is no relationship between the variables at all, while -1 or 1 means that there is a perfect negative or positive correlation. The Table 1 describes the strength of relationship.

                                                Table 1. Strength of Relationship
 

How to get this r value..

There are different correlation techniques to get this r

Let’s start with interesting stuff...

2. Pearson vs. Spearman correlation technique

Pearson correlation is parametric whilst Spearman correlation is nonparametric test.

First understand the difference in parametric Vs. Nonparametric test.



Pearson r correlation: Pearson r correlation is the most widely used correlation statistic to measure the degree of the relationship between linearly related variables. For example, in the stock market, if we want to measure how two stocks are related to each other, Pearson r correlation is used to measure the degree of relationship between the two. The following formula is used to calculate the Pearson r correlation:


r = Pearson r correlation coefficient
N = number of observations
∑xy = sum of the products of paired scores
∑x = sum of x scores
∑y = sum of y scores
∑x2= sum of squared x scores
∑y2= sum of squared y scores

Key Points :
1. In general, when the data is normally distributed we are using Pearson correlation. The normal distribution is always symmetrical about mean which looks like bell curve.

2. Linearity is not the assumption of Pearson correlation. Pearson correlation determines the degree to which relation is linear. The relation is linear if variables increase or decrease at constant speed.

Python script to compute Pearson correlation coefficient

>>> import matplotlib.pyplot as plt
>>> from scipy import stats
>>> np.random.seed(12345678)
>>> x = np.random.random(10)
>>> y = np.random.random(10)
>>> slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
where,
slope- It is the slope of the regression line.
intercept- It is the intercept of the regression line.
r-value – It is the pearson correlation value. The r value is in between -1 to 1.
P-value- The P-value is a critical value depends on the probability you are allowing for a Type-I error. It is also called as hypothesis test since it tells whether to accept or reject Null hypothesis. (Null hypothesis is a hypothesis that says there is no statistical significance between two variables. It is a hypothesis a resercher will try to disprove)
In general if p<0.05(critical value) reject null hypothesis else accept it.
Std_err- it is the standard error of the estimate.
More explanation about r-value and p-value
r-value tells about the variation within data.
P-value tells about significance of model(i.e model fits the data well)
Let’s understand the Four possibilities:
1. r-value(low) and p-value(low) – Model doesn’t explain much about variation, but is significant. (Better than nothing)
2. r-value(low) and p-value(high) – Model doesn’t explain much about variation and not significant (Worst model)
3. r-value(high) and p-value(low) – Model tells much about variation and significant. (Best model)
4.  r-value(high) and p-value(high) – Model explains well about variation but not significant. (Worthless)

Spearman rank correlation: Spearman rank correlation is a non-parametric test that is used to measure the degree of association between two variables. The Spearman rank correlation test does not carry any assumptions about the distribution of the data and is the appropriate correlation analysis when the variables are measured on a scale that is at least ordinal.
The following formula is used to calculate the Spearman rank correlation:
ρ= Spearman rank correlation
di= the difference between the ranks of corresponding variables
n= number of observations
Key Points :
1. In Spearman Rank correlation information loss is there since it is working on ranks.

2. In general, for monotonic relationship between variables Spearman rank correlation is used.  In Monotonic relation the variables tend to move in the same direction but not at constant speed.

3. If the data has outliers i.e few values are far away from others use Spearman rank correlation coefficient.


Python script to compute Spearman Rank correlation coefficient.

>>> from scipy import stats
>>> np.random.seed(12345678)
>>> x = np.random.random(10)
>>> y = np.random.random(10)
>>> scipy.stats.stats.spearmanr(x1,y1)

3. Views from Applied Perspective

Here are few suggestions from applied perspective:
1. Before taking decision whether to apply Pearson or Spearman rank correlation it is  good practice to look at the scatterplot
>>> python script to plot scatterplot
>>> import numpy as np
>>> import matplotlib.pyplot as plt
# Fixing random state for reproducibility
>>> np.random.seed(19680801)
>>> N = 50
>>> x = np.random.rand(N)
>>> y = np.random.rand(N)
>>> colors = np.random.rand(N)
>>> area = (30 * np.random.rand(N))**2  # 0 to 15 point radii
>>> plt.scatter(x, y, s=area, c=colors, alpha=0.5)
>>> plt.show()
2. For small sample I will advise to use Spearman rank correlation.
3. For the large sample use Pearson correlation.

Last One
I prefer Pearson correlation coefficient because
1. Pearson correlation is having more statistical power.
2. Pearson correlation enables more direct compatibility of finding across studies, because most of the studies report Pearson correlation.
3, In many cases there is minimal difference between Pearson and Spearman correlation coefficient.
4. Obviously it aligns with my theoretical interests.

Go Further!
I hope you enjoyed this post. The tutorial is good to start statistical analysis using Correlation. The post is very informative not only to get the knowledge of Pearson and Spearman rank correlation but also from the applicability perspective. Good Luck!

Worth reading!
Are you interested in Deep Learning- Convolutional Neural Network!
1. Document Classification using Deep Learning- Click here
2. Improving Performance of Convolutional Neural Network!  Click here












Thursday, July 5, 2018

Improving Performance of Convolutional Neural Network!

Convolutional Neural Network – a pillar algorithm of deep learning -- has been one of the most influential innovations in the field of computer vision. They have performed a lot better than traditional computer vision algorithms. These neural networks have proven to be successful in many different real-life case studies and applications, like:
·     Image classification, object detection, segmentation, face recognition; 
·     Classification of crystal structure using a convolutional neural network;
·     Self driving cars that leverage CNN based vision systems;
·     And many more, of course!
Lot of articles are available on how to build Convolution Neural Network. Hence, I am not going in detail regarding implementation of CNN. If you are interested in Document Classification using CNN please click here
The red theme of this tutorial is to know how to improve performance of CNN?
Let’s start ...
The common question is:
How can I get better performance from deep learning model?
It might be asked as:
How can I improve accuracy?
Oh God! My CNN is performing poor..
Don’t be stressed..
Here is the tutorial ..It will give you certain ideas to lift the performance of CNN.
The list is divided into 4 topics
1. Tune Parameters
2. Image Data Augmentation
3. Deeper Network Topology
4. Handel Overfitting and Underfitting problem

Oh! Cool.. Let’s start with explanation
1. Tune Parameters
To improve CNN model performance, we can tune parameters like epochs, learning rate etc..  Number of epochs definitely affect the performance. For large number of epochs , there is improvement in performance. But need to do certain experimentation for deciding epochs, learning rate.  We can see after certain epochs there is not any reduction is training loss and improvement in training accuracy. Accordingly we can decide number of epochs. Also we can use dropout layer in the CNN model. As per the application, need to decide proper optimizer during compilation of model. We can use various optimizer e.g SGD,rmsprop etc. There is need to tune model with various optimizers . All these things affect the performance of CNN.

2. Image Data Augmentation
"Deep learning is only relevant when you have a huge amount of data". It’s not wrong. CNN requires the ability to learn features automatically from the data, which is generally only possible when lots of training data is available.
If we have less training data available.. what to do?
Solution is here.. use Image Augmentation
Image augmentation parameters that are generally used to increase the data sample count are zoom, shear, rotation, preprocessing function and so on. Usage of these parameters results in generation of images having these attributes during training of Deep Learning model. Image samples generated using image augmentation, in general existing data samples increased by the rate of nearly 3x to 4x times.


Fig.1 Data Augmentation (source: wikipedia)

One more advantage of data augmentation is as we know CNN is not rotation invariant, using augmentation we can add the images in the dataset by considering rotation. Definitely it will increase the accuracy of system.

3. Deeper Network Topology
Now let’s start to talk on wide network vs deep network!

A wide neural network is possible to train with every possible input value.  Hence, these networks are very good at good at memorization, but not so good at generalization.  There are, however, a few difficulties with using an extremely wide, shallow network. Though, wide neural network is able to accept every possible input value, in the practical application we won’t have every possible value for training.
Deeper networks capture the natural “hierarchy” that is present everywhere in nature. See a convnet for example, it captures low level features in first layer, a little better but still low level features in the next layer and at higher layers object parts and simple structures are captured. The advantage of multiple layers is that they can learn features at various levels of abstraction.
So that explains why you might use a deep network rather than a very wide but shallow network.
But why not a very deep, very wide network?
The answer is we want our network to be as small as possible to produce good results. The wider network will take longer time to train. Deep networks are very computationally expensive to train. Hence, make them wide and deep enough that they work well, but no wider and deeper.

4. Handel Overfitting and Underfitting problem
In order to talk on overfitting and underfitting, let’s start with simple concept e.g Model. What is model? It is a system which maps input to output. e.g we can generate a model of image classification which takes test input image and predicts class label for it. It’s interesting!
To generate a model we divide dataset into training and testing set. We train our model with classifier e.g CNN on training set . Then we can use trained model for predicting output of test data.
Now what is Overfitting and Underfitting?
Overfitting refers to a model that models the training data too well. What is the meaning of it. Lets simplify... In the overfitting your model gives very nice accuracy on trained data but very less accuracy on test data. The meaning of this is overfitting model is having good memorization ability but less generalization ability. Our model doesn’t generalize well from our training data to unseen data.

Underfiiting refers to a model which works well on the testing data. Its very dangerous..isn’t it? Model is having good accuracy on test data, but less accuracy on training data.
In the technical terms a model that overfits has low bias and high variance. A model that underfits has high bias and less variance. In any modeling, there will always be a tradeoff between bias and variance and when we build models, we try to achieve the best balance.
Now what is bias and variance?
Bias is error w.r.t training set. Variance is how much a model changes in response to the training data. The meaning of variance is model doesn’t give good accuracy on test data.


Fig.2 Underfitting Vs. Overfitting  (Source: Wikipedia)

How to Prevent Underfitting and Overfitting?
Let’s start with Underfitting:
The Example of underfiiting is your model is giving 50% accuracy on train data and 80% accuracy on test data?
Its the worst problem..
Why it occurs?
The answer is Underfitting occurs when a model is too simple – informed by too few features or regularized too much – which makes it inflexible in learning from the dataset.
Solution...
I would suggest if there is underfitting, focus on the level of deepness of the model. You may need to add layers.. as it will give you more detailed features. As we discussed above you need to tune parameters to avoid Underfitting.

Overfitting:
The Example of overfiiting is your model is giving 99% accuracy on train data and 60% accuracy on test data?
Overfitting is a common problem in machine learning..
There are certain solutions to avoid overfitting
1. Train with more data
2. Early stopping:
3. Cross validation
let’s start to discuss
1.Train with more data:
Train with more data helps to increase accuracy of mode. Large training data may avoid the overfitting problem. In CNN we can use data augmentation to increase the size of training set.
2. Early stopping:
System is getting trained with number of iterations. Model is improved through each new iteration .. But wait.. after certain number of iterations model starts to overfit the training data. Hence, the model’s generalization ability can be weaken. So do the Early stopping. Early stopping refers stopping the training process before the learner passes that point.


Fig.3 Early Stopping (Source: Wikipedia)

3. Cross validation:
Cross validation is a nice technique to avoid overfitting problem.
What is cross validation?
Let’s start with k-fold cross validation. (where k is any integer number)
Partition the original training data set into k equal subsets. Each subset is called a fold. Let the folds be named as f1, f2, …, fk .
·     For i = 1 to i = k
·     Keep the fold fi as Validation set and keep all the remaining k-1 folds in the Cross validation training set.
·     Train your machine learning model using the cross validation training set and calculate the accuracy of your model by validating the predicted results against the validation set.
·     Estimate the accuracy of your machine learning model by averaging the accuracies derived in all the k cases of cross validation.


Fig.4 5-fold Cross Validation(Source: Wikipedia)

Fig. 4 describes 5-fold cross validation, where training dataset is divided into 5 equal sub-datsets. There are 5 iterations. In each iteration 4 sub-datasets are used for training whilst one sub-dataset is used for testing.
Cross-validation is definitely helpful to reduce overfitting problem.


Go Further!

I hope you enjoyed this post. The tutorial is good to understand how we can improve performance of CNN model..While these concepts may feel overwhelming at first, they will ‘click into place’ once you start seeing them in the context of real-world code and problems. If you are able to follow the things in the post easily or even with little more efforts, well done! Try doing some experiments ... Good Luck!


Wednesday, June 27, 2018


Document Classification Using Deep Learning

Textual Document classification is a challenging problem. In this tutorial you will learn document classification using Deep learning (Convolutional Neural Network).

Dataset-Tobacco3482 dataset.

You can download the dataset using following link.

Dataset Description:
Tobacco3482 dataset consists of total 3482 images of 10 different document classes namely, Memo, News, Note, Report, Resume, Scientific, Advertisement, Email, Form, Letter. The dataset is having two directories i.e Tobacco3482_1 and  Tobacco3482_2.

Tobacco3482_1 directory consists images of 6 document classes i.e Memo, News, Note, Report, Resume, Scientific.

Tobacco3482_2 directory consists images of 4 document classes i.e Advertisement, Email, Form, Letter.

Here are some Examples:
  



In Recent years Convolutional Neural Network enjoyed great success for Image Classification., There exist large domain differences between natural images and document images. For example, in natural image , the object of interest can appear in any region of the image. In contrast, many document images are 2D entities that occupy the whole image. So question arises whether the same architecture of  CNN is also optimal for document images. The answer is big ‘YES’.  Thanks to the beauty of CNN we can use it for natural image classification as well as document image classification.
          For the Experimentation the Tobacco3482 dataset is used. Experiments are carried out with python 2.7 on Ubuntu operating system. The following procedure need to follow for the successful implementation.

1. Import the necessary libraries:

# Import libraries
import os,cv2
from keras import backend as K
K.set_image_dim_ordering('tf')

from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.optimizers import RMSprop


2.  Image Preprocessing: 

We can use cv2.resize( ) function , since CNN is taking the input image of fixed size . So resize the images which we are using for experimentation.

input_img_resize=cv2.resize(input_img,(299,299))

3. One-hot encoding:

In one-hot encoding, we  convert the categorical data into a vector of numbers. The reason why you convert the categorical data in one hot encoding is that machine learning algorithms cannot work with categorical data directly. You generate one boolean column for each category or class. Only one of these columns could take on the value 1 for each sample. Hence, the term one-hot encoding.

For Our problem statement, the one hot encoding will be a row vector, and for each document image, it will have a dimension of 1 x 10 as there are 10 classes. The important thing to note here is that the vector consists of all zeros except for the class that it represents, and for that, it is 1. For example, the image having label of 2, the one hot encoding vector would be [0 1 0 0 0 0 0 0 0 0].

So let's convert the training and testing labels into one-hot encoding vectors: 

# convert class labels to one-hot encoding
Y = np_utils.to_categorical(labels, num_classes)

4. Train-Test-Split

We can divide the dataset for training and testing purpose using train_test_split( ) function.

X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=2)

5. Building CNN Model: 

Oh! Good...Now actual story starts. I used Keras CNN using TensorFlow platform for the training purpose. First build the model, compile it and fit it on training data.

# CNN Model                                            
model = Sequential()

model.add(Conv2D(32,(3,3),padding='same',input_shape=(299,299,1)))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
#model.add(Convolution2D(64, 3, 3))
#model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.5))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))

#sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
#model.compile(loss='categorical_crossentropy', optimizer=sgd,metrics=["accuracy"])
model.compile(loss='categorical_crossentropy', optimizer='rmsprop',metrics=["accuracy"])

# Viewing model_configuration

model.summary()

# Fit the model
model.fit(X_train, y_train, batch_size=16, nb_epoch=num_epoch, verbose=1, validation_data=(X_test, y_test))


6.Evaluate CNN Model:  

Once the model is trained we can evaluate it on Test data.

# Evaluating the model 
score = model.evaluate(X_test, y_test, verbose=0)
print('Test Loss:', score[0])
print('Test accuracy:', score[1])

Congratualtions! You will get quite good results.


7. Classification Report and Confusion Matrix:

from sklearn.metrics import classification_report,confusion_matrix
Y_pred = model.predict(X_test)
y_pred = np.argmax(Y_pred, axis=1)
target_names = ['class 0(Note)', 'class 1(Scientific)','class 2(Report)','class 3(Resume)','class 4(News)','class 5(Memo),'class 6(Advertisement)', 'class 7(Email)','class 8(Form)','class 9(Letter)']
                                               
print(classification_report(np.argmax(y_test,axis=1), y_pred,target_names=target_names))

print(confusion_matrix(np.argmax(y_test,axis=1), y_pred))


8. Save Model: 

We can save the weights of trained model .

# Saving and loading model and weights
from keras.models import model_from_json
from keras.models import load_model

# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")


9.Load Model:

In the future if you want to test using weights of trained model which we already save e.g in model.h5

# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)

# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")

 # evaluate loaded model on test data
loaded_model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

# Read the test image using cv2.imread ( )  function
print loaded_model.predict(test_image)

Go Further!

I hope you enjoyed this post. The tutorial is good start to build convolutional neural networks in Python with Keras. The code in the tutorial helps to develop document classification system. If you are able to follow easily or even with little more efforts, well done! Try doing some experiments maybe with same model architecture but using different types of public datasets available. Good Luck!



Reference:
Jayant Kumar, Peng Ye and David Doermann. "Structural Similarity for Document Image Classification and Retrieval." Pattern Recognition Letters, November 2013.