VGG16转移学习变化的输出

时间:2018-06-29 07:40:29

标签: python keras deep-learning transfer-learning

使用VGG16进行迁移学习时观察到奇怪的行为。

model = VGG16(weights='imagenet',include_top=True)
model.layers.pop()
model.layers.pop()

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

new_layer = Dense(2,activation='softmax')
inp = model.input
out = new_layer(model.layers[-1].output)

model = Model(inp,out)

但是,使用model.predict(image)时,输出在分类方面有所不同,即有时将图像分类为1类,而下次将同一图像分类为2类。

1 个答案:

答案 0 :(得分:5)

这是因为您没有设置种子。试试这个

import numpy as np
seed_value = 0
np.random.seed(seed_value)

model = VGG16(weights='imagenet',include_top=True)
model.layers.pop()
model.layers.pop()

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

new_layer = Dense(2, activation='softmax',
                  kernel_initializer=keras.initializers.glorot_normal(seed=seed_value),
                  bias_initializer=keras.initializers.Zeros())
inp = model.input
out = new_layer(model.layers[-1].output)

model = Model(inp,out)