简体   繁体   English

如何在Tensorflow输入管道中根据受过训练的模型进行预测?

[英]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.

相关问题 如何基于训练有素的Tensorflow模型进行预测? - How to make predictions based on a trained Tensorflow Model? 如何从 tensorflow 中的训练模型获得测试数据集的预测 - How to get predictions of test dataset from a trained model in tensorflow 如何使用经过训练的CNN张量流模型测试来自另一个.py文件的输入图像 - How can I use a trained CNN tensorflow model to test an input image from another .py file 如何使用训练有素的Keras模型进行新的预测? - How can I get use of trained Keras model to make new predictions? 如何使用保存在 HDF5 文件中的 Keras 训练模型进行预测? - How can I use a Keras trained model saved in a HDF5 file to make predictions? 训练 model 后,从 Keras/Tensorflow 获取预测 - Get predictions from Keras/Tensorflow once model is trained 如何将在 tensorflow 2 中训练的模型转换为 tensorflow 1 冻结图 - How can I convert a model trained in tensorflow 2 to a tensorflow 1 frozen graph 如何使用经过训练的 tensorflow.network 从新数据生成预测? - How to generate predictions from new data using trained tensorflow network? 如何从训练有素的Tensorflow分类器中获得班级预测? - How to get class predictions from a trained Tensorflow classifier? 如何使用 Google Cloud 上 Tensorflow Training 中的 model.ckpt 文件进行预测? - How can I use the model.ckpt Files from Tensorflow Training on Google Cloud for making predictions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM