RuntimeError: Input type (unsigned char) and bias type (float) should be the same

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 =
        label =
        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),
        self.layer2 = nn.Sequential(
            nn.Conv2d(16,32, kernel_size=3, padding=0, stride=2),
        self.layer3 = nn.Sequential(
            nn.Conv2d(32,64, kernel_size=3, padding=0, stride=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 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 =
        transform = transforms.Compose([
            transforms.Resize((256, 256)),
        img_tensor = transform(img)
        _key = Path(img_path).parts[3]  
        label = self.dict_classes[_key]
        return img_tensor, label

Based on the error message it seems as if the input tensor use the uint8 dtype while the model uses the expected float32 dtype. Note that PILToTensor will keep the same dtype of the input image which is most likely causing the issue. Use ToTensor() to normalize the input image and return it in float32 format, which should fix the error.

1 Like