{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom kaggle_datasets import KaggleDatasets\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport tensorflow_addons as tfa\nimport PIL\nimport shutil\n\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nGCS_PATH = KaggleDatasets().get_gcs_path('gan-getting-started')\nprint(GCS_PATH)","metadata":{"_uuid":"8fce0978-98e3-4a8d-8042-df01feb67e5a","_cell_guid":"08c3c3db-82c8-4f9e-8a39-3d4ae978a4c9","execution":{"iopub.status.busy":"2021-08-12T21:22:38.830532Z","iopub.execute_input":"2021-08-12T21:22:38.831121Z","iopub.status.idle":"2021-08-12T21:22:41.386490Z","shell.execute_reply.started":"2021-08-12T21:22:38.831058Z","shell.execute_reply":"2021-08-12T21:22:41.385373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    print('Device:', tpu.master())\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nexcept:\n    strategy = tf.distribute.get_strategy()\nprint('Number of replicas:', strategy.num_replicas_in_sync)\n\nAUTOTUNE = tf.data.experimental.AUTOTUNE\n    \nprint(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T21:22:41.391643Z","iopub.execute_input":"2021-08-12T21:22:41.391954Z","iopub.status.idle":"2021-08-12T21:22:46.832934Z","shell.execute_reply.started":"2021-08-12T21:22:41.391924Z","shell.execute_reply":"2021-08-12T21:22:46.831666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MONET_FILENAMES = tf.io.gfile.glob(str('../input/finalgan/*.tfrec'))\nprint('Monet TFRecord Files:', len(MONET_FILENAMES))\n\nPHOTO_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/photo_tfrec/*.tfrec'))\nprint('Photo TFRecord Files:', len(PHOTO_FILENAMES))","metadata":{"execution":{"iopub.status.busy":"2021-08-12T21:22:46.834672Z","iopub.execute_input":"2021-08-12T21:22:46.834989Z","iopub.status.idle":"2021-08-12T21:22:46.920747Z","shell.execute_reply.started":"2021-08-12T21:22:46.834961Z","shell.execute_reply":"2021-08-12T21:22:46.919512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_SIZE = [256, 256]\n\ndef decode_image(image):\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = (tf.cast(image, tf.float32) / 127.5) - 1\n    image = tf.reshape(image, [*IMAGE_SIZE, 3])\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","metadata":{"execution":{"iopub.status.busy":"2021-08-12T21:22:46.921977Z","iopub.execute_input":"2021-08-12T21:22:46.922298Z","iopub.status.idle":"2021-08-12T21:22:46.930660Z","shell.execute_reply.started":"2021-08-12T21:22:46.922261Z","shell.execute_reply":"2021-08-12T21:22:46.929242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dataset(filenames, labeled=True, ordered=False):\n    dataset = tf.data.TFRecordDataset(filenames)\n    dataset = dataset.map(read_tfrecord, num_parallel_calls=AUTOTUNE)\n    return dataset","metadata":{"execution":{"iopub.status.busy":"2021-08-12T21:22:46.932463Z","iopub.execute_input":"2021-08-12T21:22:46.932772Z","iopub.status.idle":"2021-08-12T21:22:46.943878Z","shell.execute_reply.started":"2021-08-12T21:22:46.932744Z","shell.execute_reply":"2021-08-12T21:22:46.942859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monet_ds = load_dataset(MONET_FILENAMES, labeled=True).batch(1)\nphoto_ds = load_dataset(PHOTO_FILENAMES, labeled=True).batch(1)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T21:22:46.945359Z","iopub.execute_input":"2021-08-12T21:22:46.945648Z","iopub.status.idle":"2021-08-12T21:22:47.146156Z","shell.execute_reply.started":"2021-08-12T21:22:46.945621Z","shell.execute_reply":"2021-08-12T21:22:47.145047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define Model Compilation Arguments","metadata":{"_uuid":"64cbeb92-fabd-4c7c-b60e-db744715df31","_cell_guid":"ef85c1f4-0b3a-409f-821f-b129dcbdf2a3","trusted":true}},{"cell_type":"code","source":"with strategy.scope():\n    monet_generator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)\n    photo_generator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)\n\n    monet_discriminator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)\n    photo_discriminator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)\n    \nwith strategy.scope():\n    monet_generator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)\n    photo_generator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)\n\n    monet_discriminator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)\n    photo_discriminator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)","metadata":{"_uuid":"8ef0d209-aa4d-4dd7-9913-1544682628bc","_cell_guid":"d6e54287-73dc-4eb9-a459-9688052a6e21","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-12T21:22:47.147701Z","iopub.execute_input":"2021-08-12T21:22:47.148035Z","iopub.status.idle":"2021-08-12T21:22:47.157487Z","shell.execute_reply.started":"2021-08-12T21:22:47.148004Z","shell.execute_reply":"2021-08-12T21:22:47.155974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Model","metadata":{"_uuid":"d06cf876-2e1c-4c53-9201-a98a9a02ff47","_cell_guid":"93bc6d72-1749-497d-b68c-6d3ddc4fc565","trusted":true}},{"cell_type":"code","source":"from keras.models import load_model\n\nmonet_generator = keras.models.load_model('../input/finalgan/monet_generator.h5')\n# photo_generator = keras.models.load_model('../input/model/photo_generator.h5' )\n# monet_discriminator = keras.models.load_model('../input/model/monet_discriminator.h5' )\n# photo_discriminator = keras.models.load_model('../input/model/photo_discriminator.h5' )","metadata":{"_uuid":"0958cb98-21a8-4648-acc9-16ca08488944","_cell_guid":"bb065a3e-7b9b-4dbf-8d45-4f80268ca18d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-12T21:22:47.159795Z","iopub.execute_input":"2021-08-12T21:22:47.160132Z","iopub.status.idle":"2021-08-12T21:22:53.314557Z","shell.execute_reply.started":"2021-08-12T21:22:47.160102Z","shell.execute_reply":"2021-08-12T21:22:53.312813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Generate Output Images And Save Them","metadata":{"_uuid":"a63f3b1e-7da0-4104-afbe-9590c38e63f7","_cell_guid":"0c1cb0ca-c03b-4949-8807-26944fa2d042","trusted":true}},{"cell_type":"code","source":"import PIL\n! mkdir ../images","metadata":{"_uuid":"5c0e34c5-8948-4223-a99b-e6547b10b61e","_cell_guid":"1305e93a-09fe-45f7-aa00-79691c6b07b9","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-12T21:22:53.316662Z","iopub.execute_input":"2021-08-12T21:22:53.317145Z","iopub.status.idle":"2021-08-12T21:22:54.088295Z","shell.execute_reply.started":"2021-08-12T21:22:53.317094Z","shell.execute_reply":"2021-08-12T21:22:54.087120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 1\nfor img in photo_ds:\n    prediction = monet_generator(img, training=False)[0].numpy()\n    prediction = (prediction * 127.5 + 127.5).astype(np.uint8)\n    im = PIL.Image.fromarray(prediction)\n    im.save(\"../images/\" + str(i) + \".jpg\")\n    i += 1\n    print(i)","metadata":{"_uuid":"e7aeab12-28d8-4418-9add-7bfa23b1ed65","_cell_guid":"18250b05-db36-435b-aec7-8f9b81ab4b88","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-12T21:22:54.090476Z","iopub.execute_input":"2021-08-12T21:22:54.091139Z","iopub.status.idle":"2021-08-12T21:44:28.602022Z","shell.execute_reply.started":"2021-08-12T21:22:54.091085Z","shell.execute_reply":"2021-08-12T21:44:28.600488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Make Zip File","metadata":{"_uuid":"8a2e049d-7a51-4dcb-9e03-f576c7d814e6","_cell_guid":"00f0885d-7229-4b1e-baa7-081a0078e665","trusted":true}},{"cell_type":"code","source":"import shutil\nshutil.make_archive(\"/kaggle/working/images\", 'zip', \"/kaggle/images\")","metadata":{"_uuid":"b8736228-5cde-4b8c-856d-5bf1ad878d79","_cell_guid":"c79d79cb-ffbe-4977-b496-8c5d90e37045","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-08-12T21:44:28.603633Z","iopub.execute_input":"2021-08-12T21:44:28.604043Z","iopub.status.idle":"2021-08-12T21:44:32.296372Z","shell.execute_reply.started":"2021-08-12T21:44:28.604008Z","shell.execute_reply":"2021-08-12T21:44:32.295179Z"},"trusted":true},"execution_count":null,"outputs":[]}]}