【问题标题】:Upgrade Tensorflow model or Retrain for SavedModel升级 TensorFlow 模型或重新训练 SavedModel
【发布时间】:2019-12-18 20:15:16
【问题描述】:

我在 2017 年关注“诗人的 Tensorflow”,并重新训练了我自己的图像集并创建了“retrained_graph.pb”和“retrained_labels.txt”
今天我需要在 Tensorflow Serving 上运行这个模型。 有两种方法可以做到这一点:

  1. 升级旧模型以将其保存为“saved_model”格式并在 Tensorflow Serving 上使用 - 我发现了一些 SO 帖子来完成它(thisthat)。

  2. 将最新的 tensorflow Hub 与 Keras 结合使用 (https://www.tensorflow.org/tutorials/images/hub_with_keras)

我正在寻找其中的最佳选择,或者一个新的选择。

【问题讨论】:

    标签: tensorflow tensorflow-serving imagenet tensorflow-hub


    【解决方案1】:

    在我看来,使用 Tensorflow Hub 或在 tf.keras.applications 内使用 Pre-Trained Models 是更可取的,因为无论哪种情况,保存模型不需要太多代码更改,使其与 Tensorflow Serving 兼容。

    tf.keras.applications中的预训练模型MobileNet重用代码如下所示:

    #Import MobileNet V2 with pre-trained weights AND exclude fully connected layers
    IMG_SIZE = 224
    
    from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
    from tensorflow.keras import Model
    
    
    IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)
    
    # Create the base model from the pre-trained model MobileNet V2
    base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                                   include_top=False,
                                                   weights='imagenet')
    
    # Add Global Average Pooling Layer
    x = base_model.output
    x = GlobalAveragePooling2D()(x)
    
    # Add a Output Layer
    my_mobilenetv2_output = Dense(5, activation='softmax')(x)
    
    # Combine whole Neural Network
    my_mobilenetv2_model = Model(inputs=base_model.input, outputs=my_mobilenetv2_output)
    

    您可以使用下面给出的代码保存模型:

    version = 1
    MODEL_DIR = 'Image_Classification_Model'
    export_path = os.path.join(MODEL_DIR, str(version))
    
    tf.keras.models.save_model(model = model, filepath = export_path)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      相关资源
      最近更新 更多