Parcourir la source

Merge branch 'release/v0.0.1'

Jérôme BUISINE il y a 5 ans
Parent
commit
cc688192bf

+ 4 - 0
.gitignore

@@ -1,3 +1,7 @@
 runs
 .python-version
 .ipynb_checkpoints
+
+# do not track blocks images
+synthesis_images/generated_blocks
+saved_models

+ 20 - 0
Dockerfile

@@ -0,0 +1,20 @@
+FROM pytorch/pytorch
+
+# Update and add usefull package
+RUN apt-get update
+RUN apt-get install -y libglib2.0-dev libsm-dev libxrender-dev libxext-dev 
+
+# Install JupyterLab 
+RUN pip install jupyterlab && jupyter serverextension enable --py jupyterlab
+
+COPY requirements.txt requirements.txt
+
+RUN pip install -r requirements.txt
+
+ENV LANG=C.UTF-8
+
+# Expose Jupyter port & cmd 
+EXPOSE 8888 
+EXPOSE 6006
+
+CMD jupyter-lab --ip=0.0.0.0 --port=8888 --no-browser --notebook-dir=/data --allow-root

+ 10 - 0
Makefile

@@ -0,0 +1,10 @@
+current_dir := $(shell pwd)
+
+build: 
+	sudo docker build . --tag=jupyterlab-gan
+
+run:
+	sudo docker run -it -p 8888:8888 -p 6006:6006 -v $(current_dir):/data --name=noise jupyterlab-gan
+
+clean:
+	sudo docker container rm noise

+ 1 - 0
ganAtariImage.py

@@ -139,6 +139,7 @@ if __name__ == "__main__":
     envs = [InputWrapper(gym.make(name)) for name in ('Breakout-v0', 'AirRaid-v0', 'Pong-v0')]
     input_shape = envs[0].observation_space.shape
 
+    print(input_shape)
     net_discr = Discriminator(input_shape=input_shape).to(device)
     net_gener = Generator(output_shape=input_shape).to(device)
     print(net_gener)

+ 193 - 0
ganSynthesisImage.py

@@ -0,0 +1,193 @@
+#!/usr/bin/env python
+import random
+import argparse
+import cv2
+import os
+
+import torch
+import torch.nn as nn
+import torch.optim as optim
+from tensorboardX import SummaryWriter
+from PIL import Image
+
+import torchvision.utils as vutils
+
+import gym
+import gym.spaces
+
+import numpy as np
+
+log = gym.logger
+log.set_level(gym.logger.INFO)
+
+LATENT_VECTOR_SIZE = 100
+DISCR_FILTERS = 64
+GENER_FILTERS = 64
+BATCH_SIZE = 16
+
+# dimension input image will be rescaled
+IMAGE_SIZE = 64
+input_shape = (3, IMAGE_SIZE, IMAGE_SIZE)
+
+LEARNING_RATE = 0.0001
+REPORT_EVERY_ITER = 50
+SAVE_IMAGE_EVERY_ITER = 200
+MAX_ITERATION = 100000
+
+data_folder = 'synthesis_images/generated_blocks'
+models_folder = 'saved_models'
+
+class Discriminator(nn.Module):
+    def __init__(self, input_shape):
+        super(Discriminator, self).__init__()
+        # this pipe converges image into the single number
+        self.conv_pipe = nn.Sequential(
+            nn.Conv2d(in_channels=input_shape[0], out_channels=DISCR_FILTERS,
+                      kernel_size=4, stride=2, padding=1),
+            nn.ReLU(),
+            nn.Conv2d(in_channels=DISCR_FILTERS, out_channels=DISCR_FILTERS*2,
+                      kernel_size=4, stride=2, padding=1),
+            nn.BatchNorm2d(DISCR_FILTERS*2),
+            nn.ReLU(),
+            nn.Conv2d(in_channels=DISCR_FILTERS * 2, out_channels=DISCR_FILTERS * 4,
+                      kernel_size=4, stride=2, padding=1),
+            nn.BatchNorm2d(DISCR_FILTERS * 4),
+            nn.ReLU(),
+            nn.Conv2d(in_channels=DISCR_FILTERS * 4, out_channels=DISCR_FILTERS * 8,
+                      kernel_size=4, stride=2, padding=1),
+            nn.BatchNorm2d(DISCR_FILTERS * 8),
+            nn.ReLU(),
+            nn.Conv2d(in_channels=DISCR_FILTERS * 8, out_channels=1,
+                      kernel_size=4, stride=1, padding=0),
+            nn.Sigmoid()
+        )
+
+    def forward(self, x):
+        conv_out = self.conv_pipe(x)
+        return conv_out.view(-1, 1).squeeze(dim=1)
+
+
+class Generator(nn.Module):
+    def __init__(self, output_shape):
+        super(Generator, self).__init__()
+        # pipe deconvolves input vector into (3, 64, 64) image
+        self.pipe = nn.Sequential(
+            nn.ConvTranspose2d(in_channels=LATENT_VECTOR_SIZE, out_channels=GENER_FILTERS * 8,
+                               kernel_size=4, stride=1, padding=0),
+            nn.BatchNorm2d(GENER_FILTERS * 8),
+            nn.ReLU(),
+            nn.ConvTranspose2d(in_channels=GENER_FILTERS * 8, out_channels=GENER_FILTERS * 4,
+                               kernel_size=4, stride=2, padding=1),
+            nn.BatchNorm2d(GENER_FILTERS * 4),
+            nn.ReLU(),
+            nn.ConvTranspose2d(in_channels=GENER_FILTERS * 4, out_channels=GENER_FILTERS * 2,
+                               kernel_size=4, stride=2, padding=1),
+            nn.BatchNorm2d(GENER_FILTERS * 2),
+            nn.ReLU(),
+            nn.ConvTranspose2d(in_channels=GENER_FILTERS * 2, out_channels=GENER_FILTERS,
+                               kernel_size=4, stride=2, padding=1),
+            nn.BatchNorm2d(GENER_FILTERS),
+            nn.ReLU(),
+            nn.ConvTranspose2d(in_channels=GENER_FILTERS, out_channels=output_shape[0],
+                               kernel_size=4, stride=2, padding=1),
+            nn.Tanh()
+        )
+
+    def forward(self, x):
+        return self.pipe(x)
+
+# here we have to generate our batches from final or noisy synthesis images
+def iterate_batches(batch_size=BATCH_SIZE):
+
+    batch = []
+    images = os.listdir(data_folder)
+    nb_images = len(images)
+
+    while True:
+        i = random.randint(0, nb_images - 1)
+
+        img = Image.open(os.path.join(data_folder, images[i]))
+        img_arr = np.asarray(img)
+
+        new_obs = cv2.resize(img_arr, (IMAGE_SIZE, IMAGE_SIZE))
+        # transform (210, 160, 3) -> (3, 210, 160)
+        new_obs = np.moveaxis(new_obs, 2, 0)
+
+        batch.append(new_obs)
+
+        if len(batch) == batch_size:
+            # Normalising input between -1 to 1
+            batch_np = np.array(batch, dtype=np.float32) * 2.0 / 255.0 - 1.0
+            yield torch.tensor(batch_np)
+            batch.clear()
+
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser()
+    parser.add_argument("--cuda", default=False, action='store_true', help="Enable cuda computation")
+    args = parser.parse_args()
+
+    device = torch.device("cuda" if args.cuda else "cpu")
+    #envs = [InputWrapper(gym.make(name)) for name in ('Breakout-v0', 'AirRaid-v0', 'Pong-v0')]
+
+    print(input_shape)
+    net_discr = Discriminator(input_shape=input_shape).to(device)
+    net_gener = Generator(output_shape=input_shape).to(device)
+    print(net_gener)
+
+    objective = nn.BCELoss()
+    gen_optimizer = optim.Adam(params=net_gener.parameters(), lr=LEARNING_RATE, betas=(0.5, 0.999))
+    dis_optimizer = optim.Adam(params=net_discr.parameters(), lr=LEARNING_RATE, betas=(0.5, 0.999))
+    writer = SummaryWriter()
+
+    gen_losses = []
+    dis_losses = []
+    iter_no = 0
+
+    true_labels_v = torch.ones(BATCH_SIZE, dtype=torch.float32, device=device)
+    fake_labels_v = torch.zeros(BATCH_SIZE, dtype=torch.float32, device=device)
+
+    for batch_v in iterate_batches():
+
+        # generate extra fake samples, input is 4D: batch, filters, x, y
+        gen_input_v = torch.FloatTensor(BATCH_SIZE, LATENT_VECTOR_SIZE, 1, 1).normal_(0, 1).to(device)
+
+        # There we get data
+        batch_v = batch_v.to(device)
+        gen_output_v = net_gener(gen_input_v)
+
+        # train discriminator
+        dis_optimizer.zero_grad()
+        dis_output_true_v = net_discr(batch_v)
+        dis_output_fake_v = net_discr(gen_output_v.detach())
+        dis_loss = objective(dis_output_true_v, true_labels_v) + objective(dis_output_fake_v, fake_labels_v)
+        dis_loss.backward()
+        dis_optimizer.step()
+        dis_losses.append(dis_loss.item())
+
+        # train generator
+        gen_optimizer.zero_grad()
+        dis_output_v = net_discr(gen_output_v)
+        gen_loss_v = objective(dis_output_v, true_labels_v)
+        gen_loss_v.backward()
+        gen_optimizer.step()
+        gen_losses.append(gen_loss_v.item())
+
+        iter_no += 1
+        if iter_no % REPORT_EVERY_ITER == 0:
+            log.info("Iter %d: gen_loss=%.3e, dis_loss=%.3e", iter_no, np.mean(gen_losses), np.mean(dis_losses))
+            writer.add_scalar("gen_loss", np.mean(gen_losses), iter_no)
+            writer.add_scalar("dis_loss", np.mean(dis_losses), iter_no)
+            gen_losses = []
+            dis_losses = []
+        if iter_no % SAVE_IMAGE_EVERY_ITER == 0:
+            writer.add_image("fake", vutils.make_grid(gen_output_v.data[:IMAGE_SIZE], normalize=True), iter_no)
+            writer.add_image("real", vutils.make_grid(batch_v.data[:IMAGE_SIZE], normalize=True), iter_no)
+
+        if iter_no >= MAX_ITERATION:
+            # end of train
+            break
+
+    # now save these two models
+    torch.save(net_discr.state_dict(), os.path.join(models_folder, 'net_discr_model.pt'))
+    torch.save(net_gener.state_dict(), os.path.join(models_folder, 'net_gener_model.pt'))

+ 297 - 0
ganSynthesisImage_200.py

@@ -0,0 +1,297 @@
+#!/usr/bin/env python
+import random
+import argparse
+import cv2
+import os, sys, getopt
+
+import torch
+import torch.nn as nn
+import torch.optim as optim
+from tensorboardX import SummaryWriter
+from PIL import Image
+
+import torchvision.utils as vutils
+
+import gym
+import gym.spaces
+
+import numpy as np
+
+log = gym.logger
+log.set_level(gym.logger.INFO)
+
+LATENT_VECTOR_SIZE = 400
+DISCR_FILTERS = 200
+GENER_FILTERS = 200
+BATCH_SIZE = 16
+
+# dimension input image will be rescaled
+IMAGE_SIZE = 200
+input_shape = (3, IMAGE_SIZE, IMAGE_SIZE)
+
+BACKUP_MODEL_NAME = "synthesis_{}_model.pt"
+BACKUP_FOLDER = "saved_models"
+BACKUP_EVERY_ITER = 1
+
+LEARNING_RATE = 0.0001
+REPORT_EVERY_ITER = 10
+SAVE_IMAGE_EVERY_ITER = 20
+MAX_ITERATION = 100000
+
+data_folder = 'synthesis_images/generated_blocks'
+
+class Discriminator(nn.Module):
+    def __init__(self, input_shape):
+        super(Discriminator, self).__init__()
+        # this pipe converges image into the single number
+        self.conv_pipe = nn.Sequential(
+            nn.Conv2d(in_channels=input_shape[0], out_channels=DISCR_FILTERS,
+                      kernel_size=4, stride=2, padding=1),
+            nn.ReLU(),
+            nn.Conv2d(in_channels=DISCR_FILTERS, out_channels=DISCR_FILTERS*2,
+                      kernel_size=4, stride=2, padding=1),
+            nn.BatchNorm2d(DISCR_FILTERS*2),
+            nn.ReLU(),
+            nn.Conv2d(in_channels=DISCR_FILTERS * 2, out_channels=DISCR_FILTERS * 4,
+                      kernel_size=4, stride=2, padding=1),
+            nn.BatchNorm2d(DISCR_FILTERS * 4),
+            nn.ReLU(),
+            nn.Conv2d(in_channels=DISCR_FILTERS * 4, out_channels=DISCR_FILTERS * 8,
+                      kernel_size=4, stride=2, padding=1),
+            nn.BatchNorm2d(DISCR_FILTERS * 8),
+            nn.ReLU(),
+
+            nn.Conv2d(in_channels=DISCR_FILTERS * 8, out_channels=DISCR_FILTERS * 16,
+                      kernel_size=8, stride=2, padding=1),
+            nn.BatchNorm2d(DISCR_FILTERS * 16),
+            nn.ReLU(),
+
+            nn.Conv2d(in_channels=DISCR_FILTERS * 16, out_channels=1,
+                      kernel_size=4, stride=1, padding=0),
+            nn.Sigmoid()
+        )
+
+    def forward(self, x):
+        conv_out = self.conv_pipe(x)
+        return conv_out.view(-1, 1).squeeze(dim=1)
+
+
+class Generator(nn.Module):
+    def __init__(self, output_shape):
+        super(Generator, self).__init__()
+        # pipe deconvolves input vector into (3, 64, 64) image
+        self.pipe = nn.Sequential(
+            nn.ConvTranspose2d(in_channels=LATENT_VECTOR_SIZE, out_channels=GENER_FILTERS * 5,
+                               kernel_size=6, stride=1, padding=0),
+            nn.BatchNorm2d(GENER_FILTERS * 5),
+            nn.ReLU(),
+            nn.ConvTranspose2d(in_channels=GENER_FILTERS * 5, out_channels=GENER_FILTERS * 4,
+                               kernel_size=4, stride=2, padding=1),
+            nn.BatchNorm2d(GENER_FILTERS * 4),
+            nn.ReLU(),
+            nn.ConvTranspose2d(in_channels=GENER_FILTERS * 4, out_channels=GENER_FILTERS * 3,
+                               kernel_size=4, stride=2, padding=1),
+            nn.BatchNorm2d(GENER_FILTERS * 3),
+            nn.ReLU(),
+
+            nn.ConvTranspose2d(in_channels=GENER_FILTERS * 3, out_channels=GENER_FILTERS * 2,
+                               kernel_size=6, stride=2, padding=1),
+            nn.BatchNorm2d(GENER_FILTERS * 2),
+            nn.ReLU(),
+
+            nn.ConvTranspose2d(in_channels=GENER_FILTERS * 2, out_channels=GENER_FILTERS,
+                               kernel_size=4, stride=2, padding=1),
+            nn.BatchNorm2d(GENER_FILTERS),
+            nn.ReLU(),
+
+            nn.ConvTranspose2d(in_channels=GENER_FILTERS, out_channels=output_shape[0],
+                               kernel_size=4, stride=2, padding=1),
+            nn.Tanh()
+        )
+
+    def forward(self, x):
+        return self.pipe(x)
+
+# here we have to generate our batches from final or noisy synthesis images
+def iterate_batches(batch_size=BATCH_SIZE):
+
+    batch = []
+    images = os.listdir(data_folder)
+    nb_images = len(images)
+
+    while True:
+        i = random.randint(0, nb_images - 1)
+
+        img = Image.open(os.path.join(data_folder, images[i]))
+        img_arr = np.asarray(img)
+
+        new_obs = cv2.resize(img_arr, (IMAGE_SIZE, IMAGE_SIZE))
+        # transform (210, 160, 3) -> (3, 210, 160)
+        new_obs = np.moveaxis(new_obs, 2, 0)
+
+        batch.append(new_obs)
+
+        if len(batch) == batch_size:
+            # Normalising input between -1 to 1
+            batch_np = np.array(batch, dtype=np.float32) * 2.0 / 255.0 - 1.0
+            yield torch.tensor(batch_np)
+            batch.clear()
+
+
+if __name__ == "__main__":
+
+    save_model = False
+    load_model = False
+    p_cuda = False
+
+    #parser = argparse.ArgumentParser()
+    #parser.add_argument("--cuda", default=False, action='store_true', help="Enable cuda computation")
+    #args = parser.parse_args()
+
+    try:
+        opts, args = getopt.getopt(sys.argv[1:], "hflc", ["help=", "folder=", "load=", "cuda="])
+    except getopt.GetoptError:
+        # print help information and exit:
+        print('python ganSynthesisImage_200.py --folder folder_name_to_save --cuda 1')
+        print('python ganSynthesisImage_200.py --load model_name_to_load ')
+        sys.exit(2)
+    for o, a in opts:
+        if o in ("-h", "--help"):
+            print('python ganSynthesisImage_200.py --folder folder_name_to_save --cuda 1')
+            print('python ganSynthesisImage_200.py --load folder_name_to_load ')
+            sys.exit()
+        elif o in ("-f", "--folder"):
+            p_model_folder = a
+            save_model = True
+        elif o in ("-l", "--load"):
+            p_load = a
+            load_model = True
+        elif o in ("-c", "--cuda"):
+            p_cuda = int(a)
+        else:
+            assert False, "unhandled option"
+
+    if save_model and load_model:
+        raise Exception("Cannot save and load model. One argurment in only required.")
+    if not save_model and not load_model:
+        print('python ganSynthesisImage_200.py --folder folder_name_to_save --cuda 1')
+        print('python ganSynthesisImage_200.py --load folder_name_to_load ')
+        print("Need at least one argurment.")
+        sys.exit(2)
+
+    device = torch.device("cuda" if p_cuda else "cpu")
+    #envs = [InputWrapper(gym.make(name)) for name in ('Breakout-v0', 'AirRaid-v0', 'Pong-v0')]
+
+
+    # prepare folder names to save models
+    if save_model:
+
+        models_folder_path = os.path.join(BACKUP_FOLDER, p_model_folder)
+        dis_model_path = os.path.join(models_folder_path, BACKUP_MODEL_NAME.format('disc'))
+        gen_model_path = os.path.join(models_folder_path, BACKUP_MODEL_NAME.format('gen'))
+
+    if load_model:
+
+        models_folder_path = os.path.join(BACKUP_FOLDER, p_load)
+        dis_model_path = os.path.join(models_folder_path, BACKUP_MODEL_NAME.format('disc'))
+        gen_model_path = os.path.join(models_folder_path, BACKUP_MODEL_NAME.format('gen'))
+
+    # Construct model
+    net_discr = Discriminator(input_shape=input_shape).to(device)
+    net_gener = Generator(output_shape=input_shape).to(device)
+    print(net_discr)
+    print(net_gener)
+
+    objective = nn.BCELoss()
+    gen_optimizer = optim.Adam(params=net_gener.parameters(), lr=LEARNING_RATE, betas=(0.5, 0.999))
+    dis_optimizer = optim.Adam(params=net_discr.parameters(), lr=LEARNING_RATE, betas=(0.5, 0.999))
+    writer = SummaryWriter()
+
+    gen_losses = []
+    dis_losses = []
+    iter_no = 0
+
+    true_labels_v = torch.ones(BATCH_SIZE, dtype=torch.float32, device=device)
+    fake_labels_v = torch.zeros(BATCH_SIZE, dtype=torch.float32, device=device)
+
+
+    # load models checkpoint if exists
+    if load_model:
+        gen_checkpoint = torch.load(gen_model_path)
+
+        net_gener.load_state_dict(gen_checkpoint['model_state_dict'])
+        gen_optimizer.load_state_dict(gen_checkpoint['optimizer_state_dict'])
+        gen_losses = gen_checkpoint['gen_losses']
+        iteration = gen_checkpoint['iteration'] # retrieve only from the gen net the iteration number
+
+        dis_checkpoint = torch.load(dis_model_path)
+
+        net_discr.load_state_dict(dis_checkpoint['model_state_dict'])
+        dis_optimizer.load_state_dict(dis_checkpoint['optimizer_state_dict'])
+        dis_losses = dis_checkpoint['dis_losses']
+
+        iter_no = iteration
+
+    for batch_v in iterate_batches():
+
+        # generate extra fake samples, input is 4D: batch, filters, x, y
+        gen_input_v = torch.FloatTensor(BATCH_SIZE, LATENT_VECTOR_SIZE, 1, 1).normal_(0, 1).to(device)
+
+        # There we get data
+        batch_v = batch_v.to(device)
+
+        gen_output_v = net_gener(gen_input_v)
+
+        # train discriminator
+        dis_optimizer.zero_grad()
+        dis_output_true_v = net_discr(batch_v)
+        dis_output_fake_v = net_discr(gen_output_v.detach())
+        dis_loss = objective(dis_output_true_v, true_labels_v) + objective(dis_output_fake_v, fake_labels_v)
+        dis_loss.backward()
+        dis_optimizer.step()
+        dis_losses.append(dis_loss.item())
+
+        # train generator
+        gen_optimizer.zero_grad()
+        dis_output_v = net_discr(gen_output_v)
+        gen_loss_v = objective(dis_output_v, true_labels_v)
+        gen_loss_v.backward()
+        gen_optimizer.step()
+        gen_losses.append(gen_loss_v.item())
+
+        iter_no += 1
+        print("Iteration : ", iter_no)
+
+        if iter_no % REPORT_EVERY_ITER == 0:
+            log.info("Iter %d: gen_loss=%.3e, dis_loss=%.3e", iter_no, np.mean(gen_losses), np.mean(dis_losses))
+            writer.add_scalar("gen_loss", np.mean(gen_losses), iter_no)
+            writer.add_scalar("dis_loss", np.mean(dis_losses), iter_no)
+            gen_losses = []
+            dis_losses = []
+
+        if iter_no % SAVE_IMAGE_EVERY_ITER == 0:
+            writer.add_image("fake", vutils.make_grid(gen_output_v.data[:IMAGE_SIZE], normalize=True), iter_no)
+            writer.add_image("real", vutils.make_grid(batch_v.data[:IMAGE_SIZE], normalize=True), iter_no)
+
+        if iter_no % BACKUP_EVERY_ITER == 0:
+            if not os.path.exists(models_folder_path):
+                os.makedirs(models_folder_path)
+
+            torch.save({
+                        'iteration': iter_no,
+                        'model_state_dict': net_gener.state_dict(),
+                        'optimizer_state_dict': gen_optimizer.state_dict(),
+                        'gen_losses': gen_losses
+                    }, gen_model_path)
+
+            torch.save({
+                        'iteration': iter_no,
+                        'model_state_dict': net_discr.state_dict(),
+                        'optimizer_state_dict': dis_optimizer.state_dict(),
+                        'dis_losses': dis_losses
+                    }, dis_model_path)
+
+
+
+
+

+ 113 - 0
noise_gan.ipynb

@@ -0,0 +1,113 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "Discriminator(\n",
+      "  (conv_pipe): Sequential(\n",
+      "    (0): Conv2d(3, 200, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))\n",
+      "    (1): ReLU()\n",
+      "    (2): Conv2d(200, 400, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))\n",
+      "    (3): BatchNorm2d(400, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
+      "    (4): ReLU()\n",
+      "    (5): Conv2d(400, 800, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))\n",
+      "    (6): BatchNorm2d(800, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
+      "    (7): ReLU()\n",
+      "    (8): Conv2d(800, 1600, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))\n",
+      "    (9): BatchNorm2d(1600, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
+      "    (10): ReLU()\n",
+      "    (11): Conv2d(1600, 3200, kernel_size=(8, 8), stride=(2, 2), padding=(1, 1))\n",
+      "    (12): BatchNorm2d(3200, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
+      "    (13): ReLU()\n",
+      "    (14): Conv2d(3200, 1, kernel_size=(4, 4), stride=(1, 1))\n",
+      "    (15): Sigmoid()\n",
+      "  )\n",
+      ")\n",
+      "Generator(\n",
+      "  (pipe): Sequential(\n",
+      "    (0): ConvTranspose2d(400, 1000, kernel_size=(6, 6), stride=(1, 1))\n",
+      "    (1): BatchNorm2d(1000, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
+      "    (2): ReLU()\n",
+      "    (3): ConvTranspose2d(1000, 800, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))\n",
+      "    (4): BatchNorm2d(800, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
+      "    (5): ReLU()\n",
+      "    (6): ConvTranspose2d(800, 600, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))\n",
+      "    (7): BatchNorm2d(600, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
+      "    (8): ReLU()\n",
+      "    (9): ConvTranspose2d(600, 400, kernel_size=(6, 6), stride=(2, 2), padding=(1, 1))\n",
+      "    (10): BatchNorm2d(400, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
+      "    (11): ReLU()\n",
+      "    (12): ConvTranspose2d(400, 200, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))\n",
+      "    (13): BatchNorm2d(200, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
+      "    (14): ReLU()\n",
+      "    (15): ConvTranspose2d(200, 3, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))\n",
+      "    (16): Tanh()\n",
+      "  )\n",
+      ")\n",
+      "Iteration :  20\n",
+      "INFO: Iter 20: gen_loss=9.649e+00, dis_loss=5.266e-01\n",
+      "Iteration :  21\n",
+      "Iteration :  22\n",
+      "Iteration :  23\n",
+      "Iteration :  24\n",
+      "Iteration :  25\n",
+      "Iteration :  26\n",
+      "Iteration :  27\n",
+      "Iteration :  28\n",
+      "Iteration :  29\n",
+      "Iteration :  30\n",
+      "INFO: Iter 30: gen_loss=6.851e+00, dis_loss=1.911e-01\n",
+      "Iteration :  31\n",
+      "Iteration :  32\n",
+      "Iteration :  33\n",
+      "Iteration :  34\n",
+      "Iteration :  35\n",
+      "Iteration :  36\n",
+      "Iteration :  37\n",
+      "Iteration :  38\n",
+      "Iteration :  39\n",
+      "Iteration :  40\n",
+      "INFO: Iter 40: gen_loss=6.467e+00, dis_loss=3.223e-01\n"
+     ]
+    }
+   ],
+   "source": [
+    "!python ganSynthesisImage_200.py --load test_model"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3",
+   "language": "python",
+   "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.6.7"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}

+ 36 - 0
prepare_data.py

@@ -0,0 +1,36 @@
+from ipfml import processing
+from PIL import Image
+
+import shutil
+import os
+
+main_image_folder = "synthesis_images"
+images_folder = os.path.join(main_image_folder, "images")
+dest_folder = os.path.join(main_image_folder, "generated_blocks")
+
+if os.path.exists(dest_folder):
+    # first remove folder if necessary
+    os.rmdir(os.path.abspath(os.path.join(os.getcwd(), dest_folder)))
+
+# create new images
+images = os.listdir(images_folder)
+
+if not os.path.exists(dest_folder):
+    os.makedirs(dest_folder)
+
+for img_path in images:
+
+    img = Image.open(os.path.join(images_folder, img_path))
+
+    blocks = processing.divide_in_blocks(img, (200, 200), pil=True)
+
+    for id, pil_block in enumerate(blocks):
+        img_name = img_path.split('/')[-1]
+        split_name = img_name.split('.')
+        block_name = split_name[0] + "_" + str(id) + "." + split_name[1]
+
+        dest_img_block = os.path.join(dest_folder, block_name)
+
+        pil_block.save(dest_img_block)
+
+

+ 3 - 1
requirements.txt

@@ -1,7 +1,9 @@
 gym
-gym[atari]
+atari_py
 torch
 torchvision
 tensorflow
 tensorboardX
 opencv-python
+ipfml
+Pillow

BIN
synthesis_images/images/SdB2_00930.png


BIN
synthesis_images/images/SdB2_00940.png


BIN
synthesis_images/images/SdB2_00950.png


BIN
synthesis_images/images/SdB2_D_00930.png


BIN
synthesis_images/images/SdB2_D_00940.png


BIN
synthesis_images/images/SdB2_D_00950.png


BIN
synthesis_images/images/appartAopt_00880.png


BIN
synthesis_images/images/appartAopt_00890.png


BIN
synthesis_images/images/appartAopt_00900.png


BIN
synthesis_images/images/cuisine01_01180.png


BIN
synthesis_images/images/cuisine01_01190.png


BIN
synthesis_images/images/cuisine01_01200.png


+ 51 - 0
tensorboard.ipynb

@@ -0,0 +1,51 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "!rm -rf runs/"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "TensorBoard 1.12.2 at http://7447cb2679c8:6006 (Press CTRL+C to quit)\n"
+     ]
+    }
+   ],
+   "source": [
+    "!tensorboard --logdir runs"
+   ]
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3",
+   "language": "python",
+   "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.6.7"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}