Why "trainNetwork" function works with GoogLeNet but not with other pretrained networks. Why the other pretrained networks need the "trainnet" function
5 views (last 30 days)
Show older comments
When i try to train a modified pretrained GoogLeNet for binary classification, with the trainnet function as bellow:
[...] =trainnet(resizeTrainImgs,lgraph_1,"crossentropy",opts);
It does not work, It displays the following message:
"Error using trainnet (line 16)
Network must be a dlnetwork or an array of layers."
0 Comments
Answers (1)
Cris LaPierre
on 26 Feb 2025
Moved: Walter Roberson
on 5 Mar 2025
It sounds like you are asking what is the difference between trainnet and trainnetwork. This answer might be helpful: https://www.mathworks.com/matlabcentral/answers/2060029-difference-between-trainnet-and-trainnetwork
We would have to see the layers of your network to say for certain, but a big difference is that DAGNetworks returns a prediction while a dlnetwork returns scores. This is why different functions are needed to train them.
layers = [
imageInputLayer([28 28 1])
convolution2dLayer(5,16,'Padding','same')
batchNormalizationLayer
reluLayer('Name','relu_1')
convolution2dLayer(3,32,'Padding','same','Stride',2)
batchNormalizationLayer
reluLayer
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
additionLayer(2,'Name','add')
averagePooling2dLayer(2,'Stride',2)
fullyConnectedLayer(10)
softmaxLayer
classificationLayer];
while in this dlnetwork example, the last layer returns the scores. The actual predictions are made using minibatchpredict.
layers = [
imageInputLayer([28 28 1],Normalization="none")
convolution2dLayer(5,16,Padding="same")
batchNormalizationLayer
reluLayer(Name="relu_1")
convolution2dLayer(3,32,Padding="same",Stride=2)
batchNormalizationLayer
reluLayer
convolution2dLayer(3,32,Padding="same")
batchNormalizationLayer
reluLayer
additionLayer(2,Name="add")
fullyConnectedLayer(numClasses)
softmaxLayer(Name="softmax")];
4 Comments
Cris LaPierre
on 5 Mar 2025
Moved: Cris LaPierre
on 5 Mar 2025
For 1, see my original answer above. Perhaps this answer is also helpful. The notable difference is what the last layer is.
For 2, it depends on the type of model you are training. Perhaps this 2023b doc page is helpful. I got there from here.
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!