欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pytorch利用Dataset讀取數(shù)據(jù)報錯問題及解決

 更新時間:2023年09月09日 16:09:12   作者:josenxiao  
這篇文章主要介紹了pytorch利用Dataset讀取數(shù)據(jù)報錯問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

報錯點

如下:

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

原來我其實沒有注意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

報錯中清晰提及這個問題

我突然反應(yīng)過來,是自己的讀取數(shù)據(jù)錯誤了:

應(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

測試打印數(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é)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python按指定列的空值刪除行的操作代碼

    Python按指定列的空值刪除行的操作代碼

    這篇文章主要介紹了Python按指定列的空值刪除行的操作代碼,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-01-01
  • python計算日期之間的放假日期

    python計算日期之間的放假日期

    這篇文章主要為大家詳細介紹了python計算日期之間的放假日期,實現(xiàn)自動查詢節(jié)日,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • 查看Python安裝路徑幾種方法小結(jié)

    查看Python安裝路徑幾種方法小結(jié)

    這篇文章主要介紹了查看Python安裝路徑幾種方法小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Python的五個標準數(shù)據(jù)類型你認識幾個

    Python的五個標準數(shù)據(jù)類型你認識幾個

    這篇文章主要為大家詳細介紹了Python標準數(shù)據(jù)類型,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • tensorflow模型的save與restore,及checkpoint中讀取變量方式

    tensorflow模型的save與restore,及checkpoint中讀取變量方式

    這篇文章主要介紹了tensorflow模型的save與restore,及checkpoint中讀取變量方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • Python利用memory_profiler查看內(nèi)存占用情況

    Python利用memory_profiler查看內(nèi)存占用情況

    memory_profiler是第三方模塊,用于監(jiān)視進程的內(nèi)存消耗以及python程序內(nèi)存消耗的逐行分析。本文將利用memory_profiler查看代碼運行占用內(nèi)存情況,感興趣的可以了解一下
    2022-06-06
  • python使用requests.post方法傳遞form-data類型的Excel數(shù)據(jù)的示例代碼

    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來模擬這種類型的請求發(fā)送呢?補充講解了python使用requests post請求發(fā)送form-data類型數(shù)據(jù),一起看看吧
    2024-01-01
  • python如何寫入dbf文件內(nèi)容及創(chuàng)建dbf文件

    python如何寫入dbf文件內(nèi)容及創(chuàng)建dbf文件

    這篇文章主要介紹了python如何寫入dbf文件內(nèi)容及創(chuàng)建dbf文件,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python 使用folium繪制leaflet地圖的實現(xiàn)方法

    Python 使用folium繪制leaflet地圖的實現(xiàn)方法

    今天小編就為大家分享一篇Python 使用folium繪制leaflet地圖的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Python讀取圖像并顯示灰度圖的實現(xiàn)

    Python讀取圖像并顯示灰度圖的實現(xiàn)

    這篇文章主要介紹了Python讀取圖像并顯示灰度圖的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12

最新評論