Missing layers in Deep Learning Toolboox results in failing to run EfficientNetV2S

조회 수: 16 (최근 30일)
I saved EfficientNetV2S in Python as follows
import tensorflow as tf
model = tf.keras.applications.efficientnet_v2.EfficientNetV2S(
include_top =True,
weights =None,
input_tensor=None,
input_shape =None,
pooling =None,
classes =2,
classifier_activation='softmax',
include_preprocessing=True
)
model.save('EfficientNetV2S')
model.save('EfficientNetV2S_.h5')
Then I also converted the created above folder EfficientNetV2S to ONNX format as follows
python -m tf2onnx.convert --saved-model ./EfficientNetV2S --output EfficientNetV2S.onnx
Now I have three ways to import this model to Matlab
First:
net = importTensorFlowNetwork('EfficientNetV2S');
Warning: The SavedModel 'EfficientNetV2S' was saved in TensorFlow version '2.8.0'. Import of SavedModel versions newer than '2.6.0' is not supported. The imported model may not
exactly match the model saved in the SavedModel.
Error using nnet.internal.cnn.tensorflow.savedmodel.TFSavedModel (line 35)
'OutputLayerType' is missing. Please specify an output layer type or set the 'TargetNetwork' to be 'dlnetwork'.
Error in nnet.internal.cnn.tensorflow.importTensorFlowNetwork (line 20)
sm = savedmodel.TFSavedModel(path, options, true);
Error in importTensorFlowNetwork (line 107)
Network = nnet.internal.cnn.tensorflow.importTensorFlowNetwork(modelFolder, varargin{:});
"'OutputLayerType' is missing" not sure what I should do about this.
Second:
net = importKerasNetwork('EfficientNetV2S_.h5');
Warning: File 'EfficientNetV2S_.h5' was saved in Keras version '2.8.0'. Import of Keras versions newer than '2.2.4' is not supported. The imported model may not exactly match the
model saved in the Keras file.
Error using nnet.internal.cnn.keras.importKerasNetwork (line 21)
Keras network does not include loss information specifying the output layer type. Specify the type using the 'OutputLayerType' argument.
Error in importKerasNetwork (line 76)
Network = nnet.internal.cnn.keras.importKerasNetwork(modelfile, varargin{:});
"Keras network does not include loss information specifying the output layer type. Specify the type using the 'OutputLayerType' argument." will it help? How should I do that?
Thrid:
net = importONNXNetwork('EfficientNetV2S.onnx')
This one doesn't generate the errors! But, then I attempt training the model, it returns NaNs. Based on the deepNetworkDesigner, I believe this is caused by the missing layers:
  • ElementWiseAffineLayer
  • IdentityLayer
  • FlattenInto2DLayer
  • ReshapeLayer
Am I doing something wrong? Any suggestions are welcome.
---------------------------------------------------------------------------------
UPDATE
---------------------------------------------------------------------------------
I've tried computing each layer separately one by one. The model fails on
activations(trainedNet,imgs,'StatefulPartiti_1327')
that returns NaN. I believe this due to missing implementation of nnet.onnx.layer.IdentityLayer
The list of the first layers is as follows
573×1 Layer array with layers:
1 'input_1' Image Input 384×384×3 images
2 'StatefulPartiti_1324' Elementwise Affine Applies an elementwise scaling followed by an addition to the input.
3 'StatefulPartiti_1323' Elementwise Affine Applies an elementwise scaling followed by an addition to the input.
4 'StatefulPartiti_1328' nnet.onnx.layer.IdentityLayer nnet.onnx.layer.IdentityLayer
5 'StatefulPartiti_1327' Convolution 24 3×3×3 convolutions with stride [2 2] and padding [0 1 0 1]
...

채택된 답변

Sivylla Paraskevopoulou
Sivylla Paraskevopoulou 2022년 5월 13일
It is recommended to use importTensorFlowNetwork over importKerasNetwork. For more details, see the Import and Export Networks (documentation) and Importing Models from TensorFlow, PyTorch, and ONNX (blog post).
So let's try your first option and specify the output layer type:
net = importTensorFlowNetwork("EfficientNetV2S_.h5",OutputLayerType="classification");
This should import your TensorFlow model as a DAGNetwork object. You can also specify the class names. EfficientNet is trained on the ImageNet data set. See how to get the class names in this example: https://github.com/matlab-deep-learning/Image-Classification-in-MATLAB-Using-Converted-TensorFlow-Model
After you define ClassNames, you can import your model with classes by running this code:
net = importTensorFlowNetwork("EfficientNetV2S_.h5",OutputLayerType="classification",Classes=ClassNames);
You can also choose to import your TensorFlow model as a dlnetwork object. A dlnetwork does not have an output layer.
net = importTensorFlowNetwork("EfficientNetV2S_.h5",TargetNetwork="dlnetwork");
  댓글 수: 5
Sivylla Paraskevopoulou
Sivylla Paraskevopoulou 2022년 6월 19일
편집: Sivylla Paraskevopoulou 2022년 6월 19일
@Arefin Shamsil, you saved your TensorFlow model in .h5 format. You should save it in SavedModel format and then import it by using the importTensorFlowNetwork function. The importTensorFlowNetwork function expects a folder that contains these variables: saved_model.pb, variables, assets.
To save the model in SavedModel format in Python:
tf.saved_model.save(model,"best_model")
To import the model in MATLAB:
net = importTensorFlowNetwork("best_model");

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by