{"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":"# Vangogh 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-29T15:51:52.378423Z","iopub.execute_input":"2023-03-29T15:51:52.378861Z","iopub.status.idle":"2023-03-29T15:52:02.342287Z","shell.execute_reply.started":"2023-03-29T15:51:52.378824Z","shell.execute_reply":"2023-03-29T15:52:02.339947Z"},"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-29T15:52:02.344466Z","iopub.execute_input":"2023-03-29T15:52:02.345710Z","iopub.status.idle":"2023-03-29T15:52:02.683859Z","shell.execute_reply.started":"2023-03-29T15:52:02.345671Z","shell.execute_reply":"2023-03-29T15:52:02.682683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load in the data\n\nWe want to keep our photo dataset and our Vangogh 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-29T15:52:02.685128Z","iopub.execute_input":"2023-03-29T15:52:02.685516Z","iopub.status.idle":"2023-03-29T15:52:02.694512Z","shell.execute_reply.started":"2023-03-29T15:52:02.685478Z","shell.execute_reply":"2023-03-29T15:52:02.693400Z"},"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-29T15:52:02.695763Z","iopub.execute_input":"2023-03-29T15:52:02.697057Z","iopub.status.idle":"2023-03-29T15:52:02.704393Z","shell.execute_reply.started":"2023-03-29T15:52:02.697024Z","shell.execute_reply":"2023-03-29T15:52:02.703435Z"},"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-29T15:52:02.707882Z","iopub.execute_input":"2023-03-29T15:52:02.708894Z","iopub.status.idle":"2023-03-29T15:52:02.715126Z","shell.execute_reply.started":"2023-03-29T15:52:02.708805Z","shell.execute_reply":"2023-03-29T15:52:02.714056Z"},"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-29T15:52:02.716298Z","iopub.execute_input":"2023-03-29T15:52:02.717245Z","iopub.status.idle":"2023-03-29T15:52:02.727812Z","shell.execute_reply.started":"2023-03-29T15:52:02.717210Z","shell.execute_reply":"2023-03-29T15:52:02.727085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vangogh_epoch_path = KaggleDatasets().get_gcs_path('vangogh-weights')\nVANGOGH_PHOTO_TEST_FILENAMES = KaggleDatasets().get_gcs_path('vangogh-photos-test')\nVANGOGH_PHOTO_TEST_FILENAMES = tf.io.gfile.glob(str(VANGOGH_PHOTO_TEST_FILENAMES + '/vangogh_photos_test_as_tfrec/*.tfrec'))\nprint(VANGOGH_PHOTO_TEST_FILENAMES)\n\ndef zip_predictions(epoch):\n    global vangogh_epoch_path\n    vangogh_epoch_glob = tf.io.gfile.glob(str(vangogh_epoch_path + f'/photo2vangogh_{epoch + 1}_epoch.h5'))\n    vangogh_epoch_glob = vangogh_epoch_glob[0]\n    vangogh_generator = keras.models.load_model(vangogh_epoch_glob)\n    os.makedirs(f'../images_vangogh_{epoch}/',exist_ok=True) # Create folder to save generated images\n\n    predict_and_save(load_dataset(VANGOGH_PHOTO_TEST_FILENAMES).batch(1), vangogh_generator, f'../images_vangogh_{epoch}/')\n    shutil.make_archive(f\"/kaggle/working/images_vangogh_{epoch}\", 'zip', f\"/kaggle/images_vangogh_{epoch}\")","metadata":{"execution":{"iopub.status.busy":"2023-03-29T15:52:02.728941Z","iopub.execute_input":"2023-03-29T15:52:02.729704Z","iopub.status.idle":"2023-03-29T15:52:16.179662Z","shell.execute_reply.started":"2023-03-29T15:52:02.729674Z","shell.execute_reply":"2023-03-29T15:52:16.178408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(10)","metadata":{"execution":{"iopub.status.busy":"2023-03-29T15:52:16.181234Z","iopub.execute_input":"2023-03-29T15:52:16.181805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(20)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(30)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(40)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(50)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(60)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(70)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(80)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(90)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(100)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(110)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(120)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(130)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_predictions(135)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}