python - VGG16 迁移学习可变输出

标签 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 类,而下一次将同一图像分类为 Class 2.

最佳答案

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

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)

关于python - VGG16 迁移学习可变输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51096667/

相关文章:

python - 请将 torch.load 与 map_location=torch.device ('cpu' 一起使用)

python - 自定义数据集上的 Mask RCNN 训练挂起

Python 基础 : How to read N ints until '\n' is found in stdin

python - 在 Keras/TensorFlow 中使用纯 numpy 指标作为指标

python - 如何解释和转换 Keras 分类器的预测值?

python - 使用 Python 和 Keras 创建两层简单 RNN 模型

machine-learning - "Flatten"在Keras中的作用是什么?

Raspbian 上的 Python - "TypeError: ' numpy.int3 2' object is not iterable'“

python - Python 中的轮廓标签

python - 获取 'str' 对象没有 peewee DateTimeField 的属性 'isoformat'