{"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":"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 os\nimport time\nimport datetime\nfrom IPython import display\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":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-14T18:04:36.925178Z","iopub.execute_input":"2023-08-14T18:04:36.925578Z","iopub.status.idle":"2023-08-14T18:04:45.112943Z","shell.execute_reply.started":"2023-08-14T18:04:36.925542Z","shell.execute_reply":"2023-08-14T18:04:45.111958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"GCS_PATH = KaggleDatasets().get_gcs_path()\nMONET_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/monet_tfrec/*.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":"2023-08-14T18:04:45.115106Z","iopub.execute_input":"2023-08-14T18:04:45.116162Z","iopub.status.idle":"2023-08-14T18:04:45.876042Z","shell.execute_reply.started":"2023-08-14T18:04:45.116123Z","shell.execute_reply":"2023-08-14T18:04:45.874199Z"},"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    \n    return image","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:04:45.877322Z","iopub.execute_input":"2023-08-14T18:04:45.877688Z","iopub.status.idle":"2023-08-14T18:04:45.883523Z","shell.execute_reply.started":"2023-08-14T18:04:45.877652Z","shell.execute_reply":"2023-08-14T18:04:45.882623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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, 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":"2023-08-14T18:04:45.886132Z","iopub.execute_input":"2023-08-14T18:04:45.886704Z","iopub.status.idle":"2023-08-14T18:04:45.895795Z","shell.execute_reply.started":"2023-08-14T18:04:45.886671Z","shell.execute_reply":"2023-08-14T18:04:45.894589Z"},"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)\n\ntrain_ds = tf.data.Dataset.zip((monet_ds, photo_ds))\ntest_ds = tf.data.Dataset.zip((monet_ds, photo_ds))","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:04:45.897269Z","iopub.execute_input":"2023-08-14T18:04:45.897873Z","iopub.status.idle":"2023-08-14T18:04:49.155080Z","shell.execute_reply.started":"2023-08-14T18:04:45.897839Z","shell.execute_reply":"2023-08-14T18:04:49.154150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example_monet = next(iter(monet_ds))\nexample_photo = next(iter(photo_ds))\n\nplt.subplot(121)\nplt.title('Photo')\nplt.imshow(example_photo[0] * 0.5 + 0.5)\n\nplt.subplot(122)\nplt.title('Monet')\nplt.imshow(example_monet[0] * 0.5 + 0.5)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:04:49.156376Z","iopub.execute_input":"2023-08-14T18:04:49.156693Z","iopub.status.idle":"2023-08-14T18:04:51.210433Z","shell.execute_reply.started":"2023-08-14T18:04:49.156662Z","shell.execute_reply":"2023-08-14T18:04:51.208743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"OUTPUT_CHANNELS = 3","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:04:51.211477Z","iopub.execute_input":"2023-08-14T18:04:51.211796Z","iopub.status.idle":"2023-08-14T18:04:51.216739Z","shell.execute_reply.started":"2023-08-14T18:04:51.211766Z","shell.execute_reply":"2023-08-14T18:04:51.215836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def downsample(filters, size, apply_batchnorm=True):\n    initializer = tf.random_normal_initializer(0., 0.02)\n\n    result = tf.keras.Sequential()\n    result.add(\n      tf.keras.layers.Conv2D(filters, size, strides=2, padding='same',\n                             kernel_initializer=initializer, use_bias=False))\n\n    if apply_batchnorm:\n        result.add(tf.keras.layers.BatchNormalization())\n\n    result.add(tf.keras.layers.LeakyReLU())\n\n    return result","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:05:02.156119Z","iopub.execute_input":"2023-08-14T18:05:02.156491Z","iopub.status.idle":"2023-08-14T18:05:02.164644Z","shell.execute_reply.started":"2023-08-14T18:05:02.156460Z","shell.execute_reply":"2023-08-14T18:05:02.163561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"down_model = downsample(3, 4)\ndown_result = down_model(example_monet)\nprint (down_result.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:05:03.991832Z","iopub.execute_input":"2023-08-14T18:05:03.992228Z","iopub.status.idle":"2023-08-14T18:05:04.030004Z","shell.execute_reply.started":"2023-08-14T18:05:03.992196Z","shell.execute_reply":"2023-08-14T18:05:04.028788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def upsample(filters, size, apply_dropout=False):\n    initializer = tf.random_normal_initializer(0., 0.02)\n\n    result = tf.keras.Sequential()\n    result.add(\n        tf.keras.layers.Conv2DTranspose(filters, size, strides=2,\n                                        padding='same',\n                                        kernel_initializer=initializer,\n                                        use_bias=False))\n\n    result.add(tf.keras.layers.BatchNormalization())\n\n    if apply_dropout:\n        result.add(tf.keras.layers.Dropout(0.5))\n\n    result.add(tf.keras.layers.ReLU())\n\n    return result","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:05:04.603411Z","iopub.execute_input":"2023-08-14T18:05:04.604389Z","iopub.status.idle":"2023-08-14T18:05:04.611814Z","shell.execute_reply.started":"2023-08-14T18:05:04.604341Z","shell.execute_reply":"2023-08-14T18:05:04.610752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"up_model = upsample(3, 4)\nup_result = up_model(down_result)\nprint (up_result.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:05:05.749932Z","iopub.execute_input":"2023-08-14T18:05:05.750609Z","iopub.status.idle":"2023-08-14T18:05:06.925145Z","shell.execute_reply.started":"2023-08-14T18:05:05.750576Z","shell.execute_reply":"2023-08-14T18:05:06.924177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Generator():\n    inputs = tf.keras.layers.Input(shape=[256, 256, 3])\n\n    down_stack = [\n        downsample(64, 4, apply_batchnorm=False),  # (batch_size, 128, 128, 64)\n        downsample(128, 4),  # (batch_size, 64, 64, 128)\n        downsample(256, 4),  # (batch_size, 32, 32, 256)\n        downsample(512, 4),  # (batch_size, 16, 16, 512)\n        downsample(512, 4),  # (batch_size, 8, 8, 512)\n        downsample(512, 4),  # (batch_size, 4, 4, 512)\n        downsample(512, 4),  # (batch_size, 2, 2, 512)\n        downsample(512, 4),  # (batch_size, 1, 1, 512)\n    ]\n\n    up_stack = [\n        upsample(512, 4, apply_dropout=True),  # (batch_size, 2, 2, 1024)\n        upsample(512, 4, apply_dropout=True),  # (batch_size, 4, 4, 1024)\n        upsample(512, 4, apply_dropout=True),  # (batch_size, 8, 8, 1024)\n        upsample(512, 4),  # (batch_size, 16, 16, 1024)\n        upsample(256, 4),  # (batch_size, 32, 32, 512)\n        upsample(128, 4),  # (batch_size, 64, 64, 256)\n        upsample(64, 4),  # (batch_size, 128, 128, 128)\n    ]\n\n    initializer = tf.random_normal_initializer(0., 0.02)\n    last = tf.keras.layers.Conv2DTranspose(OUTPUT_CHANNELS, 4,\n                                         strides=2,\n                                         padding='same',\n                                         kernel_initializer=initializer,\n                                         activation='tanh')  # (batch_size, 256, 256, 3)\n\n    x = inputs\n\n    # Downsampling through the model\n    skips = []\n    for down in down_stack:\n        x = down(x)\n        skips.append(x)\n\n    skips = reversed(skips[:-1])\n\n    # Upsampling and establishing the skip connections\n    for up, skip in zip(up_stack, skips):\n        x = up(x)\n        x = tf.keras.layers.Concatenate()([x, skip])\n\n    x = last(x)\n\n    return tf.keras.Model(inputs=inputs, outputs=x)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:05:06.927113Z","iopub.execute_input":"2023-08-14T18:05:06.927553Z","iopub.status.idle":"2023-08-14T18:05:06.938679Z","shell.execute_reply.started":"2023-08-14T18:05:06.927516Z","shell.execute_reply":"2023-08-14T18:05:06.937700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generator = Generator()\ntf.keras.utils.plot_model(generator, show_shapes=True, dpi=64)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:05:06.940220Z","iopub.execute_input":"2023-08-14T18:05:06.940929Z","iopub.status.idle":"2023-08-14T18:05:07.895847Z","shell.execute_reply.started":"2023-08-14T18:05:06.940879Z","shell.execute_reply":"2023-08-14T18:05:07.894878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gen_output = generator(example_monet, training=True)\nprint(gen_output)\nplt.imshow(gen_output[0, ...] * 0.5 + 0.5)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:12:05.479357Z","iopub.execute_input":"2023-08-14T18:12:05.479746Z","iopub.status.idle":"2023-08-14T18:12:05.925586Z","shell.execute_reply.started":"2023-08-14T18:12:05.479713Z","shell.execute_reply":"2023-08-14T18:12:05.924712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LAMBDA = 100\n\nloss_object = tf.keras.losses.BinaryCrossentropy(from_logits=True)\n\ndef generator_loss(disc_generated_output, gen_output, target):\n    gan_loss = loss_object(tf.ones_like(disc_generated_output), disc_generated_output)\n\n    # Mean absolute error\n    l1_loss = tf.reduce_mean(tf.abs(target - gen_output))\n\n    total_gen_loss = gan_loss + (LAMBDA * l1_loss)\n\n    return total_gen_loss, gan_loss, l1_loss","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:12:16.195479Z","iopub.execute_input":"2023-08-14T18:12:16.196451Z","iopub.status.idle":"2023-08-14T18:12:16.202832Z","shell.execute_reply.started":"2023-08-14T18:12:16.196404Z","shell.execute_reply":"2023-08-14T18:12:16.201824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Discriminator():\n    initializer = tf.random_normal_initializer(0., 0.02)\n\n    monet = tf.keras.layers.Input(shape=[256, 256, 3], name='monet_image')\n    photo = tf.keras.layers.Input(shape=[256, 256, 3], name='photo_image')\n\n    x = tf.keras.layers.concatenate([monet, photo])  # (batch_size, 256, 256, channels*2)\n\n    down1 = downsample(64, 4, False)(x)  # (batch_size, 128, 128, 64)\n    down2 = downsample(128, 4)(down1)  # (batch_size, 64, 64, 128)\n    down3 = downsample(256, 4)(down2)  # (batch_size, 32, 32, 256)\n\n    zero_pad1 = tf.keras.layers.ZeroPadding2D()(down3)  # (batch_size, 34, 34, 256)\n    conv = tf.keras.layers.Conv2D(512, 4, strides=1,\n                                kernel_initializer=initializer,\n                                use_bias=False)(zero_pad1)  # (batch_size, 31, 31, 512)\n\n    batchnorm1 = tf.keras.layers.BatchNormalization()(conv)\n\n    leaky_relu = tf.keras.layers.LeakyReLU()(batchnorm1)\n\n    zero_pad2 = tf.keras.layers.ZeroPadding2D()(leaky_relu)  # (batch_size, 33, 33, 512)\n\n    last = tf.keras.layers.Conv2D(1, 4, strides=1,\n                                kernel_initializer=initializer)(zero_pad2)  # (batch_size, 30, 30, 1)\n\n    return tf.keras.Model(inputs=[monet, photo], outputs=last)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:12:16.469002Z","iopub.execute_input":"2023-08-14T18:12:16.469378Z","iopub.status.idle":"2023-08-14T18:12:16.480854Z","shell.execute_reply.started":"2023-08-14T18:12:16.469345Z","shell.execute_reply":"2023-08-14T18:12:16.479797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"discriminator = Discriminator()\ntf.keras.utils.plot_model(discriminator, show_shapes=True, dpi=64)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:12:16.797385Z","iopub.execute_input":"2023-08-14T18:12:16.798124Z","iopub.status.idle":"2023-08-14T18:12:17.016862Z","shell.execute_reply.started":"2023-08-14T18:12:16.798085Z","shell.execute_reply":"2023-08-14T18:12:17.015840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"disc_out = discriminator([example_monet, gen_output], training=True)\n# print(disc_out)\nplt.imshow(disc_out[0, ..., -1], vmin=-20, vmax=20, cmap='RdBu_r')\nplt.colorbar()","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:12:26.488562Z","iopub.execute_input":"2023-08-14T18:12:26.488961Z","iopub.status.idle":"2023-08-14T18:12:26.891411Z","shell.execute_reply.started":"2023-08-14T18:12:26.488925Z","shell.execute_reply":"2023-08-14T18:12:26.890060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def discriminator_loss(disc_real_output, disc_generated_output):\n    real_loss = loss_object(tf.ones_like(disc_real_output), disc_real_output)\n\n    generated_loss = loss_object(tf.zeros_like(disc_generated_output), disc_generated_output)\n\n    total_disc_loss = real_loss + generated_loss\n\n    return total_disc_loss","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:05:52.028699Z","iopub.execute_input":"2023-08-14T18:05:52.029073Z","iopub.status.idle":"2023-08-14T18:05:52.034324Z","shell.execute_reply.started":"2023-08-14T18:05:52.029042Z","shell.execute_reply":"2023-08-14T18:05:52.033310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)\ndiscriminator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1=0.5)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:05:52.576455Z","iopub.execute_input":"2023-08-14T18:05:52.576808Z","iopub.status.idle":"2023-08-14T18:05:52.588645Z","shell.execute_reply.started":"2023-08-14T18:05:52.576778Z","shell.execute_reply":"2023-08-14T18:05:52.587683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_dir = './training_checkpoints'\ncheckpoint_prefix = os.path.join(checkpoint_dir, \"ckpt\")\ncheckpoint = tf.train.Checkpoint(generator_optimizer=generator_optimizer,\n                                 discriminator_optimizer=discriminator_optimizer,\n                                 generator=generator,\n                                 discriminator=discriminator)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:08:13.118053Z","iopub.execute_input":"2023-08-14T18:08:13.118425Z","iopub.status.idle":"2023-08-14T18:08:13.125088Z","shell.execute_reply.started":"2023-08-14T18:08:13.118395Z","shell.execute_reply":"2023-08-14T18:08:13.124116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_images(model, test_monet, photo):\n    prediction = model(test_monet, training=True)\n    plt.figure(figsize=(15, 15))\n\n    display_list = [test_monet[0], photo[0], prediction[0]]\n    title = ['Input monet', 'Ground Truth', 'Predicted Image']\n\n    for i in range(3):\n        plt.subplot(1, 3, i+1)\n        plt.title(title[i])\n        # Getting the pixel values in the [0, 1] range to plot.\n        plt.imshow(display_list[i] * 0.5 + 0.5)\n        plt.axis('off')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:10:00.452736Z","iopub.execute_input":"2023-08-14T18:10:00.453693Z","iopub.status.idle":"2023-08-14T18:10:00.461497Z","shell.execute_reply.started":"2023-08-14T18:10:00.453628Z","shell.execute_reply":"2023-08-14T18:10:00.460083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for step, (example_monet, example_photo) in test_ds.take(2).enumerate():\n    generate_images(generator, example_monet, example_photo)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:10:00.717300Z","iopub.execute_input":"2023-08-14T18:10:00.717663Z","iopub.status.idle":"2023-08-14T18:10:03.306696Z","shell.execute_reply.started":"2023-08-14T18:10:00.717631Z","shell.execute_reply":"2023-08-14T18:10:03.305612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"log_dir=\"logs/\"\n\nsummary_writer = tf.summary.create_file_writer(\n  log_dir + \"fit/\" + datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\"))","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:10:03.308988Z","iopub.execute_input":"2023-08-14T18:10:03.309445Z","iopub.status.idle":"2023-08-14T18:10:03.317017Z","shell.execute_reply.started":"2023-08-14T18:10:03.309405Z","shell.execute_reply":"2023-08-14T18:10:03.315935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@tf.function\ndef train_step(input_monet, target_photo, step):\n    with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:\n        gen_output = generator(input_monet, training=True)\n\n        disc_real_output = discriminator([input_monet, target_photo], training=True)\n        disc_generated_output = discriminator([input_monet, gen_output], training=True)\n\n        gen_total_loss, gen_gan_loss, gen_l1_loss = generator_loss(disc_generated_output, gen_output, target_photo)\n        disc_loss = discriminator_loss(disc_real_output, disc_generated_output)\n\n        generator_gradients = gen_tape.gradient(gen_total_loss,\n                                              generator.trainable_variables)\n        discriminator_gradients = disc_tape.gradient(disc_loss,\n                                                   discriminator.trainable_variables)\n\n        generator_optimizer.apply_gradients(zip(generator_gradients,\n                                              generator.trainable_variables))\n        discriminator_optimizer.apply_gradients(zip(discriminator_gradients,\n                                                  discriminator.trainable_variables))\n\n        with summary_writer.as_default():\n            tf.summary.scalar('gen_total_loss', gen_total_loss, step=step//1000)\n            tf.summary.scalar('gen_gan_loss', gen_gan_loss, step=step//1000)\n            tf.summary.scalar('gen_l1_loss', gen_l1_loss, step=step//1000)\n            tf.summary.scalar('disc_loss', disc_loss, step=step//1000)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:10:03.486581Z","iopub.execute_input":"2023-08-14T18:10:03.486958Z","iopub.status.idle":"2023-08-14T18:10:03.497638Z","shell.execute_reply.started":"2023-08-14T18:10:03.486927Z","shell.execute_reply":"2023-08-14T18:10:03.496368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fit(train_ds, test_ds, steps):\n    example_monet, example_photo  = next(iter(test_ds.take(1)))\n    start = time.time()\n\n    for step, (input_monet, target_photo) in train_ds.repeat().take(steps).enumerate():\n        if (step) % 1000 == 0:\n            display.clear_output(wait=True)\n\n            if step != 0:\n                print(f'Time taken for 1000 steps: {time.time()-start:.2f} sec\\n')\n\n            start = time.time()\n\n            generate_images(generator, example_monet, example_photo)\n            print(f\"Step: {step//1000}k\")\n        \n        train_step(input_monet, target_photo, step)\n\n        # Training step\n        if (step+1) % 10 == 0:\n            print('.', end='', flush=True)\n\n\n        # Save (checkpoint) the model every 5k steps\n        if (step + 1) % 5000 == 0:\n            checkpoint.save(file_prefix=checkpoint_prefix)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:08:18.580632Z","iopub.execute_input":"2023-08-14T18:08:18.581353Z","iopub.status.idle":"2023-08-14T18:08:18.589736Z","shell.execute_reply.started":"2023-08-14T18:08:18.581315Z","shell.execute_reply":"2023-08-14T18:08:18.588564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%load_ext tensorboard\n%tensorboard --logdir {log_dir}","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:08:19.753533Z","iopub.execute_input":"2023-08-14T18:08:19.754287Z","iopub.status.idle":"2023-08-14T18:08:26.293612Z","shell.execute_reply.started":"2023-08-14T18:08:19.754242Z","shell.execute_reply":"2023-08-14T18:08:26.292625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fit(train_ds, test_ds, steps=60000)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T03:25:54.492397Z","iopub.execute_input":"2023-08-14T03:25:54.492750Z","iopub.status.idle":"2023-08-14T04:00:56.078126Z","shell.execute_reply.started":"2023-08-14T03:25:54.492717Z","shell.execute_reply":"2023-08-14T04:00:56.077038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls {checkpoint_dir}","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:08:26.295698Z","iopub.execute_input":"2023-08-14T18:08:26.296061Z","iopub.status.idle":"2023-08-14T18:08:27.403863Z","shell.execute_reply.started":"2023-08-14T18:08:26.296025Z","shell.execute_reply":"2023-08-14T18:08:27.402545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Restoring the latest checkpoint in checkpoint_dir\ncheckpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:08:27.409739Z","iopub.execute_input":"2023-08-14T18:08:27.410516Z","iopub.status.idle":"2023-08-14T18:08:27.922214Z","shell.execute_reply.started":"2023-08-14T18:08:27.410474Z","shell.execute_reply":"2023-08-14T18:08:27.921110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Run the trained model on a few examples from the test set\nfor input_monet, target_photo in train_ds.take(5):\n    generate_images(generator, input_monet, target_photo)","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:13:08.320355Z","iopub.execute_input":"2023-08-14T18:13:08.320733Z","iopub.status.idle":"2023-08-14T18:13:12.598434Z","shell.execute_reply.started":"2023-08-14T18:13:08.320699Z","shell.execute_reply":"2023-08-14T18:13:12.597459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_, ax = plt.subplots(5, 2, figsize=(12, 12))\nfor i, (monet_img, photo_img) in test_ds.take(5).enumerate():\n#     prediction = generator(monet_img, training=False)[0].numpy()\n    prediction = generator(monet_img, training=True)[0].numpy()\n    prediction = (prediction * 127.5 + 127.5).astype(np.uint8)\n    photo_img = (photo_img[0] * 127.5 + 127.5).numpy().astype(np.uint8)\n\n    ax[i, 0].imshow(photo_img)\n    ax[i, 1].imshow(prediction)\n    ax[i, 0].set_title(\"Input Photo\")\n    ax[i, 1].set_title(\"Monet-esque\")\n    ax[i, 0].axis(\"off\")\n    ax[i, 1].axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:13:24.727178Z","iopub.execute_input":"2023-08-14T18:13:24.727550Z","iopub.status.idle":"2023-08-14T18:13:26.716643Z","shell.execute_reply.started":"2023-08-14T18:13:24.727518Z","shell.execute_reply":"2023-08-14T18:13:26.715802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import PIL\n! mkdir ../images","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:13:31.554509Z","iopub.execute_input":"2023-08-14T18:13:31.554898Z","iopub.status.idle":"2023-08-14T18:13:32.557695Z","shell.execute_reply.started":"2023-08-14T18:13:31.554851Z","shell.execute_reply":"2023-08-14T18:13:32.556338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 1\nfor monet_img, photo_img in test_ds:\n#     prediction = generator(monet_img, trainig=False)[0].numpy()\n    prediction = generator(monet_img, training=True)[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","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:14:18.310904Z","iopub.execute_input":"2023-08-14T18:14:18.311448Z","iopub.status.idle":"2023-08-14T18:14:34.758453Z","shell.execute_reply.started":"2023-08-14T18:14:18.311413Z","shell.execute_reply":"2023-08-14T18:14:34.757425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nshutil.make_archive(\"/kaggle/working/images\", 'zip', \"/kaggle/images\")","metadata":{"execution":{"iopub.status.busy":"2023-08-14T18:14:34.760537Z","iopub.execute_input":"2023-08-14T18:14:34.760919Z","iopub.status.idle":"2023-08-14T18:14:34.925478Z","shell.execute_reply.started":"2023-08-14T18:14:34.760866Z","shell.execute_reply":"2023-08-14T18:14:34.924508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}