亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 PyTorch 中進行數據增強后得到壞圖像

在 PyTorch 中進行數據增強后得到壞圖像

幕布斯6054654 2022-11-29 16:59:12
我正在研究一個核分割問題,我試圖在染色組織圖像中識別細胞核的位置。給定的訓練數據集有一張染色組織的圖片和一個帶有細胞核位置的掩碼。由于數據集很小,我想嘗試在 PyTorch 中進行數據增強,但在這樣做之后,由于某種原因,當我輸出我的蒙版圖像時,它看起來很好,但相應的組織圖像不正確。我所有的訓練圖像都在X_trainshape(128, 128, 3)中,相應的掩碼在Y_trainshape中(128, 128, 1),類似的交叉驗證圖像和掩碼分別在X_val和Y_val中。Y_trainY_val有dtype = np.bool,有。_ X_train_X_valdtype = np.uint8在數據增強之前,我會這樣檢查我的圖像:fig, axis = plt.subplots(2, 2)axis[0][0].imshow(X_train[0].astype(np.uint8))axis[0][1].imshow(np.squeeze(Y_train[0]).astype(np.uint8))axis[1][0].imshow(X_val[0].astype(np.uint8))axis[1][1].imshow(np.squeeze(Y_val[0]).astype(np.uint8))輸出如下: Before Data Augmentation對于數據擴充,我定義了一個自定義類,如下所示:在這里我導入torchvision.transforms.functional了TF和torchvision.transforms as transforms。images_np并且masks_np是 numpy 數組的輸入。class Nuc_Seg(Dataset):def __init__(self, images_np, masks_np):    self.images_np = images_np    self.masks_np = masks_npdef transform(self, image_np, mask_np):    ToPILImage = transforms.ToPILImage()    image = ToPILImage(image_np)    mask = ToPILImage(mask_np.astype(np.int32))    angle = random.uniform(-10, 10)    width, height = image.size    max_dx = 0.2 * width    max_dy = 0.2 * height    translations = (np.round(random.uniform(-max_dx, max_dx)), np.round(random.uniform(-max_dy, max_dy)))    scale = random.uniform(0.8, 1.2)    shear = random.uniform(-0.5, 0.5)    image = TF.affine(image, angle = angle, translate = translations, scale = scale, shear = shear)    mask = TF.affine(mask, angle = angle, translate = translations, scale = scale, shear = shear)    image = TF.to_tensor(image)    mask = TF.to_tensor(mask)    return image, maskdef __len__(self):    return len(self.images_np)我得到這個作為我的輸出: After Data Augmentation 1當我將行更改axis_1.imshow(img.astype(np.uint8))為時axis_1.imshow(img),我得到這張圖片: 數據擴充后 2面具的圖像是正確的,但由于某種原因,細胞核的圖像是錯誤的。使用 時.astype(np.uint8),組織圖像完全是黑色的。沒有.astype(np.uint8),核的位置是正確的,但配色方案全亂了(我希望圖像像數據增強之前看到的那樣,灰色或粉紅色),加上網格中同一圖像的 9 個副本由于某種原因顯示。你能幫我得到組織圖像的正確輸出嗎?
查看完整描述

1 回答

?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

您正在將圖像轉換為 PyTorch 張量,并且在 PyTorch 中圖像的大小為[C, H, W]。當您將它們可視化時,您正在將張量轉換回 NumPy 數組,其中圖像的大小為[H, W, C]。因此,您正在嘗試重新排列維度,但您使用的是torch.reshape,它不會交換維度,而只會以不同的方式對數據進行分區。

一個例子使這一點更清楚:

# Incrementing numbers with size 2 x 3 x 3

image = torch.arange(2 * 3 * 3).reshape(2, 3, 3)

# => tensor([[[ 0,  1,  2],

#             [ 3,  4,  5],

#             [ 6,  7,  8]],

#

#            [[ 9, 10, 11],

#             [12, 13, 14],

#             [15, 16, 17]]])


# Reshape keeps the same order of elements but for a different size

# The numbers are still incrementing from left to right

image.reshape(3, 3, 2)

# => tensor([[[ 0,  1],

#             [ 2,  3],

#             [ 4,  5]],

#

#            [[ 6,  7],

#             [ 8,  9],

#             [10, 11]],

#

#            [[12, 13],

#             [14, 15],

#             [16, 17]]])

要重新排序您可以使用的尺寸permute:


# Dimensions are swapped

# Now the numbers increment from top to bottom

image.permute(1, 2, 0)

# => tensor([[[ 0,  9],

#             [ 1, 10],

#             [ 2, 11]],

#

#            [[ 3, 12],

#             [ 4, 13],

#             [ 5, 14]],

#

#            [[ 6, 15],

#             [ 7, 16],

#             [ 8, 17]]])

使用 時.astype(np.uint8),組織圖像完全是黑色的。


PyTorch 圖像表示為值介于 [0, 1] 之間的浮點數,但 NumPy 使用介于 [0, 255] 之間的整數值。將浮點值轉換為np.uint8將導致只有 0 和 1,其中不等于 1 的所有內容都將設置為 0,因此整個圖像為黑色。


您需要將這些值乘以 255 以將它們置于 [0, 255] 范圍內。


img = img.permute(1, 2, 0) * 255

img = img.numpy().astype(np.uint8)

當您將張量轉換為 PIL 圖像時,此轉換也會自動完成transforms.ToPILImage(或者TF.to_pil_image如果您更喜歡函數式版本,則使用)并且 PIL 圖像可以直接轉換為 NumPy 數組。這樣您就不必擔心尺寸、值范圍或類型,上面的代碼可以替換為:


img = np.array(TF.to_pil_image(img))


查看完整回答
反對 回復 2022-11-29
  • 1 回答
  • 0 關注
  • 155 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號