Save a trained neural network for Raspberry

9 次查看(过去 30 天)
I have loaded AlexNet and retrained the neural network using my own images. When I run the script it it does everything properly and trains the NN properly. The new trained NN is saved to the workspace as myNet as as a .mat file. I was wondering if it's possible to save it as an .M file so I can deploy it to a Raspberry PI 4.
clc;
clear all;
close all;
alex = alexnet; % Load Neural Network AlexNet
layers = alex.Layers; %Identify Layers in Alexnet
layers(23) = fullyConnectedLayer(6); %Six Unique Images will be identified at Layer 23
layers(25) = classificationLayer %Layer 25 is the Classification Layer and properly identifies the object
allImages = imageDatastore('robot','IncludeSubfolders', true, 'LabelSource', 'foldernames'); % Identifies the images that are stored are the computer and the labels associated with them
[trainingImages, testImages] = splitEachLabel(allImages, 0.8, 'randomized'); % seperates the files into different groups for training and validation. (80% for training and 20% for validation)
opts = trainingOptions("sgdm", 'InitialLearnRate', 0.001, 'MaxEpochs', 5, 'MiniBatchSize', 64, 'Plots','training-progress'); % Different options for training the Neural Network
myNet = trainNetwork(trainingImages, layers,opts); %Will save the trained network based off AlexNet as myNet
predictedLabels = classify(myNet, testImages); % This will identify images using the trained network by using the validation images
accuracy = mean(predictedLabels ==testImages.Labels) % This will display a percentage of how accurate the model is at identifying the images.
camera = webcam; % Connect to the camera
while true
picture = camera.snapshot; % Take a picture using connected Camera
picture = imresize(picture,[227,227]); % Resize the picture to 277x277 Pixel size
label = classify(myNet, picture); % Classify the picture
image(picture); % Displays the picture
title(char(label)); % Shows the label above the picture
drawnow; % Continuously updates image from Webcam
end
  2 个评论
yanqi liu
yanqi liu 2022-1-18
yes,sir,i think mat file include weight data,if use m file,may be only the layers structure
David Duran
David Duran 2022-1-18
So what would be the best way to tranfer the trained NN to a Raspberry PI? I have watched the videos provided by MATLAB, but they are pulling the untrained neural networks from the Program files.
i.e. the code
edit_raspi_webcam_resnet

请先登录,再进行评论。

采纳的回答

Prince Kumar
Prince Kumar 2022-1-20

更多回答(1 个)

Yazan
Yazan 2022-9-28
I have the same problem; did you find a solution.
Can you share your solution please, I have another question, how did you manage to turn your Matlab code into a function?
Thank you

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by