pytorchのtorch.utils.data.Datasetを用いて自作でデータセットを作ろうとしたのですが,エラーが発生してしまいます.
自分なりに調べたのですが解決できないため詳しい方いらっしゃいましたら教えていただけましたら幸いです.
以下私のコードです.
python
1class DataSets(torch.utils.data.Dataset): 2 def __init__(self,imgdir,lbldir,tfms): 3 4 self.imgdir = imgdir 5 self.lbldir = lbldir 6 self.tfms = tfms 7 8 self.tr = [] 9 self.lbl = [] 10 self.lb = pd.read_csv(self.lbldir+'/train.csv') 11 for i in range(1821): 12 self.tr_path = glob.glob((os.path.join(self.imgdir,f'Train_{i}.jpg'))) 13 self.tr.append(tr_path) 14 15 self.lb = self.lb.iloc[:,1:] 16 self.lbl.append(self.lb.iloc[i,:]) 17 18 19 def __len__(self): 20 return len(self.tr) 21 22 def __getitem__(self,idx): 23 img = np.array(Image.open(self.tr[idx])) 24 lbl = self.lbl[idx] 25 26 if self.tfms: 27 img = self.tfms(img) 28 29 return img, lbl
そして,transformを定義後にこのようにしてDataloaderを作成しました.
python
1bs = 8 2ds = DataSets(imgdir, lbldir, tfms) 3dl = torch.utils.data.DataLoader(ds, bs, shuffle=True) 4
ここまでは問題なく進行するのですが,確認のため以下のコードを入力するとエラーが生じます.
python
1for i in dl: 2 print(i)
error
1-------------------------------------------------------------------------- 2AttributeError Traceback (most recent call last) 3/opt/conda/lib/python3.6/site-packages/PIL/Image.py in open(fp, mode) 4 2637 try: 5-> 2638 fp.seek(0) 6 2639 except (AttributeError, io.UnsupportedOperation): 7 8AttributeError: 'list' object has no attribute 'seek' 9 10During handling of the above exception, another exception occurred: 11 12AttributeError Traceback (most recent call last) 13<ipython-input-91-26c3503426c8> in <module> 14----> 1 for i in dl: 15 2 print(i) 16 17/opt/conda/lib/python3.6/site-packages/torch/utils/data/dataloader.py in __next__(self) 18 343 19 344 def __next__(self): 20--> 345 data = self._next_data() 21 346 self._num_yielded += 1 22 347 if self._dataset_kind == _DatasetKind.Iterable and \ 23 24/opt/conda/lib/python3.6/site-packages/torch/utils/data/dataloader.py in _next_data(self) 25 383 def _next_data(self): 26 384 index = self._next_index() # may raise StopIteration 27--> 385 data = self._dataset_fetcher.fetch(index) # may raise StopIteration 28 386 if self._pin_memory: 29 387 data = _utils.pin_memory.pin_memory(data) 30 31/opt/conda/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index) 32 42 def fetch(self, possibly_batched_index): 33 43 if self.auto_collation: 34---> 44 data = [self.dataset[idx] for idx in possibly_batched_index] 35 45 else: 36 46 data = self.dataset[possibly_batched_index] 37 38/opt/conda/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py in <listcomp>(.0) 39 42 def fetch(self, possibly_batched_index): 40 43 if self.auto_collation: 41---> 44 data = [self.dataset[idx] for idx in possibly_batched_index] 42 45 else: 43 46 data = self.dataset[possibly_batched_index] 44 45<ipython-input-84-1f993d703f19> in __getitem__(self, idx) 46 23 47 24 def __getitem__(self,idx): 48---> 25 img = np.array(Image.open(self.tr[idx])) 49 26 lbl = self.lbl[idx] 50 27 51 52/opt/conda/lib/python3.6/site-packages/PIL/Image.py in open(fp, mode) 53 2638 fp.seek(0) 54 2639 except (AttributeError, io.UnsupportedOperation): 55-> 2640 fp = io.BytesIO(fp.read()) 56 2641 exclusive_fp = True 57 2642 58 59AttributeError: 'list' object has no attribute 'read'
解決方法教えていただけましたら幸いです.
よろしくお願い致します.
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/27 01:24