Image Classification
Image Classification
Even though every digital image is stored in structured formats such as jpg, png, gif,
etc., it doesn't contain relevant information, which is of interest to human or computer
system. It can be converted into a structured form through image analysis.
Each class has 6,000 images. The classes in a CIFAR-10 dataset are mutually
exclusive.
At a glance:
Number of classes: 10
Size of image: 32 x 32 x 3
Note: In this course, we use only a subsetof the above dataset due to memory
constraints in online cloud platform. We will be explaining the generation of
subset in the upcoming cards
Subset Generation
As explained in dataset description, we use only a subset of CIFAR-10 dataset.
1. The dataset with 50,000 samples is split in the ratio 92:8. This split is done to
take a smaller portion of 50000 samples (i.e the 8% contains only 4000 images).
2. These 4000 samples are used for generating the train and test sets for
classification.
Here, StratifiedShuffleSplit is used to split the dataset. It splits the data by taking
equal number of samples from each class in a random manner.
#Splitting the whole training set into 92:8
seed=7
4000 samples are split in the ratio 7:3. (i.e., 2800 for training and 1200 for testing)
using StratifiedShuffleSplit.
test_data = test_data_30
test_labels = test_label_30
The quality of an image is greatly influenced by its clarity and the device used to
capture it.
The captured image may contain noise and irregularities, which can be removed
via preprocessing steps.
Normalization
Whitening
Denoising
Contrast Stretching
Background subtraction
Image Enhancement
Smoothing
In the following cards, we will describe some of the preprocessing techniques that can
be applied to images.
Normalization
Normalization is the process of converting the pixel intensity values to a normal state.
return data
train_data = normalize(train_data)
test_data = normalize(test_data)
ZCA Whitening
Normalization is followed by a ZCA whitening process.
The main aim of whitening is to reduce data redundancy, which means the features are
less correlated and have the same variance.
ZCA stands for zero-phase component analysis. ZCA whitened images resemble
the normal image.
test_data_pca = PCA(n_components=test_data_flat.shape[1]).fit_transform(test_data_fla
t)
train_data_pca = train_data_pca.T
test_data_pca = test_data_pca.T
The singular values (less in number) obtained from this refactoring process can
preserve useful features of the original image without utilizing high storage space
in the memory.
def svdFeatures(input_data):
svdArray_input_data=[]
size = input_data.shape[0]
img=color.rgb2gray(input_data[i])
U, s, V = np.linalg.svd(img, full_matrices=False);
svdArray_input_data.append(S)
svdMatrix_input_data=np.matrix(svdArray_input_data)
return svdMatrix_input_data
test_data_svd=svdFeatures(test_data)
Even the photographs of the same material will undergo scale change corresponding to
the distance from the material, focal length etc. This is one of the reasons for not
considering the raw pixel values as useful features for images.
The main aim of using SIFT for feature extraction is to obtain features that are not
sensitive to changes in scale, rotation, image resolution, illumination, etc.
Keypoint Localization
Orientation Assignment
Keypoint Descriptor
2. Train the classifier - All classifiers in scikit-learn utilizes a fit(X, y) method to fit
the model (training) for the given train data X and train label y.
3. Predict the target - Given an unlabeled observation X, the predict(X) returns the
predicted label y.
4. Evaluate the classifier model - The score(X,y) returns the score for the given test
data X and test label y.
Classification Algorithms
There are various algorithms to solve the classification problems.
Note: The explanation for the algorithms are given in the Machine Learning
Axioms course. Refer this for further details.
High-dimensional spaces.
predicted=clf.predict(test_data_flat_t)
print("score",score)
input layer
hidden layers
output layer
Cross Validation
Cross validation is considered as a model validation technique to evaluate the
performance of a model on unseen data.
Points to remember:
Cross validation gives high variance if the testing set and training set are not
drawn from the same population.
Allowing training data to be included in testing data will not give actual
performance results.
In cross validation, the number of samples used for training the model is reduced, and
the results depend upon the choice of the pair of training and testing sets.
You can refer to the various cross validation approaches from here.
The data is split into train set, validation set, and test set.
o Validation Set: The data used to tune the classifier model parameters i.e.,
to understand how well the model has been trained (as part of training
data).
o Testing Set: The data used to evaluate the performance of the classifier
(unseen data by the classifier).
This will help us to know the efficiency of our model.
Since the online platform used in this course doesn't support huge dataset, only a
few samples are taken for training and testing.
Confusion Matrix
In the table,
TP (True Positive) - The number of correct predictions that the occurrence is
positive
It visually depicts the performance in a tabular form that has two dimensions
namely, actual and predicted sets of data.
The rows and columns of the table show the count of false positives, false
negatives, true positives, and true negatives.
The first parameter shows true values and the second parameter shows predicted
values.
conf_matrix=metrics.confusion_matrix(test_labels,predicted)
print("Confusion matrix:",conf_matrix)
In the above code, test_labels are the actual labels and predicted are the predicted
labels.
Here, the diagonal elements of the confusion matrix shows the number of
correctly classified labels.
Classification Accuracy
Classification accuracy is defined as the percentage of correct predictions.
accuracy=[]
for i in range(leng):
ac=(conf_matrix[i,i]/((conf_matrix[i].sum())+.0000001))*100
accuracy.append(ac)
print accuracy
summation=0
no_of_classes = 10
for i in range(0,len(accuracy)):
summation+=accuracy[i]
overall_accuracy = summation/no_of_classes
print overall_accuracy
Classification where each data is mapped to more than one class is called
____________.
Binary
Choose the correct sequence for classifier building from the following:
initia – train- predict- evaluate
Select the correct option that directly achieves multi-class classification
(without support of binary classifiers).
K nearest
Which algorithm can be used for matching local regions in two images?
SIFT