{"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":"from google.colab import drive\ndrive.mount('/content/gdrive')\n","metadata":{"id":"3j3GUuUaC9Mg","outputId":"2b9df3e8-1312-4e95-f531-e3cce645485f"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /content/cloned-repo/dataset/Rajasthani_Paintings\n","metadata":{"id":"cAE7xnttDBi-","outputId":"e617a02a-d1d1-42b1-fe01-38daf9cef379"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Clone the entire repo.\n!git clone -l -s git://github.com/venkateshdb/artistGan.git cloned-repo\n%cd cloned-repo\n!ls","metadata":{"id":"qKso1r1T6aIK","outputId":"a8edb47b-5876-4373-ddd2-aa061f7c327e"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import requests as r\n\ndataset = r.get(\"https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/monet2photo.zip\")\n\nprint(dataset)","metadata":{"id":"95w-WrFx6yH7"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/monet2photo.zip","metadata":{"id":"Ekwhfphz-AFS","outputId":"850f2457-386c-49e7-bf2d-5c8db9134ba2"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /content/monet2photo.zip","metadata":{"id":"rUCXEUMUBhe-","outputId":"84595e7f-d7d9-4e15-f707-211f6482e6d1"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# monet2photo\nfrom os import listdir\nfrom numpy import asarray\nfrom numpy import vstack\nfrom keras.preprocessing.image import img_to_array\nfrom keras.preprocessing.image import load_img\nfrom matplotlib import pyplot as plt\nimport numpy as np\n\n# load all images in a directory into memory\ndef load_images(path, size=(256,256)):\n\tdata_list = list()\n\t# enumerate filenames in directory, assume all are images\n\tfor filename in listdir(path):\n\t\t# load and resize the image\n\t\tpixels = load_img(path + filename, target_size=size)\n\t\t# convert to numpy array\n\t\tpixels = img_to_array(pixels)\n\t\t# store\n\t\tdata_list.append(pixels)\n\treturn asarray(data_list)\n\n\n# dataset path\ndataset_raj = \"/content/cloned-repo/dataset/\"\npath = '/content/cloned-repo/monet2photo/'\n\n# load dataset A - Monet paintings\ndataA_all = load_images(dataset_raj + 'Rajasthani_Paintings/')\nprint('Loaded dataA: ', dataA_all.shape)\n\nfrom sklearn.utils import resample\n#To get a subset of all images, for faster training during demonstration\ndataA = resample(dataA_all, \n                 replace=False,     \n                 n_samples=500,    \n                 random_state=42) \n\n# load dataset B - Photos \ndataB_all = load_images(path + 'trainB/')\nprint('Loaded dataB: ', dataB_all.shape)\n#Get a subset of all images, for faster training during demonstration\n#We could have just read the list of files and only load a subset, better memory management. \ndataB = resample(dataB_all, \n                 replace=False,     \n                 n_samples=500,    \n                 random_state=42) \n\n# plot source images\nn_samples = 3\nfor i in range(n_samples):\n\tplt.subplot(2, n_samples, 1 + i)\n\tplt.axis('off')\n\tplt.imshow(dataA[i].astype('uint8'))\n# plot target image\nfor i in range(n_samples):\n\tplt.subplot(2, n_samples, 1 + n_samples + i)\n\tplt.axis('off')\n\tplt.imshow(dataB[i].astype('uint8'))\nplt.show()\n\n\n\n# load image data\ndata = [dataA, dataB]\n\nprint('Loaded', data[0].shape, data[1].shape)\n\n#Preprocess data to change input range to values between -1 and 1\n# This is because the generator uses tanh activation in the output layer\n#And tanh ranges between -1 and 1\ndef 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)\n\nfrom cycleGAN_model import define_generator, define_discriminator, define_composite_model, train\n# 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)\n\nfrom 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=7)\n\nstop1 = datetime.now()\n#Execution time of the model \nexecution_time = stop1-start1\nprint(\"Execution time is: \", execution_time)\n\n############################################\n\n# Use the saved cyclegan models for image translation\nfrom instancenormalization import InstanceNormalization  \nfrom keras.models import load_model\nfrom matplotlib import pyplot\nfrom numpy.random import randint\n\n# select a random sample of images from the dataset\ndef select_sample(dataset, n_samples):\n\t# choose random instances\n\tix = randint(0, dataset.shape[0], n_samples)\n\t# retrieve selected images\n\tX = dataset[ix]\n\treturn X\n\n# plot the image, its translation, and the reconstruction\ndef show_plot(imagesX, imagesY1, imagesY2):\n\timages = 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()\n\n# load dataset\nA_data = resample(dataA_all, \n                 replace=False,     \n                 n_samples=50,    \n                 random_state=42) # reproducible results\n\nB_data = resample(dataB_all, \n                 replace=False,     \n                 n_samples=50,    \n                 random_state=42) # reproducible results\n\nA_data = (A_data - 127.5) / 127.5\nB_data = (B_data - 127.5) / 127.5\n\n\n","metadata":{"id":"xy4zGqxGoGkn","outputId":"041fbbab-58a0-4834-d706-82dcfd370445"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load all images in a directory into memory\ndef load_images(path, size=(256,256)):\n\tdata_list = list()\n\t# enumerate filenames in directory, assume all are images\n\tfor filename in listdir(path):\n\t\t# load and resize the image\n\t\tpixels = load_img(path + filename, target_size=size)\n\t\t# convert to numpy array\n\t\tpixels = img_to_array(pixels)\n\t\t# store\n\t\tdata_list.append(pixels)\n\treturn asarray(data_list)\n","metadata":{"id":"gfQlkoRyflla"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Use the saved cyclegan models for image translation\nfrom instancenormalization import InstanceNormalization  \nfrom keras.models import load_model\nfrom matplotlib import pyplot\nfrom numpy.random import randint\n\nfrom sklearn.utils import resample\n\n# select a random sample of images from the dataset\ndef select_sample(dataset, n_samples):\n\t# choose random instances\n\tix = randint(0, dataset.shape[0], n_samples)\n\t# retrieve selected images\n\tX = dataset[ix]\n\treturn X\n\n# plot the image, its translation, and the reconstruction\ndef show_plot(imagesX, imagesY1, imagesY2):\n\timages = 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()\n# dataset path\ndataset_raj = \"/content/cloned-repo/dataset/\"\npath = '/content/cloned-repo/monet2photo/'\n\n# load dataset A - Monet paintings\ndataA_all = load_images(dataset_raj + 'Rajasthani_Paintings/')\nprint('Loaded dataA: ', dataA_all.shape)\n\nfrom sklearn.utils import resample\n#To get a subset of all images, for faster training during demonstration\ndataA = resample(dataA_all, \n                 replace=False,     \n                 n_samples=500,    \n                 random_state=42) \n\n# load dataset B - Photos \ndataB_all = load_images(path + 'trainB/')\nprint('Loaded dataB: ', dataB_all.shape)\n#Get a subset of all images, for faster training during demonstration\n#We could have just read the list of files and only load a subset, better memory management. \ndataB = resample(dataB_all, \n                 replace=False,     \n                 n_samples=500,    \n                 random_state=42) \n\n# load dataset\nA_data = resample(dataA_all, \n                 replace=False,     \n                 n_samples=50,    \n                 random_state=42) # reproducible results\n\nB_data = resample(dataB_all, \n                 replace=False,     \n                 n_samples=50,    \n                 random_state=42) # reproducible results\n\nA_data = (A_data - 127.5) / 127.5\nB_data = (B_data - 127.5) / 127.5\n","metadata":{"id":"ZzQfZJxEeefl","outputId":"d35dcb10-0c3a-4bdd-e30a-1168ae8c6ab5"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from instancenormalization import InstanceNormalization  \nfrom keras.models import load_model\nfrom matplotlib import pyplot\nfrom numpy.random import randint\n\n# select a random sample of images from the dataset\ndef select_sample(dataset, n_samples):\n\t# choose random instances\n\tix = randint(0, dataset.shape[0], n_samples)\n\t# retrieve selected images\n\tX = dataset[ix]\n\treturn X\n\n# plot the image, its translation, and the reconstruction\ndef show_plot(imagesX, imagesY1, imagesY2):\n\timages = 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()\n\n","metadata":{"id":"OQvipSTfeBCA"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# monet2photo\nfrom os import listdir\nfrom numpy import asarray\nfrom numpy import vstack\nfrom keras.preprocessing.image import img_to_array\nfrom keras.preprocessing.image import load_img\nfrom matplotlib import pyplot as plt\nimport numpy as np\nfrom PIL import Image\n\nfrom instancenormalization import InstanceNormalization  \nfrom keras.models import load_model\nfrom matplotlib import pyplot\nfrom numpy.random import randint\n\n# load the models\ncust = {'InstanceNormalization': InstanceNormalization}\nmodel_AtoB = load_model('/content/gdrive/MyDrive/Gan_model/g_model_AtoB_001500.h5', cust)\nmodel_BtoA = load_model('/content/gdrive/MyDrive/Gan_model/g_model_BtoA_001500.h5', cust)\n\n# def resize_img(img):\n\n\n\n##########################\n#Load a single custom image\ntest_image = load_img('/content/tr.jpg', 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 (Photo to Monet to Photo)\nmonet_generated  = model_BtoA.predict(test_image_input)\nphoto_reconstructed = model_AtoB.predict(monet_generated)\nshow_plot(test_image_input, monet_generated, photo_reconstructed)","metadata":{"id":"PZG-Vv-2Bujg","outputId":"5aa0e007-199d-486f-ab37-08e871045410"},"execution_count":null,"outputs":[]}]}