pytorch數據預處理錯誤的解決

發布時間: 2020-02-20 13:27:57 來源: 互聯網 欄目: python 點擊:

今天小編就為大家分享一篇pytorch數據預處理錯誤的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

出錯:

Traceback (most recent call last):
 File "train.py", line 305, in <module>
 train_model(model_conv, criterion, optimizer_conv, exp_lr_scheduler)
 File "train.py", line 145, in train_model
 for inputs, age_labels, gender_labels in dataloaders[phase]:
 File "/home/home/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 637, in __next__
 return self._process_next_batch(batch)
 File "/home/home/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 658, in _process_next_batch
 raise batch.exc_type(batch.exc_msg)
RuntimeError: Traceback (most recent call last):
 File "/home/home/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in _worker_loop
 samples = collate_fn([dataset[i] for i in batch_indices])
 File "/home/home/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 232, in default_collate
 return [default_collate(samples) for samples in transposed]
 File "/home/home/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 232, in <listcomp>
 return [default_collate(samples) for samples in transposed]
 File "/home/home/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 209, in default_collate
 return torch.stack(batch, 0, out=out)
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 224 and 228 in dimension 3 at /pytorch/aten/src/TH/generic/THTensorMoreMath.cpp:1307

這是因為輸入的大小不匹配,跟數據集有關,也跟數據預處理中的函數相關:

transforms.Resize(input_size)

該函數是按比例縮放,可能是因為該數據集的分辨率不同,所以出來的結果不是(224,224)的,解決辦法是改為使用:

transforms.Resize((input_size, input_size))

即可

以上這篇pytorch數據預處理錯誤的解決就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持我們。

本文標題: pytorch數據預處理錯誤的解決
本文地址: http://www.1921352.live/jiaoben/python/301013.html

如果認為本文對您有所幫助請贊助本站

支付寶掃一掃贊助微信掃一掃贊助

  • 支付寶掃一掃贊助
  • 微信掃一掃贊助
  • 支付寶先領紅包再贊助
    聲明:凡注明"本站原創"的所有文字圖片等資料,版權均屬編程客棧所有,歡迎轉載,但務請注明出處。
    用什么庫寫 Python 命令行程序(示例代碼詳解)Python argparse模塊使用方法解析
    Top 网上挖矿机赚钱