首页 文章

Tensorflow:如何将Tensor提供给训练有素的神经网络?

提问于
浏览
1

我不能让训练有素的神经网络工作 . 我想将一个numpy数组(基本上是一张图片)提供给我训练有素的网络 .

with tf.Session() as sess:
    # Unpersists graph from file
    with tf.gfile.FastGFile(graph_path, 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        _ = tf.import_graph_def(graph_def, name='')
    softmax_tensor = sess.graph.get_tensor_by_name('y_pred:0')
    predictions = sess.run(softmax_tensor,
                           {'DecodeJpeg/contents:0': image_data})

我总是得到这个错误:

TypeError:无法将feed_dict键解释为Tensor:名称'DecodeJpeg / contents:0'指的是不存在的Tensor . 图中不存在'DecodeJpeg / contents'操作 .

我为这个feed-dict尝试了很多不同的键,但我无法正确使用它 . 我使用dataset-api训练网络,这意味着我没有任何可以填充的tf.placeholder . 相反,网络正在通过包含张量对象的数据集上的迭代器进行馈送 . tfrecord文件是使用谷歌的this script创建的

启动我的模型功能:

input_layer = tf.reshape(features["image"], [-1, _DEFAULT_IMAGE_SIZE, _DEFAULT_IMAGE_SIZE, 3])

摘自最后:

predictions = {
    # Generate predictions (for PREDICT and EVAL mode)
    "classes": tf.argmax(input=logits, axis=1),
    # Add `softmax_tensor` to the graph. It is used for PREDICT and by the
    # `logging_hook`.
    "probabilities": tf.nn.softmax(logits, name="y_pred")
}
if mode == tf.estimator.ModeKeys.PREDICT:
    return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)

# Calculate Loss (for both TRAIN and EVAL modes)
onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=2)
loss = tf.losses.softmax_cross_entropy(onehot_labels=onehot_labels, logits=logits)

这是Topology

如何获取这些预测/如何将图像提供给网络?

2 回答

  • 0

    您可以为输入图层命名,按名称检索它,就像使用softmax张量一样,然后将其输入numpy数组 . 这是它的样子:

    # First, name your input tensor
    input_layer = tf.reshape(features["image"], [-1, _DEFAULT_IMAGE_SIZE, _DEFAULT_IMAGE_SIZE, 3], name='input_layer')
    
    ...
    
    predictions = sess.run('y_pred:0',
                           {'input_layer:0': image_data})
    

    只要image_data的形状为[1,_DEFAULT_IMAGE_SIZE,_DEFAULT_IMAGE_SIZE,3],这应该可以工作

    关于无法按名称访问DecodedJpeg张量的原因的解释是tf.Dataset运算符不在主图中 .

  • 0

    image_data 预计将是一个张量 . 您可以使用下面的代码段读取jpeg图像作为张量( image_file 是jpeg文件的位置)

    # decode the JPEG image as tensor
    image_data = tf.cast(tf.image.decode_jpeg(image_file), tf.float32)
    

相关问题