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

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

1.TypeError: Cannot handle this data typegit

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

關於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...ide

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

 

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

緣由:這個緣由是由於在使用Crossentropyloss做爲損失函數時,output=net(input)的output應該是[batchsize, channel, height, weight],而label則是[batchsize, height, weight],label是單通道灰度圖。須要用torch.squeeze取消一個維度。this

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

 

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

緣由:模型輸出的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.未完待續。

 

原文出處:https://www.cnblogs.com/ljwgis/p/12335760.html

相關文章
相關標籤/搜索