pytorch數(shù)據(jù)預處理錯誤的解決
出錯:
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
這是因為輸入的大小不匹配,跟數(shù)據(jù)集有關(guān),也跟數(shù)據(jù)預處理中的函數(shù)相關(guān):
transforms.Resize(input_size)
該函數(shù)是按比例縮放,可能是因為該數(shù)據(jù)集的分辨率不同,所以出來的結(jié)果不是(224,224)的,解決辦法是改為使用:
transforms.Resize((input_size, input_size))
即可
以上這篇pytorch數(shù)據(jù)預處理錯誤的解決就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
pytorch中部分矩陣乘法和數(shù)組乘法的小結(jié)
本文主要介紹了pytorch中部分矩陣乘法和數(shù)組乘法的小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03Python 內(nèi)置函數(shù)之隨機函數(shù)詳情
這篇文章主要介紹了Python 內(nèi)置函數(shù)之隨機函數(shù),文章將圍繞Python 內(nèi)置函數(shù)、隨機函數(shù)的相關(guān)資料展開內(nèi)容,需要的朋友可以參考一下,希望對你有所幫助2021-11-11Python3 列表,數(shù)組,矩陣的相互轉(zhuǎn)換的方法示例
這篇文章主要介紹了Python3 列表,數(shù)組,矩陣的相互轉(zhuǎn)換的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08Django Rest Framework框架構(gòu)建復雜API技能詳解
這篇文章會詳細介紹Django REST Framework的核心組成部分,包括Serializers、ViewSets、Routers、權(quán)限和認證系統(tǒng)以及測試和調(diào)試工具,文章從基礎開始,逐步深入,旨在幫助讀者掌握使用Django REST Framework構(gòu)建復雜API的技能2023-09-09Python數(shù)據(jù)分析之?Matplotlib?折線圖繪制
這篇文章主要介紹了Python數(shù)據(jù)分析之?Matplotlib?折線圖繪制,在數(shù)據(jù)分析中,數(shù)據(jù)可視化也非常重要,下文通過數(shù)據(jù)分析展開對折線圖的繪制,需要的小伙伴可以參考一下2022-05-05Python concurrent.futures模塊使用實例
這篇文章主要介紹了Python concurrent.futures模塊使用實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12