AlignedDataset

initialize 初始化

  1. 指定模式

    1. self.dir_AB = os.path.join(opt.dataroot, opt.phase)
  2. 指定要使用的转换器列表

    1. transform_list = [transforms.ToTensor(),
    2. transforms.Normalize((0.5, 0.5, 0.5),
    3. (0.5, 0.5, 0.5))]

__getitem__ 获取指定索引的数据

  1. 将图片转换为RGB模式,调整大小,并标准化(调整平均值和方差)

    1. AB = Image.open(AB_path).convert('RGB')
    2. AB = AB.resize((self.opt.loadSize * 2, self.opt.loadSize), Image.BICUBIC)
    3. AB = self.transform(AB)
  2. 拆分出A B 两个数据集,并调整图片大小

  1. A = AB[:, h_offset:h_offset + self.opt.fineSize,
  2. w_offset:w_offset + self.opt.fineSize]
  3. B = AB[:, h_offset:h_offset + self.opt.fineSize,
  4. w + w_offset:w + w_offset + self.opt.fineSize]
  1. 如果no_clipe == False,就进行裁剪

    1. if (not self.opt.no_flip) and random.random() < 0.5:
    2. idx = [i for i in range(A.size(2) - 1, -1, -1)]
    3. idx = torch.LongTensor(idx)
    4. A = A.index_select(2, idx)
    5. B = B.index_select(2, idx)
  2. 转换成灰度图 ```python if input_nc == 1: # RGB to gray tmp = A[0, …] 0.299 + A[1, …] 0.587 + A[2, …] * 0.114 A = tmp.unsqueeze(0)

if output_nc == 1: # RGB to gray tmp = B[0, …] 0.299 + B[1, …] 0.587 + B[2, …] * 0.114 B = tmp.unsqueeze(0) ```