出现错误:在Keras中加载模型时:

时间:2020-03-02 13:26:30

标签: python keras deep-learning

我定义了一个典型的暹罗网络体系结构,以获取使用temp_model(权重经过三重损失函数预训练的VGG模型)的编码,在下面的代码中,最后我训练了模型并将其保存为磁盘作为h5文件,但是当我加载时用于预测的模型,我得到一个错误(ValueError:无效的input_shape参数[[None,224,224,3),(None,224,224,3),(None,224,224,3)]:模型具有1个张量输入。)

'''

left_input = Input(shape = (224, 224, 3))
right_input = Input(shape = (224, 224, 3))

# Generate the encodings (feature vectors) for the two images
encoded_l = temp_model([left_input,left_input,left_input])
encoded_r = temp_model([right_input,right_input,right_input])

# Add a customized layer to compute the absolute difference between the encodings 
L1_layer = Lambda(lambda tensors:K.abs(tensors[0] - tensors[1]))
L1_distance = L1_layer([encoded_l, encoded_r])

L1_distance = Dense(512,activation='relu')(L1_distance)
L1_distance = Dropout(0.2)(L1_distance)

L1_distance = Dense(10,activation='relu')(L1_distance)
L1_distance = Dropout(0.2)(L1_distance)

# Add a dense layer with a sigmoid unit to generate the similarity score
prediction = Dense(1,activation='sigmoid')(L1_distance)

# Connect the inputs with the outputs
siamese_net = Model(inputs=[left_input,right_input],outputs=prediction)

siamese_net.compile(loss='binary_crossentropy', optimizer="adam",
      metrics=['accuracy'])

siamese_net.summary()

# return the model
return siamese_net 

''' -------------------------------------------------- ------------------------- ValueError跟踪(最近一次通话) 在 1 #final_model = siamese_model() ----> 2 final_model = load_model(“ triplet_loss_function_vgg16_siamese_h100_128.h5”)

/opt/anaconda3/lib/python3.7/site-packages/keras/engine/saving.py in load_model(filepath, custom_objects, compile)
    417     f = h5dict(filepath, 'r')
    418     try:
--> 419         model = _deserialize_model(f, custom_objects, compile)
    420     finally:
    421         if opened_new_file:

/opt/anaconda3/lib/python3.7/site-packages/keras/engine/saving.py in _deserialize_model(f, custom_objects, compile)
    223         raise ValueError('No model found in config.')
    224     model_config = json.loads(model_config.decode('utf-8'))
--> 225     model = model_from_config(model_config, custom_objects=custom_objects)
    226     model_weights_group = f['model_weights']
    227 

/opt/anaconda3/lib/python3.7/site-packages/keras/engine/saving.py in model_from_config(config, custom_objects)
    456                         '`Sequential.from_config(config)`?')
    457     from ..layers import deserialize
--> 458     return deserialize(config, custom_objects=custom_objects)
    459 
    460 

/opt/anaconda3/lib/python3.7/site-packages/keras/layers/__init__.py in deserialize(config, custom_objects)
     53                                     module_objects=globs,
     54                                     custom_objects=custom_objects,
---> 55                                     printable_module_name='layer')

/opt/anaconda3/lib/python3.7/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    143                     config['config'],
    144                     custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 145                                         list(custom_objects.items())))
    146             with CustomObjectScope(custom_objects):
    147                 return cls.from_config(config['config'])

/opt/anaconda3/lib/python3.7/site-packages/keras/engine/network.py in from_config(cls, config, custom_objects)
   1030                 if layer in unprocessed_nodes:
   1031                     for node_data in unprocessed_nodes.pop(layer):
-> 1032                         process_node(layer, node_data)
   1033 
   1034         name = config.get('name')

/opt/anaconda3/lib/python3.7/site-packages/keras/engine/network.py in process_node(layer, node_data)
    989             # and building the layer if needed.
    990             if input_tensors:
--> 991                 layer(unpack_singleton(input_tensors), **kwargs)
    992 
    993         def process_layer(layer_data):

/opt/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
    472             if all([s is not None
    473                     for s in to_list(input_shape)]):
--> 474                 output_shape = self.compute_output_shape(input_shape)
    475             else:
    476                 if isinstance(input_shape, list):

/opt/anaconda3/lib/python3.7/site-packages/keras/engine/network.py in compute_output_shape(self, input_shape)
    591             raise ValueError('Invalid input_shape argument ' +
    592                              str(input_shape) + ': model has ' +
--> 593                              str(len(self._input_layers)) + ' tensor inputs.')
    594 
    595         cache_key = ', '.join([str(x) for x in input_shapes])

ValueError: Invalid input_shape argument [(None, 224, 224, 3), (None, 224, 224, 3), (None, 224, 224, 3)]: model has 1 tensor inputs.

'''

'''

model_siamese = siamese_model()
from keras.callbacks import ModelCheckpoint

checkpoint = ModelCheckpoint('triplet_loss_function_vgg16_siamese_h512_10_128.h5', verbose=1, monitor='val_loss',save_best_only=True, mode='auto')

hist = model_siamese.fit_generator(generate_batch_siamese(batch_size=128, validation=False),epochs=1000, steps_per_epoch=int((total * 0.8) / 32), 
            validation_steps=int((total * 0.10) / 32),
            validation_data=generate_batch_siamese(batch_size=128, validation=True), callbacks=[checkpoint], use_multiprocessing=True)



final_model = load_model("triplet_loss_function_vgg16_siamese_h100_128.h5")

'''

2 个答案:

答案 0 :(得分:0)

尝试将用于编码生成的代码部分更改为

# Generate the encodings (feature vectors) for the two images
encoded_l = temp_model(left_input)
encoded_r = temp_model(right_input)

答案 1 :(得分:0)

这是加载嵌套模型时的常见问题,没有一个唯一的答案,但是有一些有用的链接,您可以在其中找到解决此类问题的提示。 https://github.com/keras-team/keras/pull/11847

在我的情况下,我重新定义了一个体系结构(与我的训练相同),将可训练参数设置为false,而不是使用load_models而不是load_model,它为我工作。正如我所说,没有一个答案,您必须测试并尝试其他选择。