RuntimeError: mean(): could not infer output dtype. Input dtype must be either a floating point or complex dtype. Got: Byte

code: - mean and std

import torch
from customDataset import CatsAndDogsDataset, ToTensor

#load data
dataset = CatsAndDogsDataset(csv_file =‘CatsAndDogsDataset.csv’,
root_dir = ‘pictures_resized’,
transform = ToTensor()
)

train_loader = torch.utils.data.DataLoader(dataset = dataset,
batch_size = 8,
shuffle = False,
)

def get_mean_and_std(loader):
mean = 0,
std = 0,
total_images_count = 0
for images, _ in loader:
image_count_in_a_batch = images.size(0)
images = images.view(image_count_in_a_batch, images.size(1), -1)
#print(images.shape)
mean += images.mean(2).sum(0)
std += images.std(2).sum(0)
total_images_count += image_count_in_a_batch

    mean /= total_images_count
    std /= total_images_count

    return mean, std

mean, std = get_mean_and_std(train_loader)
print(mean)
print(std)

code - customDataset
import os
import pandas as pd
import torch
from torch.utils.data import Dataset
from skimage import io

class CatsAndDogsDataset(Dataset):
def init(self, csv_file, root_dir, transform = None):
self.annotations = pd.read_csv(csv_file)
self.root_dir = root_dir
self.transform = transform

def __len__(self):
    return len(self.annotations)

def __getitem__(self, index):
    img_path = os.path.join(self.root_dir, self.annotations.iloc[index, 0])
    image = io.imread(img_path)
    y_label = torch.tensor(int(self.annotations.iloc[index, 1]))

    if self.transform:
        image = self.transform(image)

    return (image, y_label)

class ToTensor():
def call(self, image):
inputs = image
return torch.from_numpy(inputs)

Based on the error message you would need to use floating point dtype to calculate the mean of a tensor as seen here:

images = torch.randint(0, 256, (3, 224, 224)).byte()

# fails
images.mean(2)
# RuntimeError: mean(): could not infer output dtype. Input dtype must be either a floating point or complex dtype. Got: Byte

# works
images.float().mean(2)

i tried it , but it still has error:
mean += images.float().mean(2).sum(0)
TypeError: can only concatenate tuple (not “Tensor”) to tuple

Initialize the mean with = 0 instead of a tuple = 0, (note the trailing comma).

thanks, it solved my issue, but the output is not in average form. any idea ?