使用PyTorch和Keras实现 pix2pix GAN
对比两个框架实现同一个模型到底有什么区别?
第一步,我们对数据集进行图像预处理。我们在这里选择 Facades 数据集,我们将 2 张图像合并为一张,以便在训练过程中进行一些增强。
Pytorch:
def __getitem__(self, index):img = Image.open(self.files[index % len(self.files)])
w, h = img.size
img_A = img.crop((0, 0, w / 2, h))
img_B = img.crop((w / 2, 0, w, h))if np.random.random() < 0.5:
img_A = Image.fromarray(np.array(img_A)[:, ::-1, :], "RGB")
img_B = Image.fromarray(np.array(img_B)[:, ::-1, :], "RGB")img_A = self.transform(img_A)
img_B = self.transform(img_B)return {"A": img_A, "B": img_B}
Keras:
def load_batch(self, batch_size=1, is_testing=False):
data_type = "train" if not is_testing else "val"
path = glob('./datasets/%s/%s/*' % (self.dataset_name, data_type))self.n_batches = int(len(path) / batch_size)for i in range(self.n_batches-1):
batch = path[i*batch_size:(i+1)*batch_size]
imgs_A, imgs_B = [], []
for img in batch:
img = self.imread(img)
h, w, _ = img.shape
half_w = int(w/2)
img_A = img[:, :half_w, :]
img_B = img[:, half_w:, :]img_A = resize(img_A, self.img_res)
img_B = resize(img_B, self.img_res)if not is_testing and np.random.random() > 0.5:
img_A = np.fliplr(img_A)
img_B = np.fliplr(img_B)imgs_A.append(img_A)
imgs_B.append(img_B)imgs_A = np.array(imgs_A)/127.5 - 1.
imgs_B = np.array(imgs_B)/127.5 - 1.yield imgs_A, imgs_B
模型
在论文中提到使用的模型是 U-Net,所以需要使用层间的跳跃连接(恒等函数)。使用上采样和下采样卷积制作自编码器生成和判别模型。
完整文章
https://avoid.overfit.cn/post/be317cf0a41c4a48b8a80398489120b3