web-dev-qa-db-ja.com

ValueError:形状はランク1である必要がありますが、入力形状が[2,360,475,3]、[1,4]、[]、[2]の「ROIAlign / Crop」(op:「CropAndResize」)ではランク0です。

この関数にすべての入力を入れようとしましたが、以下のような問題が発生し、空の[]が何であるかわかりません。 RGBには2つの画像画像があり、元のコードは 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].

私が実行するコードは次のように表示されます。

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')
4
Wai Kin Chung

[]は、それがスカラー(rank=0のテンソルとも呼ばれる)であり、opが1Dテンソル(rank=1)を期待していることを意味します。 [batch_inds]のようなものをcrop_and_resize opに渡すか、他の方法で変更して、スカラーではなくベクトルにしてみてください。

2
Max Galkin