21

我下面this教程學習TensorFlow苗條,但在運行下面的代碼爲盜:Tensorflow修身:類型錯誤:預期INT32,得到了包含類型「_Message」的張量列表,而不是

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

我似乎得到這個組的錯誤:

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. 

這很奇怪,因爲所有這些代碼是從他們的官方指南。我是新來的TF和任何幫助,將不勝感激。

+0

回滾到TF 0.11版本似乎擺脫錯誤的。 –

回答

58

我在使用1.0版本時遇到了同樣的問題,我可以在無需回退以前的版本的情況下工作。

該問題是由api中的更改引起的。這種討論幫我找到了解決辦法:https://groups.google.com/a/tensorflow.org/forum/#!msg/discuss/OePXmC9kJ7o/SRErOoYCDQAJ

你只需要更新所有tf.concat行

例如

net = tf.concat(3, [branch_0, branch_1, branch_2, branch_3]) 

應改爲

net = tf.concat([branch_0, branch_1, branch_2, branch_3], 3) 

注意:

我能夠使用沒有pro的模型blem。但是當我想要加載預訓練體重時,我仍然有錯誤。 似乎自從他們製作檢查點文件以後,超薄模塊發生了一些變化。代碼創建的圖形和檢查點文件中存在的圖形是不同的。

注2:

我能夠通過向所有conv2d層biases_initializer=None

+0

我認爲這應該顛倒?沒有? tf.concat(concat_dim,values,name ='concat') – deef

+3

在tensorflow r1.0上它發生了變化,我將編輯我的帖子,以確定我使用的是哪個版本。 – rAyyy

0

時,我所做的工作我得到了同樣的錯誤使用pretrain權重inception_resnet_v2。

我發現

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

輸出爲shape=(10, 64, 64)

代碼想要concat輸出[0]輸出[9] =>獲取新形狀(640,64)。

但「tf.concat」API可能不允許這樣做。

(train_labels同此)

於是我寫信給

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

它可以運行!

9

明確寫出參數的名稱可以解決問題。

代替

net = tf.concat(3, [branch_0, branch_1, branch_2, branch_3]) 

使用

net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3]) 
相關問題