How to deploy my code on raspberry pi as a standalone?

1 回表示 (過去 30 日間)
Ahmed abbasi
Ahmed abbasi 2020 年 3 月 28 日
コメント済み: amgad 2020 年 6 月 2 日
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)
  1 件のコメント
Ahmed abbasi
Ahmed abbasi 2020 年 3 月 28 日
This code basically uses CNN and detects whether the image is bottle or not using image processing.

サインインしてコメントする。

採用された回答

Walter Roberson
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 件のコメント
Ahmed abbasi
Ahmed abbasi 2020 年 3 月 28 日
Okay now i got your point. Training will be done in the Host computer and than deploy and ras pi for prediction. Thank you so much. Appreciated SIR.
amgad
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 ExchangeRecognition, Object Detection, and Semantic Segmentation についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by