Save a trained neural network for Raspberry

10 views (last 30 days)
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 Comments
yanqi liu
yanqi liu on 18 Jan 2022
yes,sir,i think mat file include weight data,if use m file,may be only the layers structure
David Duran
David Duran on 18 Jan 2022
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

Sign in to comment.

Accepted Answer

Prince Kumar
Prince Kumar on 20 Jan 2022

More Answers (1)

Yazan
Yazan on 28 Sep 2022
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

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!