简体   繁体   中英

Tensorflow: How to feed a Tensor to a trained neural network?

I cant get my trained neural network to work. I want to feed a numpy array (essentially a picture) to my trained network.

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})

I always get this error:

TypeError: Cannot interpret feed_dict key as Tensor: The name 'DecodeJpeg/contents:0' refers to a Tensor which does not exist. The operation, 'DecodeJpeg/contents', does not exist in the graph.

I have tried many different keys for that feed-dict, but i cant get it right. I trained the network with the dataset-api, that means I dont have any tf.placeholder i can fill. Instead the network is beeing feed through an iterator over the dataset which contains tensor objects. The tfrecord file was created using this script from google

Start of my model function:

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

Excerpt from the end:

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)

This is the Topology

How can I get those predictions / how can I feed an image to the network?

image_data is expected to be a tensor. You can read jpeg image as a tensor using below snippet ( image_file is location of jpeg file)

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

You could give a name to your input layer, retrieve it by name like you do for the softmax tensor and then feed it your numpy array. Here is what it looks like :

# 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})

This should work as long as image_data's shape is [1, _DEFAULT_IMAGE_SIZE, _DEFAULT_IMAGE_SIZE, 3]

The explanation as of why you can't access the DecodedJpeg tensors by name is that tf.Dataset operators are not in the main graph.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM