为什么我不能在Keras中加载保存的暹罗模型?

时间:2019-12-04 11:55:09

标签: python keras callback google-colaboratory

我正在尝试构建个人Re-ID系统,并且我使用暹罗体系进行模型训练。我使用callbacks.ModelCheckpoint在每个时代保存模型。加载保存的模型时发生错误。

我使用VGG16预训练模型进行训练:

input_shape = (160,60,3)
conv_base = VGG16(weights='imagenet',
                  include_top=False,
                  input_shape=(160, 60, 3))

output = conv_base.layers[-5].output

x=Flatten()(output)
x=Dense(512,activation='relu')(x)
out=Dense(512,activation='relu')(x)

conv_base = Model(conv_base.input, output=out)

for layer in conv_base.layers[:-11]:
    layer.trainable = False

创建暹罗模型:

# We have 2 inputs, 1 for each picture
left_input = Input((160,60,3))
right_input = Input((160,60,3))

# We will use 2 instances of 1 network for this task
convnet = Sequential([
    InputLayer(input_shape=(160, 60, 3)),
    conv_base
])
# Connect each 'leg' of the network to each input
# Remember, they have the same weights
encoded_l = convnet(left_input)
encoded_r = convnet(right_input)

# Getting the L1 Distance between the 2 encodings
L1_layer = Lambda(lambda tensor:K.abs(tensor[0] - tensor[1]))

# Add the distance function to the network
L1_distance = L1_layer([encoded_l, encoded_r])

prediction = Dense(1,activation='sigmoid')(L1_distance)
siamese_net = Model(inputs=[left_input,right_input],outputs=prediction)

#optimizer = Adam(0.00006, decay=2.5e-4)
sgd = optimizers.RMSprop(lr=1e-4)
#//TODO: get layerwise learning rates and momentum annealing scheme described in paperworking
siamese_net.compile(loss="binary_crossentropy", optimizer=sgd, metrics=['accuracy'])

培训网络:

checkpoint = ModelCheckpoint('drive/My Drive/thesis/new change parametr/model/model-{epoch:03d}.h5', verbose=1, save_weights_only=False,monitor='val_loss', mode='auto')

newmodel=siamese_net.fit([left_train,right_train], targets,
          batch_size=64,
          epochs=2,
          verbose=1,shuffle=True, validation_data=([valid_left,valid_right],valid_targets),callbacks=[checkpoint])

模型存储在每个时期,但在加载时会出现以下错误:

loaded_model= load_model('drive/My Drive/thesis/new change parametr/model/model-001.h5')

错误:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-6-8de2283b355f> in <module>()
      1 
----> 2 loaded_model= load_model('drive/My Drive/thesis/new change parametr/model/model-001.h5')
      3 print('Load succesfuly')
      4 
      5 #siamese_net.load_weights('drive/My Drive/thesis/new change parametr/weight/model-{epoch:03d}.h5')

7 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py in preprocess_weights_for_loading(layer, weights, original_keras_version, original_backend, reshape)
    939                                  str(weights[0].size) + '. ')
    940             weights[0] = np.reshape(weights[0], layer_weights_shape)
--> 941         elif layer_weights_shape != weights[0].shape:
    942             weights[0] = np.transpose(weights[0], (3, 2, 0, 1))
    943             if layer.__class__.__name__ == 'ConvLSTM2D':

IndexError: list index out of range

我的代码在Google合作实验室执行。我已经在网上搜索过,问题可能出在暹罗架构的使用上。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

按如下所示创建网络时,加载保存的模型时发生错误:

input_shape = (160,60,3)
conv_base = VGG16(weights='imagenet',
                  include_top=False,
                  input_shape=(160, 60, 3))

output = conv_base.layers[-5].output

x=Flatten()(output)
x=Dense(512,activation='relu')(x)
out=Dense(512,activation='relu')(x)

conv_base = Model(conv_base.input, output=out)

for layer in conv_base.layers[:-11]:
    layer.trainable = False

# We have 2 inputs, 1 for each picture
left_input = Input((160,60,3))
right_input = Input((160,60,3))

# We will use 2 instances of 1 network for this task
convnet = Sequential([
    InputLayer(input_shape=(160, 60, 3)),
    conv_base
])

通过更改创建模型解决了问题:

# We have 2 inputs, 1 for each picture
left_input = Input((160,60,3))
right_input = Input((160,60,3))

conv_base = VGG16(weights='imagenet',
                  include_top=False,
                  input_shape=(160, 60, 3))

output = conv_base.layers[-5].output

x=Flatten()(output)
x=Dense(512,activation='relu')(x)
out=Dense(512,activation='relu')(x)

for layer in conv_base.layers[:-11]:
    layer.trainable = False 

convnet = Model(conv_base.input, output=out)

然后:

loaded_model= load_model('drive/My Drive/thesis/new change parametr/model/model-001.h5')
print('Load successfully')