python - keras 预训练模型提供新的输入占位符

标签 python tensorflow keras

我有一个经过训练的模型。像这样

model_inceptionv3_conv = InceptionV3(weights='imagenet', include_top=False)
for layer in model_inceptionv3_conv.layers:
    layer.trainable = False
x = model_inceptionv3_conv.output
x = GlobalAveragePooling2D()(x)
predictions = Dense(NB_CLASSES, activation='sigmoid', name='predictions')(x)
my_model = Model(inputs=model_inceptionv3_conv.input, outputs=predictions)
my_model.fit(...)

现在我不想为该模型提供占位符,但发生了某些值未初始化的情况。这段代码 preds = model(x) 会生成一个新图吗?

x = tf.placeholder(tf.float32, shape=(None, 299, 299,
                                          3))
preds = model(x)
sess.run(preds, feed_dict={x: x_val})

错误 FailedPreconditionError:尝试使用未初始化的值batch_normalization_86/moving_mean......

最佳答案

您的错误在这里:

preds = my_model(x)

应该是:

preds = my_model.predict(x)

这是一个工作示例:

NB_CLASSES = 2
model_inceptionv3_conv = InceptionV3(weights='imagenet', include_top=False)
for layer in model_inceptionv3_conv.layers:
    layer.trainable = False
x = model_inceptionv3_conv.output
x = GlobalAveragePooling2D()(x)
predictions = Dense(NB_CLASSES, activation='softmax', name='predictions')(x)
my_model = Model(inputs=model_inceptionv3_conv.input, outputs=predictions)
test_img = np.random.rand(1,299,299,3)
preds = my_model.predict(test_img)

玩得开心!

关于python - keras 预训练模型提供新的输入占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58544272/

相关文章:

Keras:model.evaluate_generator 和 model.predict_generator 有什么区别

python-3.x - 使用 Keras 和 TensorFlow 后端可重现结果

python - 我可以将类定义放入 __init__.py 中吗?

Python:如果两列没有出现在另一个 pandas 列中,如何删除 Pandas 中的行?

java - 多语言应用程序的项目目录结构

python - 如何在numpy python数组的行尾添加数字

tensorflow - 将 Lasagne BatchNormLayer 转换为 Keras BatchNormalization 层

python - Tensorflow:将张量作为一个整体进行字符串化(不创建字符串张量)

python - Tensorflow:AttributeError: 'function' 对象没有属性 'graph'

python - Keras 输入层和 Tensorflow 占位符之间的区别