{"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":"markdown","source":"# Final Project - Advanced Topics in Deep Learning\n\n---\n<br>\n\n### Name and ID:\nStudent 1: Michal Kabra - (ID. 322724105)\n<br>\nStudent 2: Adi Levanoni - (ID. 208296525)\n<br>\nStudent 3: Ido Moskovitz - (ID. 212107791)\n<br>\nStudent 4: Itay Habani - (ID. 211547807)","metadata":{"id":"XXqIDAd1SeMX"}},{"cell_type":"markdown","source":"## Import Libraries","metadata":{"id":"gwf7VgwsUh2l"}},{"cell_type":"code","source":"# Install additional libraries\n!pip install tensorflow-addons","metadata":{"id":"90Yl64_XX852","outputId":"0bd76c27-99b2-49e7-891b-043ff69d6d89","execution":{"iopub.status.busy":"2023-03-22T16:59:48.622986Z","iopub.execute_input":"2023-03-22T16:59:48.623716Z","iopub.status.idle":"2023-03-22T16:59:58.390869Z","shell.execute_reply.started":"2023-03-22T16:59:48.623676Z","shell.execute_reply":"2023-03-22T16:59:58.389612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pickle\nfrom numpy.random import randint\nfrom random import random\nfrom keras.layers import Conv2D, Conv2DTranspose, LeakyReLU, Activation, Concatenate\nimport tensorflow as tf\nimport tensorflow_datasets as tfds\nfrom matplotlib import pyplot as plt\nfrom tensorflow_addons.layers import InstanceNormalization\nfrom keras.models import Model\nfrom keras.api._v2.keras.layers import Input\nfrom keras.optimizers import Adam\nfrom keras.initializers import RandomNormal\nfrom datetime import datetime\nimport humanize\nimport cv2\nfrom PIL import Image\nimport zipfile","metadata":{"id":"mNrDZSbSUhAl","execution":{"iopub.status.busy":"2023-03-22T17:19:57.241129Z","iopub.execute_input":"2023-03-22T17:19:57.242220Z","iopub.status.idle":"2023-03-22T17:19:57.250173Z","shell.execute_reply.started":"2023-03-22T17:19:57.242161Z","shell.execute_reply":"2023-03-22T17:19:57.249053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Constants","metadata":{"id":"O3AzyDG-fbJL"}},{"cell_type":"code","source":"plots_path = '/plots'\nmodels_path = '/models'","metadata":{"id":"xkdsFxnlfgE6","execution":{"iopub.status.busy":"2023-03-22T16:59:58.406443Z","iopub.execute_input":"2023-03-22T16:59:58.407041Z","iopub.status.idle":"2023-03-22T16:59:58.417244Z","shell.execute_reply.started":"2023-03-22T16:59:58.407002Z","shell.execute_reply":"2023-03-22T16:59:58.416195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading the data","metadata":{"id":"7RQCeEZj-faG"}},{"cell_type":"code","source":"def get_files_full_path(parent_directory):\n    return [f'{parent_directory}/{f}' for f in os.listdir(parent_directory)]","metadata":{"execution":{"iopub.status.busy":"2023-03-22T16:59:58.420811Z","iopub.execute_input":"2023-03-22T16:59:58.421197Z","iopub.status.idle":"2023-03-22T16:59:58.426970Z","shell.execute_reply.started":"2023-03-22T16:59:58.421122Z","shell.execute_reply":"2023-03-22T16:59:58.425820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"flat = lambda list: [item for sublist in list for item in sublist]\nsub_files = lambda parent: flat([[f'{parent}/{folder}/{f}' for f in get_files_full_path(parent)] for folder in listdir(parent)])","metadata":{"execution":{"iopub.status.busy":"2023-03-22T16:59:58.428466Z","iopub.execute_input":"2023-03-22T16:59:58.429067Z","iopub.status.idle":"2023-03-22T16:59:58.436682Z","shell.execute_reply.started":"2023-03-22T16:59:58.429022Z","shell.execute_reply":"2023-03-22T16:59:58.435419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def add_to_zip(file_path, zip_path, name):\n    with zipfile.ZipFile(zip_path, 'a') as zipf:\n        destination = f'{name}.png' if name else file_path.split('/')[-1]\n        zipf.write(file_path, destination)","metadata":{"execution":{"iopub.status.busy":"2023-03-22T17:20:03.149644Z","iopub.execute_input":"2023-03-22T17:20:03.150623Z","iopub.status.idle":"2023-03-22T17:20:03.156937Z","shell.execute_reply.started":"2023-03-22T17:20:03.150559Z","shell.execute_reply":"2023-03-22T17:20:03.155750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preprocess the data","metadata":{"id":"aXzW13axDfaF"}},{"cell_type":"code","source":"original_size = (256, 256, 3)\nsize = (320, 320, 3)\n\nresize = lambda image: cv2.resize(image, dsize=size[:2], interpolation=cv2.INTER_CUBIC) / 127.5 - 1\n# 0-255        ->        0-1      ->        0-2        ->      -1 - 1\nrescale_back = lambda image: ((image + 1) * 127.5).astype(np.uint8)\nresize_to_original_size = lambda image: cv2.resize(rescale_back(image), dsize=original_size[:2], interpolation=cv2.INTER_CUBIC)","metadata":{"id":"A6MP9JxWDiuH","execution":{"iopub.status.busy":"2023-03-22T16:59:58.447631Z","iopub.execute_input":"2023-03-22T16:59:58.448203Z","iopub.status.idle":"2023-03-22T16:59:58.456165Z","shell.execute_reply.started":"2023-03-22T16:59:58.448166Z","shell.execute_reply":"2023-03-22T16:59:58.454723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"load_images = lambda folder: np.array([np.array(Image.open(image).resize(size[:2])) for image in get_files_full_path(folder)])","metadata":{"execution":{"iopub.status.busy":"2023-03-22T16:59:58.457970Z","iopub.execute_input":"2023-03-22T16:59:58.458408Z","iopub.status.idle":"2023-03-22T16:59:58.464873Z","shell.execute_reply.started":"2023-03-22T16:59:58.458314Z","shell.execute_reply":"2023-03-22T16:59:58.463711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Started loading images')\n# Split the dataset into training and testing sets\nmonet = load_images('/kaggle/input/gan-getting-started/monet_jpg')\nphoto = load_images('/kaggle/input/gan-getting-started/photo_jpg')\nprint('Finished loading images')","metadata":{"id":"9LCBOMNw-h44","outputId":"8223ddc6-7411-49b7-ab89-49dab2f71467","execution":{"iopub.status.busy":"2023-03-22T16:59:58.466931Z","iopub.execute_input":"2023-03-22T16:59:58.467374Z","iopub.status.idle":"2023-03-22T17:01:16.518063Z","shell.execute_reply.started":"2023-03-22T16:59:58.467337Z","shell.execute_reply":"2023-03-22T17:01:16.516999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_monet = monet.astype(float) / 127.5 - 1\ntrain_photo = photo[:800].astype(float) / 127.5 - 1","metadata":{"id":"FswF0k3TkCBb","execution":{"iopub.status.busy":"2023-03-22T17:01:16.522678Z","iopub.execute_input":"2023-03-22T17:01:16.522977Z","iopub.status.idle":"2023-03-22T17:01:17.867538Z","shell.execute_reply.started":"2023-03-22T17:01:16.522948Z","shell.execute_reply":"2023-03-22T17:01:17.866498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"monet:\", monet.shape)\nprint(\"photo:\", photo.shape)\nprint(\"train monet:\", train_monet.shape)\nprint(\"train photo:\", train_photo.shape)","metadata":{"id":"68W72Qqgtvxv","outputId":"3c3a14bd-cbb9-4a2e-de35-544b2bfacf7e","execution":{"iopub.status.busy":"2023-03-22T17:31:08.377525Z","iopub.execute_input":"2023-03-22T17:31:08.378049Z","iopub.status.idle":"2023-03-22T17:31:08.388022Z","shell.execute_reply.started":"2023-03-22T17:31:08.378000Z","shell.execute_reply":"2023-03-22T17:31:08.386560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Defining the models","metadata":{"id":"nMaCN7fOYPwN"}},{"cell_type":"code","source":"def resnet_block(filters_count, input_layer):\n\tinit = RandomNormal(stddev=0.02)\n \n\tg = Conv2D(filters_count, (3,3), padding='same', kernel_initializer=init)(input_layer)\n\tg = InstanceNormalization(axis=-1)(g)\n\tg = Activation('relu')(g)\n \n\tg = Conv2D(filters_count, (3,3), padding='same', kernel_initializer=init)(g)\n\tg = InstanceNormalization(axis=-1)(g)\n \n\tg = Concatenate()([g, input_layer])\n \n\treturn g","metadata":{"id":"ewvS5Bqa73oB","execution":{"iopub.status.busy":"2023-03-22T17:01:17.877266Z","iopub.execute_input":"2023-03-22T17:01:17.877693Z","iopub.status.idle":"2023-03-22T17:01:17.889057Z","shell.execute_reply.started":"2023-03-22T17:01:17.877656Z","shell.execute_reply":"2023-03-22T17:01:17.888067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Discriminators <br>\nSize of patchGAN was calculated by using:\nreceptive field = (output_size - 1) * stride + kernel size","metadata":{"id":"9CepiOeDcR2E"}},{"cell_type":"markdown","source":"103X103 PatchGAN","metadata":{"id":"BPu2N-VoYVxF"}},{"cell_type":"code","source":"def discriminator103(image_shape, name=\"\", learning_rate=0.0002):  \n\tinit = RandomNormal(stddev=0.02)\n \n\tinput_layer = Input(shape=image_shape)\n \n\td = Conv2D(64, (2,2), strides=(3,3), padding='same', kernel_initializer=init)(input_layer)\n\td = LeakyReLU(alpha=0.2)(d)\n \n\td = Conv2D(128, (4,4), strides=(2,2), padding='same', kernel_initializer=init)(d)\n\td = InstanceNormalization(axis=-1)(d)\n\td = LeakyReLU(alpha=0.2)(d)\n \n\td = Conv2D(256, (4,4), strides=(2,2), padding='same', kernel_initializer=init)(d)\n\td = InstanceNormalization(axis=-1)(d)\n\td = LeakyReLU(alpha=0.2)(d)\n \n\td = Conv2D(512, (4,4), padding='same', kernel_initializer=init)(d)\n\td = InstanceNormalization(axis=-1)(d)\n\td = LeakyReLU(alpha=0.2)(d)\n \n\toutput_layer = Conv2D(1, (4,4), padding='same', kernel_initializer=init)(d)\n \n\tmodel = Model(input_layer, output_layer, name=name)\n\n\toptimizer = Adam(lr=learning_rate, beta_1=0.5) \n\tmodel.compile(loss='mse', optimizer=optimizer, loss_weights=[0.5])\n\n\treturn model","metadata":{"id":"JU59zX7qYXi5","execution":{"iopub.status.busy":"2023-03-22T17:01:17.890609Z","iopub.execute_input":"2023-03-22T17:01:17.891083Z","iopub.status.idle":"2023-03-22T17:01:17.901891Z","shell.execute_reply.started":"2023-03-22T17:01:17.891048Z","shell.execute_reply":"2023-03-22T17:01:17.900720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Generators","metadata":{"id":"UpS27hg1cVS0"}},{"cell_type":"code","source":"def generator(image_shape, name, residual_blocks_count=9):\n\tinit = RandomNormal(stddev=0.02)\n \n\tin_image = Input(shape=image_shape)\n\n\tg = Conv2D(64, (7,7), padding='same', kernel_initializer=init)(in_image)\n\tg = InstanceNormalization(axis=-1)(g)\n\tg = Activation('relu')(g)\n \n\tg = Conv2D(128, (3,3), strides=(2,2), padding='same', kernel_initializer=init)(g)\n\tg = InstanceNormalization(axis=-1)(g)\n\tg = Activation('relu')(g)\n \n\tg = Conv2D(256, (3,3), strides=(2,2), padding='same', kernel_initializer=init)(g)\n\tg = InstanceNormalization(axis=-1)(g)\n\tg = Activation('relu')(g)\n \n\tfor _ in range(residual_blocks_count):\n\t\tg = resnet_block(256, g)\n  \n\tg = Conv2DTranspose(128, (3,3), strides=(2,2), padding='same', kernel_initializer=init)(g)\n\tg = InstanceNormalization(axis=-1)(g)\n\tg = Activation('relu')(g)\n \n\tg = Conv2DTranspose(64, (3,3), strides=(2,2), padding='same', kernel_initializer=init)(g)\n\tg = InstanceNormalization(axis=-1)(g)\n\tg = Activation('relu')(g)\n \n\tg = Conv2D(3, (7,7), padding='same', kernel_initializer=init)(g)\n\tg = InstanceNormalization(axis=-1)(g)\n\tout_image = Activation('tanh')(g)\n \n\tmodel = Model(in_image, out_image, name=name)\n \n\treturn model","metadata":{"id":"AR90bR6tcZTG","execution":{"iopub.status.busy":"2023-03-22T17:01:17.903726Z","iopub.execute_input":"2023-03-22T17:01:17.904141Z","iopub.status.idle":"2023-03-22T17:01:17.915934Z","shell.execute_reply.started":"2023-03-22T17:01:17.904106Z","shell.execute_reply":"2023-03-22T17:01:17.914740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Composited model","metadata":{"id":"BevAPBk6edjB"}},{"cell_type":"code","source":"def cycle_gan(forward_gen, discriminator, backward_gen, image_shape, learning_rate=0.0002):  \n\t# Only make the forward generator trainable\n  # We call this function twice, so both generators will be trained\n\tforward_gen.trainable = True\n  \n\tbackward_gen.trainable = False\n\tdiscriminator.trainable = False\n    \n\t# Adversarial loss\n\tinput_gen = Input(shape=image_shape)\n\tforward_output = forward_gen(input_gen)\n\toutput_d = discriminator(forward_output)\n \n\t# Identity loss\n\tinput_id = Input(shape=image_shape) \n\toutput_id = forward_gen(input_id)\n\t\n\t# Forward cycle loss\n\t# resized_forward_output = tf.image.resize(forward_output, backward_gen.input_shape)\n\toutput_f = backward_gen(forward_output)\n \n\t# Backward cycle loss\n\tbackward_output = backward_gen(input_id)\n\toutput_b = forward_gen(backward_output)\n    \n\t# define model graph\n\tmodel = Model([input_gen, input_id],\n                [output_d, output_id, output_f, output_b])\n\t\n  # define the optimizer\n\toptimizer = Adam(lr=learning_rate, beta_1=0.5)\n \n\tmodel.compile(loss=['mse', 'mae', 'mae', 'mae'], \n                loss_weights=[1, 5, 10, 10], optimizer=optimizer)\n \n\treturn model","metadata":{"id":"88t8XhEtejrC","execution":{"iopub.status.busy":"2023-03-22T17:01:17.917465Z","iopub.execute_input":"2023-03-22T17:01:17.918578Z","iopub.status.idle":"2023-03-22T17:01:17.928824Z","shell.execute_reply.started":"2023-03-22T17:01:17.918539Z","shell.execute_reply":"2023-03-22T17:01:17.927811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train Functions","metadata":{"id":"b-qdQyG0NffM"}},{"cell_type":"code","source":"# Get random real images and labels\ndef get_real_images(dataset, batch_size, patch_shape=0):\n  # Choose random images\n\trandom_indexes = randint(0, dataset.shape[0], batch_size)\n \n\t# retrieve selected images\n\timages = dataset[random_indexes]\n \n\t# Generate \"true\" labels\n\tlabels = np.ones(len(images))\n \n\treturn images, labels\n\n# Get fake images\ndef generate_fake_images(generator, dataset, patch_shape=0):\n\t# Generate fake images from the dataset\n\timages = generator.predict(dataset, verbose=0)\n \n\t# Generate \"fake\" labels\n\tlabels = np.zeros(len(images))\n \n\treturn images, labels","metadata":{"id":"UL1aPDt1Nlie","execution":{"iopub.status.busy":"2023-03-22T17:01:17.930739Z","iopub.execute_input":"2023-03-22T17:01:17.931179Z","iopub.status.idle":"2023-03-22T17:01:17.941030Z","shell.execute_reply.started":"2023-03-22T17:01:17.931128Z","shell.execute_reply":"2023-03-22T17:01:17.940121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def summarize_performance(iteration, generator, dataset, name, experiment_name=\"\", count=5):\n  # Get random images and generate fake ones\n  real_images, _ = get_real_images(dataset, count)\n  fake_images, _ = generate_fake_images(generator, real_images)\n  \n  # scale all pixels from [-1,1] to [0,255]\n  presentable_real_images = rescale_back(real_images)\n  presentable_fake_images = rescale_back(fake_images)\n\n  title = generator.name if generator.name else name\n  plt.suptitle(f'{title} - Iteration {iteration}')\n\n  # plot real images\n  for i, image in enumerate(presentable_real_images):\n    plt.subplot(2, count, 1 + i)\n    plt.axis('off')\n    plt.imshow(image)\n\n  # plot translated image\n  for i, image in enumerate(presentable_fake_images):\n    plt.subplot(2, count, 1 + count + i)\n    plt.axis('off')\n    plt.imshow(image)\n\n  experiment_folder = f'{experiment_name}/' if not experiment_name else ''\n\n  # save plot to file\n  plot_directory = f'{plots_path}/{experiment_folder}{title}'\n\n  if not os.path.exists(plot_directory):\n    os.makedirs(plot_directory)\n\n  plt.savefig(f'{plot_directory}/{iteration}.png')\n  plt.show(block=True)","metadata":{"id":"WVBAbHV8f_kM","execution":{"iopub.status.busy":"2023-03-22T17:01:17.942943Z","iopub.execute_input":"2023-03-22T17:01:17.943399Z","iopub.status.idle":"2023-03-22T17:01:17.953402Z","shell.execute_reply.started":"2023-03-22T17:01:17.943362Z","shell.execute_reply":"2023-03-22T17:01:17.952215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Function that maintains the generated images pool, which helps with the diversity of the training data, and lowers overfitting.","metadata":{"id":"7HUvA6fCPlOw"}},{"cell_type":"code","source":"def update_image_pool(pool, images, max_size=50):\n\tselected = list()\n \n\tfor image in images:\n\t\tif len(pool) < max_size:\n\t\t\t# Fill the pool\n\t\t\tpool.append(image)\n\t\t\tselected.append(image)\n\t\telif random() < 0.5:\n\t\t\t# use original image, without using or changing the pool\n\t\t\tselected.append(image)\n\t\telse:\n\t\t\t# replace an existing image and use replaced image\n\t\t\trandomIndex = randint(0, len(pool))\n\t\t\tselected.append(pool[randomIndex])\n\t\t\tpool[randomIndex] = image\n\n\treturn np.asarray(selected)","metadata":{"id":"QgR-rKrAaXEY","execution":{"iopub.status.busy":"2023-03-22T17:01:17.954948Z","iopub.execute_input":"2023-03-22T17:01:17.955374Z","iopub.status.idle":"2023-03-22T17:01:17.965650Z","shell.execute_reply.started":"2023-03-22T17:01:17.955340Z","shell.execute_reply":"2023-03-22T17:01:17.964511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train cyclegan models\ndef train(discriminator_a, discriminator_b, generator_a2b, generator_b2a, model_a2b, model_b2a, dataset, batch_size=1, epochs=1, experiment_name=\"\", with_pool=False):\n\tprint('Starting training...')\n\t\n\t# determine the output square shape of the discriminator\n\tpatch_shape = discriminator_a.output_shape[1]\n\t\n  # unpack dataset\n\ttrainA, trainB = dataset\n\t\n  # prepare image pool for fake images\n\tpoolA, poolB = list(), list()\n\t\n  # calculate the number of batches per training epoch\n\tbatch_count = int(len(trainA) / batch_size)\n \n\t# calculate the number of training iterations\n\tsteps_count = batch_count * epochs\n    \n\thistory = list()\n\n\t# manually enumerate epochs\n\tfor i in range(steps_count):\n\t\t# select a batch of real samples from each domain (A and B)\n\t\ta_images, a_labels = get_real_images(trainA, batch_size, patch_shape)\n\t\tb_images, b_labels = get_real_images(trainB, batch_size, patch_shape)\n\n    # generate a batch of fake samples using both B to A and A to B generators.\n\t\tfake_b_images, fake_b_labels = generate_fake_images(generator_a2b, a_images, patch_shape)\n\t\tfake_a_images, fake_a_labels = generate_fake_images(generator_b2a, b_images, patch_shape)\n\t\t\n    # update fake images in the pool.\n\t\tif(with_pool):\n\t\t\tfake_b_images = update_image_pool(poolB, fake_b_images)\n\t\t\tfake_a_images = update_image_pool(poolA, fake_a_images)\n\n    # update generator A->B via the composite model\n\t\tgenerator_a2b_loss, _, _, _, _ = model_a2b.train_on_batch([a_images, b_images],\n\t\t                                                          [b_labels, b_images, a_images, b_images])\n\n\t\t# update discriminator for B -> [real/fake]\n\t\tdiscriminator_b_loss = discriminator_b.train_on_batch(b_images, b_labels)\n\t\tdiscriminator_b_fake_loss = discriminator_b.train_on_batch(fake_b_images, fake_b_labels)\n        \n\t\t# update generator B->A via the composite model\n\t\tgenerator_b2a_loss, _, _, _, _  = model_b2a.train_on_batch([b_images, a_images],\n\t\t                                                           [a_labels, a_images, b_images, a_images])\n\t\t# update discriminator for A -> [real/fake]\n\t\tdiscriminator_a_loss = discriminator_a.train_on_batch(a_images, a_labels)\n\t\tdiscriminator_a_fake_loss = discriminator_a.train_on_batch(fake_a_images, fake_a_labels)\n\t\n\t\titeration = i + 1\n\t\n\t\t# Show performance \n\t\tif iteration == 1 or iteration % 100 == 0:\n\t\t\tprint(f'\\nIteration {iteration}: dA[{discriminator_a_loss:.3f},{discriminator_a_fake_loss:.3f}] dB[{discriminator_b_loss:.3f},{discriminator_b_fake_loss:.3f}] g[{generator_a2b_loss:.3f},{generator_b2a_loss:.3f}]')\n\t \n\t\t\tsummarize_performance(iteration, generator_a2b, trainA, 'AtoB', experiment_name=experiment_name)\n\t\t\tprint()\n\t\t\tsummarize_performance(iteration, generator_b2a, trainB, 'BtoA', experiment_name=experiment_name)\n\n\t\t\thistory.append({\n\t\t\t\t\t'iteration': iteration,\n\t\t\t\t\t'gen_a2b': generator_a2b_loss,\n\t\t\t\t\t'gen_b2a': generator_b2a_loss,\n\t\t\t\t\t'dis_a': discriminator_a_loss,\n\t\t\t\t\t'dis_b': discriminator_b_loss\n\t\t\t})\n\t \n\treturn history","metadata":{"id":"IommWKpVHe0h","execution":{"iopub.status.busy":"2023-03-22T17:01:17.967645Z","iopub.execute_input":"2023-03-22T17:01:17.967910Z","iopub.status.idle":"2023-03-22T17:01:17.980674Z","shell.execute_reply.started":"2023-03-22T17:01:17.967886Z","shell.execute_reply":"2023-03-22T17:01:17.979642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_gen_losses(history):\n  iterations = [iteration['iteration'] for iteration in history]\n  gen_a2b = [iteration['gen_a2b'] for iteration in history]\n  gen_b2a = [iteration['gen_b2a'] for iteration in history]\n  \n  _, ax = plt.subplots()\n  ax.set_title('Generator Losses')\n  ax.set_xlabel('Iteration')\n  ax.set_ylabel('Loss')\n\n  ax.plot(iterations, gen_a2b, label='gen_a2b')\n  ax.plot(iterations, gen_b2a, label='gen_b2a')\n\n  ax.legend()\n  plt.show()\n\n\ndef plot_dis_losses(history):\n  iterations = [iteration['iteration'] for iteration in history]\n  dis_a = [iteration['dis_a'] for iteration in history]\n  dis_b = [iteration['dis_b'] for iteration in history]\n  \n  _, ax = plt.subplots()\n  ax.set_title('Discriminator Losses')\n  ax.set_xlabel('Iteration')\n  ax.set_ylabel('Loss')\n\n  ax.plot(iterations, dis_a, label='dis_a')\n  ax.plot(iterations, dis_b, label='dis_b')\n\n  ax.legend()\n  plt.show()\n\n\ndef plot_model_history(history):\n    plot_gen_losses(history)\n    print()\n    plot_dis_losses(history)","metadata":{"id":"A9jvx0hp76Vm","execution":{"iopub.status.busy":"2023-03-22T17:01:17.982048Z","iopub.execute_input":"2023-03-22T17:01:17.982624Z","iopub.status.idle":"2023-03-22T17:01:17.994238Z","shell.execute_reply.started":"2023-03-22T17:01:17.982577Z","shell.execute_reply":"2023-03-22T17:01:17.993267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training","metadata":{"id":"WTdMQwKJf_BX"}},{"cell_type":"code","source":"# Train with image pool with the 103x103 PatchGAN discriminator\n\nprint('started model building')\ndiscriminator = discriminator103\n\nimage_shape = size\n\nmonet_to_photo = generator(image_shape, 'Monet2Photo')\nphoto_to_monet = generator(image_shape, 'Photo2Monet')\n\ndiscriminator_monet = discriminator(image_shape, 'MonetDiscriminator')\ndiscriminator_photo = discriminator(image_shape, 'PhotoDiscriminator')\n\nmonet_gan = cycle_gan(monet_to_photo, discriminator_photo, photo_to_monet, image_shape)\nphoto_gan = cycle_gan(photo_to_monet, discriminator_monet, monet_to_photo, image_shape)\nprint('finished model buildiing')","metadata":{"id":"RP8L4rXbzGTO","outputId":"8056456b-1b4f-4f1e-bb17-53717009b33d","execution":{"iopub.status.busy":"2023-03-22T17:56:54.650104Z","iopub.execute_input":"2023-03-22T17:56:54.651265Z","iopub.status.idle":"2023-03-22T17:57:03.297665Z","shell.execute_reply.started":"2023-03-22T17:56:54.651207Z","shell.execute_reply":"2023-03-22T17:57:03.296626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"experiment_name='experiment3'\nprint('started training')\n\ntrain_dataset = (train_photo[:500], train_monet)\n\nhistory3 = train(discriminator_photo, discriminator_monet, photo_to_monet, monet_to_photo, photo_gan, monet_gan, train_dataset, epochs=5, experiment_name=experiment_name, with_pool=True)\nprint('finished training')","metadata":{"id":"Iy5PmyzDzGTP","outputId":"1114d9b4-fcbe-4df0-ecc6-bbe0216b85d3","execution":{"iopub.status.busy":"2023-03-22T17:57:03.299651Z","iopub.execute_input":"2023-03-22T17:57:03.300022Z","iopub.status.idle":"2023-03-22T18:06:39.720241Z","shell.execute_reply.started":"2023-03-22T17:57:03.299984Z","shell.execute_reply":"2023-03-22T18:06:39.718877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"outputs = '/tmp/outputs'\nzip_file = '/kaggle/working/images.zip'\n\nbatches = np.split(photo[:7000], 14)\n\ntry:\n    shutil.rmtree(outputs)\nexcept:\n    pass\n   \ntry:\n    os.remove(zip_file)\nexcept:\n    pass\n    \nfor index, batch in enumerate(batches):    \n    generated_photos, _ = generate_fake_images(photo_to_monet, batch)\n\n    output_directory = f'{outputs}/output{index}'\n                             \n    if not os.path.exists(output_directory):\n        os.makedirs(output_directory)\n\n    for i, image in enumerate(generated_photos):\n        image_path = f'{output_directory}/{index+1}.png'\n        scaled_image = resize_to_original_size(image)\n        \n        cv2.imwrite(image_path, cv2.cvtColor(scaled_image, cv2.COLOR_RGB2BGR))\n        add_to_zip(image_path, zip_file, f'batch{(index + 1)}_image{(i + 1)}')","metadata":{"execution":{"iopub.status.busy":"2023-03-22T17:38:54.970933Z","iopub.execute_input":"2023-03-22T17:38:54.971657Z","iopub.status.idle":"2023-03-22T17:49:09.886637Z","shell.execute_reply.started":"2023-03-22T17:38:54.971618Z","shell.execute_reply":"2023-03-22T17:49:09.885518Z"},"trusted":true},"execution_count":null,"outputs":[]}]}