【问题标题】:Tensorflow 2.0 Model subclassingTensorflow 2.0 模型子类化
【发布时间】:2021-03-02 07:42:01
【问题描述】:

我制作了这个将 resnet 合并到模型中的函数。它运作良好,我可以保存它。 我的问题是我无法加载它,因为它需要一个调用函数。我不确定如何把它变成一门课。尝试在底部。一些指针会有所帮助。

def build_network():

    inp = Input(shape=(256,256,3))

    resnet = tf.keras.applications.ResNet152V2(
        include_top=False, weights='imagenet', input_tensor=None,
        input_shape=(256,256,3), pooling=None, classes=1000

    )
    # classifier_activation='softmax'
    x = resnet(inp)
    x = GlobalAveragePooling2D()(x)
    x = Dropout(0.3)(x)
    x = Dense(9, activation='softmax')(x)
    model = tf.keras.Model(inputs=inp,outputs = x)
    opt = tf.keras.optimizers.SGD(momentum=0.9)

    # optimizer = 'adam',
    model.compile(loss='categorical_crossentropy',
                    optimizer = opt,
                    metrics=['accuracy'])

    model.summary()

    return model

class Resnet(tf.keras.Model):

    def __init__(self, num_classes=9):
        super(Resnet, self).__init__()
        self.block_1 = tf.keras.applications.ResNet152V2(
            include_top=False, weights='imagenet', input_tensor=None,
            input_shape=(256,256,3), pooling=None, classes=1000)
        self.global_pool = layers.GlobalAveragePooling2D()
        self.dropout = Dropout(0.3)
        self.classifier = Dense(num_classes, activation = 'softmax')

    def call(self, inputs):
        x = self.block_1(inputs)
        x = self.global_pool(x)
        x = self.dropout(x)
        x = self.classifier(x)
        return tf.keras.Model(inputs = inputs, outputs = x)

【问题讨论】:

标签: python tensorflow keras model subclass


【解决方案1】:

使用子类化 API 实际上会使您的模型不可序列化(请参阅"Limitations section in the "What are Symbolic and Imperative APIs in TensorFlow 2.0? " blogpost):

命令式模型也更难检查、复制或克隆。

例如,model.save()、model.get_config() 和 clone_model 不适用于子类模型。同样,model.summary() 只为您提供层列表(并且不提供有关它们如何连接的信息,因为无法访问)。

编辑:从 Tensorflow 2.4 开始,可以将 save_traces 参数传递给 model.save 以序列化使用子类化 API 构建的模型。见https://www.tensorflow.org/guide/keras/save_and_serialize#how_savedmodel_handles_custom_objects

这里有一个简单的例子来说明如何做到这一点:

import tensorflow as tf
from tensorflow.keras.layers import (Dense, Dropout, GlobalAveragePooling2D,
                                     Input)


def build_network():
    inp = Input(shape=(256, 256, 3))
    resnet = tf.keras.applications.ResNet152V2(include_top=False,
                                               weights="imagenet",
                                               input_tensor=None,
                                               input_shape=(256, 256, 3),
                                               pooling=None,
                                               classes=1000)
    # classifier_activation="softmax"
    x = resnet(inp)
    x = GlobalAveragePooling2D()(x)
    x = Dropout(0.3)(x)
    x = Dense(9, activation="softmax")(x)
    model = tf.keras.Model(inputs=inp, outputs=x)
    # optimizer = "adam",
    opt = tf.keras.optimizers.SGD(momentum=0.9)
    model.compile(loss="categorical_crossentropy",
                  optimizer=opt,
                  metrics=["accuracy"])
    model.summary()
    return model


if __name__ == "__main__":
    model = build_network()
    model.summary()
    # Save
    model.save("my_model.h5")
    # Load
    loaded_model = tf.keras.models.load_model("my_model.h5")
    loaded_model.summary()

要从您的build_network 函数加载您保存的模型,请使用tf.keras.models.load_model

【讨论】:

  • Using the subclassing API will actually make your model unserializable :这不再是真的,请参阅指南:Save and load Keras models
猜你喜欢
  • 2019-08-30
  • 2020-05-24
  • 2020-05-23
  • 2019-12-16
  • 1970-01-01
  • 2020-04-10
  • 2020-08-09
  • 1970-01-01
  • 2020-01-26
相关资源
最近更新 更多