{"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":"## Dependencies","metadata":{"id":"gQKk4hDxzU3s"}},{"cell_type":"code","source":"!pip install tensorflow-addons","metadata":{"id":"QLfPdYsWcM5r","outputId":"96ce8919-7489-4962-c750-e76aed53bb64","execution":{"iopub.status.busy":"2021-07-21T13:23:58.912528Z","iopub.execute_input":"2021-07-21T13:23:58.912982Z","iopub.status.idle":"2021-07-21T13:24:08.186685Z","shell.execute_reply.started":"2021-07-21T13:23:58.912888Z","shell.execute_reply":"2021-07-21T13:24:08.185515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os, random, json, PIL, shutil, re\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport tensorflow.keras.layers as layers\nimport tensorflow_addons as tfa\nfrom tensorflow.keras import Model, losses, optimizers\nimport time","metadata":{"id":"hmzO-BFxzU3s","execution":{"iopub.status.busy":"2021-07-21T13:24:08.188463Z","iopub.execute_input":"2021-07-21T13:24:08.18877Z","iopub.status.idle":"2021-07-21T13:24:15.893912Z","shell.execute_reply.started":"2021-07-21T13:24:08.188736Z","shell.execute_reply":"2021-07-21T13:24:15.892727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## TPU configuration","metadata":{"id":"KR4IGR6EzU3s"}},{"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    print(f'Running on TPU {tpu.master()}')\nexcept ValueError:\n    tpu = None\n\nif tpu:\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nelse:\n    strategy = tf.distribute.get_strategy()\n\n\nREPLICAS = strategy.num_replicas_in_sync\nprint(f'REPLICAS: {REPLICAS}')\nAUTO = tf.data.experimental.AUTOTUNE","metadata":{"id":"d9ix3TGhzU3s","outputId":"0620d811-8c63-4d50-aabd-7011e4f72bfe","execution":{"iopub.status.busy":"2021-07-21T13:24:15.895773Z","iopub.execute_input":"2021-07-21T13:24:15.896097Z","iopub.status.idle":"2021-07-21T13:24:21.766005Z","shell.execute_reply.started":"2021-07-21T13:24:15.896066Z","shell.execute_reply":"2021-07-21T13:24:21.765052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model parameters","metadata":{"id":"tZac16P0zU3t"}},{"cell_type":"code","source":"HEIGHT = 256\nWIDTH = 256\nCHANNELS = 3\nEPOCHS = 50\nBATCH_SIZE = 32","metadata":{"id":"LJYdExXTzU3t","execution":{"iopub.status.busy":"2021-07-21T13:24:21.767699Z","iopub.execute_input":"2021-07-21T13:24:21.768345Z","iopub.status.idle":"2021-07-21T13:24:21.773958Z","shell.execute_reply.started":"2021-07-21T13:24:21.768212Z","shell.execute_reply":"2021-07-21T13:24:21.772634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data","metadata":{"id":"LTIBsojNzU3t"}},{"cell_type":"code","source":"try:\n    from kaggle_datasets import KaggleDatasets\n    GCS_PATH = KaggleDatasets().get_gcs_path('gan-getting-started')\nexcept:\n    GCS_PATH = \"gs://kds-2ee06126c50f46e241ae426668de2fce51b526beb231d382373580fa\"\n\n\nMONET_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/monet_tfrec/*.tfrec'))\nPHOTO_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/photo_tfrec/*.tfrec'))\n","metadata":{"id":"3k1rz3OkVf3K","execution":{"iopub.status.busy":"2021-07-21T13:26:42.825381Z","iopub.execute_input":"2021-07-21T13:26:42.8258Z","iopub.status.idle":"2021-07-21T13:26:43.302031Z","shell.execute_reply.started":"2021-07-21T13:26:42.825767Z","shell.execute_reply":"2021-07-21T13:26:43.300774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Auxiliar functions","metadata":{"id":"u_OieYHMzU3u"}},{"cell_type":"code","source":"def decode_image(image):\n    image = tf.image.decode_jpeg(image, channels=CHANNELS)\n    image = (tf.cast(image, tf.float32) / 127.5) - 1\n    image = tf.reshape(image, [HEIGHT, WIDTH, CHANNELS])\n    return image\n\ndef read_tfrecord(example):\n    tfrecord_format = {\n        'image_name': tf.io.FixedLenFeature([], tf.string),\n        'image':      tf.io.FixedLenFeature([], tf.string),\n        'target':     tf.io.FixedLenFeature([], tf.string)\n    }\n    example = tf.io.parse_single_example(example, tfrecord_format)\n    image = decode_image(example['image'])\n    return image\n\ndef load_dataset(filenames):\n    dataset = tf.data.TFRecordDataset(filenames)\n    dataset = dataset.map(read_tfrecord, num_parallel_calls=AUTO)\n    return dataset","metadata":{"id":"_mjAEuKmzU3u","execution":{"iopub.status.busy":"2021-07-21T13:26:46.792222Z","iopub.execute_input":"2021-07-21T13:26:46.792599Z","iopub.status.idle":"2021-07-21T13:26:46.801565Z","shell.execute_reply.started":"2021-07-21T13:26:46.792568Z","shell.execute_reply":"2021-07-21T13:26:46.800234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Generating datasets","metadata":{"id":"HwvGPoq-zU3w"}},{"cell_type":"code","source":"\nmonet_ds = load_dataset(MONET_FILENAMES).batch(1)\nphoto_ds = load_dataset(PHOTO_FILENAMES).batch(1)\n\n\nfast_photo_ds = load_dataset(PHOTO_FILENAMES).batch(32*strategy.num_replicas_in_sync).prefetch(32)\nfid_photo_ds = load_dataset(PHOTO_FILENAMES).take(1024).batch(32*strategy.num_replicas_in_sync).prefetch(32)\nfid_monet_ds = load_dataset(MONET_FILENAMES).batch(32*strategy.num_replicas_in_sync).prefetch(32)","metadata":{"id":"bWhWZgHQh3p8","outputId":"3f909c79-2a46-4e8d-f050-c0bc60f61d1e","execution":{"iopub.status.busy":"2021-07-21T13:26:48.914539Z","iopub.execute_input":"2021-07-21T13:26:48.915119Z","iopub.status.idle":"2021-07-21T13:26:49.202873Z","shell.execute_reply.started":"2021-07-21T13:26:48.915072Z","shell.execute_reply":"2021-07-21T13:26:49.201729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize predictions","metadata":{"id":"-2vQwaYDzU3z"}},{"cell_type":"code","source":"# model_path = '../input/d0eb4546-f602-4898-9e06-29a68eddf64a/model.h5'\nmodel_path = '../input/300-pics-model/model.h5'\n\n# load a model for inference\nloaded_model = tf.keras.models.load_model(model_path, compile=False)\n\n# do some inference and plot\nwith strategy.scope():\n    row = 4\n    col = 2\n    ds_iter = iter(photo_ds)\n    plt.figure(figsize=(24, 24))\n    for j in range(0, row * (col * 2), 2):\n        example_sample = next(ds_iter)\n        plt.subplot(row, col * 2, j + 1)\n        plt.title('Input image')\n        plt.imshow(example_sample[0] * 0.5 + 0.5)\n        plt.axis('off')\n        \n        generated_sample = loaded_model(example_sample)\n        plt.subplot(row, col * 2, j + 2)\n        plt.title('Generated image')\n        plt.imshow(generated_sample[0] * 0.5 + 0.5)\n        plt.axis('off')\n    plt.show()","metadata":{"id":"jK8XBOUtjMhB","outputId":"889cab65-7b0d-4565-a44c-fbfd1e5f520f","execution":{"iopub.status.busy":"2021-07-21T13:29:47.965138Z","iopub.execute_input":"2021-07-21T13:29:47.965519Z","iopub.status.idle":"2021-07-21T13:29:57.825421Z","shell.execute_reply.started":"2021-07-21T13:29:47.965473Z","shell.execute_reply":"2021-07-21T13:29:57.823879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission","metadata":{"id":"Ryyzlu9EOmhQ"}},{"cell_type":"code","source":"import PIL\n! mkdir ../images\n! mkdir ../original_images","metadata":{"id":"b0f3_trij0d9","execution":{"iopub.status.busy":"2021-07-21T13:30:11.697832Z","iopub.execute_input":"2021-07-21T13:30:11.698234Z","iopub.status.idle":"2021-07-21T13:30:13.211151Z","shell.execute_reply.started":"2021-07-21T13:30:11.698202Z","shell.execute_reply":"2021-07-21T13:30:13.210088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 1\nfor img in fast_photo_ds:\n    prediction = loaded_model(img, training=False).numpy()\n    prediction = (prediction * 127.5 + 127.5).astype(np.uint8)\n    for pred in prediction:\n        im = PIL.Image.fromarray(pred)\n        im.save(\"../images/\" + str(i) + \".jpg\")\n        i += 1\n#         if i % 500 == 0:\n#             print(str(i) + ' prediction images created')\n\ni = 1\nfor img in fast_photo_ds.unbatch():\n    orig_img = img.numpy()\n    orig_img = (orig_img * 127.5 + 127.5).astype(np.uint8)\n    orig_img = PIL.Image.fromarray(orig_img)\n    orig_img.save(\"../original_images/\" + str(i) + \".jpg\")\n    i += 1\n#     if i % 500 == 0:\n#         print(str(i) + ' original images created')","metadata":{"id":"rtwoLucsj75L","execution":{"iopub.status.busy":"2021-07-21T13:32:05.706012Z","iopub.execute_input":"2021-07-21T13:32:05.706435Z","iopub.status.idle":"2021-07-21T13:36:53.490091Z","shell.execute_reply.started":"2021-07-21T13:32:05.706399Z","shell.execute_reply":"2021-07-21T13:36:53.488738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nshutil.make_archive(\"/kaggle/working/images/\", 'zip', \"../images\")\nshutil.make_archive(\"/kaggle/working/original_images\", 'zip', \"../original_images\")","metadata":{"id":"Y7zBgKLkkAie","outputId":"4e38db32-e237-478d-8b9c-640580576ee7","execution":{"iopub.status.busy":"2021-07-21T13:37:31.861985Z","iopub.execute_input":"2021-07-21T13:37:31.86236Z","iopub.status.idle":"2021-07-21T13:37:38.104634Z","shell.execute_reply.started":"2021-07-21T13:37:31.862321Z","shell.execute_reply":"2021-07-21T13:37:38.103373Z"},"trusted":true},"execution_count":null,"outputs":[]}]}