Python keras预先训练的模型提供了一个新的输入占位符
我有一个受过训练的模特。像这样Python keras预先训练的模型提供了一个新的输入占位符,python,tensorflow,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(
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})
错误
FailedPremissionError:尝试使用未初始化值批处理\u规范化\u 86/移动\u表示……您的错误如下:
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)
玩得开心 如果要获得梯度d(preds)/d(x),可以使用
K.gradients
并将其封装到K.function
从tensorflow.keras导入后端为K
grad=K.梯度(my_model.output,my_model.input)
get_grad=K.函数(my_model.input,grad)
测试数据=np.random.rand(1299299,3)
res\u grad=获取(测试\u img)
如果要在图形中添加新的占位符,则必须遵循以下步骤:-
- 使用
函数将模型的权重保存在一个文件中my\u model.Save()
- 通过添加占位符和加载模型权重再次构建图形
input_layer = tf.placeholder(tf.float32, shape=(None, 299, 299,3))
model_inceptionv3_conv = InceptionV3(weights='imagenet', include_top=False, input_tensor=input_layer)
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=input_layer, outputs=predictions)
my_model.load_weights('model.h5')
现在您可以使用my_model.predict()或sess.run()进行预测,如-
sess.run(predictions, feed_dict={input_layer: x_val})
或
您还可以参考我的GitHub jupyter笔记本,了解如何将预处理步骤添加到Keras模型中-我发现我自己的代码存在问题。我用错了ses。我以前使用tensorflow.keras设置sess,但我的原始代码仅基于keras,而不是tensorflow.keras 以前
import tensorflow as tf
tf.keras.backend.set_session(sess)
之后
然后,可以运行以下代码。未初始化的值消失了
x = tf.placeholder(tf.float32, shape=(None, 299, 299,
3))
preds = model(x)
sess.run(preds, feed_dict={x: x_val})
此外,我将输出层的sigmoid激活替换为softmax激活,这在多类场景中更有意义;)我知道怎么做。但我只是不想喂张量。函数my_model.predict()的输入是numpy我输入张量的原因是我想计算网络输出到x的梯度。公式为d(preds)/d(x)
import keras
keras.backend.set_session(sess)
x = tf.placeholder(tf.float32, shape=(None, 299, 299,
3))
preds = model(x)
sess.run(preds, feed_dict={x: x_val})