【问题标题】:Invalid argument: Input to reshape is a tensor with 64 values, but the requested shape has 4无效参数:reshape 的输入是一个有 64 个值的张量,但请求的形状有 4 个
【发布时间】:2020-03-25 14:50:43
【问题描述】:

我正在从事人脸检测任务,我是计算机视觉的新手。但我遇到了一些问题,我尝试过一些改变,但对我不起作用。

import numpy as np
data = np.load("images.npy",allow_pickle=True)


IMAGE_HEIGHT=224
IMAGE_WIDTH=224
masks = np.zeros((int(data.shape[0]), IMAGE_HEIGHT, IMAGE_WIDTH))
X_train = np.zeros((int(data.shape[0]), IMAGE_HEIGHT, IMAGE_WIDTH, 3))
for index in range(data.shape[0]):
   img = data[index][0]
   img = cv2.resize(img, dsize=(IMAGE_HEIGHT, IMAGE_WIDTH), interpolation=cv2.INTER_CUBIC)
   try:
     img = img[:, :, :3]
   except:
     continue
   X_train[index] = preprocess_input(np.array(img, dtype=np.float32))
   for i in data[index][1]:
      x1 = int(i["points"][0]['x'] * IMAGE_WIDTH)
      x2 = int(i["points"][1]['x'] * IMAGE_WIDTH)
      y1 = int(i["points"][0]['y'] * IMAGE_HEIGHT)
      y2 = int(i["points"][1]['y'] * IMAGE_HEIGHT)
      masks[index][y1:y2, x1:x2] = 1


      model.fit(X_train,masks, epochs=30,batch_size = 1, verbose=1, callbacks=[checkpoint, reduce_lr, stop])

我已从 image.npy 读取图像数据

执行上述代码后出现以下错误:

  validArgumentError: 2 root error(s) found.
     (0) Invalid argument: Input to reshape is a tensor with 64 values, but the requested shape has 4
     [[{{node reshape/Reshape}}]]
     [[loss/mul/_1213]]
     (1) Invalid argument: Input to reshape is a tensor with 64 values, but the requested shape has 4
     [[{{node reshape/Reshape}}]]
     0 successful operations.
     0 derived errors ignored.

数据格式如下

    array([array([[[207, 216, 227, 255],
    [206, 216, 227, 255],
    [207, 216, 227, 255],
    ...,
    [ 35,  33,  34, 255],
    [ 35,  33,  34, 255],
    [ 35,  33,  34, 255]],

   [[207, 216, 227, 255],
    [207, 216, 227, 255],
    [207, 216, 227, 255],
    ...,
    [ 35,  32,  33, 255],
    [ 35,  33,  34, 255],
    [ 35,  33,  34, 255]],

   [[207, 216, 227, 255],
    [207, 216, 227, 255],
    [207, 215, 227, 255],
    ...,
    [ 35,  33,  33, 255],
    [ 35,  33,  34, 255],
    [ 35,  33,  34, 255]],

   ...,

   [[ 31,  21,  17, 255],
    [ 31,  22,  18, 255],
    [ 31,  22,  18, 255],
    ...,
    [  0,   1,   4, 255],
    [  0,   1,   4, 255],
    [  0,   1,   4, 255]],

   [[ 31,  22,  18, 255],
    [ 31,  22,  18, 255],
    [ 31,  22,  18, 255],
    ...,
    [  0,   1,   4, 255],
    [  0,   1,   4, 255],
    [  0,   1,   4, 255]],

   [[ 31,  22,  18, 255],
    [ 30,  22,  17, 255],
    [ 31,  22,  18, 255],
    ...,
    [  0,   1,   4, 255],
    [  0,   1,   4, 255],
    [  0,   1,   4, 255]]], dtype=uint8),

     list([{'label': ['Face'], 'notes': '', 'points': [{'x': 
     0.7053087757313109, 'y': 0.23260437375745527}, {'x': 
     0.7692307692307693, 'y': 0.36182902584493043}], 'imageWidth': 1280, 
     'imageHeight': 697}])],
     dtype=object)

我只是对什么是 x 和 y 以及如何解决这个问题感到困惑

【问题讨论】:

    标签: python numpy tensorflow


    【解决方案1】:

    您可能在代码的某些函数中使用了tf.reshape

    tf.reshape 不会改变张量中元素的顺序或总数。错误表明,您正试图将元素总数从 64 个减少到 4 个。

    在下面的示例中,我重新创建了您的场景,其中我将 shape 参数指定为 2 for tf.reshape,它不能容纳原始张量的所有元素,因此会引发错误 -

    代码 -

    %tensorflow_version 2.x
    import tensorflow as tf
    t1 = tf.Variable([1,2,2,4,5,6])
    
    t2 = tf.reshape(t1, 2)
    

    输出 -

    ---------------------------------------------------------------------------
    InvalidArgumentError                      Traceback (most recent call last)
    <ipython-input-3-0ff1d701ff22> in <module>()
          3 t1 = tf.Variable([1,2,2,4,5,6])
          4 
    ----> 5 t2 = tf.reshape(t1, 2)
    
    3 frames
    /usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)
    
    InvalidArgumentError: Input to reshape is a tensor with 6 values, but the requested shape has 2 [Op:Reshape]
    

    tf.reshape 应该是这样一种方式,即元素的排列可以改变,但元素的总数必须保持不变。所以解决方法是将形状更改为[2,3] -

    代码 -

    %tensorflow_version 2.x
    import tensorflow as tf
    t1 = tf.Variable([1,2,2,4,5,6])
    
    t2 = tf.reshape(t1, [2,3])
    print(t2)
    

    输出 -

    tf.Tensor(
    [[1 2 2]
     [4 5 6]], shape=(2, 3), dtype=int32)
    

    希望这能回答您的问题。快乐学习。

    【讨论】:

    • @Rajanikant Shukla - 希望我们已经回答了您的问题。如果您对答案感到满意,请您接受并投票。
    猜你喜欢
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2019-12-01
    • 1970-01-01
    相关资源
    最近更新 更多