Save a trained neural network for Raspberry

조회 수: 5 (최근 30일)
David Duran
David Duran 2022년 1월 18일
답변: Yazan 2022년 9월 28일
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에서 Deep Learning Toolbox에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by