【问题标题】:Trying to reshape my numpy array to have an extra dimension试图重塑我的 numpy 数组以获得额外的维度
【发布时间】:2019-11-10 20:36:24
【问题描述】:

我目前正在使用此代码来获取图像的灰度图像表示,并以(512, 370, 1) 数组的格式表示它。

img_instance = cv2.imread(df.iloc[i][x_col]) / 255.
img_instance = cv2.resize(img_instance, (target_size[1], target_size[0]))
mask_instance = cv2.imread(df.iloc[i][y_col], cv2.IMREAD_GRAYSCALE) / 255.
mask_instance = cv2.resize(mask_instance, (target_size[1], target_size[0]))
print(mask_instance.shape)
mask_instance.reshape(target_size[0], target_size[1], 1)
print(mask_instance.shape)

第一次打印,我得到一个(512, 370),然后是第二个打印语句上的(512, 370)。这对我来说是个问题,因为我正在尝试构建一个 Keras 模型,其中我有一个看起来像下面的层的层,我试图将遮罩实例拟合到该层。

__________________________________________________________________________________________________
conv2d_19 (Conv2D)              (None, 512, 370, 1)  17          activation_18[0][0]

【问题讨论】:

  • mask_instance = mask_instance[:, :, np.newaxis] 是向数组添加尾随维度的另一种方式。

标签: python-3.x numpy opencv keras


【解决方案1】:
  • reshape 不能就地工作!
  • 形状必须是元组。

mask_instance = mask_instance.reshape((target_size[0], target_size[1], 1))

【讨论】:

    【解决方案2】:

    似乎np.reshape() 没有添加维度,但要使用的正确函数是mask_instance = np.expand_dims(mask_instance, axis=2)。轴为 2,因为后端在通道最后一个模型上运行。

    【讨论】:

      猜你喜欢
      • 2021-02-16
      • 2023-03-23
      • 2016-11-07
      • 1970-01-01
      • 2017-06-10
      • 2020-03-18
      • 2021-03-15
      • 2014-10-16
      • 2013-07-08
      相关资源
      最近更新 更多