{"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 numpy as np \nimport pandas as pd \n\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-10T17:26:38.139321Z","iopub.execute_input":"2023-04-10T17:26:38.139731Z","iopub.status.idle":"2023-04-10T17:26:38.170324Z","shell.execute_reply.started":"2023-04-10T17:26:38.139691Z","shell.execute_reply":"2023-04-10T17:26:38.169183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 32\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport tensorflow_addons as tfa\n\nimport matplotlib.pyplot as plt\nimport re\ntry:\n    from kaggle_datasets import KaggleDatasets\nexcept:\n    pass","metadata":{"execution":{"iopub.status.busy":"2023-04-10T17:26:38.172123Z","iopub.execute_input":"2023-04-10T17:26:38.172710Z","iopub.status.idle":"2023-04-10T17:26:48.849560Z","shell.execute_reply.started":"2023-04-10T17:26:38.172671Z","shell.execute_reply":"2023-04-10T17:26:48.848186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"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)\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":{"execution":{"iopub.status.busy":"2023-04-10T17:26:48.851054Z","iopub.execute_input":"2023-04-10T17:26:48.851792Z","iopub.status.idle":"2023-04-10T17:26:48.869245Z","shell.execute_reply.started":"2023-04-10T17:26:48.851723Z","shell.execute_reply":"2023-04-10T17:26:48.867824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"GCS_PATH = KaggleDatasets().get_gcs_path(\"gan-getting-started\")\n\nMONET_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/monet_tfrec/*.tfrec'))\nPHOTO_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/photo_tfrec/*.tfrec'))","metadata":{"execution":{"iopub.status.busy":"2023-04-10T17:26:48.872705Z","iopub.execute_input":"2023-04-10T17:26:48.873515Z","iopub.status.idle":"2023-04-10T17:26:49.398474Z","shell.execute_reply.started":"2023-04-10T17:26:48.873469Z","shell.execute_reply":"2023-04-10T17:26:49.396954Z"},"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    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","metadata":{"execution":{"iopub.status.busy":"2023-04-10T17:26:49.401007Z","iopub.execute_input":"2023-04-10T17:26:49.401808Z","iopub.status.idle":"2023-04-10T17:26:49.411249Z","shell.execute_reply.started":"2023-04-10T17:26:49.401757Z","shell.execute_reply":"2023-04-10T17:26:49.409831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dataset(filenames):\n    dataset = tf.data.TFRecordDataset(filenames)\n    dataset = dataset.map(read_tfrecord, num_parallel_calls=AUTOTUNE)\n    return dataset\n\nmonet_ds = load_dataset(MONET_FILENAMES).batch(1)\nphoto_ds = load_dataset(PHOTO_FILENAMES).batch(1)\n\n\nfast_photo_ds = load_dataset(PHOTO_FILENAMES).batch(32*strategy.num_replicas_in_sync).prefetch(32)\nfid_photo_ds = load_dataset(PHOTO_FILENAMES).take(1024).batch(32*strategy.num_replicas_in_sync).prefetch(32)\nfid_monet_ds = load_dataset(MONET_FILENAMES).batch(32*strategy.num_replicas_in_sync).prefetch(32)","metadata":{"execution":{"iopub.status.busy":"2023-04-10T17:26:49.412924Z","iopub.execute_input":"2023-04-10T17:26:49.413407Z","iopub.status.idle":"2023-04-10T17:26:49.794812Z","shell.execute_reply.started":"2023-04-10T17:26:49.413363Z","shell.execute_reply":"2023-04-10T17:26:49.793576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_gan_dataset(monet_files, photo_files, augment=None, repeat=True, shuffle=True, batch_size=1):\n\n    monet_ds = load_dataset(monet_files)\n    photo_ds = load_dataset(photo_files)\n    \n    if augment:\n        monet_ds = monet_ds.map(augment, num_parallel_calls=AUTOTUNE)\n        photo_ds = photo_ds.map(augment, num_parallel_calls=AUTOTUNE)\n\n    if repeat:\n        monet_ds = monet_ds.repeat()\n        photo_ds = photo_ds.repeat()\n        \n#     if shuffle:\n#         monet_ds = monet_ds.shuffle(2048)\n#         photo_ds = photo_ds.shuffle(2048)\n        \n    monet_ds = monet_ds.batch(batch_size, drop_remainder=True)\n    photo_ds = photo_ds.batch(batch_size, drop_remainder=True)\n#     monet_ds = monet_ds.cache()\n#     photo_ds = photo_ds.cache()\n    monet_ds = monet_ds.prefetch(AUTOTUNE)\n    photo_ds = photo_ds.prefetch(AUTOTUNE)\n    \n    gan_ds = tf.data.Dataset.zip((monet_ds, photo_ds))\n    \n    return gan_ds","metadata":{"execution":{"iopub.status.busy":"2023-04-10T17:26:49.796057Z","iopub.execute_input":"2023-04-10T17:26:49.796360Z","iopub.status.idle":"2023-04-10T17:26:49.804482Z","shell.execute_reply.started":"2023-04-10T17:26:49.796331Z","shell.execute_reply":"2023-04-10T17:26:49.803320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_dataset = get_gan_dataset(MONET_FILENAMES, PHOTO_FILENAMES, augment=None, repeat=True, shuffle=True, batch_size=BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2023-04-10T17:26:49.805868Z","iopub.execute_input":"2023-04-10T17:26:49.807014Z","iopub.status.idle":"2023-04-10T17:26:49.888165Z","shell.execute_reply.started":"2023-04-10T17:26:49.806973Z","shell.execute_reply":"2023-04-10T17:26:49.886807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n\n    inception_model = tf.keras.applications.InceptionV3(input_shape=(256,256,3),pooling=\"avg\",include_top=False)\n\n    mix3  = inception_model.get_layer(\"mixed9\").output\n    f0 = tf.keras.layers.GlobalAveragePooling2D()(mix3)\n\n    inception_model = tf.keras.Model(inputs=inception_model.input, outputs=f0)\n    inception_model.trainable = False\n\n    \n    \n    def calculate_activation_statistics_mod(images,fid_model):\n\n            act=tf.cast(fid_model.predict(images), tf.float32)\n\n            mu = tf.reduce_mean(act, axis=0)\n            mean_x = tf.reduce_mean(act, axis=0, keepdims=True)\n            mx = tf.matmul(tf.transpose(mean_x), mean_x)\n            vx = tf.matmul(tf.transpose(act), act)/tf.cast(tf.shape(act)[0], tf.float32)\n            sigma = vx - mx\n            return mu, sigma\n    myFID_mu2, myFID_sigma2 = calculate_activation_statistics_mod(fid_monet_ds,inception_model)        \n    fids=[]","metadata":{"execution":{"iopub.status.busy":"2023-04-10T17:26:49.889579Z","iopub.execute_input":"2023-04-10T17:26:49.889954Z","iopub.status.idle":"2023-04-10T17:27:17.947683Z","shell.execute_reply.started":"2023-04-10T17:26:49.889919Z","shell.execute_reply":"2023-04-10T17:27:17.946555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    def calculate_frechet_distance(mu1,sigma1,mu2,sigma2):\n        fid_epsilon = 1e-14\n       \n        covmean = tf.linalg.sqrtm(tf.cast(tf.matmul(sigma1,sigma2),tf.complex64))\n#         isgood=tf.cast(tf.math.is_finite(covmean), tf.int32)\n#         if tf.size(isgood)!=tf.math.reduce_sum(isgood):\n#             return 0\n\n        covmean = tf.cast(tf.math.real(covmean),tf.float32)\n  \n        tr_covmean = tf.linalg.trace(covmean)\n\n\n        return tf.matmul(tf.expand_dims(mu1 - mu2, axis=0),tf.expand_dims(mu1 - mu2, axis=1)) + tf.linalg.trace(sigma1) + tf.linalg.trace(sigma2) - 2 * tr_covmean\n\n\n    \n    \n    def FID(images,gen_model,inception_model=inception_model,myFID_mu2=myFID_mu2, myFID_sigma2=myFID_sigma2):\n                inp = layers.Input(shape=[256, 256, 3], name='input_image')\n                x  = gen_model(inp)\n                x=inception_model(x)\n                fid_model = tf.keras.Model(inputs=inp, outputs=x)\n                \n                mu1, sigma1= calculate_activation_statistics_mod(images,fid_model)\n\n                fid_value = calculate_frechet_distance(mu1, sigma1,myFID_mu2, myFID_sigma2)\n\n\n                return fid_value","metadata":{"execution":{"iopub.status.busy":"2023-04-10T17:27:17.950173Z","iopub.execute_input":"2023-04-10T17:27:17.950535Z","iopub.status.idle":"2023-04-10T17:27:17.962396Z","shell.execute_reply.started":"2023-04-10T17:27:17.950502Z","shell.execute_reply":"2023-04-10T17:27:17.961222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def up_sample(filters, size, apply_dropout=False):\n    initializer = tf.random_normal_initializer(0., 0.02)\n    gamma_init = keras.initializers.RandomNormal(mean=0.0, stddev=0.02)\n\n    layer = keras.Sequential()\n    layer.add(layers.Conv2DTranspose(filters, size, strides=2, padding='same', kernel_initializer=initializer,use_bias=False))\n    layer.add(tfa.layers.InstanceNormalization(gamma_initializer=gamma_init))\n\n    if apply_dropout:\n        layer.add(layers.Dropout(0.5))\n\n    layer.add(layers.ReLU())\n\n    return layer","metadata":{"execution":{"iopub.status.busy":"2023-04-10T17:29:54.220200Z","iopub.execute_input":"2023-04-10T17:29:54.221345Z","iopub.status.idle":"2023-04-10T17:29:54.229735Z","shell.execute_reply.started":"2023-04-10T17:29:54.221294Z","shell.execute_reply":"2023-04-10T17:29:54.228451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}