AlignedDataset
initialize 初始化
指定模式
self.dir_AB = os.path.join(opt.dataroot, opt.phase)
指定要使用的转换器列表
transform_list = [transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5),
(0.5, 0.5, 0.5))]
__getitem__ 获取指定索引的数据
将图片转换为RGB模式,调整大小,并标准化(调整平均值和方差)
AB = Image.open(AB_path).convert('RGB')
AB = AB.resize((self.opt.loadSize * 2, self.opt.loadSize), Image.BICUBIC)
AB = self.transform(AB)
拆分出A B 两个数据集,并调整图片大小
A = AB[:, h_offset:h_offset + self.opt.fineSize,
w_offset:w_offset + self.opt.fineSize]
B = AB[:, h_offset:h_offset + self.opt.fineSize,
w + w_offset:w + w_offset + self.opt.fineSize]
如果
no_clipe == False
,就进行裁剪if (not self.opt.no_flip) and random.random() < 0.5:
idx = [i for i in range(A.size(2) - 1, -1, -1)]
idx = torch.LongTensor(idx)
A = A.index_select(2, idx)
B = B.index_select(2, idx)
转换成灰度图 ```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) ```