简体   繁体   English

在 keras 中加载 model 时出现图表断开错误

[英]Graph disconnected error when loading model in keras

I have a model that works and fit correctly.我有一个可以正常工作且适合的 model。 But if I save the model after training, when I try to load it, it throws this error:但是如果我在训练后保存 model,当我尝试加载它时,它会抛出这个错误:

ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 256, 256, 3), dtype=tf.float32, name='InputLucaSchifoso'), name='InputLucaSchifoso', description="created by layer 'InputLucaSchifoso'") at layer "conv2d_5LucaSchifoso". ValueError: Graph disconnected: cannot get value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 256, 256, 3), dtype=tf.float32, name='InputLucaSchifoso'), name='InputLucaSchifoso', description="由“conv2d_5LucaSchifoso”层的“InputLucaSchifoso”层创建。 The following previous layers were accessed without issue: []访问以下先前层没有问题:[]

This is the creation of the model and its training that works whitout errors这是 model 的创建及其无错误的训练

# These models are loaded previously
model_dict = {
    "InceptionV3": model_InceptionV3,
    "LucaSchifoso": model_LucaSchifoso,
    "MobileNetV2": model_MobileNetV2, 
    "Resnet50": model_Resnet50
}

# Every layer's name must be unique
for model_name in model_dict.keys():
    for layer in model_dict[model_name].layers:
        layer._name += model_name

# Create Model
proc_layer_dict = {}

input_layer = tfk.layers.Input(shape=input_shape, name="input_layer")

layers_dict = {}

for model_name in preprocessing_function_dict:
    proc_layer_dict[model_name] = tfk.layers.Lambda(
        preprocessing_function_dict[model_name], name="lambda_" + model_name
    )(input_layer)
    
    layers_dict[model_name] = []
    layers_dict[model_name].append(proc_layer_dict[model_name])
    for layer in model_dict[model_name].layers:
        layers_dict[model_name].append(layer(layers_dict[model_name][-1]))

maxpool_LucaSchifoso1 = tfkl.MaxPooling2D(
        name='maxpool_LucaSchifoso1',
        pool_size = (3, 3)
    )(layers_dict["LucaSchifoso"][10])

flatten_LucaSchifoso1 = tfkl.Flatten(name='flatten_LucaSchifoso1')(maxpool_LucaSchifoso1)

concatenate_layer = tfkl.Concatenate()([layers_dict["InceptionV3"][2],
                                        layers_dict["MobileNetV2"][2],
                                        flatten_LucaSchifoso1, 
                                        layers_dict["Resnet50"][2]])

dropout_mergione1 = tfkl.Dropout(0.3, name='dropout_mergione1', seed=seed)(concatenate_layer)
dense_mergione1 = tfkl.Dense(units=512, name='dense_mergione1', kernel_initializer=tfk.initializers.GlorotUniform(seed), activation='relu')(dropout_mergione1)
dropout_mergione2 = tfkl.Dropout(0.3, name='dropout_mergione2', seed=seed)(dense_mergione1)
output_mergione = tfkl.Dense(name='output_mergione', units=14, activation='softmax', kernel_initializer=tfk.initializers.GlorotUniform(seed))(dropout_mergione2)

modellone = tfk.Model(inputs=input_layer, outputs=output_mergione, name='model')

modellone.compile(loss=tfk.losses.CategoricalCrossentropy(), optimizer=tfk.optimizers.Adam(), metrics='loss')

# Fit the Model
history = modellone.fit(
        x = train_gen,
        epochs = epochs,
        validation_data = valid_gen,
    ).history

# Save trained model
modellone.save("best")

The four loaded models are saved and loaded correctly when they are standalone, so I think that the problem is not there.四个加载的模型在独立时保存和加载正确,所以我认为问题不存在。

This is the row that throws the error:这是引发错误的行:

# Load model
model = tf.keras.models.load_model('best')

This is the result of tfk.utils.plot_model(modellone)这是tfk.utils.plot_model(modellone)的结果在此处输入图像描述

I apologize if the code is not enough to test the problem, but I don't know how to make it reproducible without add all the code.如果代码不足以测试问题,我深表歉意,但我不知道如何在不添加所有代码的情况下使其可重现。 I hope you can help me anyway.无论如何,我希望你能帮助我。

The problem was generated by the input layers inside the model, for some reason they don't create any problem during compiling and training of the model, but they do during loading.问题是由 model 内部的输入层产生的,由于某种原因,它们在 model 的编译和训练过程中不会产生任何问题,但在加载过程中会产生问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM