Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Shape must be rank 1 but is rank 0 for 'ROIAlign/Crop' (op: 'CropAndResize') with input shapes: [2,360,475,3], [1,4], [], [2]

I tried to give all input in this function but it comes out problem like below , i not sure what is the empty [] is . There are 2 image image in RGB and the original code is from https://github.com/CharlesShang/FastMaskRCNN/blob/master/libs/layers/crop.py.

Traceback (most recent call last):
  File "croptest.py", line 77, in <module>
    crop(img, boxes, batch_inds,1,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 2632, in create_op
    set_shapes_for_outputs(ret)
  File "/home/ubuntu/Desktop/WK/my_project/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1911, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "/home/ubuntu/Desktop/WK/my_project/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1861, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "/home/ubuntu/Desktop/WK/my_project/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 595, in call_cpp_shape_fn
    require_shape_fn)
  File "/home/ubuntu/Desktop/WK/my_project/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 659, in _call_cpp_shape_fn_impl
    raise ValueError(err.message)
ValueError: Shape must be rank 1 but is rank 0 for 'ROIAlign/Crop' (op: 'CropAndResize') with input shapes: [2,360,475,3], [1,4], [], [2].

the code i run is shown as below.

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)
  """
  with tf.name_scope(scope):
    boxes = [x / (stride+0.0) for x in boxes]
    boxes = tf.reshape(boxes, [-1, 4])
    print(images)

    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
crop(img, boxes, batch_inds,1,7,7,'ROIAlign')
like image 966
Go Go Gadget 2 Avatar asked Dec 07 '17 11:12

Go Go Gadget 2


1 Answers

[] means that it was a scalar (aka tensor with rank=0), and the op is expecting a 1D tensor (rank=1). Try to pass something like [batch_inds] to the crop_and_resize op, or change it in some other way to make it a vector, not a scalar.

like image 116
Max Galkin Avatar answered Oct 16 '22 09:10

Max Galkin