Multi-Label icon indicating copy to clipboard operation
Multi-Label copied to clipboard

AttributeError: 'int' object has no attribute 'item'

Open chillizex opened this issue 3 years ago • 1 comments

I just downloaded the dataset and the notebook and encounter error when training the model. I didn't modify any code yet. I guess there is something wrong with the getitem or the MyDataset class. What should I do?

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
C:\Users\CHILLI~1\AppData\Local\Temp/ipykernel_31604/3002996250.py in <module>
----> 1 train(model,dataloader , criterion, optimizer,sgdr_partial,num_epochs=10)

C:\Users\CHILLI~1\AppData\Local\Temp/ipykernel_31604/2037274202.py in train(model, data_loader, criterion, optimizer, scheduler, num_epochs)
     17       running_corrects = 0.0
     18 
---> 19       for data , target in data_loader[phase]:
     20         #load the data and target to respective device
     21         data , target = data.to(device)  , target.to(device)

c:\Users\chillizex\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in __next__(self)
    528             if self._sampler_iter is None:
    529                 self._reset()
--> 530             data = self._next_data()
    531             self._num_yielded += 1
    532             if self._dataset_kind == _DatasetKind.Iterable and \

c:\Users\chillizex\anaconda3\lib\site-packages\torch\utils\data\dataloader.py in _next_data(self)
    568     def _next_data(self):
    569         index = self._next_index()  # may raise StopIteration
--> 570         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    571         if self._pin_memory:
    572             data = _utils.pin_memory.pin_memory(data)

c:\Users\chillizex\anaconda3\lib\site-packages\torch\utils\data\_utils\fetch.py in fetch(self, possibly_batched_index)
     47     def fetch(self, possibly_batched_index):
     48         if self.auto_collation:
---> 49             data = [self.dataset[idx] for idx in possibly_batched_index]
     50         else:
     51             data = self.dataset[possibly_batched_index]

c:\Users\chillizex\anaconda3\lib\site-packages\torch\utils\data\_utils\fetch.py in <listcomp>(.0)
     47     def fetch(self, possibly_batched_index):
     48         if self.auto_collation:
---> 49             data = [self.dataset[idx] for idx in possibly_batched_index]
     50         else:
     51             data = self.dataset[possibly_batched_index]

c:\Users\chillizex\anaconda3\lib\site-packages\torch\utils\data\dataset.py in __getitem__(self, idx)
    469         if isinstance(idx, list):
    470             return self.dataset[[self.indices[i] for i in idx]]
--> 471         return self.dataset[self.indices[idx]]
    472 
    473     def __len__(self):

C:\Users\CHILLI~1\AppData\Local\Temp/ipykernel_31604/2466280676.py in __getitem__(self, idx)
      7 
      8   def __getitem__(self,idx):
----> 9     d = self.df.iloc[idx.item()]
     10     image = Image.open(self.img_dir/d.image).convert("RGB")
     11     label = torch.tensor(d[1:].tolist() , dtype=torch.float32)

AttributeError: 'int' object has no attribute 'item'

chillizex avatar May 09 '22 13:05 chillizex

You have to delete item()

hariouat avatar Jan 10 '23 13:01 hariouat