{"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 numpy as np\nimport pandas as pd\nimport glob\nimport tensorflow as tf\nimport cv2\nfrom PIL import Image\nfrom tensorflow.keras.applications.vgg16 import VGG16\nimport random\nimport os\nimport fnmatch","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-12T13:00:08.065074Z","iopub.execute_input":"2021-09-12T13:00:08.065494Z","iopub.status.idle":"2021-09-12T13:00:13.851514Z","shell.execute_reply.started":"2021-09-12T13:00:08.065406Z","shell.execute_reply":"2021-09-12T13:00:13.850340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 64\nauto = tf.data.AUTOTUNE","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:00:13.853437Z","iopub.execute_input":"2021-09-12T13:00:13.853844Z","iopub.status.idle":"2021-09-12T13:00:13.858987Z","shell.execute_reply.started":"2021-09-12T13:00:13.853801Z","shell.execute_reply":"2021-09-12T13:00:13.857646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monet_paths = glob.glob(\"../input/gan-getting-started/monet_jpg/*.jpg\")\nmonet_paths.extend(glob.glob(\"../input/monet-paintings-jpg-berkeley/*.jpg\"))\n\npath = '../input/impressionist-classifier-data'\nfiles_list = [os.path.join(dirpath, f)\n    for dirpath, dirnames, files in os.walk(path)\n    for f in fnmatch.filter(files, '*.jpg')]\nmonet_paths.extend(files_list)\n\npath = '../input/van-gogh-paintings'\nfiles_list = [os.path.join(dirpath, f)\n    for dirpath, dirnames, files in os.walk(path)\n    for f in fnmatch.filter(files, '*.jpg')]\nmonet_paths.extend(files_list)\n\npath = '../input/impressionistlandscapespaintings/content/drive/MyDrive/impressionist_landscapes_resized_1024'\nfiles_list = [os.path.join(dirpath, f)\n    for dirpath, dirnames, files in os.walk(path)\n    for f in fnmatch.filter(files, '*.jpg')]\nmonet_paths.extend(files_list)\n\nphoto_paths = glob.glob(\"../input/gan-getting-started/photo_jpg/*.jpg\")","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:00:13.861283Z","iopub.execute_input":"2021-09-12T13:00:13.862036Z","iopub.status.idle":"2021-09-12T13:00:19.896549Z","shell.execute_reply.started":"2021-09-12T13:00:13.861989Z","shell.execute_reply":"2021-09-12T13:00:19.895345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_jpg(l):\n    l = f\"{l}\"\n    l = l[2:-1]\n    image = cv2.imread(l)\n    image = cv2.resize(image, (224,224))\n    image = np.ndarray.astype(image, np.float32)\n    \n    return image\n\ndef read_jpg_tf(lt):\n    image = tf.numpy_function(read_jpg, [lt], tf.float32)\n    return image","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:00:19.898447Z","iopub.execute_input":"2021-09-12T13:00:19.898934Z","iopub.status.idle":"2021-09-12T13:00:19.909683Z","shell.execute_reply.started":"2021-09-12T13:00:19.898872Z","shell.execute_reply":"2021-09-12T13:00:19.908323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = VGG16(weights='imagenet')\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:00:51.977330Z","iopub.execute_input":"2021-09-12T13:00:51.977792Z","iopub.status.idle":"2021-09-12T13:00:54.278967Z","shell.execute_reply.started":"2021-09-12T13:00:51.977760Z","shell.execute_reply":"2021-09-12T13:00:54.277870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_feature(array):\n    model = VGG16(weights='imagenet')\n    model = tf.keras.Model(inputs=model.input, outputs=model.get_layer(\"block5_pool\").output)\n    model.trainable = False\n    result = []\n\n    pred = model.predict(array)\n    result.append(pred)\n    return np.squeeze(np.array(result), axis=0)","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:04:14.341474Z","iopub.execute_input":"2021-09-12T13:04:14.341865Z","iopub.status.idle":"2021-09-12T13:04:14.349957Z","shell.execute_reply.started":"2021-09-12T13:04:14.341828Z","shell.execute_reply":"2021-09-12T13:04:14.348330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"photo_loader = tf.data.Dataset.list_files(photo_paths)\n\nmonet_loader = tf.data.Dataset.list_files(monet_paths)","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:02:23.520295Z","iopub.execute_input":"2021-09-12T13:02:23.520664Z","iopub.status.idle":"2021-09-12T13:02:35.343185Z","shell.execute_reply.started":"2021-09-12T13:02:23.520633Z","shell.execute_reply":"2021-09-12T13:02:35.342027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monet_loader = (monet_loader.map(read_jpg_tf)\n                .shuffle(1024)\n                .batch(batch_size, drop_remainder=True)\n                .prefetch(auto))\n\nphoto_loader = (photo_loader.map(read_jpg_tf)\n                .shuffle(1024)\n                .batch(batch_size, drop_remainder=True)\n                .prefetch(auto))","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:02:37.902190Z","iopub.execute_input":"2021-09-12T13:02:37.902534Z","iopub.status.idle":"2021-09-12T13:02:37.983657Z","shell.execute_reply.started":"2021-09-12T13:02:37.902505Z","shell.execute_reply":"2021-09-12T13:02:37.982522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"photo_features = extract_feature(photo_loader)","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:04:19.300603Z","iopub.execute_input":"2021-09-12T13:04:19.300989Z","iopub.status.idle":"2021-09-12T13:04:44.346912Z","shell.execute_reply.started":"2021-09-12T13:04:19.300958Z","shell.execute_reply":"2021-09-12T13:04:44.345744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"photo_features.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:05:03.691808Z","iopub.execute_input":"2021-09-12T13:05:03.692190Z","iopub.status.idle":"2021-09-12T13:05:03.702440Z","shell.execute_reply.started":"2021-09-12T13:05:03.692159Z","shell.execute_reply":"2021-09-12T13:05:03.700853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"photo_features_loader = tf.data.Dataset.from_tensor_slices(photo_features).shuffle(1024).batch(batch_size*2,  drop_remainder=True).prefetch(auto)","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:05:18.418633Z","iopub.execute_input":"2021-09-12T13:05:18.419079Z","iopub.status.idle":"2021-09-12T13:05:18.986867Z","shell.execute_reply.started":"2021-09-12T13:05:18.419047Z","shell.execute_reply":"2021-09-12T13:05:18.985567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generator = tf.keras.Sequential([\n    tf.keras.Input(shape=[7, 7, 512]),\n    tf.keras.layers.Conv2DTranspose(128, kernel_size=5, strides=2, padding=\"same\", \n                                    activation=\"selu\"),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2DTranspose(64, kernel_size=5, strides=2, padding=\"same\", \n                                    activation=\"selu\"),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2DTranspose(32, kernel_size=5, strides=2, padding=\"same\", \n                                    activation=\"selu\"),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2DTranspose(16, kernel_size=5, strides=2, padding=\"same\", \n                                    activation=\"selu\"),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2DTranspose(8, kernel_size=5, strides=2, padding=\"same\", \n                                    activation=\"selu\"),\n    tf.keras.layers.BatchNormalization(),\n    tf.keras.layers.Conv2DTranspose(3, kernel_size=5, strides=1, padding=\"same\", \n                                    activation=\"tanh\"),\n])","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:09:58.640144Z","iopub.execute_input":"2021-09-12T13:09:58.640517Z","iopub.status.idle":"2021-09-12T13:09:58.815690Z","shell.execute_reply.started":"2021-09-12T13:09:58.640484Z","shell.execute_reply":"2021-09-12T13:09:58.814677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generator.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:09:59.895194Z","iopub.execute_input":"2021-09-12T13:09:59.895641Z","iopub.status.idle":"2021-09-12T13:09:59.913867Z","shell.execute_reply.started":"2021-09-12T13:09:59.895595Z","shell.execute_reply":"2021-09-12T13:09:59.911613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"discriminator = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(8, kernel_size=5, strides=2, padding=\"same\", \n                           activation=tf.keras.layers.LeakyReLU(0.2), input_shape=[224,224,3]),\n    tf.keras.layers.Dropout(0.4),\n    tf.keras.layers.Conv2D(16, kernel_size=5, strides=2, padding=\"same\", \n                           activation=tf.keras.layers.LeakyReLU(0.2)),\n    tf.keras.layers.Dropout(0.4),\n    tf.keras.layers.Conv2D(32, kernel_size=5, strides=2, padding=\"same\", \n                           activation=tf.keras.layers.LeakyReLU(0.2)),\n    tf.keras.layers.Dropout(0.4),\n    tf.keras.layers.Conv2D(64, kernel_size=5, strides=2, padding=\"same\", \n                           activation=tf.keras.layers.LeakyReLU(0.2)),\n    tf.keras.layers.Dropout(0.4),\n    tf.keras.layers.Conv2D(128, kernel_size=5, strides=2, padding=\"same\", \n                           activation=tf.keras.layers.LeakyReLU(0.2)),\n    tf.keras.layers.Dropout(0.4),\n    tf.keras.layers.Conv2D(256, kernel_size=5, strides=2, padding=\"same\", \n                           activation=tf.keras.layers.LeakyReLU(0.2)),\n    tf.keras.layers.Dropout(0.4),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(1, activation=\"sigmoid\")\n])","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:10:08.764015Z","iopub.execute_input":"2021-09-12T13:10:08.764448Z","iopub.status.idle":"2021-09-12T13:10:08.876997Z","shell.execute_reply.started":"2021-09-12T13:10:08.764403Z","shell.execute_reply":"2021-09-12T13:10:08.875904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"discriminator.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:10:09.616007Z","iopub.execute_input":"2021-09-12T13:10:09.616453Z","iopub.status.idle":"2021-09-12T13:10:09.636485Z","shell.execute_reply.started":"2021-09-12T13:10:09.616407Z","shell.execute_reply":"2021-09-12T13:10:09.635356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gan = tf.keras.Sequential([generator, discriminator])","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:10:38.883030Z","iopub.execute_input":"2021-09-12T13:10:38.883550Z","iopub.status.idle":"2021-09-12T13:10:38.895242Z","shell.execute_reply.started":"2021-09-12T13:10:38.883494Z","shell.execute_reply":"2021-09-12T13:10:38.893754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gan.build([None,7,7,512])","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:19:51.467267Z","iopub.execute_input":"2021-09-12T13:19:51.467653Z","iopub.status.idle":"2021-09-12T13:19:51.601673Z","shell.execute_reply.started":"2021-09-12T13:19:51.467623Z","shell.execute_reply":"2021-09-12T13:19:51.600624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_gan(gan, monet_loader, photo_features_loader, batch_size, epochs):\n    gen, dis = gan.layers\n    for epoch in range(epochs):\n        for monet_batch, photo_batch in zip(monet_loader, photo_features_loader):\n            #load feature data, reshape and concatenate\n            generated_images = gen(photo_batch[:batch_size])\n            generated_images = tf.cast(generated_images, tf.float32)\n            monet_batch = tf.cast(monet_batch, tf.float32)\n            mix_data = tf.concat([generated_images, monet_batch], axis=0)\n            mix_label = tf.constant([[0.]]*batch_size + [[1.]]*batch_size)            \n            #train discriminator\n            dis.trainable = True\n            dis.train_on_batch(mix_data, mix_label)\n            #train generator\n            generated_label = tf.constant([[1.]]*batch_size)\n            dis.trainable = False\n            gan.train_on_batch(photo_batch[batch_size:], generated_label)","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:19:53.989511Z","iopub.execute_input":"2021-09-12T13:19:53.989974Z","iopub.status.idle":"2021-09-12T13:19:53.998510Z","shell.execute_reply.started":"2021-09-12T13:19:53.989896Z","shell.execute_reply":"2021-09-12T13:19:53.997189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"discriminator.compile(loss=\"binary_crossentropy\", optimizer=\"rmsprop\")\ndiscriminator.trainable=False\ngan.compile(loss=\"binary_crossentropy\", optimizer=\"rmsprop\")","metadata":{"execution":{"iopub.status.busy":"2021-09-12T13:19:57.950151Z","iopub.execute_input":"2021-09-12T13:19:57.950525Z","iopub.status.idle":"2021-09-12T13:19:58.189550Z","shell.execute_reply.started":"2021-09-12T13:19:57.950480Z","shell.execute_reply":"2021-09-12T13:19:58.188483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gan(gan, monet_loader, photo_features_loader, batch_size, 3)","metadata":{"execution":{"iopub.status.busy":"2021-09-12T15:34:20.196149Z","iopub.execute_input":"2021-09-12T15:34:20.196550Z","iopub.status.idle":"2021-09-12T15:39:52.694150Z","shell.execute_reply.started":"2021-09-12T15:34:20.196507Z","shell.execute_reply":"2021-09-12T15:39:52.692986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.models.save_model(gan, \"GAN_PAST\", save_format='h5')","metadata":{"execution":{"iopub.status.busy":"2021-09-12T15:48:53.819638Z","iopub.execute_input":"2021-09-12T15:48:53.820132Z","iopub.status.idle":"2021-09-12T15:48:53.930297Z","shell.execute_reply.started":"2021-09-12T15:48:53.820097Z","shell.execute_reply":"2021-09-12T15:48:53.929193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def produce(generator, photo_feature_loader):\n    outputs = generator([photo_feature_loader])\n    for n, output in enumerate(outputs):\n        output.save(f\"gan_{n}.jpg\")","metadata":{"execution":{"iopub.status.busy":"2021-09-12T15:48:54.928229Z","iopub.execute_input":"2021-09-12T15:48:54.928622Z","iopub.status.idle":"2021-09-12T15:48:54.934943Z","shell.execute_reply.started":"2021-09-12T15:48:54.928590Z","shell.execute_reply":"2021-09-12T15:48:54.933458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = next(iter(photo_features_loader))\noutputs = generator([inputs])","metadata":{"execution":{"iopub.status.busy":"2021-09-12T15:48:55.892202Z","iopub.execute_input":"2021-09-12T15:48:55.892613Z","iopub.status.idle":"2021-09-12T15:48:57.151444Z","shell.execute_reply.started":"2021-09-12T15:48:55.892582Z","shell.execute_reply":"2021-09-12T15:48:57.150397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.imshow(outputs.numpy()[0])","metadata":{"execution":{"iopub.status.busy":"2021-09-12T15:48:59.511368Z","iopub.execute_input":"2021-09-12T15:48:59.511853Z","iopub.status.idle":"2021-09-12T15:49:00.382421Z","shell.execute_reply.started":"2021-09-12T15:48:59.511797Z","shell.execute_reply":"2021-09-12T15:49:00.381323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(outputs.numpy()[1])","metadata":{"execution":{"iopub.status.busy":"2021-09-12T15:49:02.540208Z","iopub.execute_input":"2021-09-12T15:49:02.540625Z","iopub.status.idle":"2021-09-12T15:49:02.798552Z","shell.execute_reply.started":"2021-09-12T15:49:02.540592Z","shell.execute_reply":"2021-09-12T15:49:02.797216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(photo[1001])","metadata":{"execution":{"iopub.status.busy":"2021-09-12T15:49:06.879250Z","iopub.execute_input":"2021-09-12T15:49:06.879628Z","iopub.status.idle":"2021-09-12T15:49:06.907572Z","shell.execute_reply.started":"2021-09-12T15:49:06.879596Z","shell.execute_reply":"2021-09-12T15:49:06.905300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"outputs.numpy()[0]==outputs.numpy()[1]","metadata":{"execution":{"iopub.status.busy":"2021-09-12T15:49:22.061458Z","iopub.execute_input":"2021-09-12T15:49:22.061833Z","iopub.status.idle":"2021-09-12T15:49:22.180534Z","shell.execute_reply.started":"2021-09-12T15:49:22.061801Z","shell.execute_reply":"2021-09-12T15:49:22.179391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def produce_image(list):\n    for l in list:\n        image = generator(l)\n        cv2.imwrite(\".jpg\", outputs","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}