![](/img/trans.png)
[英]Is this the best way to add an extra dimension to a numpy array in one line of code?
[英]Trying to reshape my numpy array to have an extra dimension
我目前正在使用此代碼來獲取圖像的灰度圖像表示形式,並以(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]
reshape
無法就地工作! 。
mask_instance = mask_instance.reshape((target_size[0], target_size[1], 1))
似乎np.reshape()
沒有添加尺寸,但是要使用的正確函數是mask_instance = np.expand_dims(mask_instance, axis=2)
。 軸為2,因為后端在通道最后一個模型上運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.