【问题标题】:different output of model.fit (after loading model no training) and model.predict in keraskeras中model.fit(加载模型无训练后)和model.predict的不同输出
【发布时间】:2019-10-28 16:00:52
【问题描述】:

def get_vgg_twoeyes() 是我的模型的定义。 我已经加载了一个预训练模型,它是在同一台计算机上训练的,然后我想对模型进行微调。在重新训练模型之前,我将 model.trainable 设置为 false 以确保模型的权重是固定的。在我训练之前,权重与保存的权重相同。我发现 model.fit 的输出与 model.predict 的输出不同。 因为我假设与 model.predict 具有相同权重的 model.fit 应该输出相同的结果,因为 model.trainable 为 false,这意味着 model.fit 的行为符合 model.predict。

def get_vgg_twoeyes(optimizer='adam', model_type='VGG16',fc1_size=1024, fc2_size=512, fc3_size=256):

kern_init = initializers.glorot_normal()
img_input = Input(shape=(36, 60, 3), name='img_input')
headpose_input = Input(shape=(2,), name='headpose_input')

# create the base pre-trained model
if model_type == 'VGG19':
    base_model = VGG19(input_tensor=img_input, weights='imagenet', include_top=False)
elif model_type == 'VGG16':
    base_model = VGG16(input_tensor=img_input, weights='imagenet', include_top=False)
else:
    raise Exception('Unknown model type in get_vgg_twoeyes')

# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)

# let's add a fully-connected layer
x = Dense(fc1_size, kernel_initializer=kern_init)(x)
x = concatenate([x, headpose_input])
x = BatchNormalization()(x)
x = Activation('relu')(x)

x = Dense(fc2_size, kernel_initializer=kern_init)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)

gaze_predictions = Dense(2, kernel_initializer=kern_init, name='pred_gaze')(x)

# this is the model we will train
model = Model(inputs=[img_input, headpose_input], outputs=gaze_predictions)
model.compile(optimizer=optimizer, loss=angle_loss, metrics=['accuracy', accuracy_angle])
return model

# fine-tune the model
models=load_model(model_path + "15Fold" + prefix + ''+str(i) + 
    suffix + ".h5",custom_objects={'accuracy_angle':accuracy_angle, 
     'angle_loss': angle_loss}))

model.trainable=False
adam = Adam(lr=0.0001, beta_1=0.9, beta_2=0.95)
model.compile(optimizer=adam, loss=angle_loss, metrics= ['accuracy', accuracy_angle]) 
model.fit({'img_input':cal_images,'headpose_input':cal_headposes},
cal_gazes,shuffle=False,batch_size=32,epochs=1,callbacks= 
  [losshistory()])

predgaze=model.predict({'img_input': cal_images, 'headpose_input': 
   cal_headposes},  batch_size=2,verbose=1)

【问题讨论】:

    标签: python keras deep-learning


    【解决方案1】:

    在设置model.trainable=False 之后,您可能必须再次编译模型。否则,您可以像

    一样手动单独冻结图层

    for l in model.layers: l.trainable=False

    【讨论】:

    • 我使用model.compile再次编译模型。我已经检查了model.fit之前和之后的权重。权重没有改变。
    • 我认为可能 batch_norm 没有设置为验证模式。基本上,batchnorm 在训练时计算批量平均值和方差,但在测试时计算平均值和方差,因此根据您是在训练还是在测试,会导致不同的输出。为确保使用 model.fit 获得相同的输出,您可能需要为所有层显式设置 trainable = False。另外,您可能想阅读此blog.datumbox.com/…
    • 非常感谢,原因是批标准化层。尽管在将所有可训练层设置为 false 后我得到了不同的输出,但仍然存在不同的输出。可能这和你说的是训练还是测试有关。但是在将批量标准化可训练设置为 false 之前,输出很接近。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多