国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

DataLoader問題解決:RuntimeError: stack expects each tensor to be equal size, but got [3, 200, 200]entry1

這篇具有很好參考價值的文章主要介紹了DataLoader問題解決:RuntimeError: stack expects each tensor to be equal size, but got [3, 200, 200]entry1。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

? ? ? ? 最近,在數(shù)據(jù)集處理并載入DataLoader進行訓練的時候出現(xiàn)了問題:

RuntimeError: stack expects each tensor to be equal size, 
but got [3, 200, 200] at entry 0 and [1, 200, 200] at entry 1

? ? ? ? 我看了一下,大意就是維度也就是通道數(shù)不匹配,所以我覺得應該是數(shù)據(jù)集圖片出現(xiàn)了問題。以下是我的普通數(shù)據(jù)集處理代碼:

import torch
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader
import os
from PIL import Image

transform = transforms.Compose([
            transforms.RandomCrop((200, 200)),    #需要進行同意大小,不然會報錯
            transforms.ToTensor(),
])

class PreprocessDataset(Dataset):
    """預處理數(shù)據(jù)集類"""

    def __init__(self, HRPath):
        """初始化預處理數(shù)據(jù)集類"""
        img_names = os.listdir(HRPath)
        self.HR_imgs = [HRPath + "/" + img_name for img_name in img_names]

    def __len__(self):
        """獲取數(shù)據(jù)長度"""
        return len(self.HR_imgs)

    def __getitem__(self, index):
        """獲取數(shù)據(jù)"""
        HR_img = self.HR_imgs[index]

        HR_img = Image.open(HR_img)

        HR_img = transform(HR_img)

        return HR_img     

if __name__ == '__main__':
    HRPath = r"D:\datasets\ImageNet\train"
    datasets = PreprocessDataset(HRPath)
    trainData = DataLoader(datasets, batch_size=1, shuffle=False)
    for i, HR_img in enumerate(trainData):
        print(i, HR_img.shape)

? ? ? ? ? 我一張一張圖片放入DataLoader,然后按順序一張一張的查看它們的維度,然后出現(xiàn)如下錯誤:

'''結(jié)果'''
146 torch.Size([1, 3, 200, 200])
147 torch.Size([1, 3, 200, 200])

ValueError: empty range for randrange() (0,-55, -55)

? ? ? ? 我找到出錯前按順序查到了第149(從0開始)張圖片的維度,點開發(fā)現(xiàn)這張圖片的最短邊小于200,不能進行隨機裁剪transforms.RandomCrop((200, 200)),所以我進行了transforms.Resize(400),把圖片最短邊放大到400。

transform = transforms.Compose([
            transforms.Resize(400),
            transforms.RandomCrop((200, 200)),    #需要進行同意大小,不然會報錯
            transforms.ToTensor(),
])

? ? ? ? 在次運行并不發(fā)生錯誤,但這是在DataLoader的batch_size=1張圖片的情況下。所以把batch_size改成多張圖片再次運行:

if __name__ == '__main__':
    HRPath = r"D:\datasets\ImageNet\train"
    # os.listdir(HRPath)
    datasets = PreprocessDataset(HRPath)
    a = datasets[89]
    print(a.shape)
    trainData = DataLoader(datasets, batch_size=16, shuffle=False)
    for i, HR_img in enumerate(trainData):
        print(i, HR_img.shape)

? ? ? ? 發(fā)生錯誤:

'''結(jié)果'''
0 torch.Size([16, 3, 200, 200])
1 torch.Size([16, 3, 200, 200])
2 torch.Size([16, 3, 200, 200])
3 torch.Size([16, 3, 200, 200])
4 torch.Size([16, 3, 200, 200])

RuntimeError: stack expects each tensor to be equal size, 
but got [3, 200, 200] at entry 0 and [1, 200, 200] at entry 9

? ? ? ? 從不出錯的結(jié)果上看,定位圖片問題所在的索引應該在80-96之間,那么縮小問題圖片的方位,把batch_size=2:

if __name__ == '__main__':
    HRPath = r"D:\datasets\ImageNet\train"
    # os.listdir(HRPath)
    datasets = PreprocessDataset(HRPath)
    a = datasets[89]
    print(a.shape)
    trainData = DataLoader(datasets, batch_size=2, shuffle=False)
    for i, HR_img in enumerate(trainData):
        print(i, HR_img.shape)

? ? ? ? 錯誤定位到第89或者第90張圖片:

'''結(jié)果'''
0 torch.Size([2, 3, 200, 200])
...
...
43 torch.Size([2, 3, 200, 200])

RuntimeError: stack expects each tensor to be equal size, but got [3, 200, 200] at entry 0 and [1, 200, 200] at entry 1

? ? ? ? 輸出第89張圖片的維度:

if __name__ == '__main__':
    HRPath = r"D:\datasets\ImageNet\train"
    # os.listdir(HRPath)
    datasets = PreprocessDataset(HRPath)
    a = datasets[89]
    print(a.shape)

? ? ? ? 結(jié)果:

torch.Size([1, 200, 200])

? ? ? ? 真的是通道數(shù)不統(tǒng)一,醉了啊!

? ? ? ? 解決方法,在圖片預處理的時候,將所有圖片都轉(zhuǎn)成"RGB"三通道的模式:

HR_img = Image.open(HR_img).convert('RGB')   #全部以三通道形式打開

? ? ? ? 解決完成?。。?!

DataLoader問題解決:RuntimeError: stack expects each tensor to be equal size, but got [3, 200, 200]entry1

?文章來源地址http://www.zghlxwxcb.cn/news/detail-424711.html

?

到了這里,關(guān)于DataLoader問題解決:RuntimeError: stack expects each tensor to be equal size, but got [3, 200, 200]entry1的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • 【解決問題】RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton

    【解決問題】RuntimeError: The size of tensor a (80) must match the size of tensor b (56) at non-singleton

    你可以去github上,這兒我用的是YOLOv5.5的版本,就去Tags6里面的model/common.py里面去找到這個SPPF的類,把它拷過來到你這個Tags5的model/common.py里面,這樣你的代碼就也有這個類了,還要引入一個warnings包就行了 點開common.py文件 將這個復制到對應的類就行了。 剛解決了上一個問題,結(jié)

    2024年02月16日
    瀏覽(19)
  • RuntimeError: DataLoader worker is killed by signal: Killed.報錯解決

    一、問題描述 使用pytorch進行訓練時,訓練了僅幾個batch后出現(xiàn)報錯信息: 這個報錯和DataLoader有關(guān),定位到訓練腳本中的代碼: 二、問題分析 通過設置num_workers,DataLoader實例可以使用多少個子進程進行數(shù)據(jù)加載,從而加快網(wǎng)絡的訓練過程。 默認情況下,num_workers值被設置為

    2024年02月12日
    瀏覽(19)
  • D2L,Pytorch win10下使用dataloader時出現(xiàn)runtimeerror: dataloader worker (pid(s) ) exited unexpectedly的解決方案

    D2L,Pytorch win10下使用dataloader時出現(xiàn)runtimeerror: dataloader worker (pid(s) ) exited unexpectedly的解決方案

    ????????我是用python3.8+pytorch1.11,在李沐老師的課程展示中使用的環(huán)境是linux沒有報錯,但win10下在dataloader使用前需要加上 例如: 即可成功運行 ????????網(wǎng)上還有其他方法將num_worker設為0也可以單線程成功運行,不過直接導入d2l的dataloader沒有對應的參數(shù) 可能原因: ??

    2024年02月13日
    瀏覽(18)
  • 成功解決RuntimeError: batch2 must be a 3D tensor

    成功解決RuntimeError: batch2 must be a 3D tensor

    成功解決RuntimeError: batch2 must be a 3D tensor。 在深度學習的世界中,張量是構(gòu)建一切的核心。它們是數(shù)據(jù)的容器,是模型訓練的基石。然而,當我們嘗試使用 torch.bmm() 函數(shù)進行批量矩陣乘法時,可能會遇到一個常見的錯誤:“RuntimeError: batch2 must be a 3D tensor”。這個錯誤提示似乎

    2024年02月22日
    瀏覽(47)
  • 報錯解決:RuntimeError: expected scalar type Long but found Float

    報錯解決:RuntimeError: expected scalar type Long but found Float

    nn.Linear需要作用于浮點數(shù),這里可能輸入了整數(shù)類型的張量作為參數(shù)。 報錯: 把a轉(zhuǎn)為float,結(jié)果為:

    2024年02月09日
    瀏覽(24)
  • 【已解決】Pytorch RuntimeError: expected scalar type Double but found Float

    【已解決】Pytorch RuntimeError: expected scalar type Double but found Float

    本文作者: slience_me 在訓練模型時候,將數(shù)據(jù)集輸入到網(wǎng)絡中去,在執(zhí)行卷積nn.conv1d()的時候,報出此錯誤 報錯堆棧信息 tensor的數(shù)據(jù)類型dtype不正確 這個錯誤通常是由于數(shù)據(jù)類型不匹配導致的。在PyTorch中,張量有不同的數(shù)據(jù)類型,如float32(FloatTensor)和float64(DoubleTensor)等

    2024年01月15日
    瀏覽(22)
  • dataset dataloader tensor list情況

    如上面代碼所示,getitem期望返回一個tensor list 一個tensor,但是調(diào)用dataloader時只能接收到一個list,從打印內(nèi)容中可以看到,dataloader中將getitem中返回的兩個值都合并了。 從上述驗證代碼中可以看到,即使是相同類型的返回值,也不能分開來接收,返回的具體值為: 程序也會自

    2024年02月11日
    瀏覽(17)
  • 解決pytorch報錯——RuntimeError: Expected to have finished reduction in the prior iteration...

    解決pytorch報錯——RuntimeError: Expected to have finished reduction in the prior iteration...

    之前寫代碼時碰到了這樣一個錯誤: RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one. This error indicates that your module has parameters that were not used in producing loss. You can enable unused parameter detection by (1) passing the keyword argument find_unused_parameters=True to torch.nn.pa

    2023年04月17日
    瀏覽(19)
  • 【解決】RuntimeError: Boolean value of Tensor with more than one value is ambiguous

    在用pytorch進行損失函數(shù)計算時,報錯誤: 翻譯過來就是說: 具有多個值的張量的布爾值不明確? 我是這報錯: 啥意思?,你問我,我也不知道呀!、、、 ?錯誤原因分析: 其實是,因為我損失函數(shù)調(diào)用時沒有初始化,所以導致報錯 其實我是初始化了,但是因為沒有+(),

    2024年02月16日
    瀏覽(19)
  • TypeError: expected Tensor as element 0 in argument 0, but got numpy.ndarray解決辦法

    TypeError: expected Tensor as element 0 in argument 0, but got numpy.ndarray解決辦法

    需要Tensor變量,我卻給了numpy變量,所以轉(zhuǎn)化一下就好啦!!

    2024年02月07日
    瀏覽(25)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包