Oh, it is too stupid.
In my method transforms, I made mistakes mask = resize(image), so that in the DataLoader the dimension mismatch error will occur in the default_collate, it is not the DataLoader’s problem.
My code as follows:
rgb_mean = (0.4353, 0.4452, 0.4131)
rgb_std = (0.2044, 0.1924, 0.2013)

class MyDataset(Dataset):
    def __init__(self,
                 config,
                 subset,
                 data_transforms= None,
                 target_transforms=None):
        super(MyDataset, self).__init__()
        assert subset == 'train' or subset == 'valid' or subset == 'test'
        self.config = config
        self.root = self.config.root_dir
        self.subset = subset
        self.data = self.config.data_folder_name
        self.target = self.config.target_folder_name

        self.data_transforms = data_transforms if data_transforms!=None else TF.to_tensor
        self.target_transforms = target_transforms if target_transforms!= None else TF.to_tensor

        self.mapping = {
            0: 0,
            255: 1,
        }
        self.data_list = glob.glob(os.path.join(
            self.root,
            subset,
            self.data,
            '*.tif'
        ))
        self.target_list = glob.glob(os.path.join(
            self.root,
            subset,
            self.target,
            '*.tif'
        ))
    def mask_to_class(self, mask):
        for k in self.mapping:
            mask[mask == k] = self.mapping[k]
        return mask
    def transfroms(self, image, mask):
        resize = transforms.Resize(size=(self.config.input_size, self.config.input_size))
        image = resize(image)
        mask = resize(mask)
        if ranom.random() > 0.5:
            image = TF.hflip(image)
            mask = TF.hflip(mask)
        if random.random() > 0.5:
            image = TF.vflip(image)
            mask = TF.vflip(mask)
        image = TF.to_tensor(image)
        image = TF.normalize(image, mean=rgb_mean, std=rgb_std)
        mask = torch.from_numpy(np.array(mask, dtype=np.uint8))
        mask = self.mask_to_class(mask)
        mask = mask.long()
        return image, mask
    def __getitem__(self, index):
        datas = Image.open(self.data_list[index]).convert('RGB')
        targets = Image.open(self.target_list[index]).convert('P')
        t_datas, t_targets = self.transfroms(datas, targets)
        return t_datas, t_targets
    def __len__(self):
        return len(self.data_list)

And there are something to pay attention:

If you want to change grayscale mask to class, the Image method from PIL doesn’t work, we should read the masks in P or L

TF.to_tensor() will normalize images or masks to [0,1], so if change masks to class and tensor, we should convert mask to tensor by torch.from_numpy() and thus we can change masks to class and make them Tensor. Don’t foget convert the dtype to long.