遙感圖像語義分割知識點記錄


第一次做遙感圖像多分類的語義分割,有點力不從心。在此記錄一下一些遇到的bug。(https://github.com/milesial/Pytorch-UNet)源碼地址

1.TypeError: Cannot handle this data type

原因:在pytorch中tensor默認是CHW,而PIL中是HWC。在tensorboardX中的SummaryWriter.add_imge()的函數默認dataformats='CHW',並存在convert_to_HWC操作,致使自己本來是就是HWC的tensor,變成了WCH的numpy類型。從而導致在PIL的fromarray操作無法識別數據類型。

關於tensor、PIL以及numpy轉換的問題見下:

https://blog.csdn.net/daydayjump/article/details/88808394?utm_source=distribute.pc_relevant.none-task

 

2.RuntimeError: CUDA out of memory. Tried to allocate...

原因:batch_size或者輸入圖片尺寸太大了電腦吃不消,改小一點。

 

3.1only batches of spatial targets supported (non-empty 3D tensors) but got targets of size

原因:這個原因是因為在使用Crossentropyloss作為損失函數時,output=net(input)的output應該是[batchsize, channel, height, weight],而label則是[batchsize, height, weight],label是單通道灰度圖。需要用torch.squeeze取消一個維度。

而在BCELoss中,兩者都是[batchsize, channel, height, weight]

 

4.cuda runtime error: device-side assert triggerde at/pytorch...

原因:模型輸出的label與實際的label標簽類別數量是否相同?此外,label里不能有-1這個索引。

 

5.在改動Unet的代碼跑自己的數據集時,尤其要注意實例化的dataset中,數據的讀取是否存在問題。

一般使用os.listdir\os.path.join\splittext組合來讀取圖片路徑,有關os\shutil命令如下:

https://www.cnblogs.com/andy-x/p/10144658.html

 

6.predict出圖時,為了可視化結果。

有時不用Dataloader而是直接讀取圖像路徑。為了輸入圖像到網絡,需要先把圖片數據轉換成tensor,進行resize重采樣。然后用unsqueeze擴充1個維度(batchsize)再輸入網絡,得到輸出后,進行softmax計算概率后再減去batchsize的維度。這里存疑,為什么別人說是直接把輸出argmax就可以了?我這里最后得到的是bool型的矩陣。但是結果並不理想,目前還不清楚是為什么?

 

class Transformer(object):
def __init__(self, size, interpolation=Image.BILINEAR):
self.size = size
self.interpolation = interpolation
self.toTensor = transforms.ToTensor()

def __call__(self, img_):
img_ = img_.resize(self.size, self.interpolation)
img_ = self.toTensor(img_)
return img_


model = UNet(3,7) model_path = './checkpoint/Unet/model/netG_final.pth' model.load_state_dict(torch.load(model_path,map_location='cpu')) model.eval() test_image_path = r'D:\DeepGlobe_LandCover_CVPR2018\train\src\1.jpg' test_image = Image.open(test_image_path) print('Operating...') transformer = Transformer((256, 256)) img = transformer(test_image) img = img.unsqueeze(0) with torch.no_grad(): label_image = model(img) label_image = F.softmax(label_image, dim=1) #計算loss時,損失函數內置softmax,而網絡層沒有,所以要可視化,需要softmax 。多分類softmax,二分類sigmoid label_image = label_image.squeeze(0) full_mask = label_image.squeeze().numpy() full_mask = full_mask > 0.5

 7.補充一個rgb2label的代碼

 

def color2annotation(input_path, output_path): # image = scipy.misc.imread(input_path)
    # imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imread instead.
    image = imageio.imread(input_path) image = (image >= 128).astype(np.uint8) image = 4 * image[:, :, 0] + 2 * image[:, :, 1] + image[:, :, 2] cat_image = np.zeros((2448,2448), dtype=np.uint8) cat_image[image == 3] = 0  # (Cyan: 011) Urban land
    cat_image[image == 6] = 1  # (Yellow: 110) Agriculture land
    cat_image[image == 5] = 2  # (Purple: 101) Rangeland
    cat_image[image == 2] = 3  # (Green: 010) Forest land
    cat_image[image == 1] = 4  # (Blue: 001) Water
    cat_image[image == 7] = 5  # (White: 111) Barren land
    cat_image[image == 0] = 6  # (Black: 000) Unknown


    # scipy.misc.imsave(output_path, cat_image)
 imageio.imsave(output_path, cat_image) pass

 

 8.未完待續。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM