tf.image.crop_and_resize:关于形状大小的错误

时间:2019-06-24 16:28:21

标签: python numpy tensorflow tensor

我正在尝试使用tf.image.crop_and_resize裁剪图像,但是出现了一个我不明白的错误,下面将进一步说明。

我试图将我的sample_excerpt重塑为不同的形状,因为这是我的错误抱怨的地方,特别是与tf.image.crop_and_resize希望图像为密集的形状张量这一事实有关[1] 。这让我感到困惑,因为在文档中它必须是[batch,height,width,depth]形状。

import matplotlib.pyplot as plt
from keras.preprocessing import image
import numpy as np

# grab slice or numpy arrays from dataset
feature = hdf5_file['val_features'][0, ...]
sample_excerpt = feature[:,1400:1515]

scales = list(np.arange(0.8, 1.0, 0.01))
# make a list for box dimensions
boxes = np.zeros((len(scales), 4))

# fill the boxes array with sequentially changing  dimensions
for i, scale in enumerate(scales):
    x1 = y1 = 0.5 - (0.5 * scale)
    x2 = y2 = 0.5 + (0.5 * scale)
    boxes[i] = [x1, y1, x2, y2]

excerpt_reshaped = sample_excerpt.reshape((1,sample_excerpt.shape[0], sample_excerpt.shape[1],1))

crops = tf.image.crop_and_resize([excerpt_reshaped], boxes=boxes, box_ind=np.zeros(len(scales)), crop_size=(80, 115))

我希望将numpy形状转换为(批处理,高度,宽度,深度)将使其与tf.image.crop_and_resize兼容,但是却出现以下错误:


ValueError                                Traceback (most recent call last)
/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
    510                 as_ref=input_arg.is_ref,
--> 511                 preferred_dtype=default_dtype)
    512           except TypeError as err:

/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx, accept_symbolic_tensors)
   1174     if ret is None:
-> 1175       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
   1176 

/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
    303   _ = as_ref
--> 304   return constant(v, dtype=dtype, name=name)
    305 

/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name)
    244   return _constant_impl(value, dtype, shape, name, verify_shape=False,
--> 245                         allow_broadcast=True)
    246 

/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
    282           value, dtype=dtype, shape=shape, verify_shape=verify_shape,
--> 283           allow_broadcast=allow_broadcast))
    284   dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)

/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape, allow_broadcast)
    474                          (values, list(nparray.shape),
--> 475                           _GetDenseDimensions(values)))
    476 

ValueError: Argument must be a dense tensor: [array([[[-0.2746672 , -0.2746672 , -0.2746672 , ..., -0.2746672 ,
         -0.2746672 , -0.2746672 ],
        [-0.2746672 , -0.2746672 , -0.2746672 , ..., -0.2746672 ,
         -0.2746672 , -0.2746672 ],
        [-0.2746672 , -0.2746672 , -0.2746672 , ..., -0.2746672 ,
         -0.2746672 , -0.2746672 ],
        ...,
        [ 4.01187315,  4.28077045,  3.79459085, ...,  2.70409744,
          3.30734571,  3.01116682],
        [ 2.82011643,  3.15533266,  3.1251727 , ...,  2.45012212,
          2.20143238,  1.92358158],
        [ 0.0115974 , -0.18517987, -0.13355623, ..., -0.2746672 ,
         -0.2746672 , -0.2746672 ]]])] - got shape [1, 1, 80, 115], but wanted [1].

0 个答案:

没有答案