This is my Dataset class.
import os
import torch.utils.data
from PIL import Image
from PIL import ImageFile


class MyDataset(torch.utils.data.Dataset):

    def __init__(self, root_dir_img, root_dir_gt, transform=None):

        self.root_dir_img = root_dir_img
        self.root_dir_gt = root_dir_gt
        self.transform = transform

        img_names = [os.path.join(root_dir_img, name) for name in os.listdir(root_dir_img) if
                     os.path.isfile(os.path.join(root_dir_img, name))]

        gt_names = [os.path.join(root_dir_gt, name) for name in os.listdir(root_dir_gt) if
                    os.path.isfile(os.path.join(root_dir_gt, name))]

        self.img_files = []
        self.gt_files = []

        for i in range(len(img_names)):
            self.img_files.append(Image.open(img_names[i]))
            self.gt_files.append(Image.open(gt_names[i]))

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

    def __getitem__(self, idx):

        ImageFile.LOAD_TRUNCATED_IMAGES = True

        img = self.img_files[idx]
        gt = self.gt_files[idx]

        sample = {'image': img, 'mask': gt}

        if self.transform:
            sample = self.transform(sample)
            img = sample['image']
            gt = sample['mask']

        return img, gt

I will check grad.norm() now.