First CNN experiment (w. matlab)

matlab project in which is used trained ResNet50 to obtain a vector representation of the input (from Flower dataset of Tensorflow), from layer ‘fc1000’, that are classified using fitcecoc (“multiclass SVM”).
It’s an easy ‘project’ (also available online) very helpful to get into matlab deep learning world.

% download image data
url='http://download.tensorflow.org/example_images/flower_photos.tgz';
downloadFolder=tempdir;
filename=fullfile(downloadFolder, 'flower_dataset.tgz');

imageFolder=fullfile(downloadFolder, 'flower_photos');%to uncompress datas

if ~exist(imageFolder,'dir') % download only once
    disp('Downloading Flower Dataset (218 MB)...');
    websave(filename,url);
    untar(filename,downloadFolder)
end

% load images
imds=imageDatastore(imageFolder, 'LabelSource', 'foldernames', 'IncludeSubFolders', true);
daisy=find(imds.Labels=='daisy', 1);
% figure
% imshow(readimage(imds, daisy));

tbl=countEachLabel(imds)%summarize the # of images per cat
%       Label       Count
%     __________    _____
% 
%     daisy          633 
%     dandelion      898 
%     roses          641 
%     sunflowers     699 
%     tulips         799 
% % make images number balanced
% % soing some pruning

minSetCount=min(tbl{:,2});
% limit images number for sake of time
maxNumImages=1000;
minSetCount=min(maxNumImages, minSetCount);
imds=splitEachLabel(imds,minSetCount, 'randomize'); %to tirm the set
countEachLabel(imds) %ora imgs bilanciate

% load pretrained NETWORK (from matlab)
net=resnet50();
net.Layers(end) %per ispezione ultimo layer rete

numel(net.Layers(end).ClassNames) %classi di net
% % nel nostro caso la usiamo per op di classificazione di altre classi....
% %     non le sue native

% division in train and test set
% percentages are 30-70%
[trainingSet, testSet]=splitEachLabel(imds, 0.3, 'randomize'); %randomize per non avere ris statico(?)

% preprocess image for cnn

% % our network only manages rgb images 224x224. without saving 'em all in this format
% % we re gonna use augmentedimageDatastore for the resize and gray to rgb converison on the fly
% % possible to do also other augmentation technique if desired

imageSize=net.Layers(1).InputSize;
augmentedTrainSet=augmentedImageDatastore(imageSize, trainingSet, 'ColorPreprocessing', 'gray2rgb');
augmentedTestSet=augmentedImageDatastore(imageSize, testSet, 'ColorPreprocessing', 'gray2rgb');

% extract training features using cnn

w1=net.Layers(2).Weights;

w1=mat2gray(w1);
w1=imresize(w1,5);

% figure
% montage(w1) %it displays a montage of network weights
% title('first convolutional layer weights') 
% deep layers features available using :  deepDreamImage

% % see mathworks example.......

% extraction form deeper one
featureLayer='fc1000';
trainingFeatures=activations(net, augmentedTrainSet, featureLayer, ...
                        'MiniBatchSize', 32, 'OutputAs', 'columns');
% output is arranged in columns cause it will speed up the svm that follows

% Train a MultiClass SVM classifier using cnn features
trainingLabels=trainingSet.Labels;

% % we train fast svm classifier using a linear solver(SGD solver). it speeds up the training with high
% % dimensional cnn features vector

size(trainingFeatures)
size(trainingLabels)
classifier=fitcecoc(trainingFeatures, trainingLabels, ...
                'Learners', 'Linear', 'Coding', 'onevsall', 'ObservationsIn', 'columns');
            
% valutazione del classificatore
testFeatures=activations(net, augmentedTestSet, featureLayer, ...
                    'MiniBatchSize', 32, 'outputAs', 'columns');
predictedLabels=predict(classifier, testFeatures, 'ObservationsIn', 'columns');

testLabels=testSet.Labels;
confMat=confusionmat(testLabels, predictedLabels);
% converto valori in percentuale
confMat=bsxfun(@rdivide, confMat, sum(confMat,2))

mean(diag(confMat))

% use the classifier on the test set
cnt=0;
for i=1:1:numel(testSet.Labels)
    disp(i);
    testImage=readimage(testSet, i);
    testLabel=testSet.Labels(i);
    
    ds=augmentedImageDatastore(imageSize, testImage, 'ColorPreprocessing', 'gray2rgb');
    imgFeatures=activations(net, ds, featureLayer, 'OutputAs', 'columns');
    predictedLabel=predict(classifier, imgFeatures, 'ObservationsIn', 'columns');
    
    if(predictedLabel==testLabel)
        cnt=cnt+1;
    end
end
acc=cnt/numel(testSet.Labels);
disp(acc)

One Reply to “First CNN experiment (w. matlab)”

Comments are closed.