2017-12-20 23 views
0

코드를 실행할 때 다음과 같은 오류가 나타납니다. 나는 "feat"로 명명 된 Tensor의 가치가 무엇인지 알고 싶습니다.텐서 값을 인쇄 할 수 없습니다

Traceback (most recent call last): 
    File "croptest.py", line 80, in <module> 
    print (sess.run(feat)) 
    File "/home/ubuntu/Desktop/WK/my_project/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 895, in run 
    run_metadata_ptr) 
    File "/home/ubuntu/Desktop/WK/my_project/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1124, in _run 
    feed_dict_tensor, options, run_metadata) 
    File "/home/ubuntu/Desktop/WK/my_project/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run 
    options, run_metadata) 
    File "/home/ubuntu/Desktop/WK/my_project/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1340, in _do_call 
    raise type(e)(node_def, op, message) 
tensorflow.python.framework.errors_impl.OutOfRangeError: box_ind has values outside [0, batch) 
    [[Node: ROIAlign/Crop = CropAndResize[T=DT_UINT8, extrapolation_value=0, method="bilinear", _device="/job:localhost/replica:0/task:0/cpu:0"](ROIAlign/Crop/image, ROIAlign/Reshape_2, ROIAlign/Crop/box_ind, ROIAlign/Crop/crop_size)]] 

Caused by op u'ROIAlign/Crop', defined at: 
    File "croptest.py", line 73, in <module> 
    feat =crop(img, boxes, batch_inds,16,7,7,'ROIAlign') 
    File "croptest.py", line 64, in crop 
    name='Crop') 
    File "/home/ubuntu/Desktop/WK/my_project/lib/python2.7/site-packages/tensorflow/python/ops/gen_image_ops.py", line 166, in crop_and_resize 
    name=name) 
    File "/home/ubuntu/Desktop/WK/my_project/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op 
    op_def=op_def) 
    File "/home/ubuntu/Desktop/WK/my_project/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op 
    original_op=self._default_original_op, op_def=op_def) 
    File "/home/ubuntu/Desktop/WK/my_project/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1204, in __init__ 
    self._traceback = self._graph._extract_stack() # pylint: disable=protected-access 

OutOfRangeError (see above for traceback): box_ind has values outside [0, batch) 
    [[Node: ROIAlign/Crop = CropAndResize[T=DT_UINT8, extrapolation_value=0, method="bilinear", _device="/job:localhost/replica:0/task:0/cpu:0"](ROIAlign/Crop/image, ROIAlign/Reshape_2, ROIAlign/Crop/box_ind, ROIAlign/Crop/crop_size)]] 

입력 이미지는 2 개의 RGB 이미지입니다. " init = tf.global_variables_initializer()"전에 모든 코드를 실행할 수 있습니다. 텐서를 인쇄하는 방법이 잘못 되었습니까? 텐서를 인쇄하는 더 나은 방법. 다음은 내가 실행 한 코드입니다 : 나는 오류가 tensorflow.shape의 사용에서 올 수 있다고 생각

from __future__ import absolute_import 
    from __future__ import division 
    from __future__ import print_function 
    import glob 
    import tensorflow as tf 
    import numpy as np 
    import cv2 

def crop(images, boxes, batch_inds, stride, pooled_height, pooled_width, scope): 
    """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) 
    """ 
    #print(tf.shape(images)) 
    with tf.name_scope(scope): 
    boxes = [x/(stride+0.0) for x in boxes] 
    boxes = tf.reshape(boxes, [-1, 4]) 
    print(images) 
    # normalize the boxes and swap x y dimensions 
    print(images.shape) 
    shape = tf.shape(images) 
    boxes = tf.reshape(boxes, [-1, 2]) # to (x, y) 
    xs = boxes[:, 0] 
    ys = boxes[:, 1] 
    xs = xs/tf.cast(shape[2], tf.float32) 
    ys = ys/tf.cast(shape[1], tf.float32) 
    boxes = tf.concat([ys[:, tf.newaxis], xs[:, tf.newaxis]], axis=1) 
    boxes = tf.reshape(boxes, [-1, 4]) # to (y1, x1, y2, x2) 
    assert_op = tf.Assert(tf.greater(tf.size(images), 0), [images, batch_inds]) 
    print(assert_op) 
    print("-----------------------") 
    print(images.astype('float')) 
    print("-----------------------") 
    print(batch_inds) 
    x=images.astype('float') 
    print("-----------------------") 
    print(batch_inds) 
    print("-----------------------") 
    print(pooled_height) 
    print("-----------------------") 
    pools =[pooled_height, pooled_width] 

    arg = tf.convert_to_tensor(x, dtype=tf.float32) 
    arg1 = tf.convert_to_tensor(batch_inds) 
    with tf.control_dependencies([assert_op, arg,arg1 ]): 
     return tf.image.crop_and_resize(images, boxes, batch_inds, 
             pools, 
             method='bilinear', 
             name='Crop') 
images = [cv2.imread(file) for file in glob.glob("/home/ubuntu/Pictures/TeImage/*.png")] 
img= np.asarray(images) 
boxes = [100, 100, 200, 200] 
batch_inds=[2] 
feat =crop(img, boxes, batch_inds,16,7,7,'ROIAlign') 

init=tf.global_variables_initializer() 
sess=tf.Session() 
sess.run(init) 
print (sess.run(feat)) 

답변

0

, 그것은 텐서를 위해 디자인 된 것 같습니다, 어쩌면 이미지 배열은 적절한 tnesor에없는 양식을 작성하십시오. 그러나 tp를 np (bumpy.shape)로 바꾸면 이미지의 모양을 인쇄하고 수행 할 작업을 정확하게 수행해야하지만 입력에 유연성이 있어야합니다.

+0

모양 = tf.shape (이미지)를 np (bumpy.shape.image)로 변경한다는 것을 의미합니까? –

+0

print (tf.shape (images)) – jgd10

+0

crop() 함수는 함수를 수정하지 않고 텐서를 출력하고 싶다면 계산을 수행하기 위해 tf.shape 함수가 필요합니다. 가능한가? –