{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport gc\nimport itertools\nfrom pprint import pprint\nimport random\n\nimport numpy as np\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torchvision\nfrom torchvision import transforms\nimport torch.optim as optim\nfrom torch.utils.data import Dataset, DataLoader\nfrom torch.autograd import Variable","metadata":{"execution":{"iopub.status.busy":"2021-08-12T19:06:07.057118Z","iopub.execute_input":"2021-08-12T19:06:07.057509Z","iopub.status.idle":"2021-08-12T19:06:08.402499Z","shell.execute_reply.started":"2021-08-12T19:06:07.057431Z","shell.execute_reply":"2021-08-12T19:06:08.401642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # This Python 3 environment comes with many helpful analytics libraries installed\n# # It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# # For example, here's several helpful packages to load\n\n# import numpy as np # linear algebra\n# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# # Input data files are available in the read-only \"../input/\" directory\n# # For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input/gan-getting-started/monet_jpg'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# # You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# # You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"execution":{"iopub.status.busy":"2021-08-12T19:06:08.405803Z","iopub.execute_input":"2021-08-12T19:06:08.406064Z","iopub.status.idle":"2021-08-12T19:06:08.411946Z","shell.execute_reply.started":"2021-08-12T19:06:08.406039Z","shell.execute_reply":"2021-08-12T19:06:08.411123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# set numpy seed to always get the same 30 style images\nnp.random.seed(0)\ntorch.random.manual_seed(0)\nrandom.seed(0)\n\ndef sample_images(paths, n_samples=30):\n    idxs = np.sort(np.random.choice(len(paths), n_samples, replace=False))\n\n    return paths[idxs]","metadata":{"execution":{"iopub.status.busy":"2021-08-12T19:06:08.413835Z","iopub.execute_input":"2021-08-12T19:06:08.414182Z","iopub.status.idle":"2021-08-12T19:06:08.423449Z","shell.execute_reply.started":"2021-08-12T19:06:08.414146Z","shell.execute_reply":"2021-08-12T19:06:08.422663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load the images paths","metadata":{"execution":{"iopub.status.busy":"2021-08-09T08:20:29.351114Z","iopub.execute_input":"2021-08-09T08:20:29.351741Z","iopub.status.idle":"2021-08-09T08:20:29.359158Z","shell.execute_reply.started":"2021-08-09T08:20:29.351705Z","shell.execute_reply":"2021-08-09T08:20:29.358264Z"}}},{"cell_type":"code","source":"def load_images(path, image_shape=(256,256)):\n    monet_path = os.path.join(path, 'monet_jpg')\n    photo_path = os.path.join(path, 'photo_jpg')\n\n    style_images_paths = np.array(list(os.listdir(monet_path)))\n    content_images_paths = np.array(list(os.listdir(photo_path)))\n\n    sampled_style_images_paths = sample_images(style_images_paths)\n    sampled_content_images_paths = sample_images(content_images_paths, n_samples=7000)\n\n    return sampled_style_images_paths, sampled_content_images_paths\n\n# dataset path\ntrain_path = '/kaggle/input/gan-getting-started'\nstyle_imgs, content_imgs = load_images(train_path)\n\nprint(content_imgs.shape)\nprint(style_imgs.shape)\npprint(style_imgs)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T19:06:08.426513Z","iopub.execute_input":"2021-08-12T19:06:08.426801Z","iopub.status.idle":"2021-08-12T19:06:08.586780Z","shell.execute_reply.started":"2021-08-12T19:06:08.426768Z","shell.execute_reply":"2021-08-12T19:06:08.585932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/nspitzern/kaggle-monet-competition.git\n    \n!mkdir /kaggle/temp\n!mv /kaggle/working/kaggle-monet-competition /kaggle/temp/kaggle-monet-competition","metadata":{"execution":{"iopub.status.busy":"2021-08-12T19:06:08.588061Z","iopub.execute_input":"2021-08-12T19:06:08.588431Z","iopub.status.idle":"2021-08-12T19:06:38.032235Z","shell.execute_reply.started":"2021-08-12T19:06:08.588392Z","shell.execute_reply":"2021-08-12T19:06:38.030978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"kaggle_working_dir = '/kaggle/working'\nkaggle_working_output_dir = '/kaggle/working/output'\nos.mkdir(kaggle_working_output_dir)\n\nkaggle_my_files = '/kaggle/temp/kaggle-monet-competition'\n\nwith open(os.path.join(kaggle_my_files, 'style_files_path.npy'), 'rb') as f:\n    style_imgs = np.load(f)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T19:06:38.033911Z","iopub.execute_input":"2021-08-12T19:06:38.034331Z","iopub.status.idle":"2021-08-12T19:06:38.048487Z","shell.execute_reply.started":"2021-08-12T19:06:38.034282Z","shell.execute_reply":"2021-08-12T19:06:38.047107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(20, 20))\ncolumns = 6\nrows = 5\nfor i in range(1, columns*rows +1):\n    img = Image.open(os.path.join(train_path, 'monet_jpg', style_imgs[i - 1]))\n    fig.add_subplot(rows, columns, i)\n    plt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-12T19:06:38.050460Z","iopub.execute_input":"2021-08-12T19:06:38.050891Z","iopub.status.idle":"2021-08-12T19:06:42.578410Z","shell.execute_reply.started":"2021-08-12T19:06:38.050847Z","shell.execute_reply":"2021-08-12T19:06:42.577353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MonetSampledDataset(Dataset):\n    def __init__(self, root_path, content_root_path, style_root_path, content_paths, style_paths, style_transforms):\n        self.root_path = root_path\n        self.content_root_path = content_root_path\n        self.style_root_path = style_root_path\n        self.content_paths = content_paths\n        self.style_paths = style_paths\n        self.style_transforms = style_transforms\n        self.to_tensor = transforms.Compose([\n                transforms.ToTensor(), \n                transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # normalize the image\n        ])\n\n    def __getitem__(self, content_idx):\n        # get the current content image\n        content_image_path = self.content_paths[content_idx]\n        content_image = Image.open(os.path.join(self.root_path, self.content_root_path, content_image_path))\n\n        # sample a style image\n        style_idx = np.random.choice(len(self.style_paths))\n        style_image_path = self.style_paths[style_idx]\n        style_image = Image.open(os.path.join(self.root_path,  self.style_root_path, style_image_path))\n\n        # convert to tensors\n        content_image = self.to_tensor(content_image)\n        original_style_image = self.to_tensor(style_image.copy())\n        style_image = self.style_transforms(style_image)\n\n        return {'photo': content_image, 'monet': original_style_image, 'monet_aug': style_image}\n\n    def __len__(self):\n        return len(self.content_paths)\n    \n    def resample_content_images(self):\n        photo_path = os.path.join(self.root_path, self.content_root_path)\n        content_images_paths = np.array(list(os.listdir(photo_path)))\n        self.content_paths = sample_images(content_images_paths, n_samples=1000)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T19:06:42.581223Z","iopub.execute_input":"2021-08-12T19:06:42.581661Z","iopub.status.idle":"2021-08-12T19:06:42.595297Z","shell.execute_reply.started":"2021-08-12T19:06:42.581613Z","shell.execute_reply":"2021-08-12T19:06:42.594481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"style_transforms = transforms.Compose([\n    transforms.RandomHorizontalFlip(p=0.5),\n#     transforms.RandomRotation(15),\n    transforms.ColorJitter(brightness=0.05, contrast=0.05, saturation=0.05, hue=0.05),\n    transforms.RandomResizedCrop((256, 256)),\n    transforms.ToTensor(), # numpy array to tensor\n    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # normalize the image between [-1,1]              \n])","metadata":{"execution":{"iopub.status.busy":"2021-08-12T19:06:42.597161Z","iopub.execute_input":"2021-08-12T19:06:42.597869Z","iopub.status.idle":"2021-08-12T19:06:42.606801Z","shell.execute_reply.started":"2021-08-12T19:06:42.597824Z","shell.execute_reply":"2021-08-12T19:06:42.605791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor2img = transforms.Compose([transforms.Normalize((-1, -1, -1), (2, 2 ,2)), # normalize the image between [0, 1]\n                                 transforms.ToPILImage()])\n\ndef tensor2image(image):\n    return tensor2img(image)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T19:06:42.608357Z","iopub.execute_input":"2021-08-12T19:06:42.608943Z","iopub.status.idle":"2021-08-12T19:06:42.618721Z","shell.execute_reply.started":"2021-08-12T19:06:42.608908Z","shell.execute_reply":"2021-08-12T19:06:42.617496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Create Dataset and Dataloader","metadata":{"execution":{"iopub.status.busy":"2021-08-09T08:20:53.233617Z","iopub.execute_input":"2021-08-09T08:20:53.234301Z","iopub.status.idle":"2021-08-09T08:20:53.240031Z","shell.execute_reply.started":"2021-08-09T08:20:53.234251Z","shell.execute_reply":"2021-08-09T08:20:53.238948Z"}}},{"cell_type":"code","source":"# define dataset\nmonet_dataset = MonetSampledDataset(root_path=train_path,\n                                    content_root_path='photo_jpg',\n                                    style_root_path='monet_jpg',\n                                    content_paths=content_imgs,\n                                    style_paths=style_imgs,\n                                    style_transforms=style_transforms)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T19:06:42.620190Z","iopub.execute_input":"2021-08-12T19:06:42.620747Z","iopub.status.idle":"2021-08-12T19:06:42.627724Z","shell.execute_reply.started":"2021-08-12T19:06:42.620708Z","shell.execute_reply":"2021-08-12T19:06:42.626918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(monet_dataset))\nprint(monet_dataset[0]['photo'].shape)\nprint(monet_dataset[0]['monet'].shape)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T19:06:42.629135Z","iopub.execute_input":"2021-08-12T19:06:42.629759Z","iopub.status.idle":"2021-08-12T19:06:42.784231Z","shell.execute_reply.started":"2021-08-12T19:06:42.629717Z","shell.execute_reply":"2021-08-12T19:06:42.783013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define Generator","metadata":{"execution":{"iopub.status.busy":"2021-08-09T08:20:53.399941Z","iopub.execute_input":"2021-08-09T08:20:53.400463Z","iopub.status.idle":"2021-08-09T08:20:53.404138Z","shell.execute_reply.started":"2021-08-09T08:20:53.400421Z","shell.execute_reply":"2021-08-09T08:20:53.403329Z"}}},{"cell_type":"code","source":"class ResidualBlock(nn.Module):\n    def __init__(self, in_features):\n        super(ResidualBlock, self).__init__()\n\n        self.conv_block = nn.Sequential(\n            nn.ReflectionPad2d(1),\n            nn.Conv2d(in_features, in_features, 3),\n            nn.InstanceNorm2d(in_features),\n            nn.ReLU(inplace=True),\n            nn.ReflectionPad2d(1),\n            nn.Conv2d(in_features, in_features, 3),\n            nn.InstanceNorm2d(in_features)\n        )\n\n    def forward(self, x):\n        return x + self.conv_block(x)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T19:06:42.785759Z","iopub.execute_input":"2021-08-12T19:06:42.786176Z","iopub.status.idle":"2021-08-12T19:06:42.794738Z","shell.execute_reply.started":"2021-08-12T19:06:42.786131Z","shell.execute_reply":"2021-08-12T19:06:42.793376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Generator(nn.Module):\n    def __init__(self, input_size, output_size, n_residual_blocks=9):\n        super(Generator, self).__init__()\n\n        # Initial convolution block\n        model = [\n            nn.ReflectionPad2d(3),\n            nn.Conv2d(input_size, 64, kernel_size=7),\n            nn.InstanceNorm2d(64),\n            nn.ReLU(inplace=True)\n        ]\n\n        # Downsampling\n        in_features = 64\n        out_features = in_features * 2\n\n        for _ in range(2):\n            model += [\n                nn.Conv2d(in_features, out_features, kernel_size=3, stride=2, padding=1),\n                nn.InstanceNorm2d(out_features),\n                nn.ReLU(inplace=True)\n            ]\n\n            in_features = out_features\n            out_features = in_features * 2\n\n        # Residual blocks\n        for _ in range(n_residual_blocks):\n            model += [\n                ResidualBlock(in_features=in_features)\n            ]\n\n        # Upsampling\n        out_features = in_features // 2\n        for _ in range(2):\n            model += [\n                nn.ConvTranspose2d(in_features, out_features, kernel_size=3, stride=2, padding=1, output_padding=1),\n                nn.InstanceNorm2d(out_features),\n                nn.ReLU(inplace=True)\n            ]\n\n            in_features = out_features\n            out_features = in_features // 2\n\n        # Output layer\n        model += [\n                  nn.ReflectionPad2d(3),\n                  nn.Conv2d(64, output_size, 7),\n                  nn.Tanh()\n        ]\n\n        self.model = nn.Sequential(*model)\n\n    def forward(self, x):\n        return self.model(x)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T19:06:42.796914Z","iopub.execute_input":"2021-08-12T19:06:42.797472Z","iopub.status.idle":"2021-08-12T19:06:42.811515Z","shell.execute_reply.started":"2021-08-12T19:06:42.797425Z","shell.execute_reply":"2021-08-12T19:06:42.810166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_model(path, generator_photo2monet, device):\n    generator_photo2monet.load_state_dict(torch.load(os.path.join(path, 'generator_photo2monet.pth')))\n\n    generator_photo2monet.to(device)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T19:06:42.813187Z","iopub.execute_input":"2021-08-12T19:06:42.813602Z","iopub.status.idle":"2021-08-12T19:06:42.823575Z","shell.execute_reply.started":"2021-08-12T19:06:42.813562Z","shell.execute_reply":"2021-08-12T19:06:42.822642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"in_channels = 3\nout_channels = 3\ndevice = 'cuda' if torch.cuda.is_available else 'cpu'\n\ngenerator_photo2monet = Generator(in_channels, out_channels).to(device)\nload_model(os.path.join(kaggle_my_files, 'pretrain_train/models'), generator_photo2monet, device)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T19:09:16.520788Z","iopub.execute_input":"2021-08-12T19:09:16.521118Z","iopub.status.idle":"2021-08-12T19:09:16.670527Z","shell.execute_reply.started":"2021-08-12T19:09:16.521087Z","shell.execute_reply":"2021-08-12T19:09:16.669670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 1\n\n# dataset loader\nmonet_dataloder = DataLoader(monet_dataset, batch_size=1, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-09T13:03:54.463256Z","iopub.execute_input":"2021-08-09T13:03:54.466011Z","iopub.status.idle":"2021-08-09T13:03:54.474878Z","shell.execute_reply.started":"2021-08-09T13:03:54.465965Z","shell.execute_reply":"2021-08-09T13:03:54.473685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create Fake images","metadata":{"execution":{"iopub.status.busy":"2021-08-09T08:20:58.052504Z","iopub.execute_input":"2021-08-09T08:20:58.053022Z","iopub.status.idle":"2021-08-09T08:20:58.061584Z","shell.execute_reply.started":"2021-08-09T08:20:58.052987Z","shell.execute_reply":"2021-08-09T08:20:58.060755Z"}}},{"cell_type":"code","source":"def save_image(output_path, image, i):\n    output_path = os.path.join(output_path, f'{i}.jpg')\n    image.save(output_path)","metadata":{"execution":{"iopub.status.busy":"2021-08-09T13:03:54.476706Z","iopub.execute_input":"2021-08-09T13:03:54.477572Z","iopub.status.idle":"2021-08-09T13:03:54.678487Z","shell.execute_reply.started":"2021-08-09T13:03:54.477505Z","shell.execute_reply":"2021-08-09T13:03:54.677051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for l in os.listdir(kaggle_working_dir):\n#     if l.endswith('.jpg'):\n#         os.remove(os.path.join(kaggle_working_dir, l))","metadata":{"execution":{"iopub.status.busy":"2021-08-09T13:05:15.154414Z","iopub.execute_input":"2021-08-09T13:05:15.154827Z","iopub.status.idle":"2021-08-09T13:05:15.168161Z","shell.execute_reply.started":"2021-08-09T13:05:15.154796Z","shell.execute_reply":"2021-08-09T13:05:15.166917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = []\n\ngenerator_photo2monet.eval()\n\n\nTensor = torch.cuda.FloatTensor if torch.cuda.is_available else torch.Tensor\ninput_photo = Tensor(BATCH_SIZE, in_channels, 256, 256).to(device)\n\nfor i, batch in enumerate(monet_dataloder):\n    real_photo = Variable(input_photo.copy_(batch['photo'])).to(device)\n    \n    fake_monet = generator_photo2monet(real_photo).cpu().squeeze().detach()\n    \n    fake_monet = fake_monet.cpu().detach()\n    fake_monet = tensor2image(fake_monet)\n    \n    save_image(kaggle_working_output_dir, fake_monet, i)\n    \n    print(f'Image #{i} created...')","metadata":{"execution":{"iopub.status.busy":"2021-08-09T13:05:36.215438Z","iopub.execute_input":"2021-08-09T13:05:36.215891Z","iopub.status.idle":"2021-08-09T13:12:12.242215Z","shell.execute_reply.started":"2021-08-09T13:05:36.215857Z","shell.execute_reply":"2021-08-09T13:12:12.241126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\n\nshutil.rmtree(kaggle_my_files)","metadata":{"execution":{"iopub.status.busy":"2021-08-09T12:53:41.939308Z","iopub.execute_input":"2021-08-09T12:53:41.939779Z","iopub.status.idle":"2021-08-09T12:53:42.363921Z","shell.execute_reply.started":"2021-08-09T12:53:41.939721Z","shell.execute_reply":"2021-08-09T12:53:42.362766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from zipfile import ZipFile\n\nwith ZipFile('images.zip', 'w') as zip:\n    for file in os.listdir(kaggle_working_output_dir):\n        if file.endswith('.jpg'):\n            zip.write(os.path.join(kaggle_working_output_dir, file), file)\n    print('zip created')","metadata":{"execution":{"iopub.status.busy":"2021-08-09T13:22:51.50001Z","iopub.execute_input":"2021-08-09T13:22:51.500384Z","iopub.status.idle":"2021-08-09T13:22:52.588501Z","shell.execute_reply.started":"2021-08-09T13:22:51.500353Z","shell.execute_reply":"2021-08-09T13:22:52.58722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree(kaggle_working_output_dir)\n# os.listdir(kaggle_working_output_dir)","metadata":{"execution":{"iopub.status.busy":"2021-08-09T13:24:30.486351Z","iopub.execute_input":"2021-08-09T13:24:30.486781Z","iopub.status.idle":"2021-08-09T13:24:30.734744Z","shell.execute_reply.started":"2021-08-09T13:24:30.486749Z","shell.execute_reply":"2021-08-09T13:24:30.733705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.remove('images.zip')","metadata":{"execution":{"iopub.status.busy":"2021-08-09T13:22:44.744025Z","iopub.execute_input":"2021-08-09T13:22:44.744502Z","iopub.status.idle":"2021-08-09T13:22:44.761322Z","shell.execute_reply.started":"2021-08-09T13:22:44.744465Z","shell.execute_reply":"2021-08-09T13:22:44.760055Z"},"trusted":true},"execution_count":null,"outputs":[]}]}