Python Tensorflow Slim:TypeError:预期为 int32,得到包含“_Message”类型张量的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41813665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tensorflow Slim: TypeError: Expected int32, got list containing Tensors of type '_Message' instead
提问by Deepankar Arya
I am following thistutorial for learning TensorFlow Slim but upon running the following code for Inception:
我正在按照本教程学习 TensorFlow Slim,但在为 Inception 运行以下代码时:
import numpy as np
import os
import tensorflow as tf
import urllib2
from datasets import imagenet
from nets import inception
from preprocessing import inception_preprocessing
slim = tf.contrib.slim
batch_size = 3
image_size = inception.inception_v1.default_image_size
checkpoints_dir = '/tmp/checkpoints/'
with tf.Graph().as_default():
url = 'https://upload.wikimedia.org/wikipedia/commons/7/70/EnglishCockerSpaniel_simon.jpg'
image_string = urllib2.urlopen(url).read()
image = tf.image.decode_jpeg(image_string, channels=3)
processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False)
processed_images = tf.expand_dims(processed_image, 0)
# Create the model, use the default arg scope to configure the batch norm parameters.
with slim.arg_scope(inception.inception_v1_arg_scope()):
logits, _ = inception.inception_v1(processed_images, num_classes=1001, is_training=False)
probabilities = tf.nn.softmax(logits)
init_fn = slim.assign_from_checkpoint_fn(
os.path.join(checkpoints_dir, 'inception_v1.ckpt'),
slim.get_model_variables('InceptionV1'))
with tf.Session() as sess:
init_fn(sess)
np_image, probabilities = sess.run([image, probabilities])
probabilities = probabilities[0, 0:]
sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])]
plt.figure()
plt.imshow(np_image.astype(np.uint8))
plt.axis('off')
plt.show()
names = imagenet.create_readable_names_for_imagenet_labels()
for i in range(5):
index = sorted_inds[i]
print('Probability %0.2f%% => [%s]' % (probabilities[index], names[index]))
I seem to be getting this set of errors:
我似乎遇到了这组错误:
Traceback (most recent call last):
File "DA_test_pred.py", line 24, in <module>
logits, _ = inception.inception_v1(processed_images, num_classes=1001, is_training=False)
File "/home/deepankar1994/Desktop/MTP/TensorFlowEx/TFSlim/models/slim/nets/inception_v1.py", line 290, in inception_v1
net, end_points = inception_v1_base(inputs, scope=scope)
File "/home/deepankar1994/Desktop/MTP/TensorFlowEx/TFSlim/models/slim/nets/inception_v1.py", line 96, in inception_v1_base
net = tf.concat(3, [branch_0, branch_1, branch_2, branch_3])
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 1053, in concat
dtype=dtypes.int32).get_shape(
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 651, in convert_to_tensor
as_ref=False)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 716, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 176, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/constant_op.py", line 165, in constant
tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 367, in make_tensor_proto
_AssertCompatible(values, dtype)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_util.py", line 302, in _AssertCompatible
(dtype.name, repr(mismatch), type(mismatch).__name__))
TypeError: Expected int32, got list containing Tensors of type '_Message' instead.
This is strange because all of this code is from their official guide. I am new to TF and any help would be appreciated.
这很奇怪,因为所有这些代码都来自他们的官方指南。我是 TF 的新手,任何帮助将不胜感激。
回答by rAyyy
I got the same problem when using the 1.0 released and I could make it work without having to roll back on a previous version.
我在使用 1.0 发布时遇到了同样的问题,我可以让它工作而不必回滚到以前的版本。
The problem is caused by change in the api. That discussion helped me to find the solution: Google group > Recent API Changes in TensorFlow
问题是由 api 的更改引起的。那次讨论帮助我找到了解决方案:谷歌组 > TensorFlow 中的最近 API 变化
You just have to update all the line with tf.concat
您只需要使用 tf.concat 更新所有行
for example
例如
net = tf.concat(3, [branch_0, branch_1, branch_2, branch_3])
should be changed to
应该改为
net = tf.concat([branch_0, branch_1, branch_2, branch_3], 3)
Note:
笔记:
I was able to use the models without problem. But I still got error afterward when wanting to load the pretrained weight. Seems that the slim module got several changed since they made the checkpoint file. The graph created by the code and the one present in the checkpoint file were different.
我能够毫无问题地使用这些模型。但是当我想加载预训练的重量时,我仍然遇到错误。自从他们制作检查点文件以来,slim 模块似乎发生了一些变化。代码创建的图形与检查点文件中的图形不同。
Note2:
笔记2:
I was able to use the pretrain weights for inception_resnet_v2 by adding to all conv2d layer biases_initializer=None
通过添加到所有 conv2d 层,我能够使用 inception_resnet_v2 的预训练权重 biases_initializer=None
回答by Fariborz Ghavamian
explicitly writing the name of the arguments solves the problem.
明确地写出参数的名字可以解决这个问题。
instead of
代替
net = tf.concat(3, [branch_0, branch_1, branch_2, branch_3])
use
用
net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
回答by nabin
I found most people answering wrong way. Its just due to the change in the tf.concat. It works in the following way.
我发现大多数人都回答错误。这只是由于 tf.concat 的变化。它按以下方式工作。
net = tf.concat(3, [branch_0, branch_1, branch_2, branch_3])
net = tf.concat(3, [branch_0, branch_1, branch_2, branch_3])
use the following
使用以下
net = tf.concat(values=[branch_0, branch_1, branch_2, branch_3],axis=3,)
net = tf.concat(values=[branch_0, branch_1, branch_2, branch_3],axis=3,)
Remember while passing the keyword arguments should be before the others.
请记住,传递关键字参数时应在其他参数之前。
回答by u7625321
I got same error when I did the work.
我在做工作时遇到了同样的错误。
I found that
我找到
logits = tf.nn.xw_plus_b(tf.concat(outputs, 0), w, b)
loss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(
labels=tf.concat(train_labels, 0), logits=logits))
The output is shape=(10, 64, 64)
.
输出是shape=(10, 64, 64)
。
The code want concat outputs[0] to outputs[9] => get a new shape(640,64).
代码希望将输出 [0] 连接到输出 [9] => 获得一个新形状 (640,64)。
But the "tf.concat" API may not allow to do this.
但是“tf.concat”API 可能不允许这样做。
(train_labels same to this)
(train_labels 与此相同)
So I write to
所以我写信给
A = tf.concat(0,[outputs[0],outputs[1]])
A = tf.concat(0,[A,outputs[2]])
A = tf.concat(0,[A,outputs[3]])
A = tf.concat(0,[A,outputs[4]])
A = tf.concat(0,[A,outputs[5]])
A = tf.concat(0,[A,outputs[6]])
A = tf.concat(0,[A,outputs[7]])
A = tf.concat(0,[A,outputs[8]])
A = tf.concat(0,[A,outputs[9]])
B = tf.concat(0,[train_labels[0],train_labels[1]])
B = tf.concat(0,[B,train_labels[2]])
B = tf.concat(0,[B,train_labels[3]])
B = tf.concat(0,[B,train_labels[4]])
B = tf.concat(0,[B,train_labels[5]])
B = tf.concat(0,[B,train_labels[6]])
B = tf.concat(0,[B,train_labels[7]])
B = tf.concat(0,[B,train_labels[8]])
B = tf.concat(0,[B,train_labels[9]])
logits = tf.nn.xw_plus_b(tf.concat(0, A), w, b)
loss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(
labels=tf.concat(0, B), logits=logits))
It can run!
它可以运行!