【问题标题】:bad shape array after convertition from h5 to tflite从 h5 转换为 tflite 后的坏形状数组
【发布时间】:2021-11-30 08:28:56
【问题描述】:

我通过 kerasretinanet(由 fizyr)训练了一个模型。 我将其转换为 .tflite 但在查看 .tflite 模板信息时遇到问题 型号信息:

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': {}}]

将模型转换为 .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)

培训:

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

如图所示,数组形状条目为 (1, 1, 1, 3)。 这怎么可能?我在转换中做错了吗?此模型目前是否不适用于推理?

更新:我的转换模型的结构: https://imgur.com/a/j1ne2Na

【问题讨论】:

    标签: keras retinanet


    【解决方案1】:

    我认为这对你有用:

    所以首先,你应该使用 pip 安装keras_retinanet

    pip install keras_retinanet
    

    然后执行这个脚本

    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)
    

    【讨论】:

    • 我使用谷歌 colab。我尝试使用该代码,但出现此错误:ModuleNotFoundError: No module named 'tensorflow.contrib'
    • 您使用的是什么版本的 Tensorflow?
    • Tensorflow 2.x.x(具体版本不记得了,是 tensorflow v2)
    • 你能给我确切的版本吗?
    • 张量流 2.6。 (我正在使用带有 GPU 的谷歌 colab)
    猜你喜欢
    • 2022-12-22
    • 2022-06-30
    • 2020-09-10
    • 2020-10-10
    • 2021-12-09
    • 1970-01-01
    • 2019-06-10
    • 2019-04-14
    • 2020-12-15
    相关资源
    最近更新 更多