{"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":"# Cezanne 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-28T16:11:27.001164Z","iopub.execute_input":"2023-03-28T16:11:27.001857Z","iopub.status.idle":"2023-03-28T16:11:38.098550Z","shell.execute_reply.started":"2023-03-28T16:11:27.001817Z","shell.execute_reply":"2023-03-28T16:11:38.097565Z"},"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-28T16:11:38.100765Z","iopub.execute_input":"2023-03-28T16:11:38.102193Z","iopub.status.idle":"2023-03-28T16:11:38.340180Z","shell.execute_reply.started":"2023-03-28T16:11:38.102145Z","shell.execute_reply":"2023-03-28T16:11:38.339041Z"},"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-28T16:11:38.342157Z","iopub.execute_input":"2023-03-28T16:11:38.342820Z","iopub.status.idle":"2023-03-28T16:11:38.351261Z","shell.execute_reply.started":"2023-03-28T16:11:38.342776Z","shell.execute_reply":"2023-03-28T16:11:38.349570Z"},"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-28T16:11:38.354316Z","iopub.execute_input":"2023-03-28T16:11:38.355256Z","iopub.status.idle":"2023-03-28T16:11:38.370063Z","shell.execute_reply.started":"2023-03-28T16:11:38.355172Z","shell.execute_reply":"2023-03-28T16:11:38.368533Z"},"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-28T16:11:38.373456Z","iopub.execute_input":"2023-03-28T16:11:38.374206Z","iopub.status.idle":"2023-03-28T16:11:38.383030Z","shell.execute_reply.started":"2023-03-28T16:11:38.374159Z","shell.execute_reply":"2023-03-28T16:11:38.381929Z"},"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-28T16:11:38.384241Z","iopub.execute_input":"2023-03-28T16:11:38.385929Z","iopub.status.idle":"2023-03-28T16:11:38.398061Z","shell.execute_reply.started":"2023-03-28T16:11:38.385811Z","shell.execute_reply":"2023-03-28T16:11:38.396599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cezanne_epoch_path = KaggleDatasets().get_gcs_path('cezanne-weights')\nCEZANNE_PHOTO_TEST_FILENAMES = KaggleDatasets().get_gcs_path('cezanne-photos-test')\nCEZANNE_PHOTO_TEST_FILENAMES = tf.io.gfile.glob(str(CEZANNE_PHOTO_TEST_FILENAMES + '/cezanne_photos_test_as_tfrec/*.tfrec'))\nprint(CEZANNE_PHOTO_TEST_FILENAMES)\n\ndef zip_predictions(epoch):\n    global cezanne_epoch_path\n    cezanne_epoch_glob = tf.io.gfile.glob(str(cezanne_epoch_path + f'/photo2cezanne_{epoch + 1}_epoch.h5'))\n    cezanne_epoch_glob = cezanne_epoch_glob[0]\n    cezanne_generator = keras.models.load_model(cezanne_epoch_glob)\n    os.makedirs(f'../images_cezanne_{epoch}/',exist_ok=True) # Create folder to save generated images\n\n    predict_and_save(load_dataset(CEZANNE_PHOTO_TEST_FILENAMES).batch(1), cezanne_generator, f'../images_cezanne_{epoch}/')\n    shutil.make_archive(f\"/kaggle/working/images_cezanne_{epoch}\", 'zip', f\"/kaggle/images_cezanne_{epoch}\")","metadata":{"execution":{"iopub.status.busy":"2023-03-28T16:12:12.248649Z","iopub.execute_input":"2023-03-28T16:12:12.249181Z","iopub.status.idle":"2023-03-28T16:12:13.343586Z","shell.execute_reply.started":"2023-03-28T16:12:12.249131Z","shell.execute_reply":"2023-03-28T16:12:13.341602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(10)","metadata":{"execution":{"iopub.status.busy":"2023-03-28T16:12:16.197606Z","iopub.execute_input":"2023-03-28T16:12:16.197945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(20)","metadata":{"execution":{"iopub.status.busy":"2023-03-28T16:11:53.505322Z","iopub.status.idle":"2023-03-28T16:11:53.505786Z","shell.execute_reply.started":"2023-03-28T16:11:53.505570Z","shell.execute_reply":"2023-03-28T16:11:53.505593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(30)","metadata":{"execution":{"iopub.status.busy":"2023-03-28T16:11:53.507513Z","iopub.status.idle":"2023-03-28T16:11:53.508359Z","shell.execute_reply.started":"2023-03-28T16:11:53.508168Z","shell.execute_reply":"2023-03-28T16:11:53.508197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(40)","metadata":{"execution":{"iopub.status.busy":"2023-03-28T16:11:53.509467Z","iopub.status.idle":"2023-03-28T16:11:53.509876Z","shell.execute_reply.started":"2023-03-28T16:11:53.509672Z","shell.execute_reply":"2023-03-28T16:11:53.509691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(50)","metadata":{"execution":{"iopub.status.busy":"2023-03-28T16:11:53.511153Z","iopub.status.idle":"2023-03-28T16:11:53.511491Z","shell.execute_reply.started":"2023-03-28T16:11:53.511337Z","shell.execute_reply":"2023-03-28T16:11:53.511355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(60)","metadata":{"execution":{"iopub.status.busy":"2023-03-28T16:11:53.513530Z","iopub.status.idle":"2023-03-28T16:11:53.514037Z","shell.execute_reply.started":"2023-03-28T16:11:53.513794Z","shell.execute_reply":"2023-03-28T16:11:53.513818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(70)","metadata":{"execution":{"iopub.status.busy":"2023-03-28T16:11:53.515661Z","iopub.status.idle":"2023-03-28T16:11:53.516035Z","shell.execute_reply.started":"2023-03-28T16:11:53.515851Z","shell.execute_reply":"2023-03-28T16:11:53.515870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(80)","metadata":{"execution":{"iopub.status.busy":"2023-03-28T16:11:53.517305Z","iopub.status.idle":"2023-03-28T16:11:53.517651Z","shell.execute_reply.started":"2023-03-28T16:11:53.517486Z","shell.execute_reply":"2023-03-28T16:11:53.517503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(90)","metadata":{"execution":{"iopub.status.busy":"2023-03-28T16:11:53.519845Z","iopub.status.idle":"2023-03-28T16:11:53.520533Z","shell.execute_reply.started":"2023-03-28T16:11:53.520217Z","shell.execute_reply":"2023-03-28T16:11:53.520259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(100)","metadata":{"execution":{"iopub.status.busy":"2023-03-28T16:11:53.522423Z","iopub.status.idle":"2023-03-28T16:11:53.522855Z","shell.execute_reply.started":"2023-03-28T16:11:53.522651Z","shell.execute_reply":"2023-03-28T16:11:53.522682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(110)","metadata":{"execution":{"iopub.status.busy":"2023-03-28T16:11:53.524099Z","iopub.status.idle":"2023-03-28T16:11:53.524462Z","shell.execute_reply.started":"2023-03-28T16:11:53.524290Z","shell.execute_reply":"2023-03-28T16:11:53.524308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(120)","metadata":{"execution":{"iopub.status.busy":"2023-03-28T16:11:53.525753Z","iopub.status.idle":"2023-03-28T16:11:53.526144Z","shell.execute_reply.started":"2023-03-28T16:11:53.525937Z","shell.execute_reply":"2023-03-28T16:11:53.525956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(130)","metadata":{"execution":{"iopub.status.busy":"2023-03-28T16:11:53.527572Z","iopub.status.idle":"2023-03-28T16:11:53.527966Z","shell.execute_reply.started":"2023-03-28T16:11:53.527776Z","shell.execute_reply":"2023-03-28T16:11:53.527798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(135)","metadata":{"execution":{"iopub.status.busy":"2023-03-28T16:11:53.530075Z","iopub.status.idle":"2023-03-28T16:11:53.530607Z","shell.execute_reply.started":"2023-03-28T16:11:53.530362Z","shell.execute_reply":"2023-03-28T16:11:53.530386Z"},"trusted":true},"execution_count":null,"outputs":[]}]}