Image 使用dataset类上的自定义函数调用DataLoader时的执行块

Image 使用dataset类上的自定义函数调用DataLoader时的执行块,image,pytorch,dataset,transformation,Image,Pytorch,Dataset,Transformation,我正在用Colab运行我的代码。我使用Dataloader批量加载数据集进行培训和验证,并使用tqdm可视化培训过程。但每次我执行代码时,我都会被困在一个随机点上(在历元0中,意味着数据集从未被遍历过),说程序仍在运行,但进度条被冻结。一个有趣的现象在于输入的自定义转换函数,因为一旦我放弃这些转换,一切都会顺利进行。功能包括: class Sharpen(object): def __init__(self, p=0.5): self.p = p def

我正在用Colab运行我的代码。我使用
Dataloader
批量加载数据集进行培训和验证,并使用
tqdm
可视化培训过程。但每次我执行代码时,我都会被困在一个随机点上(在历元0中,意味着数据集从未被遍历过),说程序仍在运行,但进度条被冻结。一个有趣的现象在于输入的自定义转换函数,因为一旦我放弃这些转换,一切都会顺利进行。功能包括:

class Sharpen(object):
    def __init__(self, p=0.5):
        self.p = p
    
    def __call__(self, sample):
        if random.uniform(0.0, 1.0) < self.p:
            return sample
        kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32)
        for i in range(len(sample['img_list'])):
            sample['img_list'][i] = cv2.filter2D(sample['img_list'][i], -1, kernel=kernel)
            
        return sample


class Rotation(object):
    def __init__(self, angle=5, fill_value=0, p=0.5):
        self.angle = angle
        self.fill_value = fill_value
        self.p = p

    def __call__(self, sample):
        if random.uniform(0.0, 1.0) < self.p:
            return sample
        for i in range(len(sample['img_list'])):
            ang_rot = np.random.uniform(self.angle) - self.angle / 2
            h, w, _ = sample["img_list"][i].shape 
            if h*w == 0:
                continue 
            transform = cv2.getRotationMatrix2D((w / 2, h / 2), ang_rot, 1)
            sample["img_list"][i] = cv2.warpAffine(sample["img_list"][i], transform, (w, h),
                              borderValue=self.fill_value)
            
        return sample


class Translation(object):
    def __init__(self, fill_value=0, p=0.5):
        self.fill_value = fill_value
        self.p = p
        self.SIGMA = 1e-3

    def __call__(self, sample):
        rand_num = random.uniform(0.0, 1.0)
        if rand_num <= self.p:
            return sample
        
        for i in range(len(sample['img_list'])):
            h, w, _ = sample["img_list"][i].shape
            if h*w == 0:
                continue
            trans_range = (w / 10, h / 10)
            tr_x = trans_range[0] * rand_num - trans_range[0] / 2 + self.SIGMA
            tr_y = trans_range[1] * rand_num - trans_range[1] / 2 + self.SIGMA
            transform = np.float32([[1, 0, tr_x], [0, 1, tr_y]])
            sample["img_list"][i] = cv2.warpAffine(sample["img_list"][i], transform, (w, h), 
                                borderValue=self.fill_value)
        
        return sample

class Normalization(object):
    def __init__(self, mean=(0,0,0), std=(255,255,255)):
        self.mean = mean
        self.std = std
    
    def __call__(self, sample):
        # norm_func = transforms.Normalize(self.mean, self.std)
        for i in range(len(sample['img_list'])):
            for j in range(3):  # for colored image
                sample['img_list'][i][:,:,j] = np.array(list(map(lambda x: (x-self.mean[j])/self.std[j], sample['img_list'][i][:,:,j])),
                                     dtype=np.float32)
            # sample['img_list'][i] = norm_func(torch.Tensor(sample['img_list'][i]))
        return sample
类锐化(对象):
定义初始值(自,p=0.5):
self.p=p
定义调用(自我,样本):
如果随机均匀(0.0,1.0)如果随机数问题已解决。这是因为数据。如果输入图像是空的,加载过程将被卡住。

后续:我发现问题在于OpenCV操作的for循环,因为程序仅在使用规范化作为转换类的情况下才能顺利运行。cv2内部一定有一些问题。。。。每个类的其他三个调用函数中的操作。