The error message appears clear enough but I do not understand why I am getting it or how to fix it.
My code is as follows;
for epoch in range(hyper.epochs):
epoch_loss = 0
epoch_accuracy = 0
for data, label in train_loader:
data = data.to(gpu.device)
label = label.to(gpu.device)
output = model(data)
The error is from the last line above.
The model is defined as
import torch.nn as nn
# Input Layer: It represent input image data.
# Conv Layer: This layer will extract features from image.
# Pooling Layer: This layer reduces the spatial volume of input image after convolution.
# Fully Connected Layer: It connect the network from a layer to another layer
# Output Layer: It is the predicted values layer.
class Cnn(nn.Module):
def __init__(self):
super(Cnn, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(3, 16, kernel_size=3, padding=0, stride=2),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.MaxPool2d(2)
)
self.layer2 = nn.Sequential(
nn.Conv2d(16,32, kernel_size=3, padding=0, stride=2),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(2)
)
self.layer3 = nn.Sequential(
nn.Conv2d(32,64, kernel_size=3, padding=0, stride=2),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(2)
)
self.fc1 = nn.Linear(3*3*64,10)
self.dropout = nn.Dropout(0.5)
self.fc2 = nn.Linear(10,2)
self.relu = nn.ReLU()
def forward(self,x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = out.view(out.size(0),-1)
out = self.relu(self.fc1(out))
out = self.fc2(out)
return out
the error is on the line; out = self.layer1(x)
The dataloader is reading in images;
import torch
from torch.utils.data import Dataset
from torchvision import transforms
from PIL import Image
from pathlib import Path
class dataset(Dataset):
def __init__(self, image_paths, dict_classes, logging):
self.image_paths = image_paths
self.dict_classes = dict_classes
self.logging = logging
#dataset length
def __len__(self):
return len(self.image_paths)
#load an one of images
def __getitem__(self, idx):
img_path = self.image_paths[idx]
img = Image.open(img_path)
transform = transforms.Compose([
transforms.PILToTensor(),
transforms.Resize((256, 256)),
transforms.RandomResizedCrop(256)
])
img_tensor = transform(img)
_key = Path(img_path).parts[3]
label = self.dict_classes[_key]
return img_tensor, label