How to deploy my code on raspberry pi as a standalone?
1 次查看(过去 30 天)
显示 更早的评论
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)
采纳的回答
Walter Roberson
2020-3-28
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()
4 个评论
amgad
2020-6-2
A small question:
how to deploy 'load' to Raspberry Pi in order to have the code run as standalone
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Recognition, Object Detection, and Semantic Segmentation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!