使用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类。
答案 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)