Azzera filtri
Azzera filtri

How to make an image regression CNN with two labels

10 visualizzazioni (ultimi 30 giorni)
Hi,
Im currently trying to train a CNN capable of detecting vehicle deviation angle and distance (from road centre) from camera image data at the front of a vehicle (I can show some example images if you want!).
I know MATLABs NN designer comes with some pre-trained NNs - and someone already recommended ResNet-18 to me (although probably any simple CNN would do). I'd replace the last two layers with a regression layer.
I already have (some) training data with labels but i know ill need to get some more.
From what i understand, i need to make an image array as well as an array with the labels (distance and angle) in a different array. Other than typing 1000 filenames as in ImgArray = [Filename1, Filename2, ... , Filename1000], there surely must be a better way to do this? Datastores?
I then need to figure out how to connect this to the ResNet-18 in the NN designer app? I know also you can export the NN as code too.
I hope i've explained this well enough. Thanks in advance for any information - im grateful for any help or advice you can give :)

Risposte (1)

Sai Pavan
Sai Pavan il 27 Set 2023
Hi Samuel,
I understand that you are trying to know the procedure of making image datastore of vehicle camera image dataset and a table with labels of angle and distance. I understand that you also want to replace the last two layers of a pre-trained neural network model with a regression layer and generate the code of the final neural network model.
To prepare the data, create an image datastore using the imageDatastore function, specifying the folder path where your images are stored. You can create a table with the corresponding labels for angle and distance.
imds = imageDatastore('path_to_images_folder');
labels = table(angle_labels, distance_labels, 'VariableNames', {'Angle', 'Distance'});
To load the pre-trained ResNet-18 model, you can use the resnet18 function to load the pre-trained ResNet-18 model. Then, modify the last layer of the model by replacing the last fully connected layer with a regression layer using the replaceLayer function. And then use the layerGraph function to access the layers of the ResNet-18 model and connect the image input layer of the model to the image datastore using the replaceLayer function and finally connect the output of the model to your regression layer. You can use the trainNetwork function to train the model on your data by specifying the image input layer, the regression layer, and other training options.
net = resnet18;
net = replaceLayer(net, 'fc1000', regressionLayer('Name', 'regression'));
lgraph = layerGraph(net);
lgraph = replaceLayer(lgraph, 'data', imageInputLayer([224 224 3])); % Modify input layer
lgraph = connectLayers(lgraph, 'avg_pool', 'regression'); % Connect to regression layer
options = trainingOptions('adam', 'MaxEpochs', 10, 'MiniBatchSize', 32);
trainedNet = trainNetwork(imds, labels, lgraph, options);
Once the modified model is trained, you can export the model as code using the codegen function. This will allow you to use the model outside of the NN designer app.
codegen -config cfg trainedNet
Hope it helps.
Regards,
Sai Pavan

Community Treasure Hunt

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

Start Hunting!

Translated by