{"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 os\nimport tensorflow as tf\nfrom tensorflow.keras import layers \nfrom kaggle_datasets import KaggleDatasets\nGCS_PATH = KaggleDatasets().get_gcs_path()\nAUTOTUNE = tf.data.experimental.AUTOTUNE\nMONET_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/monet_tfrec/*.tfrec'))\nPHOTO_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/photo_tfrec/*.tfrec'))\n\n# 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)\n# except:\n#     strategy = tf.distribute.get_strategy()\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\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\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, [256,256, 3])\n    return image\nmonet_ds = load_dataset(MONET_FILENAMES, labeled=True).batch(1)\nphoto_ds = load_dataset(PHOTO_FILENAMES, labeled=True).batch(1)\nprint(monet_ds)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-21T09:11:32.657088Z","iopub.execute_input":"2023-07-21T09:11:32.657482Z","iopub.status.idle":"2023-07-21T09:11:44.328078Z","shell.execute_reply.started":"2023-07-21T09:11:32.657452Z","shell.execute_reply":"2023-07-21T09:11:44.327147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generator_model():\n    model =tf.keras.Sequential()\n    input0= layers.Input(shape=(256,256,3))\n    input1=layers.Conv2DTranspose(128,4,padding='same',strides=(1,1))(input0)\n    input2=layers.BatchNormalization()(input1)\n    input3=layers.LeakyReLU()(input2)\n    \n    input4=layers.UpSampling2D()(input3)\n    input5=layers.Conv2D(256,4,padding='same',strides=(1,1))(input4)\n    input6=layers.BatchNormalization()(input5)\n    input7=layers.LeakyReLU()(input6)\n\n    input8=layers.UpSampling2D()(input7)\n    input9=layers.Conv2D(512,4,padding='same',strides=(2,2))(input8)\n    input10=layers.BatchNormalization()(input9)\n    input11=layers.LeakyReLU()(input10)\n\n    input12=layers.Conv2D(512,4,padding='same',strides=(2,2))(input11)\n    input13=layers.BatchNormalization()(input12)\n    input14=layers.LeakyReLU()(input13)\n    \n    input15=layers.Conv2D(256,4,padding='same',strides=(1,1))(input14)\n    input16=layers.BatchNormalization()(input15)\n    input17=layers.LeakyReLU()(input16)\n    \n    input18=layers.Conv2D(128,4,padding='same',strides=(1,1))(input17)\n    input19=layers.BatchNormalization()(input18)\n    input20=layers.LeakyReLU()(input19)\n    \n    output=layers.Conv2D(3,4,padding='same',activation='sigmoid')(input20)\n    return  tf.keras.Model(inputs=input0, outputs=output)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-07-21T09:11:44.329981Z","iopub.execute_input":"2023-07-21T09:11:44.330276Z","iopub.status.idle":"2023-07-21T09:11:44.344279Z","shell.execute_reply.started":"2023-07-21T09:11:44.330250Z","shell.execute_reply":"2023-07-21T09:11:44.343149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generator_monet=generator_model()\ngenerator_photo=generator_model()\ngenerated_image=generator_monet(next(iter(monet_ds)),training=False)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T09:11:44.345882Z","iopub.execute_input":"2023-07-21T09:11:44.346425Z","iopub.status.idle":"2023-07-21T09:11:58.508948Z","shell.execute_reply.started":"2023-07-21T09:11:44.346391Z","shell.execute_reply":"2023-07-21T09:11:58.508037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(generated_image[0,:,:,0])\ngenerated_image.shape","metadata":{"execution":{"iopub.status.busy":"2023-07-21T09:11:58.511782Z","iopub.execute_input":"2023-07-21T09:11:58.512481Z","iopub.status.idle":"2023-07-21T09:11:59.012721Z","shell.execute_reply.started":"2023-07-21T09:11:58.512444Z","shell.execute_reply":"2023-07-21T09:11:59.011795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Model\ndef discrimatior_model():\n    input0=layers.Input(shape=[256,256,3])\n    input1=layers.Conv2D(128,4,strides=(1,1),padding='same')(input0)\n    input2=layers.LeakyReLU()(input1)\n    input3=layers.Dropout(0.2)(input2)\n    \n    input4=layers.Conv2D(128,4,strides=(1,1),padding='same')(input3)\n    input5=layers.LeakyReLU()(input4)\n    input6=layers.Dropout(0.2)(input5)\n    \n    input7=layers.Conv2D(256,4,strides=(2,2),padding='same')(input6)\n    input8=layers.LeakyReLU()(input7)\n    input9=layers.Dropout(0.2)(input8)\n\n    input10=layers.ZeroPadding2D()(input9)\n    input11=layers.Flatten()(input10)\n    output=layers.Dense(1)(input11)\n    return tf.keras.Model(inputs=input0, outputs=output)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T09:11:59.016808Z","iopub.execute_input":"2023-07-21T09:11:59.019020Z","iopub.status.idle":"2023-07-21T09:11:59.032999Z","shell.execute_reply.started":"2023-07-21T09:11:59.018984Z","shell.execute_reply":"2023-07-21T09:11:59.032182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"discrimator=discrimatior_model()\nprint(discrimator(generated_image))","metadata":{"execution":{"iopub.status.busy":"2023-07-21T09:11:59.036839Z","iopub.execute_input":"2023-07-21T09:11:59.039042Z","iopub.status.idle":"2023-07-21T09:11:59.380389Z","shell.execute_reply.started":"2023-07-21T09:11:59.039010Z","shell.execute_reply":"2023-07-21T09:11:59.379327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"monet_discrimator=discrimatior_model()\nphoto_discrimator=discrimatior_model()","metadata":{"execution":{"iopub.status.busy":"2023-07-21T09:11:59.381912Z","iopub.execute_input":"2023-07-21T09:11:59.382274Z","iopub.status.idle":"2023-07-21T09:11:59.509047Z","shell.execute_reply.started":"2023-07-21T09:11:59.382220Z","shell.execute_reply":"2023-07-21T09:11:59.508181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cross_entropy=tf.keras.losses.BinaryCrossentropy(from_logits=True)\ndef discriminator_loss(real,fake):\n    real_loss=cross_entropy(tf.ones_like(real),real)\n    fake_loss=cross_entropy(tf.zeros_like(fake),fake)\n    loss=real_loss+fake_loss\n    return loss\ndef generator_loss(fake):\n    return cross_entropy(tf.zeros_like(fake),fake)\nmonet_generator_optimizer=tf.keras.optimizers.Adam(1e-4)\nmonet_discriminator_optimizer=tf.keras.optimizers.Adam(1e-4)\nphoto_generator_optimizer=tf.keras.optimizers.Adam(1e-4)\nphoto_discriminator_optimizer=tf.keras.optimizers.Adam(1e-4)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T09:11:59.510198Z","iopub.execute_input":"2023-07-21T09:11:59.510541Z","iopub.status.idle":"2023-07-21T09:11:59.526382Z","shell.execute_reply.started":"2023-07-21T09:11:59.510509Z","shell.execute_reply":"2023-07-21T09:11:59.525427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def calc_cycle_loss(real_image, cycled_image):\n    loss1=tf.reduce_mean(tf.abs(real_image - cycled_image))\n    return 10 * loss1\ndef identity_loss(real_image, same_image):\n    loss = tf.reduce_mean(tf.abs(real_image - same_image))\n    return 5 * loss","metadata":{"execution":{"iopub.status.busy":"2023-07-21T09:11:59.527895Z","iopub.execute_input":"2023-07-21T09:11:59.528340Z","iopub.status.idle":"2023-07-21T09:11:59.535801Z","shell.execute_reply.started":"2023-07-21T09:11:59.528308Z","shell.execute_reply":"2023-07-21T09:11:59.534802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@tf.function\ndef train_image(batch_data):\n    real_monet,real_photo=batch_data\n    with tf.GradientTape(persistent=True) as gen_tape,tf.GradientTape(persistent=True) as disc_tape:\n        fake_monet=generator_monet(real_photo,training=True)\n        cycled_photo=generator_photo(fake_monet,training=True)\n        \n        fake_photo=generator_photo(real_monet,training=True)\n        cycled_monet=generator_monet(fake_photo,training=True)\n        \n        same_monet=generator_monet(real_monet,training=True)\n        same_photo=generator_photo(real_photo,training=True)\n        \n        real_monet_output=monet_discrimator(same_monet,training=True)\n        fake_monet_output=monet_discrimator(cycled_monet,training=True)\n        \n        real_photo_output=photo_discrimator(same_photo,training=True)\n        fake_photo_output=photo_discrimator(cycled_photo,training=True)\n        \n#         total_cycle_loss = calc_cycle_loss(real_monet, cycled_monet) + calc_cycle_loss(real_photo, cycled_photo)\n        \n        monet_gen_loss=generator_loss(fake_monet_output)\n        photo_gen_loss=generator_loss(fake_photo_output)\n        \n#         total_gen_g_loss = monet_loss + total_cycle_loss + identity_loss(real_monet, same_monet)\n#         total_gen_f_loss = gen_f_loss + total_cycle_loss + identity_loss(real_x, same_x)\n        \n        monet_disc_loss=discriminator_loss(real_monet_output,fake_monet_output)\n        photo_disc_loss=discriminator_loss(real_photo_output,fake_photo_output)\n    gradients_monet_generator=gen_tape.gradient(monet_gen_loss,generator_monet.trainable_variables)\n    gradients_photo_generator=gen_tape.gradient(photo_gen_loss,generator_photo.trainable_variables)\n    gradients_monet_discrimator=disc_tape.gradient(monet_disc_loss,monet_discrimator.trainable_variables)\n    gradients_photo_discrimator=disc_tape.gradient(photo_disc_loss,photo_discrimator.trainable_variables)\n    \n    monet_generator_optimizer.apply_gradients(zip(gradients_monet_generator,generator_monet.trainable_variables))\n    monet_discriminator_optimizer.apply_gradients(zip(gradients_monet_discrimator,monet_discrimator.trainable_variables))\n    photo_generator_optimizer.apply_gradients(zip(gradients_photo_generator,generator_photo.trainable_variables))\n    photo_discriminator_optimizer.apply_gradients(zip(gradients_photo_discrimator,photo_discrimator.trainable_variables))\n","metadata":{"execution":{"iopub.status.busy":"2023-07-21T09:11:59.540077Z","iopub.execute_input":"2023-07-21T09:11:59.540388Z","iopub.status.idle":"2023-07-21T09:11:59.551017Z","shell.execute_reply.started":"2023-07-21T09:11:59.540365Z","shell.execute_reply":"2023-07-21T09:11:59.549851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.__version__","metadata":{"execution":{"iopub.status.busy":"2023-07-21T09:11:59.552723Z","iopub.execute_input":"2023-07-21T09:11:59.553395Z","iopub.status.idle":"2023-07-21T09:11:59.567510Z","shell.execute_reply.started":"2023-07-21T09:11:59.553363Z","shell.execute_reply":"2023-07-21T09:11:59.566487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time\ndef train(epochs):\n    for epoch in range(epochs):\n        start=time.time()\n        for batch in tf.data.Dataset.zip((monet_ds, photo_ds)):\n            train_image(batch)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T09:11:59.568709Z","iopub.execute_input":"2023-07-21T09:11:59.569351Z","iopub.status.idle":"2023-07-21T09:11:59.575898Z","shell.execute_reply.started":"2023-07-21T09:11:59.569319Z","shell.execute_reply":"2023-07-21T09:11:59.574888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# checkpoint_path = \"./kaggle/working/checkpoints/train\"\n\n# ckpt = tf.train.Checkpoint(generator_monet=generator_monet,\n#                            generator_photo=generator_photo,\n#                            monet_discrimator=monet_discrimator,\n#                            photo_discrimator=photo_discrimator,\n#                            monet_generator_optimizer=monet_generator_optimizer,\n#                            photo_generator_optimizer=photo_generator_optimizer,\n#                            monet_discriminator_optimizer=monet_discriminator_optimizer,\n#                            photo_discriminator_optimizer=photo_discriminator_optimizer)\n\n# ckpt_manager = tf.train.CheckpointManager(ckpt, checkpoint_path, max_to_keep=5)\n\n# if ckpt_manager.latest_checkpoint:\n#     ckpt.restore(ckpt_manager.latest_checkpoint)\n#     print ('Latest checkpoint restored!!')","metadata":{"execution":{"iopub.status.busy":"2023-07-21T09:11:59.577011Z","iopub.execute_input":"2023-07-21T09:11:59.577324Z","iopub.status.idle":"2023-07-21T09:11:59.586017Z","shell.execute_reply.started":"2023-07-21T09:11:59.577302Z","shell.execute_reply":"2023-07-21T09:11:59.584816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train(20)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T09:11:59.587057Z","iopub.execute_input":"2023-07-21T09:11:59.587457Z","iopub.status.idle":"2023-07-21T10:06:26.623419Z","shell.execute_reply.started":"2023-07-21T09:11:59.587426Z","shell.execute_reply":"2023-07-21T10:06:26.621545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=generator_monet(next(iter(photo_ds)))\n","metadata":{"execution":{"iopub.status.busy":"2023-07-21T10:06:26.624324Z","iopub.status.idle":"2023-07-21T10:06:26.624670Z","shell.execute_reply.started":"2023-07-21T10:06:26.624501Z","shell.execute_reply":"2023-07-21T10:06:26.624518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generator_monet.save(\"./kaggle/working/monetmodel.h5\")\ngenerator_photo.save(\"./kaggle/working/photomodel.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-07-21T10:06:26.626987Z","iopub.status.idle":"2023-07-21T10:06:26.627697Z","shell.execute_reply.started":"2023-07-21T10:06:26.627452Z","shell.execute_reply":"2023-07-21T10:06:26.627476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n_, ax = plt.subplots(5, 2, figsize=(100, 100))\nfor i, img in enumerate(photo_ds.take(5)):\n    prediction = generator_monet(img, training=False)[0].numpy()\n    prediction = (prediction * 127.5 + 127.5).astype(np.uint8)\n    img = (img[0] * 127.5 + 127.5).numpy().astype(np.uint8)\n\n    ax[i, 0].imshow(img)\n    ax[i, 1].imshow(prediction)","metadata":{"execution":{"iopub.status.busy":"2023-07-21T10:06:32.421020Z","iopub.status.idle":"2023-07-21T10:06:32.421821Z","shell.execute_reply.started":"2023-07-21T10:06:32.421574Z","shell.execute_reply":"2023-07-21T10:06:32.421597Z"},"trusted":true},"execution_count":null,"outputs":[]}]}