【发布时间】:2017-02-06 14:45:25
【问题描述】:
我正在尝试运行此代码
import os
import tensorflow as tf
from datasets import imagenet
from nets import inception_resnet_v2
from preprocessing import inception_preprocessing
checkpoints_dir = 'model'
slim = tf.contrib.slim
batch_size = 3
image_size = 299
with tf.Graph().as_default():
with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
logits, _ = inception_resnet_v2.inception_resnet_v2([1, 299, 299, 3], num_classes=1001, is_training=False)
probabilities = tf.nn.softmax(logits)
init_fn = slim.assign_from_checkpoint_fn(
os.path.join(checkpoints_dir, 'inception_resnet_v2_2016_08_30.ckpt'),
slim.get_model_variables('InceptionResnetV2'))
with tf.Session() as sess:
init_fn(sess)
imgPath = '.../image_3.jpeg'
testImage_string = tf.gfile.FastGFile(imgPath, 'rb').read()
testImage = tf.image.decode_jpeg(testImage_string, channels=3)
np_image, probabilities = sess.run([testImage, probabilities])
probabilities = probabilities[0, 0:]
sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])]
names = imagenet.create_readable_names_for_imagenet_labels()
for i in range(15):
index = sorted_inds[i]
print((probabilities[index], names[index]))
但是TF显示错误:ValueError: rank of shape must be at least 4 not: 1
我认为问题在于输入张量形状[1, 299, 299, 3]。如何为 3 通道 JPEG 图像输入张量???
还有一个类似的问题 (Using pre-trained inception_resnet_v2 with Tensorflow)。我在代码 input_tensor 中看到了 - 不幸的是,有解释什么是 input_tensor。也许我在问一些不言而喻的事情,但我卡住了!非常感谢您的任何建议!
【问题讨论】:
-
你能验证你的testImage向量是一个4维数组吗
-
是的,testimage 是 4D 张量。如果我在
with tf.Graph().as_default()之后写imgPath, testImage_string and test_image而不是[1, 299, 299, 3]写test_image一切正常。我的意图是手动放置 4D 输入张量,然后在会话部分我想在许多不同的图像上测试模型。 TF
标签: python computer-vision tensorflow deep-learning