2016-11-09 56 views
0

我正在研究tensoflow,并且想测试slim的示例。当我命令./scripts/train_lenet_on_mnist.sh,程序运行到eval_image_classifier给出一个类型错误,错误信息如下:带有TypeError的Tensoflow slim eval_image_classifier无法将dict_values转换为张量或操作

I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcublas.so.8.0 locally 
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcudnn.so.5 locally 
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcufft.so.8.0 locally 
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcuda.so.1 locally 
I tensorflow/stream_executor/dso_loader.cc:111] successfully opened CUDA library libcurand.so.8.0 locally 
INFO:tensorflow:Scale of 0 disables regularizer. 
INFO:tensorflow:Evaluating /tmp/lenet-model/model.ckpt-20002 
INFO:tensorflow:Starting evaluation at 2016-11-09-02:55:57 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:951] Found device 0 with properties: 
name: Quadro K5000 
major: 3 minor: 0 memoryClockRate (GHz) 0.7055 
pciBusID 0000:03:00.0 
Total memory: 3.94GiB 
Free memory: 3.61GiB 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:972] DMA: 0 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] 0: Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:1041] Creating TensorFlow device (/gpu:0) -> (device: 0, name: Quadro K5000, pci bus id: 0000:03:00.0) 
INFO:tensorflow:Executing eval ops 
INFO:tensorflow:Executing eval_op 1/100 
INFO:tensorflow:Error reported to Coordinator: <class 'TypeError'>, Fetch argument dict_values([<tf.Tensor 'accuracy/update_op:0' shape=() dtype=float32>, <tf.Tensor 'recall_at_5/update_op:0' shape=() dtype=float32>]) has invalid type <class 'dict_values'>, must be a string or Tensor. (Can not convert a dict_values into a Tensor or Operation.) 
Traceback (most recent call last): 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 218, in init 
fetch, allow_tensor=True, allow_operation=True)) 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 2455, in as_graph_element 
return self._as_graph_element_locked(obj, allow_tensor, allow_operation) 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 2547, in _as_graph_element_locked 
% (type(obj).name, types_str)) 
TypeError: Can not convert a dict_values into a Tensor or Operation. 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
File "eval_image_classifier.py", line 191, in <module> 
tf.app.run() 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/platform/app.py", line 30, in run 
sys.exit(main(sys.argv[:1] + flags_passthrough)) 
File "eval_image_classifier.py", line 187, in main 
variables_to_restore=variables_to_restore) 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/slim/python/slim/evaluation.py", line 359, in evaluate_once 
global_step=global_step) 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/slim/python/slim/evaluation.py", line 260, in evaluation 
sess.run(eval_op, eval_op_feed_dict) 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 717, in run 
run_metadata_ptr) 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 902, in _run 
fetch_handler = _FetchHandler(self._graph, fetches, feed_dict_string) 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 358, in init 
self._fetch_mapper = _FetchMapper.for_fetch(fetches) 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 189, in for_fetch 
return _ElementFetchMapper(fetches, contraction_fn) 
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 222, in init 
% (fetch, type(fetch), str(e))) 
TypeError: Fetch argument dict_values([<tf.Tensor 'accuracy/update_op:0' shape=() dtype=float32>, <tf.Tensor 'recall_at_5/update_op:0' shape=() dtype=float32>]) has invalid type <class 'dict_values'>, must be a string or Tensor. (Can not convert a dict_values into a Tensor or Operation.) 

我不知道发生了什么节目,我因此未修订的任何代码,只需从github上下载代码包,然后下载和下载数据,这样可以给出正确的结果。是否有帮助我?我在线等待。谢谢

回答

1

问题是兼容的python2python3。正如我使用python3进行解释,但词典中的键不同于p ython2python3

Python2,简直就是一个字典对象上调用keys()函数将返回你所期望的,在Python3keys()不再返回list,而是一个视图对象,这样可避免类型错误和兼容性可以通过简单地维持将dict_keys对象转换为一个列表,然后可以在Python2Python3中正常编制索引。

我编辑eval_image_classifier使用eval_op=list(names_to_updates.values()),那么它可以很好地工作。

0

为eval_image_classifier.py其他python3变化

for name, value in names_to_values.iteritems(): 

to 

for name, value in names_to_values.items(): 
相关问题