{"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":"# Monet Predictions","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport tensorflow_addons as tfa\n\nfrom kaggle_datasets import KaggleDatasets\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport cv2\nfrom pathlib import Path\n\nSIZE=60\n\ntry:\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":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","execution":{"iopub.status.busy":"2023-03-27T14:42:29.326833Z","iopub.execute_input":"2023-03-27T14:42:29.328008Z","iopub.status.idle":"2023-03-27T14:42:40.385090Z","shell.execute_reply.started":"2023-03-27T14:42:29.327963Z","shell.execute_reply":"2023-03-27T14:42:40.383232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\nuser_credential = user_secrets.get_gcloud_credential()\nuser_secrets.set_tensorflow_credential(user_credential)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:40.387931Z","iopub.execute_input":"2023-03-27T14:42:40.388835Z","iopub.status.idle":"2023-03-27T14:42:40.686583Z","shell.execute_reply.started":"2023-03-27T14:42:40.388790Z","shell.execute_reply":"2023-03-27T14:42:40.685141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load in the data\n\nWe want to keep our photo dataset and our Monet dataset separate. First, load in the filenames of the TFRecords.","metadata":{}},{"cell_type":"code","source":"import re\n\ndef count_data_items(filenames):\n    n = [int(re.compile(r\"-([0-9]*)\\.\").search(filename).group(1)) for filename in filenames]\n    return np.sum(n)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:40.688158Z","iopub.execute_input":"2023-03-27T14:42:40.688575Z","iopub.status.idle":"2023-03-27T14:42:40.695910Z","shell.execute_reply.started":"2023-03-27T14:42:40.688529Z","shell.execute_reply":"2023-03-27T14:42:40.694611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"All the images for the competition are already sized to 256x256. As these images are RGB images, set the channel to 3. Additionally, we need to scale the images to a [-1, 1] scale. Because we are building a generative model, we don't need the labels or the image id so we'll only return the image from the TFRecord.","metadata":{}},{"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\": 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=AUTOTUNE)\n    \n    return dataset","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:40.699105Z","iopub.execute_input":"2023-03-27T14:42:40.699700Z","iopub.status.idle":"2023-03-27T14:42:40.711304Z","shell.execute_reply.started":"2023-03-27T14:42:40.699659Z","shell.execute_reply":"2023-03-27T14:42:40.710246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create submission file","metadata":{}},{"cell_type":"code","source":"import PIL\ndef predict_and_save(input_ds, generator_model, output_path):\n    i = 1\n    for img in input_ds:\n        prediction = generator_model(img, training=False)[0].numpy() # make predition\n        prediction = (prediction * 127.5 + 127.5).astype(np.uint8)   # re-scale\n        im = PIL.Image.fromarray(prediction)\n        im.save(f'{output_path}{str(i)}.jpg')\n        i += 1","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:40.713194Z","iopub.execute_input":"2023-03-27T14:42:40.714045Z","iopub.status.idle":"2023-03-27T14:42:40.725191Z","shell.execute_reply.started":"2023-03-27T14:42:40.713971Z","shell.execute_reply":"2023-03-27T14:42:40.723671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport h5py\nfrom keras.models import load_model\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:40.727120Z","iopub.execute_input":"2023-03-27T14:42:40.727529Z","iopub.status.idle":"2023-03-27T14:42:40.744028Z","shell.execute_reply.started":"2023-03-27T14:42:40.727493Z","shell.execute_reply":"2023-03-27T14:42:40.742265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monet_epoch_path = KaggleDatasets().get_gcs_path('monet-weights-for-prediction')\nMONET_PHOTO_TEST_FILENAMES = KaggleDatasets().get_gcs_path('monet-photos-test')\nMONET_PHOTO_TEST_FILENAMES = tf.io.gfile.glob(str(MONET_PHOTO_TEST_FILENAMES + '/monet_photos_test_as_tfrec/*.tfrec'))\nprint(MONET_PHOTO_TEST_FILENAMES)\n\ndef zip_predictions(epoch):\n    global ukiyoe_epoch_path\n    monet_epoch_glob = tf.io.gfile.glob(str(monet_epoch_path + f'/photo2monet_{epoch + 1}_epoch.h5'))\n    monet_epoch_glob = monet_epoch_glob[0]\n    monet_generator = keras.models.load_model(monet_epoch_glob)\n    os.makedirs(f'../images_monet_{epoch}/',exist_ok=True) # Create folder to save generated images\n\n    predict_and_save(load_dataset(MONET_PHOTO_TEST_FILENAMES).batch(1), monet_generator, f'../images_monet_{epoch}/')\n    shutil.make_archive(f\"/kaggle/working/images_monet_{epoch}\", 'zip', f\"/kaggle/images_monet_{epoch}\")","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:43:09.197566Z","iopub.execute_input":"2023-03-27T14:43:09.198034Z","iopub.status.idle":"2023-03-27T14:43:11.842816Z","shell.execute_reply.started":"2023-03-27T14:43:09.197998Z","shell.execute_reply":"2023-03-27T14:43:11.841882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(10)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:43:14.643639Z","iopub.execute_input":"2023-03-27T14:43:14.644061Z","iopub.status.idle":"2023-03-27T14:43:57.318852Z","shell.execute_reply.started":"2023-03-27T14:43:14.644027Z","shell.execute_reply":"2023-03-27T14:43:57.317386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(20)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:53.203267Z","iopub.status.idle":"2023-03-27T14:42:53.203703Z","shell.execute_reply.started":"2023-03-27T14:42:53.203491Z","shell.execute_reply":"2023-03-27T14:42:53.203514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(30)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:53.205351Z","iopub.status.idle":"2023-03-27T14:42:53.205765Z","shell.execute_reply.started":"2023-03-27T14:42:53.205557Z","shell.execute_reply":"2023-03-27T14:42:53.205579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(40)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:53.207932Z","iopub.status.idle":"2023-03-27T14:42:53.208353Z","shell.execute_reply.started":"2023-03-27T14:42:53.208127Z","shell.execute_reply":"2023-03-27T14:42:53.208147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(50)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:53.210252Z","iopub.status.idle":"2023-03-27T14:42:53.210641Z","shell.execute_reply.started":"2023-03-27T14:42:53.210444Z","shell.execute_reply":"2023-03-27T14:42:53.210464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(60)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:53.211909Z","iopub.status.idle":"2023-03-27T14:42:53.212329Z","shell.execute_reply.started":"2023-03-27T14:42:53.212096Z","shell.execute_reply":"2023-03-27T14:42:53.212116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(70)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:53.214113Z","iopub.status.idle":"2023-03-27T14:42:53.214557Z","shell.execute_reply.started":"2023-03-27T14:42:53.214357Z","shell.execute_reply":"2023-03-27T14:42:53.214380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(80)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:53.215888Z","iopub.status.idle":"2023-03-27T14:42:53.216491Z","shell.execute_reply.started":"2023-03-27T14:42:53.216285Z","shell.execute_reply":"2023-03-27T14:42:53.216309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(90)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:53.218400Z","iopub.status.idle":"2023-03-27T14:42:53.219119Z","shell.execute_reply.started":"2023-03-27T14:42:53.218758Z","shell.execute_reply":"2023-03-27T14:42:53.218793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(100)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:53.220931Z","iopub.status.idle":"2023-03-27T14:42:53.221576Z","shell.execute_reply.started":"2023-03-27T14:42:53.221261Z","shell.execute_reply":"2023-03-27T14:42:53.221295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(110)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:53.223330Z","iopub.status.idle":"2023-03-27T14:42:53.223753Z","shell.execute_reply.started":"2023-03-27T14:42:53.223559Z","shell.execute_reply":"2023-03-27T14:42:53.223580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(120)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:53.225431Z","iopub.status.idle":"2023-03-27T14:42:53.225833Z","shell.execute_reply.started":"2023-03-27T14:42:53.225635Z","shell.execute_reply":"2023-03-27T14:42:53.225655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(130)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:53.227563Z","iopub.status.idle":"2023-03-27T14:42:53.228294Z","shell.execute_reply.started":"2023-03-27T14:42:53.228044Z","shell.execute_reply":"2023-03-27T14:42:53.228070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(135)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T14:42:53.229946Z","iopub.status.idle":"2023-03-27T14:42:53.230365Z","shell.execute_reply.started":"2023-03-27T14:42:53.230133Z","shell.execute_reply":"2023-03-27T14:42:53.230153Z"},"trusted":true},"execution_count":null,"outputs":[]}]}