[英]How can I make predictions from a trained model inside a Tensorflow input pipeline?
I am trying to train a model for emotion recognition, which uses one of VGG's layer's output as an input. 我正在尝试训练一种情绪识别模型,该模型使用VGG图层的输出之一作为输入。
I could manage what I want by running the prediction in a first step, saving the extracted features and then using them as input to my network, but I am looking for a way to do the whole process at once. 我可以通过第一步运行预测,保存提取的特征,然后将其用作我的网络的输入来管理所需的东西,但是我正在寻找一种可以立即完成整个过程的方法。
The second model uses a concatenated array of feature maps as input (I am working with video data), so I am not able to simply wire it to the output of VGG. 第二种模型使用要素地图的级联数组作为输入(我正在处理视频数据),因此无法简单地将其连接到VGG的输出。
I tried to use a map operation as depicted in the tf.data.dataset
API documentations this way : 我试图通过
tf.data.dataset
API文档中描述的方式使用map操作:
def trimmed_vgg16():
vgg16 = tf.keras.applications.vgg16.VGG16(input_shape=(224,224,3))
trimmed = tf.keras.models.Model(inputs=vgg16.get_input_at(0),
outputs=vgg16.layers[-3].get_output_at(0))
return trimmed
vgg16 = trimmed_vgg16()
def _extract_vgg_features(images, labels):
pred = vgg16_model.predict(images, batch_size=batch_size, steps=1)
return pred, labels
dataset = #load the dataset (image, label) as usual
dataset = dataset.map(_extract_vgg_features)
But I'm getting this error : Tensor Tensor("fc1/Relu:0", shape=(?, 4096), dtype=float32) is not an element of this graph
which is pretty explicit. 但是我遇到了这个错误:
Tensor Tensor("fc1/Relu:0", shape=(?, 4096), dtype=float32) is not an element of this graph
它是非常明确的。 I'm stuck here, as I don't see a good way of inserting the trained model in the same graph and getting predictions "on the fly". 我被困在这里,因为我看不到在同一张图中插入训练好的模型并“即时”获得预测的好方法。
Is there a clean way of doing this or something similar ? 有一种干净的方法可以做到这一点吗?
Edit: missed a line. 编辑:错过了一行。
Edit2: added details Edit2:添加了详细信息
You should be able to connect the layers by first creating the vgg16
and then retrieving the output of the model as such and afterward you can use that tensor as an input to your own network. 您应该能够通过首先创建
vgg16
,然后像这样检索模型的输出来连接各层,然后可以将该张量用作您自己的网络的输入。
vgg16 = tf.keras.applications.vgg16.VGG16(input_shape=(224,224,3))
network_input = vgg16.get_input_at(0)
vgg16_out = vgg16.layers[-3].get_output_at(0) # use this tensor as input to your own network
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.