简体   繁体   English

ValueError:Shape必须为1级,但对于'ROIAlign / Crop'(op:'CropAndResize')排名为0,输入形状为:[2,360,475,3],[1,4],[],[2]

[英]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 . 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].

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')

[] means that it was a scalar (aka tensor with rank=0 ), and the op is expecting a 1D tensor ( rank=1 ). []表示它是一个标量(也就是具有rank=0张量),并且op期望一维张量( 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. 尝试将[batch_inds]类的内容传递给crop_and_resize操作crop_and_resize ,或者以其他方式更改它以使其成为向量,而不是标量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:形状必须为 0 级,但对于“ReadFile”(操作:“ReadFile”)为 1 级,输入形状为:[1] - ValueError: Shape must be rank 0 but is rank 1 for 'ReadFile' (op: 'ReadFile') with input shapes: [1] Tensorflow-ValueError:形状必须为0级,但输入范围为[],[10],[]的“范围”(操作数:“范围”)的“极限”必须为1级 - Tensorflow - ValueError: Shape must be rank 0 but is rank 1 for 'limit' for 'range' (op: 'Range') with input shapes: [], [10], [] ValueError: Shape must be rank 2 but is rank 1 for &#39;MatMul&#39; (op: &#39;MatMul&#39;) with input shape: [2], [2,3] - ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul') with input shapes: [2], [2,3] ValueError:形状必须为 2 级,但对于 'in_top_k/InTopKV2'(操作:'InTopKV2')为 4 级,输入形状为:[?,28,28,10], [?], [] - ValueError: Shape must be rank 2 but is rank 4 for 'in_top_k/InTopKV2' (op: 'InTopKV2') with input shapes: [?,28,28,10], [?], [] 形状必须为2级,但输入形状为[100,100],[?, 15,100]的&#39;MatMul_46&#39;(op:&#39;MatMul&#39;)的等级为3 - Shape must be rank 2 but is rank 3 for 'MatMul_46' (op: 'MatMul') with input shapes: [100,100], [?,15,100] ValueError:形状必须为 2 级,但“MatMul”为 1 级 - ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' TensorFlow 推荐者 - ValueError:形状必须为 2 级,但为 3 级 - TensorFlow Recommenders - ValueError: Shape must be rank 2 but is rank 3 Tensorflow:ValueError:Shape必须是等级2,但是等级3 - Tensorflow : ValueError: Shape must be rank 2 but is rank 3 ValueError:Shape必须是2级,但是&#39;MatMul&#39;的排名是3 - ValueError: Shape must be rank 2 but is rank 3 for 'MatMul' Tensorflow: ValueError: Shape must be rank 4 but is rank 5 - Tensorflow: ValueError: Shape must be rank 4 but is rank 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM