I am having the same issue. I am new to Python and Pytorch.
My code has three parts:
- first part is where I define the class function;
- third and final part is using the googlenet model
- following is the second part…followed by the error message:
import torch
import torchvision
import torch.nn as nn # All neural network modules, nn.Linear, nn.Conv2d, BatchNorm, Loss functions
import torch.optim as optim # for all optimization algorithms, SGD, Adam, etc.
import torchvision.transforms as transforms #transformations we can perform on our dataset
from torch.utils.data import DataLoader #Gives easier dataset management and creates mini batches
#Set device
device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)
#hyperparameters
in_channel = 3
num_classes = 10
learning_rate = 1e-3
batch_size = 16
num_epochs = 1
img_size = 224
training_data = Abhidataset(
csv_file = ‘Book1.csv’,
root_dir = ‘dataset_1’,
transform = transforms.ToTensor()
)
#Load Data
train_set, test_set = torch.utils.data.random_split(training_data,[16,5])
train_loader = DataLoader(training_data, batch_size = batch_size, shuffle = True, collate_fn=batch_size)
test_loader = DataLoader(training_data, batch_size = batch_size, shuffle = True, collate_fn=batch_size)
ERROR MESSAGE:
TypeError Traceback (most recent call last)
in
11 losses=[]
12
—> 13 for batch_idx, (data, targets) in enumerate(train_loader):
14 #Get data to cuda if possible
15 data = data.to(device=device)
~/opt/anaconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py in next(self)
519 if self._sampler_iter is None:
520 self._reset()
→ 521 data = self._next_data()
522 self._num_yielded += 1
523 if self._dataset_kind == _DatasetKind.Iterable and \
~/opt/anaconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py in _next_data(self)
559 def _next_data(self):
560 index = self._next_index() # may raise StopIteration
→ 561 data = self._dataset_fetcher.fetch(index) # may raise StopIteration
562 if self._pin_memory:
563 data = _utils.pin_memory.pin_memory(data)
~/opt/anaconda3/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
42 def fetch(self, possibly_batched_index):
43 if self.auto_collation:
—> 44 data = [self.dataset[idx] for idx in possibly_batched_index]
45 else:
46 data = self.dataset[possibly_batched_index]
~/opt/anaconda3/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py in (.0)
42 def fetch(self, possibly_batched_index):
43 if self.auto_collation:
—> 44 data = [self.dataset[idx] for idx in possibly_batched_index]
45 else:
46 data = self.dataset[possibly_batched_index]
in getitem(self, idx)
19 label = self.img_labels.iloc[idx, 1]
20 if self.transform:
—> 21 image = self.transform(image)
22 # if self.target_transform:
23 # #label = self.target_transform(label)
~/opt/anaconda3/lib/python3.8/site-packages/torchvision/transforms/transforms.py in call(self, pic)
95 Tensor: Converted image.
96 “”"
—> 97 return F.to_tensor(pic)
98
99 def repr(self):
~/opt/anaconda3/lib/python3.8/site-packages/torchvision/transforms/functional.py in to_tensor(pic)
100 “”"
101 if not(F_pil._is_pil_image(pic) or _is_numpy(pic)):
→ 102 raise TypeError(‘pic should be PIL Image or ndarray. Got {}’.format(type(pic)))
103
104 if _is_numpy(pic) and not _is_numpy_image(pic):
TypeError: pic should be PIL Image or ndarray. Got <class ‘torch.Tensor’>