outputFolder = fullfile('Caltech')
rootFolder = fullfile(outputFolder, '101_ObjectCategories')
categories = {'normal_NB_resized', 'slow_OB_resized'}
imds = imageDatastore(fullfile(rootFolder,categories), 'LabelSource','foldernames')
tbl = countEachLabel(imds)
minSetCount = min(tbl{:,2})
imds = splitEachLabel(imds, minSetCount, 'randomize')
normal = find(imds.Labels == 'normal', 1)
slow = find(imds.Labels == 'slow', 1)
numel(net.Layers(end).ClassNames);
[trainingSet, testSet] = splitEachLabel(imds, 0.7, 'randomize');
imageSize = net.Layers(1).InputSize;
augmentedTrainingSet = augmentedImageDatastore(imageSize,...
trainingSet, 'ColorPreprocessing', 'gray2rgb');
augmentedTestSet = augmentedImageDatastore(imageSize,...
testSet, 'ColorPreprocessing', 'gray2rgb');
w1 = net.Layers(2).Weights;
trainingFeatures = activations(net,...
augmentedTrainingSet, featureLayer, 'MiniBatchSize', 64, 'OutputAs', 'columns');
trainingLables = trainingSet.Labels;
classifier = fitcecoc(trainingFeatures, trainingLables,...
'Learner', 'Linear', 'Coding', 'onevsall', 'ObservationsIn', 'columns');
testFeatures = activations(net,...
augmentedTestSet, featureLayer, 'MiniBatchSize', 64, 'OutputAs', 'columns');
predictLabels = predict(classifier, testFeatures, 'ObservationsIn', 'columns');
testLables = testSet.Labels
confMat = confusionmat(testLables, predictLabels)
confMat = bsxfun(@rdivide, confMat, sum(confMat,2))
newImage = imread(fullfile('test.jpg'));
ds = augmentedImageDatastore(imageSize,...
newImage, 'ColorPreprocessing', 'gray2rgb');
imageFeatures = activations(net,...
ds, featureLayer, 'MiniBatchSize', 64, 'OutputAs', 'columns');
label = predict(classifier, imageFeatures, 'ObservationsIn', 'columns');
sprintf('loaded image belongs to %s class', label)
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'Shuffle','every-epoch', ...
'ValidationData',imds, ...
'ValidationFrequency',30, ...
'Plots','training-progress');
netT = trainNetwork(imds,layers,options);