我试图获取代码的输出,但错误在with tf.control_dependencies()
。错误如下所示:原始代码来自enter link description here
Traceback (most recent call last):
File "croptest.py", line 80, in <module>
crop(Image,boxes,batch_inds);
File "croptest.py", line 55, in crop
with tf.control_dependencies([assert_op, images, batch_inds]):
File "/home/ubuntu/Desktop/WK/my_project/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3936, in control_dependencies
return get_default_graph().control_dependencies(control_inputs)
File "/home/ubuntu/Desktop/WK/my_project/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3665, in control_dependencies
c = self.as_graph_element(c)
File "/home/ubuntu/Desktop/WK/my_project/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2708, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "/home/ubuntu/Desktop/WK/my_project/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2797, in _as_graph_element_locked
% (type(obj).__name__, types_str))
TypeError: Can not convert a list into a Tensor or Operation.
我相信代码没有错误,我只是好奇控制依赖关系是如何工作的,即使是所有输入都是如此。 我运行的代码如下:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
def crop(images, boxes, batch_inds, stride = 1, pooled_height = 2, pooled_width = 2, scope='ROIAlign'):
"""Cropping areas of features into fixed size
Params:
--------
images: a 4-d Tensor of shape (N, H, W, C)
boxes: rois in the original image, of shape (N, ..., 4), [x1, y1, x2, y2]
batch_inds:
Returns:
--------
A Tensor of shape (N, pooled_height, pooled_width, C)
"""
with tf.name_scope(scope):
#
boxes = boxes / (stride + 0.0)
print("boxes again=",boxes)
boxes = tf.reshape(boxes, [-1, 4])
print ("2=======================================================")
print("boxes again=",boxes)
# normalize the boxes and swap x y dimensions
shape = tf.shape(images)
boxes = tf.reshape(boxes, [-1, 2]) # to (x, y)
print ("3=======================================================")
print(boxes)
xs = boxes[:, 0]
ys = boxes[:, 1]
print ("4=======================================================")
print(xs,ys)
xs = xs / tf.cast(shape[2], tf.float32)
ys = ys / tf.cast(shape[1], tf.float32)
print ("5=======================================================")
print("again xs,ys",xs,ys)
boxes = tf.concat([ys[:, tf.newaxis], xs[:, tf.newaxis]], axis=1)
boxes = tf.reshape(boxes, [-1, 4]) # to (y1, x1, y2, x2)
print ("6=======================================================")
print("again boxes", boxes)
#if batch_inds is False:
# num_boxes = tf.shape(boxes)[0]
# batch_inds = tf.zeros([num_boxes], dtype=tf.int32, name='batch_inds')
# batch_inds = boxes[:, 0] * 0
# batch_inds = tf.cast(batch_inds, tf.int32)
# assert_op = tf.Assert(tf.greater(tf.shape(images)[0], tf.reduce_max(batch_inds)), [images, batch_inds])
assert_op = tf.Assert(tf.greater(tf.size(images), 0), [images, batch_inds])
print ("7=======================================================")
print("assert_op", assert_op)
print ("8=======================================================")
with tf.control_dependencies([assert_op, images, batch_inds]):
return tf.image.crop_and_resize(images, boxes, batch_inds,
[pooled_height, pooled_width],
method='bilinear',
name='Crop')
这是我设置的输入:
Image =[[[[1, 1, 1,1], [2, 2, 2,2]], [[3,3, 3, 3], [4,4, 4, 4]]]]
print ("=======================================================")
box = [[1, 1, 2, 2]]
boxes = tf.constant(box, tf.float32)
batch_inds=[1]
batch_inds = np.zeros((4,), dtype=np.int32)
batch_inds = tf.convert_to_tensor(batch_inds)
print("boxes=", boxes)
print (Image)
print(tf.shape(Image));
crop(Image,boxes,batch_inds);
如果我不想修改crop()
功能,我的输入有什么问题?
谢谢。
答案 0 :(得分:0)
我通过添加
解决了这个问题 在tf.convert_to_tensor (Image)
之后 Image =[[[[1, 1, 1,1], [2, 2, 2,2]], [[3,3, 3, 3], [4,4, 4, 4]]]]