【问题标题】:Why can't I load my saved siamese model in Keras?为什么我无法在 Keras 中加载我保存的连体模型?
【发布时间】:2020-03-29 05:21:14
【问题描述】:

我正在尝试构建一个个人 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 Colaboratory 上执行。我在网上搜索过,问题可能是因为使用了连体架构。任何帮助将不胜感激!

【问题讨论】:

    标签: python keras callback google-colaboratory


    【解决方案1】:

    创建网络时加载保存的模型时出错如下:

    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')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-18
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 2021-11-30
      相关资源
      最近更新 更多