使用PyTorch和Keras实现 pix2pix GAN

对比两个框架实现同一个模型到底有什么区别?

第一步,我们对数据集进行图像预处理。我们在这里选择 Facades 数据集,我们将 2 张图像合并为一张,以便在训练过程中进行一些增强。

Pytorch:

  1. def __getitem__(self, index):img = Image.open(self.files[index % len(self.files)])
  2. w, h = img.size
  3. img_A = img.crop((0, 0, w / 2, h))
  4. img_B = img.crop((w / 2, 0, w, h))if np.random.random() < 0.5:
  5. img_A = Image.fromarray(np.array(img_A)[:, ::-1, :], "RGB")
  6. img_B = Image.fromarray(np.array(img_B)[:, ::-1, :], "RGB")img_A = self.transform(img_A)
  7. img_B = self.transform(img_B)return {"A": img_A, "B": img_B}

Keras:

  1. def load_batch(self, batch_size=1, is_testing=False):
  2. data_type = "train" if not is_testing else "val"
  3. 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):
  4. batch = path[i*batch_size:(i+1)*batch_size]
  5. imgs_A, imgs_B = [], []
  6. for img in batch:
  7. img = self.imread(img)
  8. h, w, _ = img.shape
  9. half_w = int(w/2)
  10. img_A = img[:, :half_w, :]
  11. img_B = img[:, half_w:, :]img_A = resize(img_A, self.img_res)
  12. img_B = resize(img_B, self.img_res)if not is_testing and np.random.random() > 0.5:
  13. img_A = np.fliplr(img_A)
  14. img_B = np.fliplr(img_B)imgs_A.append(img_A)
  15. imgs_B.append(img_B)imgs_A = np.array(imgs_A)/127.5 - 1.
  16. imgs_B = np.array(imgs_B)/127.5 - 1.yield imgs_A, imgs_B

模型

在论文中提到使用的模型是 U-Net,所以需要使用层间的跳跃连接(恒等函数)。使用上采样和下采样卷积制作自编码器生成和判别模型。

完整文章

https://avoid.overfit.cn/post/be317cf0a41c4a48b8a80398489120b3

posted @ 2022-08-18 09:32  deephub  阅读(57)  评论(0编辑  收藏  举报