ValueError:未知层:功能

时间:2020-07-24 07:14:19

标签: python tensorflow keras

我在合作实验室制作了CNN,并在每个时期保存了模型。我导出了h5文件,现在尝试在一些测试图像上运行模型。这是主要错误:

ValueError: Unknown layer: Functional

这是我用来运行模型并在每个时期保存的代码:

epochs = 50

callbacks = [
    tf.keras.callbacks.TensorBoard(log_dir='./logs'),
    keras.callbacks.ModelCheckpoint("save_at_{epoch}.h5"),
]
model.compile(
    optimizer=keras.optimizers.Adam(1e-3),
    loss="binary_crossentropy",
    metrics=["accuracy"],
)
model.fit(
    train_ds, epochs=epochs, callbacks=callbacks, validation_data=val_ds,
)

模型运行后,我刚刚从colab侧边栏本地下载了h5文件。我从本地磁盘重新上传了文件,这是我尝试加载模型的方法:

# load and evaluate a saved model
from tensorflow.keras.models import load_model

# load model#
loaded_model = load_model('save_at_47.h5')
loaded_model.layers[0].input_shape

这是完整的追溯:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-6af7396280fa> in <module>()
      3 
      4 # load model#
----> 5 loaded_model = load_model('save_at_47.h5')
      6 loaded_model.layers[0].input_shape

5 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/save.py in load_model(filepath, custom_objects, compile)
    182     if (h5py is not None and (
    183         isinstance(filepath, h5py.File) or h5py.is_hdf5(filepath))):
--> 184       return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
    185 
    186     if sys.version_info >= (3, 4) and isinstance(filepath, pathlib.Path):

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/hdf5_format.py in load_model_from_hdf5(filepath, custom_objects, compile)
    176     model_config = json.loads(model_config.decode('utf-8'))
    177     model = model_config_lib.model_from_config(model_config,
--> 178                                                custom_objects=custom_objects)
    179 
    180     # set weights

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/model_config.py in model_from_config(config, custom_objects)
     53                     '`Sequential.from_config(config)`?')
     54   from tensorflow.python.keras.layers import deserialize  # pylint: disable=g-import-not-at-top
---> 55   return deserialize(config, custom_objects=custom_objects)
     56 
     57 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/serialization.py in deserialize(config, custom_objects)
    107       module_objects=globs,
    108       custom_objects=custom_objects,
--> 109       printable_module_name='layer')

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    360     config = identifier
    361     (cls, cls_config) = class_and_config_for_serialized_keras_object(
--> 362         config, module_objects, custom_objects, printable_module_name)
    363 
    364     if hasattr(cls, 'from_config'):

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/generic_utils.py in class_and_config_for_serialized_keras_object(config, module_objects, custom_objects, printable_module_name)
    319   cls = get_registered_object(class_name, custom_objects, module_objects)
    320   if cls is None:
--> 321     raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
    322 
    323   cls_config = config['config']

ValueError: Unknown layer: Functional

似乎有几个类似的questions herehere。更改导入方法仍无济于事,尝试创建一些kind of custom对象也没有任何作用。

6 个答案:

答案 0 :(得分:6)

从头开始重建网络:

image_size = (212, 212)
batch_size = 32

data_augmentation = keras.Sequential(
    [
        layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical"),
        layers.experimental.preprocessing.RandomRotation(0.8),
    ]
)


def make_model(input_shape, num_classes):
    inputs = keras.Input(shape=input_shape)
    # Image augmentation block
    x = data_augmentation(inputs)

    # Entry block
    x = layers.experimental.preprocessing.Rescaling(1.0 / 255)(x)
    x = layers.Conv2D(32, 3, strides=2, padding="same")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation("relu")(x)

    x = layers.Conv2D(64, 3, padding="same")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation("relu")(x)

    previous_block_activation = x  # Set aside residual

    for size in [128, 256, 512, 728]:
        x = layers.Activation("relu")(x)
        x = layers.SeparableConv2D(size, 3, padding="same")(x)
        x = layers.BatchNormalization()(x)

        x = layers.Activation("relu")(x)
        x = layers.SeparableConv2D(size, 3, padding="same")(x)
        x = layers.BatchNormalization()(x)

        x = layers.MaxPooling2D(3, strides=2, padding="same")(x)

        # Project residual
        residual = layers.Conv2D(size, 1, strides=2, padding="same")(
            previous_block_activation
        )
        x = layers.add([x, residual])  # Add back residual
        previous_block_activation = x  # Set aside next residual

    x = layers.SeparableConv2D(1024, 3, padding="same")(x)
    x = layers.BatchNormalization()(x)
    x = layers.Activation("relu")(x)

    x = layers.GlobalAveragePooling2D()(x)
    if num_classes == 2:
        activation = "sigmoid"
        units = 1
    else:
        activation = "softmax"
        units = num_classes

    x = layers.Dropout(0.5)(x)
    outputs = layers.Dense(units, activation=activation)(x)
    return keras.Model(inputs, outputs)


model = make_model(input_shape=image_size + (3,), num_classes=2)
keras.utils.plot_model(model, show_shapes=False)

加载权重:

model.load_weights('save_at_47.h5')

并对图像进行预测:

# Running inference on new data
img = keras.preprocessing.image.load_img(
    "le_image.jpg", target_size=image_size
)
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)  # Create batch axis

predictions = model.predict(img_array)
score = predictions[0]
print(
    "This image is %.2f percent negative and %.2f percent positive."
    % (100 * (1 - score), 100 * score)
)

答案 1 :(得分:3)

当我使用tf 2.3.0时,我遇到了同样的问题,我降级到tf 2.2.0,它可以正常工作

答案 2 :(得分:2)

在这里讨论救了我一些麻烦!我共享一个不同的解决方案,更容易为我做的。

和TF2.3之间TF2.2变化是相当轻微的。我用下T2.2下TF2.3我需要加载和火车书面model_config.yaml文件时有同样的错误。这两个版本用于与仅Conv2D层的模型结构中的差异是简单修复手动或使用的sed:

正如所看到的下面一个需要改变class_name: Functional {成{1}},并除去配置class_name: Model是新在Conv2D和可能其他转化率的层。为了保持一致性我也末改变了keras版本。显然,这仅在组参数保留为 groups: 1 时才有效。

default: 1

答案 3 :(得分:2)

如果您使用较旧的 TF(在我的情况下为 2.1.0)并尝试解压缩由较新的 TF(例如 2.4.1)打包的 h5 文件并遇到此错误,则以下技巧可能会很有用。

your_model = tf.keras.models.load_model(
    path_to_h5,
    custom_objects={'Functional':tf.keras.models.Model})

答案 4 :(得分:1)

在colab上使用tf 2.3训练模型并将tf 2.2加载到我的本地计算机中时,我遇到了同样的问题。解决方案是使用以下命令升级TensorFlow:

pip3 install --upgrade tensorflow

答案 5 :(得分:0)

例如,解决此错误的方法非常简单。原因是您已经在Tensorflow的“ 2.3.0”版本和Keras的“ 2.4.3”版本(在Colab或本地)上训练了模型。现在您可以通过Keras&TensorFlow的另一个版本访问保存的模型(.h5)。它会给你错误。解决方案是重新训练带有升级版本的模型,或者将TF&Keras降级为与训练模型相同的版本。