【问题标题】:Tflite 200mb bigTflite 200mb 大
【发布时间】:2020-08-16 10:28:53
【问题描述】:

我正在构建一个应该对花朵进行分类的模型。所以我用 Tensorflow 创建了一个模型:

  keras.layers.Conv2D(128, (3,3), activation='relu', input_shape=(imageShape[0], imageShape[1],3)),
  keras.layers.MaxPooling2D(2,2),
  keras.layers.Dropout(0.5),
  
  keras.layers.Conv2D(256, (3,3), activation='relu'),
  
  keras.layers.MaxPooling2D(2,2), 
 
  keras.layers.Conv2D(512, (3,3), activation='relu'),
  
  keras.layers.MaxPooling2D(2,2),
 
  keras.layers.Flatten(),
      
  keras.layers.Dropout(0.3),      
  
  keras.layers.Dense(280, activation='relu'),
  
  keras.layers.Dense(4, activation='softmax')

  opt = tf.keras.optimizers.RMSprop()
  model.compile(loss='categorical_crossentropy',
              optimizer= opt,
              metrics=['accuracy'])

训练时我将检查点保存为 .h5

checkpoint = ModelCheckpoint("preSaved"+str(time.time())+".h5", monitor='val_loss', verbose=1, 
save_best_only=True, save_weights_only=False, mode='auto', period=1)

现在我得到了一个损失非常低的 epoch,并希望将其转换为 .tflite 以将其上传到 Firebase(在 Android Studio 应用中使用它)。

import tensorflow as tf

new_model= tf.keras.models.load_model(filepath="model.h5")
tflite_converter = tf.lite.TFLiteConverter.from_keras_model(new_model)
tflite_converter.inference_type=tf.uint8
tflite_converter.default_ranges_stats=[min_value,max_value]
tflite_converter.quantized_input_stats={"conv2d_6_input_6:0"[mean,std]}
tflite_converter.post_training_quantize=True
tflite_model = tflite_converter.convert()
open("tf_lite_model.tflite", "wb").write(tflite_model)

.h5 大约有 335mb,最终 .tflite 有 160mb。但 Firebase 只允许 .tflite 达到 60mb,如果我使用本地模型,它需要几分钟才能加载。我读到 .tflite 通常更小。 我的模型是否存在问题,或者我将其转换为 .tflite 时是否存在问题?

【问题讨论】:

    标签: tensorflow keras tensorflow-lite firebase-machine-learning


    【解决方案1】:

    模型大小很大程度上取决于您的模型架构(构成模型的不同层以及每层中的参数数量)。您可以尝试更改这些以获得更小的模型。

    Here 是图像分类模型更简单的架构。当然,请记住,使用更小的模型可能比更复杂的版本具有更低的准确度。

    【讨论】:

    • 好的,感谢您的回复。您认为模型架构是问题所在?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 2018-11-06
    相关资源
    最近更新 更多