MATLAB: How to deploy the code on raspberry pi as a standalone

image processing

vid = videoinput('winvideo', 1);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img)
h = findobj('type','figure');
n = length(h);
for k=1:n
baseFileName = sprintf('Img #%d.png', k);
fullFileName = fullfile('C:\Users\Cv\Desktop\image classification2 - copy',['img' '.bmp']);
imwrite(img, fullFileName);
end
outputFolder = fullfile('caltech102');
rootFolder = fullfile(outputFolder, '101_ObjectCategories');
categories = {'Bottles', 'NotBottles'};
imds = imageDatastore(fullfile(rootFolder,categories),'LabelSource', 'foldernames');
tb1 = countEachLabel(imds)
minSetCount = min(tb1{:,2})
imds = splitEachLabel(imds, minSetCount, 'randomize');
countEachLabel(imds);
Bottles = find(imds.Labels == 'Bottles', 1);
NotBottles = find(imds.Labels == 'NotBottles', 1);
% figure
% subplot(2,2,1);
% imshow(readimage(imds,airplanes));
% subplot(2,2,2);
% imshow(readimage(imds,ferry));
% subplot(2,2,3);
% imshow(readimage(imds,laptop));
net = resnet50();
figure
plot(net)
title('Architecture of ResNet-50');
set(gca, 'YLim', [150 170]);
net.Layers(1);
net.Layers(end);
numel(net.Layers(end).ClassNames);
[trainingSet, testSet] = splitEachLabel(imds, 0.3, 'randomize');
imageSize = net.Layers(1).InputSize;
augmentedTrainingSet = augmentedImageDatastore(imageSize, ...
trainingSet, 'ColorPreprocessing', 'gray2rgb');
augmentedTestSet = augmentedImageDatastore(imageSize, ...
testSet, 'ColorPreprocessing', 'gray2rgb');
w1 = net.Layers(2).Weights;
w1 = mat2gray(w1);
figure
montage(w1)
title('First Convolutional Layer Weight')
featureLayer = 'fc1000';
trainingFeatures = activations(net, ...
augmentedTrainingSet, featureLayer, 'MiniBatchSize', 32, 'OutputAs', 'columns');
trainingLables = trainingSet.Labels;
classifier = fitcecoc(trainingFeatures,trainingLables, ...
'Learner', 'Linear', 'Coding', 'onevsall','ObservationsIn', 'columns');
testFeatures = activations(net, ...
augmentedTestSet, featureLayer, 'MiniBatchSize', 32, 'OutputAs', 'columns');
predictLabels = predict(classifier, testFeatures, 'ObservationsIn','columns');
testLables = testSet.Labels;
confMat = confusionmat(testLables,predictLabels);
confMat = bsxfun(@rdivide, confMat, sum(confMat,2));
mean(diag(confMat));
newImage = imread(fullfile('img.bmp'));
ds = augmentedImageDatastore(imageSize, ...
newImage, 'ColorPreprocessing', 'gray2rgb');
imageFeatures = activations(net, ...
ds, featureLayer, 'MiniBatchSize', 32, 'OutputAs', 'columns');
Label = predict(classifier, imageFeatures, 'ObservationsIn','columns');
sprintf('The Loaded image belongs to %s class', Label)

Best Answer

It is not possible to deploy CNN training to hardware.
It is not possible to deploy augmentedImageDatastore to hardware.
Your strategy would have to be to train on the host, and save the net and activations, and load() it in the code that was deployed to hardware, where you would use it only to predict()
Related Question