pytorch利用Dataset讀取數(shù)據(jù)報(bào)錯(cuò)問題及解決
報(bào)錯(cuò)點(diǎn)
如下:
Traceback (most recent call last):
File "read_data.py", line 100, in <module>
for i , (image,seg) in enumerate(train_loader):
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 819, in __next__
return self._process_data(data)
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 846, in _process_data
data.reraise()
File "/usr/local/lib/python3.6/dist-packages/torch/_utils.py", line 369, in reraise
raise self.exc_type(msg)
TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py", line 178, in _worker_loop
data = fetcher.fetch(index)
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
data = [self.dataset[idx] for idx in possibly_batched_index]
File "read_data.py", line 91, in __getitem__
])(img)
File "/usr/local/lib/python3.6/dist-packages/torchvision/transforms/transforms.py", line 61, in __call__
img = t(img)
File "/usr/local/lib/python3.6/dist-packages/torchvision/transforms/transforms.py", line 238, in __call__
return F.center_crop(img, self.size)
File "/usr/local/lib/python3.6/dist-packages/torchvision/transforms/functional.py", line 374, in center_crop
w, h = img.size
TypeError: 'int' object is not iterable
原來(lái)我其實(shí)沒有注意Datset與PIL下面的Image的關(guān)系:
def __getitem__(self, index):
img = cv2.imread(self.image_name[index],cv2.COLOR_BGR2RGB)
#img = np.transpose(img,(2,1,0))
img = cv2.resize(img,(self.size,self.size))
seg = cv2.imread(self.image_seg[index],cv2.COLOR_BGR2RGB)
seg = cv2.resize(seg,(self.size,self.size) )
seg = convert_from_color_segmentation(seg)
#seg = torch.from_numpy(seg)
if self.transform is not None:
img = self.transform(img)
return img , seg報(bào)錯(cuò)中清晰提及這個(gè)問題
我突然反應(yīng)過來(lái),是自己的讀取數(shù)據(jù)錯(cuò)誤了:
應(yīng)該為:
def __getitem__(self, index):
#img = cv2.imread(self.image_name[index],cv2.COLOR_BGR2RGB)
img = Image.open(self.image_name[index])
#img = np.transpose(img,(2,1,0))
#img = cv2.resize(img,(self.size,self.size))
seg = cv2.imread(self.image_seg[index],cv2.COLOR_BGR2RGB)
seg = cv2.resize(seg,(self.size,self.size) )
seg = convert_from_color_segmentation(seg)
#seg = torch.from_numpy(seg)
if self.transform is not None:
img = self.transform(img)
return img , seg測(cè)試打印數(shù)據(jù)
完美解決
transform = transforms.Compose([transforms.Resize((300,300)),transforms.RandomCrop((224,224)),transforms.ToTensor(),transforms.Normalize([0.485,0.456,0.406],[0.229,0.224,0.225])])
#transform = transforms.Compose([
# transforms.CenterCrop((278,278)),transforms.Resize((224,224)),transforms.ToTensor()
# ])
train_data = GetParasetData(size=224,train=True,transform=transform)
train_loader = DataLoader(train_data,batch_size=64,shuffle=True,num_workers=2)
for i , (image,seg) in enumerate(train_loader):
print(image.shape,seg.shape)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python的五個(gè)標(biāo)準(zhǔn)數(shù)據(jù)類型你認(rèn)識(shí)幾個(gè)
這篇文章主要為大家詳細(xì)介紹了Python標(biāo)準(zhǔn)數(shù)據(jù)類型,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03
tensorflow模型的save與restore,及checkpoint中讀取變量方式
這篇文章主要介紹了tensorflow模型的save與restore,及checkpoint中讀取變量方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-05-05
Python利用memory_profiler查看內(nèi)存占用情況
memory_profiler是第三方模塊,用于監(jiān)視進(jìn)程的內(nèi)存消耗以及python程序內(nèi)存消耗的逐行分析。本文將利用memory_profiler查看代碼運(yùn)行占用內(nèi)存情況,感興趣的可以了解一下2022-06-06
python使用requests.post方法傳遞form-data類型的Excel數(shù)據(jù)的示例代碼
這篇文章介紹了python使用requests.post方法傳遞form-data類型的Excel數(shù)據(jù)的示例代碼,某些post接口,需要發(fā)送multipart/form-data類型的數(shù)據(jù),如何使用python requests來(lái)模擬這種類型的請(qǐng)求發(fā)送呢?補(bǔ)充講解了python使用requests post請(qǐng)求發(fā)送form-data類型數(shù)據(jù),一起看看吧2024-01-01
python如何寫入dbf文件內(nèi)容及創(chuàng)建dbf文件
這篇文章主要介紹了python如何寫入dbf文件內(nèi)容及創(chuàng)建dbf文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Python 使用folium繪制leaflet地圖的實(shí)現(xiàn)方法
今天小編就為大家分享一篇Python 使用folium繪制leaflet地圖的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-07-07
Python讀取圖像并顯示灰度圖的實(shí)現(xiàn)
這篇文章主要介紹了Python讀取圖像并顯示灰度圖的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

