{"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":"# Importing Dependencies","metadata":{}},{"cell_type":"code","source":"pip install git+https://www.github.com/keras-team/keras-contrib.git","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:38.780989Z","iopub.execute_input":"2023-01-15T07:25:38.781366Z","iopub.status.idle":"2023-01-15T07:25:53.944684Z","shell.execute_reply.started":"2023-01-15T07:25:38.781282Z","shell.execute_reply":"2023-01-15T07:25:53.943472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from random import random\nfrom numpy import load\nfrom numpy import zeros\nfrom numpy import ones\nfrom numpy import asarray\nfrom numpy.random import randint\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import Adam\nfrom keras.initializers import RandomNormal\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Input, Conv2D, Conv2DTranspose, LeakyReLU, Activation, Concatenate\nfrom keras_contrib.layers.normalization.instancenormalization import InstanceNormalization\n# Install keras_contrib : https://github.com/keras-team/keras-contrib \nfrom matplotlib import pyplot","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:53.947202Z","iopub.execute_input":"2023-01-15T07:25:53.947609Z","iopub.status.idle":"2023-01-15T07:25:59.286103Z","shell.execute_reply.started":"2023-01-15T07:25:53.947567Z","shell.execute_reply":"2023-01-15T07:25:59.284899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Defining Discriminator","metadata":{}},{"cell_type":"code","source":"def define_discriminator(image_shape):\n\t# weight initialization\n\tinit = RandomNormal(stddev=0.02)\n\t# source image input\n\tin_image = Input(shape=image_shape)\n\t# C64: 4x4 kernel Stride 2x2\n\td = Conv2D(64, (4,4), strides=(2,2), padding='same', kernel_initializer=init)(in_image)\n\td = LeakyReLU(alpha=0.2)(d)\n\t# C128: 4x4 kernel Stride 2x2\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\t# C256: 4x4 kernel Stride 2x2\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\t# C512: 4x4 kernel Stride 2x2 \n    # Not in the original paper. Comment this block if you want.\n\td = Conv2D(512, (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\t# second last output layer : 4x4 kernel but Stride 1x1\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\t# patch output\n\tpatch_out = Conv2D(1, (4,4), padding='same', kernel_initializer=init)(d)\n\t# define model\n\tmodel = Model(in_image, patch_out)\n\t# compile model\n    #The model is trained with a batch size of one image and Adam opt. \n    #with a small learning rate and 0.5 beta. \n    #The loss for the discriminator is weighted by 50% for each model update.\n    #This slows down changes to the discriminator relative to the generator model during training.\n\tmodel.compile(loss='mse', optimizer=Adam(lr=0.0002, beta_1=0.5), loss_weights=[0.5])\n\treturn model","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:59.287557Z","iopub.execute_input":"2023-01-15T07:25:59.288246Z","iopub.status.idle":"2023-01-15T07:25:59.306351Z","shell.execute_reply.started":"2023-01-15T07:25:59.288208Z","shell.execute_reply":"2023-01-15T07:25:59.303776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Defining Resnet Block","metadata":{}},{"cell_type":"code","source":"def resnet_block(n_filters, input_layer):\n\t# weight initialization\n\tinit = RandomNormal(stddev=0.02)\n\t# first convolutional layer\n\tg = Conv2D(n_filters, (3,3), padding='same', kernel_initializer=init)(input_layer)\n\tg = InstanceNormalization(axis=-1)(g)\n\tg = Activation('relu')(g)\n\t# second convolutional layer\n\tg = Conv2D(n_filters, (3,3), padding='same', kernel_initializer=init)(g)\n\tg = InstanceNormalization(axis=-1)(g)\n\t# concatenate merge channel-wise with input layer\n\tg = Concatenate()([g, input_layer])\n\treturn g","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:59.309763Z","iopub.execute_input":"2023-01-15T07:25:59.310210Z","iopub.status.idle":"2023-01-15T07:25:59.322818Z","shell.execute_reply.started":"2023-01-15T07:25:59.310156Z","shell.execute_reply":"2023-01-15T07:25:59.321800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Defining Generator","metadata":{}},{"cell_type":"code","source":"def define_generator(image_shape, n_resnet=9):\n\t# weight initialization\n\tinit = RandomNormal(stddev=0.02)\n\t# image input\n\tin_image = Input(shape=image_shape)\n\t# c7s1-64\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\t# d128\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\t# d256\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\t# R256\n\tfor _ in range(n_resnet):\n\t\tg = resnet_block(256, g)\n\t# u128\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\t# u64\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\t# c7s1-3\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\t# define model\n\tmodel = Model(in_image, out_image)\n\treturn model","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:59.323952Z","iopub.execute_input":"2023-01-15T07:25:59.324295Z","iopub.status.idle":"2023-01-15T07:25:59.337202Z","shell.execute_reply.started":"2023-01-15T07:25:59.324258Z","shell.execute_reply":"2023-01-15T07:25:59.336238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Defining Composite Model","metadata":{}},{"cell_type":"code","source":"def define_composite_model(g_model_1, d_model, g_model_2, image_shape):\n\t# Make the generator of interest trainable as we will be updating these weights.\n    #by keeping other models constant.\n    #Remember that we use this same function to train both generators,\n    #one generator at a time. \n\tg_model_1.trainable = True\n\t# mark discriminator and second generator as non-trainable\n\td_model.trainable = False\n\tg_model_2.trainable = False\n    \n\t# adversarial loss\n\tinput_gen = Input(shape=image_shape)\n\tgen1_out = g_model_1(input_gen)\n\toutput_d = d_model(gen1_out)\n\t# identity loss\n\tinput_id = Input(shape=image_shape)\n\toutput_id = g_model_1(input_id)\n\t# cycle loss - forward\n\toutput_f = g_model_2(gen1_out)\n\t# cycle loss - backward\n\tgen2_out = g_model_2(input_id)\n\toutput_b = g_model_1(gen2_out)\n    \n\t# define model graph\n\tmodel = Model([input_gen, input_id], [output_d, output_id, output_f, output_b])\n\t\n    # define the optimizer\n\topt = Adam(lr=0.0002, beta_1=0.5)\n\t# compile model with weighting of least squares loss and L1 loss\n\tmodel.compile(loss=['mse', 'mae', 'mae', 'mae'], \n               loss_weights=[1, 5, 10, 10], optimizer=opt)\n\treturn model","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:59.338713Z","iopub.execute_input":"2023-01-15T07:25:59.339381Z","iopub.status.idle":"2023-01-15T07:25:59.348168Z","shell.execute_reply.started":"2023-01-15T07:25:59.339342Z","shell.execute_reply":"2023-01-15T07:25:59.347208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Our Data","metadata":{}},{"cell_type":"code","source":"def load_real_samples(filename):\n\t# load the dataset\n\tdata = load(filename)\n\t# unpack arrays\n\tX1, X2 = data['arr_0'], data['arr_1']\n\t# scale from [0,255] to [-1,1]\n\tX1 = (X1 - 127.5) / 127.5\n\tX2 = (X2 - 127.5) / 127.5\n\treturn [X1, X2]","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:59.349483Z","iopub.execute_input":"2023-01-15T07:25:59.350348Z","iopub.status.idle":"2023-01-15T07:25:59.360826Z","shell.execute_reply.started":"2023-01-15T07:25:59.350303Z","shell.execute_reply":"2023-01-15T07:25:59.359767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_real_samples(dataset, n_samples, patch_shape):\n\t# choose random instances\n\tix = randint(0, dataset.shape[0], n_samples)\n\t# retrieve selected images\n\tX = dataset[ix]\n\t# generate 'real' class labels (1)\n\ty = ones((n_samples, patch_shape, patch_shape, 1))\n\treturn X, y","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:59.362364Z","iopub.execute_input":"2023-01-15T07:25:59.363029Z","iopub.status.idle":"2023-01-15T07:25:59.370039Z","shell.execute_reply.started":"2023-01-15T07:25:59.362995Z","shell.execute_reply":"2023-01-15T07:25:59.369231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_fake_samples(g_model, dataset, patch_shape):\n\t# generate fake images\n\tX = g_model.predict(dataset)\n\t# create 'fake' class labels (0)\n\ty = zeros((len(X), patch_shape, patch_shape, 1))\n\treturn X, y","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:59.371334Z","iopub.execute_input":"2023-01-15T07:25:59.372362Z","iopub.status.idle":"2023-01-15T07:25:59.380702Z","shell.execute_reply.started":"2023-01-15T07:25:59.372327Z","shell.execute_reply":"2023-01-15T07:25:59.379535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving Our Model","metadata":{}},{"cell_type":"code","source":"def save_models(step, g_model_AtoB, g_model_BtoA):\n\t# save the first generator model\n\tfilename1 = 'g_model_AtoB_%06d.h5' % (step+1)\n\tg_model_AtoB.save(filename1)\n\t# save the second generator model\n\tfilename2 = 'g_model_BtoA_%06d.h5' % (step+1)\n\tg_model_BtoA.save(filename2)\n\tprint('>Saved: %s and %s' % (filename1, filename2))","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:59.385556Z","iopub.execute_input":"2023-01-15T07:25:59.385852Z","iopub.status.idle":"2023-01-15T07:25:59.392283Z","shell.execute_reply.started":"2023-01-15T07:25:59.385827Z","shell.execute_reply":"2023-01-15T07:25:59.390799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving Images After Each Epoch","metadata":{}},{"cell_type":"code","source":"def summarize_performance(step, g_model, trainX, name, n_samples=5):\n\t# select a sample of input images\n\tX_in, _ = generate_real_samples(trainX, n_samples, 0)\n\t# generate translated images\n\tX_out, _ = generate_fake_samples(g_model, X_in, 0)\n\t# scale all pixels from [-1,1] to [0,1]\n\tX_in = (X_in + 1) / 2.0\n\tX_out = (X_out + 1) / 2.0\n\t# plot real images\n\tfor i in range(n_samples):\n\t\tpyplot.subplot(2, n_samples, 1 + i)\n\t\tpyplot.axis('off')\n\t\tpyplot.imshow(X_in[i])\n\t# plot translated image\n\tfor i in range(n_samples):\n\t\tpyplot.subplot(2, n_samples, 1 + n_samples + i)\n\t\tpyplot.axis('off')\n\t\tpyplot.imshow(X_out[i])\n\t# save plot to file\n\tfilename1 = '%s_generated_plot_%06d.png' % (name, (step+1))\n\tpyplot.savefig(filename1)\n\tpyplot.close()","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:59.393834Z","iopub.execute_input":"2023-01-15T07:25:59.394498Z","iopub.status.idle":"2023-01-15T07:25:59.404520Z","shell.execute_reply.started":"2023-01-15T07:25:59.394463Z","shell.execute_reply":"2023-01-15T07:25:59.403429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def update_image_pool(pool, images, max_size=50):\n\tselected = list()\n\tfor image in images:\n\t\tif len(pool) < max_size:\n\t\t\t# stock 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 image, but don't add it to 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\tix = randint(0, len(pool))\n\t\t\tselected.append(pool[ix])\n\t\t\tpool[ix] = image\n\treturn asarray(selected)","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:59.406967Z","iopub.execute_input":"2023-01-15T07:25:59.407281Z","iopub.status.idle":"2023-01-15T07:25:59.414823Z","shell.execute_reply.started":"2023-01-15T07:25:59.407248Z","shell.execute_reply":"2023-01-15T07:25:59.413637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Our Model","metadata":{}},{"cell_type":"code","source":"def train(d_model_A, d_model_B, g_model_AtoB, g_model_BtoA, c_model_AtoB, c_model_BtoA, dataset, epochs=1):\n\t# define properties of the training run\n\tn_epochs, n_batch, = epochs, 1  #batch size fixed to 1 as suggested in the paper\n\t# determine the output square shape of the discriminator\n\tn_patch = d_model_A.output_shape[1]\n\t# unpack dataset\n\ttrainA, trainB = dataset\n\t# prepare image pool for fake images\n\tpoolA, poolB = list(), list()\n\t# calculate the number of batches per training epoch\n\tbat_per_epo = int(len(trainA) / n_batch)\n\t# calculate the number of training iterations\n\tn_steps = bat_per_epo * n_epochs\n    \n\t# manually enumerate epochs\n\tfor i in range(n_steps):\n\t\t# select a batch of real samples from each domain (A and B)\n\t\tX_realA, y_realA = generate_real_samples(trainA, n_batch, n_patch)\n\t\tX_realB, y_realB = generate_real_samples(trainB, n_batch, n_patch)\n\t\t# generate a batch of fake samples using both B to A and A to B generators.\n\t\tX_fakeA, y_fakeA = generate_fake_samples(g_model_BtoA, X_realB, n_patch)\n\t\tX_fakeB, y_fakeB = generate_fake_samples(g_model_AtoB, X_realA, n_patch)\n\t\t# update fake images in the pool. Remember that the paper suggstes a buffer of 50 images\n\t\tX_fakeA = update_image_pool(poolA, X_fakeA)\n\t\tX_fakeB = update_image_pool(poolB, X_fakeB)\n        \n\t\t# update generator B->A via the composite model\n\t\tg_loss2, _, _, _, _  = c_model_BtoA.train_on_batch([X_realB, X_realA], [y_realA, X_realA, X_realB, X_realA])\n\t\t# update discriminator for A -> [real/fake]\n\t\tdA_loss1 = d_model_A.train_on_batch(X_realA, y_realA)\n\t\tdA_loss2 = d_model_A.train_on_batch(X_fakeA, y_fakeA)\n\t\t\n        # update generator A->B via the composite model\n\t\tg_loss1, _, _, _, _ = c_model_AtoB.train_on_batch([X_realA, X_realB], [y_realB, X_realB, X_realA, X_realB])\n\t\t# update discriminator for B -> [real/fake]\n\t\tdB_loss1 = d_model_B.train_on_batch(X_realB, y_realB)\n\t\tdB_loss2 = d_model_B.train_on_batch(X_fakeB, y_fakeB)\n\t\t\n        # summarize performance\n        #Since our batch size =1, the number of iterations would be same as the size of our dataset.\n        #In one epoch you'd have iterations equal to the number of images.\n        #If you have 100 images then 1 epoch would be 100 iterations\n\t\tprint('Iteration>%d, dA[%.3f,%.3f] dB[%.3f,%.3f] g[%.3f,%.3f]' % (i+1, dA_loss1,dA_loss2, dB_loss1,dB_loss2, g_loss1,g_loss2))\n\t\t# evaluate the model performance periodically\n        #If batch size (total images)=100, performance will be summarized after every 75th iteration.\n\t\tif (i+1) % (bat_per_epo * 1) == 0:\n\t\t\t# plot A->B translation\n\t\t\tsummarize_performance(i, g_model_AtoB, trainA, 'AtoB')\n\t\t\t# plot B->A translation\n\t\t\tsummarize_performance(i, g_model_BtoA, trainB, 'BtoA')\n\t\tif (i+1) % (bat_per_epo * 1) == 0:\n\t\t\t# save the models\n            # #If batch size (total images)=100, model will be saved after \n            #every 75th iteration x 5 = 375 iterations.\n\t\t\tsave_models(i, g_model_AtoB, g_model_BtoA)","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:59.416786Z","iopub.execute_input":"2023-01-15T07:25:59.417205Z","iopub.status.idle":"2023-01-15T07:25:59.430456Z","shell.execute_reply.started":"2023-01-15T07:25:59.417150Z","shell.execute_reply":"2023-01-15T07:25:59.429340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Importing Dependencies","metadata":{}},{"cell_type":"code","source":"import os\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import img_to_array\nfrom tensorflow.keras.preprocessing.image import load_img\nfrom numpy import savez_compressed\nfrom matplotlib import pyplot\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:59.432249Z","iopub.execute_input":"2023-01-15T07:25:59.432742Z","iopub.status.idle":"2023-01-15T07:25:59.442410Z","shell.execute_reply.started":"2023-01-15T07:25:59.432706Z","shell.execute_reply":"2023-01-15T07:25:59.441405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load all images in a directory into memory","metadata":{}},{"cell_type":"code","source":"source = \"/kaggle/input/gan-getting-started/photo_jpg\"\ntarget = \"/kaggle/input/gan-getting-started/monet_jpg\"","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:59.444248Z","iopub.execute_input":"2023-01-15T07:25:59.444653Z","iopub.status.idle":"2023-01-15T07:25:59.452790Z","shell.execute_reply.started":"2023-01-15T07:25:59.444620Z","shell.execute_reply":"2023-01-15T07:25:59.451844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_images(source, target, size=(256,256)):\n    photo = []\n    monet = []\n    for image in os.listdir(source):\n        img_photo = load_img(os.path.join(source, image), target_size=size)\n        img_photo = img_to_array(img_photo)\n        photo.append(img_photo)\n    for image in os.listdir(target):   \n        img_monet = load_img(os.path.join(target, image), target_size=size)\n        img_monet = img_to_array(img_monet)\n        monet.append(img_monet)\n    return np.asarray(photo), np.asarray(monet) ","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:59.454417Z","iopub.execute_input":"2023-01-15T07:25:59.454817Z","iopub.status.idle":"2023-01-15T07:25:59.464467Z","shell.execute_reply.started":"2023-01-15T07:25:59.454770Z","shell.execute_reply":"2023-01-15T07:25:59.463444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"src_images, tar_images = load_images(source, target)","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:25:59.467122Z","iopub.execute_input":"2023-01-15T07:25:59.467414Z","iopub.status.idle":"2023-01-15T07:27:21.380566Z","shell.execute_reply.started":"2023-01-15T07:25:59.467391Z","shell.execute_reply":"2023-01-15T07:27:21.379485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"src_images.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:27:21.382472Z","iopub.execute_input":"2023-01-15T07:27:21.382820Z","iopub.status.idle":"2023-01-15T07:27:21.392022Z","shell.execute_reply.started":"2023-01-15T07:27:21.382785Z","shell.execute_reply":"2023-01-15T07:27:21.391095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tar_images.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:27:21.393740Z","iopub.execute_input":"2023-01-15T07:27:21.394502Z","iopub.status.idle":"2023-01-15T07:27:21.402551Z","shell.execute_reply.started":"2023-01-15T07:27:21.394466Z","shell.execute_reply":"2023-01-15T07:27:21.401650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plotting Samples","metadata":{}},{"cell_type":"code","source":"n_samples = 3\nfor i in range(n_samples):\n\tpyplot.subplot(2, n_samples, 1 + i)\n\tpyplot.axis('off')\n\tpyplot.imshow(src_images[i].astype('uint8'))\n# plot target image\nfor i in range(n_samples):\n\tpyplot.subplot(2, n_samples, 1 + n_samples + i)\n\tpyplot.axis('off')\n\tpyplot.imshow(tar_images[i].astype('uint8'))\npyplot.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:27:21.403960Z","iopub.execute_input":"2023-01-15T07:27:21.404581Z","iopub.status.idle":"2023-01-15T07:27:21.885976Z","shell.execute_reply.started":"2023-01-15T07:27:21.404546Z","shell.execute_reply":"2023-01-15T07:27:21.885022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = [src_images, tar_images]","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:27:21.887108Z","iopub.execute_input":"2023-01-15T07:27:21.887466Z","iopub.status.idle":"2023-01-15T07:27:21.892598Z","shell.execute_reply.started":"2023-01-15T07:27:21.887434Z","shell.execute_reply":"2023-01-15T07:27:21.891667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing Data","metadata":{}},{"cell_type":"code","source":"def preprocess_data(data):\n\t# load compressed arrays\n\t# unpack arrays\n\tX1, X2 = data[0], data[1]\n\t# scale from [0,255] to [-1,1]\n\tX1 = (X1 - 127.5) / 127.5\n\tX2 = (X2 - 127.5) / 127.5\n\treturn [X1, X2]\n\ndataset = preprocess_data(data)","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:27:21.894016Z","iopub.execute_input":"2023-01-15T07:27:21.894657Z","iopub.status.idle":"2023-01-15T07:27:27.288575Z","shell.execute_reply.started":"2023-01-15T07:27:21.894575Z","shell.execute_reply":"2023-01-15T07:27:27.287231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Defining Our CycleGAN Model","metadata":{}},{"cell_type":"code","source":"# define input shape based on the loaded dataset\nimage_shape = dataset[0].shape[1:]\n# generator: A -> B\ng_model_AtoB = define_generator(image_shape)\n# generator: B -> A\ng_model_BtoA = define_generator(image_shape)\n# discriminator: A -> [real/fake]\nd_model_A = define_discriminator(image_shape)\n# discriminator: B -> [real/fake]\nd_model_B = define_discriminator(image_shape)\n# composite: A -> B -> [real/fake, A]\nc_model_AtoB = define_composite_model(g_model_AtoB, d_model_B, g_model_BtoA, image_shape)\n# composite: B -> A -> [real/fake, B]\nc_model_BtoA = define_composite_model(g_model_BtoA, d_model_A, g_model_AtoB, image_shape)","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:27:27.290946Z","iopub.execute_input":"2023-01-15T07:27:27.291730Z","iopub.status.idle":"2023-01-15T07:27:35.073584Z","shell.execute_reply.started":"2023-01-15T07:27:27.291690Z","shell.execute_reply":"2023-01-15T07:27:35.072583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training","metadata":{}},{"cell_type":"code","source":"from datetime import datetime \nstart1 = datetime.now() \n# train models\ntrain(d_model_A, d_model_B, g_model_AtoB, g_model_BtoA, c_model_AtoB, c_model_BtoA, dataset, epochs=5)\n\nstop1 = datetime.now()\n#Execution time of the model \nexecution_time = stop1-start1\nprint(\"Execution time is: \", execution_time)","metadata":{"execution":{"iopub.status.busy":"2023-01-15T07:27:35.075234Z","iopub.execute_input":"2023-01-15T07:27:35.075583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test Model","metadata":{}},{"cell_type":"code","source":"# Use the saved cyclegan models for image translation\nfrom keras_contrib.layers.normalization.instancenormalization import InstanceNormalization\nfrom tensorflow.keras.preprocessing.image import load_img\nfrom tensorflow.keras.preprocessing.image import img_to_array\nfrom keras.models import load_model\nfrom matplotlib import pyplot\nfrom numpy.random import randint\nimport numpy as np","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Models","metadata":{}},{"cell_type":"code","source":"# load the models\ncust = {'InstanceNormalization': InstanceNormalization}\nmodel_AtoB = load_model('models/g_model_AtoB.h5', cust)\nmodel_BtoA = load_model('models/g_model_BtoA.h5', cust)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_plot(imagesX, imagesY1, imagesY2):\n\timages = np.vstack((imagesX, imagesY1, imagesY2))\n\ttitles = ['Real', 'Generated', 'Reconstructed']\n\t# scale from [-1,1] to [0,1]\n\timages = (images + 1) / 2.0\n\t# plot images row by row\n\tfor i in range(len(images)):\n\t\t# define subplot\n\t\tpyplot.subplot(1, len(images), 1 + i)\n\t\t# turn off axis\n\t\tpyplot.axis('off')\n\t\t# plot raw pixel data\n\t\tpyplot.imshow(images[i])\n\t\t# title\n\t\tpyplot.title(titles[i])\n\tpyplot.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Generate Images","metadata":{}},{"cell_type":"code","source":"#Load a single custom image\ntest_image = load_img('test.png', target_size=(256,256))\ntest_image = img_to_array(test_image)\ntest_image_input = np.array([test_image])  # Convert single image to a batch.\ntest_image_input = (test_image_input - 127.5) / 127.5\n\n# plot B->A->B (Colored to B/W to Colored)\nbw_generated  = model_BtoA.predict(test_image_input)\ncolored_reconstructed = model_AtoB.predict(bw_generated)\nshow_plot(test_image_input, bw_generated, colored_reconstructed)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}