I trained a model via keras retinanet (by fizyr). I converted it to .tflite but ran into issues when viewing the .tflite template information Model informations:
tflife_input_details = tflife_model.get_input_details()
[{'name': 'input_1', 'index': 0, 'shape': array([1, 1, 1, 3], dtype=int32), 'shape_signature':
array([-1, -1, -1, 3], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0,
0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([],
dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]
tflife_output_details = tflife_model.get_output_details()
[{'name': 'Identity', 'index': 354, 'shape': array([1, 1, 4], dtype=int32), 'shape_signature':
array([-1, -1, 4], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0),
'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([],
dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'Identity_1',
'index': 344, 'shape': array([1, 1, 1], dtype=int32), 'shape_signature': array([-1, -1, 1],
dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0),
'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([],
dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]
Convert model to .tflite
import tensorflow as tf
from keras_retinanet.models import load_model
loaded_model = load_model("snapshots/resnet50_csv_10.h5", backbone_name='resnet50')
converter = tf.lite.TFLiteConverter.from_keras_model(loaded_model)
tflite_model = converter.convert()
with tf.io.gfile.GFile('final.tflite', 'wb') as f:
f.write(tflite_model)
Training:
Model: "retinanet"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, None, None, 0
__________________________________________________________________________________________________
conv1 (Conv2D) (None, None, None, 6 9408 input_1[0][0]
__________________________________________________________________________________________________
bn_conv1 (BatchNormalization) (None, None, None, 6 256 conv1[0][0]
__________________________________________________________________________________________________
conv1_relu (Activation) (None, None, None, 6 0 bn_conv1[0][0]
__________________________________________________________________________________________________
pool1 (MaxPooling2D) (None, None, None, 6 0 conv1_relu[0][0]
__________________________________________________________________________________________________
res2a_branch2a (Conv2D) (None, None, None, 6 4096 pool1[0][0]
__________________________________________________________________________________________________
bn2a_branch2a (BatchNormalizati (None, None, None, 6 256 res2a_branch2a[0][0]
__________________________________________________________________________________________________
res2a_branch2a_relu (Activation (None, None, None, 6 0 bn2a_branch2a[0][0]
__________________________________________________________________________________________________
padding2a_branch2b (ZeroPadding (None, None, None, 6 0 res2a_branch2a_relu[0][0]
__________________________________________________________________________________________________
res2a_branch2b (Conv2D) (None, None, None, 6 36864 padding2a_branch2b[0][0]
__________________________________________________________________________________________________
bn2a_branch2b (BatchNormalizati (None, None, None, 6 256 res2a_branch2b[0][0]
__________________________________________________________________________________________________
res2a_branch2b_relu (Activation (None, None, None, 6 0 bn2a_branch2b[0][0]
__________________________________________________________________________________________________
res2a_branch2c (Conv2D) (None, None, None, 2 16384 res2a_branch2b_relu[0][0]
__________________________________________________________________________________________________
res2a_branch1 (Conv2D) (None, None, None, 2 16384 pool1[0][0]
__________________________________________________________________________________________________
bn2a_branch2c (BatchNormalizati (None, None, None, 2 1024 res2a_branch2c[0][0]
__________________________________________________________________________________________________
bn2a_branch1 (BatchNormalizatio (None, None, None, 2 1024 res2a_branch1[0][0]
__________________________________________________________________________________________________
res2a (Add) (None, None, None, 2 0 bn2a_branch2c[0][0]
bn2a_branch1[0][0]
__________________________________________________________________________________________________
res2a_relu (Activation) (None, None, None, 2 0 res2a[0][0]
__________________________________________________________________________________________________
res2b_branch2a (Conv2D) (None, None, None, 6 16384 res2a_relu[0][0]
__________________________________________________________________________________________________
bn2b_branch2a (BatchNormalizati (None, None, None, 6 256 res2b_branch2a[0][0]
__________________________________________________________________________________________________
res2b_branch2a_relu (Activation (None, None, None, 6 0 bn2b_branch2a[0][0]
__________________________________________________________________________________________________
padding2b_branch2b (ZeroPadding (None, None, None, 6 0 res2b_branch2a_relu[0][0]
__________________________________________________________________________________________________
res2b_branch2b (Conv2D) (None, None, None, 6 36864 padding2b_branch2b[0][0]
__________________________________________________________________________________________________
bn2b_branch2b (BatchNormalizati (None, None, None, 6 256 res2b_branch2b[0][0]
__________________________________________________________________________________________________
res2b_branch2b_relu (Activation (None, None, None, 6 0 bn2b_branch2b[0][0]
__________________________________________________________________________________________________
res2b_branch2c (Conv2D) (None, None, None, 2 16384 res2b_branch2b_relu[0][0]
__________________________________________________________________________________________________
bn2b_branch2c (BatchNormalizati (None, None, None, 2 1024 res2b_branch2c[0][0]
__________________________________________________________________________________________________
...
__________________________________________________________________________________________________
C4_reduced (Conv2D) (None, None, None, 2 262400 res4f_relu[0][0]
__________________________________________________________________________________________________
P4_merged (Add) (None, None, None, 2 0 P5_upsampled[0][0]
C4_reduced[0][0]
__________________________________________________________________________________________________
P4_upsampled (UpsampleLike) (None, None, None, 2 0 P4_merged[0][0]
res3d_relu[0][0]
__________________________________________________________________________________________________
C3_reduced (Conv2D) (None, None, None, 2 131328 res3d_relu[0][0]
__________________________________________________________________________________________________
P6 (Conv2D) (None, None, None, 2 4718848 res5c_relu[0][0]
__________________________________________________________________________________________________
P3_merged (Add) (None, None, None, 2 0 P4_upsampled[0][0]
C3_reduced[0][0]
__________________________________________________________________________________________________
C6_relu (Activation) (None, None, None, 2 0 P6[0][0]
__________________________________________________________________________________________________
P3 (Conv2D) (None, None, None, 2 590080 P3_merged[0][0]
__________________________________________________________________________________________________
P4 (Conv2D) (None, None, None, 2 590080 P4_merged[0][0]
__________________________________________________________________________________________________
P5 (Conv2D) (None, None, None, 2 590080 C5_reduced[0][0]
__________________________________________________________________________________________________
P7 (Conv2D) (None, None, None, 2 590080 C6_relu[0][0]
__________________________________________________________________________________________________
regression_submodel (Functional (None, None, 4) 2443300 P3[0][0]
P4[0][0]
P5[0][0]
P6[0][0]
P7[0][0]
__________________________________________________________________________________________________
classification_submodel (Functi (None, None, 1) 2381065 P3[0][0]
P4[0][0]
P5[0][0]
P6[0][0]
P7[0][0]
__________________________________________________________________________________________________
regression (Concatenate) (None, None, 4) 0 regression_submodel[0][0]
regression_submodel[1][0]
regression_submodel[2][0]
regression_submodel[3][0]
regression_submodel[4][0]
__________________________________________________________________________________________________
classification (Concatenate) (None, None, 1) 0 classification_submodel[0][0]
classification_submodel[1][0]
classification_submodel[2][0]
classification_submodel[3][0]
classification_submodel[4][0]
==================================================================================================
Total params: 36,382,957
Trainable params: 12,821,805
Non-trainable params: 23,561,152
__________________________________________________________________________________________________
None
As shown, the array shape entry is (1, 1, 1, 3). How is it possible? Did I do something wrong with the conversion? Does this model currently not work in inference?
UPDATE: The structure of my converted model: https://i.stack.imgur.com/m444F.jpg
I think that this will works for you :
So first of all, you should install
keras_retinanet
using pipAnd than execute this script