How to replace the classes in a pretrained network's output classification layer?
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Dominik Mattioli
am 10 Jun. 2021
Beantwortet: Anshika Chaurasia
am 14 Jun. 2021
I've followed the MATLAB tutorial for training an image classification model (link below) and it works well for my 2 classes, but when I export it and then import it later on, it's basing predictions off of the original pretrained network's 1000 classes.
exportONNXNetwork( net, 'my_class_model.onnx' ), 'NetworkName', 'myModel' );
%... saved, then import it later on when needed:
fn = 'my_class_model.onnx';
classes = ["Class1" "Class2"];
mynet = importONNXNetwork( fn, 'OutputLayerType', 'classification', 'Classes', classes );
% Error results:
% Error using nnet.internal.cnn.onnx.importONNXNetwork>iCheckOutputLayerTypeAndClasses (line 116)
% Expected the number of classes specified to match the number of classes in the classification output
% layer: 1000. Instead it was 2.
If you remove the 'Classes' input, there is no error. However, the predictions are then given for the original 1,000 classes, not my two.
Do I need to replace the classification layer before training? Or is there an exporting parameter for this?
Link to tutorial:
0 Kommentare
Akzeptierte Antwort
Anshika Chaurasia
am 14 Jun. 2021
Hi,
As correctly mentioned you need to replace the classification layer and fully connected layer in net before training, so that network classify image for two classes.
You can refer to the following documentation and video for transfer learning in pretrained network:
Hope it helps!
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Image Data Workflows finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!