pytorch dataloader 取batch_size時候 出現bug


1、RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 342 and 281 in dimension 3 at /pytorch/aten/src/TH/generic/THTensorMoreMath.cpp:1333
2、RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 3 and 1 in dimension 1

bug1 修改方法是將圖片resize 成一個尺度:
解決1:
如果用transforms來resize ,關於尺寸的transform就是transforms.Resize(input_size, interpolation=3)了。
查看Resize的文檔:
size (sequence or int) – Desired output size. If size is a sequence like (h, w), output size will be matched to this.
If size is an int, smaller edge of the image will be matched to this number. i.e, if height > width, then image will be rescaled to (size * height / width, size)
size這個參數既可以是一個數字,又可以是一個tuple,如果是數字,圖片處理后的輸出尺寸每次都會被重新計算為(input_size*height/width, size),這樣如果輸入圖片尺寸不一致,輸出圖片尺寸會不一致, 將導致bug1 問題

解決2:
也可以直接用opencv resize

bug2 將圖片全部轉成一個channel 比如RGB


免責聲明!

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



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