{"cells":[{"metadata":{"papermill":{"duration":0.039761,"end_time":"2021-01-13T11:05:16.45262","exception":false,"start_time":"2021-01-13T11:05:16.412859","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## Version Log\n\n1. Initial NB\n2. Added [monet-tfrecords-extdata](https://www.kaggle.com/doanquanvietnamca/monet-tfrecords-extdata) & trained for full 31 epochs"},{"metadata":{"papermill":{"duration":0.035612,"end_time":"2021-01-13T11:05:16.523922","exception":false,"start_time":"2021-01-13T11:05:16.48831","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Necessary Imports"},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2021-01-13T11:05:16.6019Z","iopub.status.busy":"2021-01-13T11:05:16.60098Z","iopub.status.idle":"2021-01-13T11:05:23.28096Z","shell.execute_reply":"2021-01-13T11:05:23.280296Z"},"papermill":{"duration":6.721754,"end_time":"2021-01-13T11:05:23.281093","exception":false,"start_time":"2021-01-13T11:05:16.559339","status":"completed"},"tags":[],"trusted":false},"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","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:23.368752Z","iopub.status.busy":"2021-01-13T11:05:23.367955Z","iopub.status.idle":"2021-01-13T11:05:27.632146Z","shell.execute_reply":"2021-01-13T11:05:27.631332Z"},"papermill":{"duration":4.315533,"end_time":"2021-01-13T11:05:27.632301","exception":false,"start_time":"2021-01-13T11:05:23.316768","status":"completed"},"tags":[],"trusted":false},"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)\n    \nexcept:\n    strategy = tf.distribute.get_strategy()\n\nprint(\"Number of replicas : \", strategy.num_replicas_in_sync)\n\n\nAUTO = tf.data.experimental.AUTOTUNE\nprint(tf.__version__)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:27.726349Z","iopub.status.busy":"2021-01-13T11:05:27.72555Z","iopub.status.idle":"2021-01-13T11:05:28.76156Z","shell.execute_reply":"2021-01-13T11:05:28.762211Z"},"papermill":{"duration":1.09215,"end_time":"2021-01-13T11:05:28.762357","exception":false,"start_time":"2021-01-13T11:05:27.670207","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"from kaggle_datasets import KaggleDatasets\nGCS_PATH = KaggleDatasets().get_gcs_path('gan-getting-started')\nGCS_PATH","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.037941,"end_time":"2021-01-13T11:05:28.837049","exception":false,"start_time":"2021-01-13T11:05:28.799108","status":"completed"},"tags":[]},"cell_type":"markdown","source":"Version 2 update....\n\nAdding extra Monet TFRecords Data"},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:28.914835Z","iopub.status.busy":"2021-01-13T11:05:28.914108Z","iopub.status.idle":"2021-01-13T11:05:29.391718Z","shell.execute_reply":"2021-01-13T11:05:29.391139Z"},"papermill":{"duration":0.516923,"end_time":"2021-01-13T11:05:29.391835","exception":false,"start_time":"2021-01-13T11:05:28.874912","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"GCS_PATH_MONET = KaggleDatasets().get_gcs_path('monet-tfrecords-extdata')\nGCS_PATH_MONET","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:29.76843Z","iopub.status.busy":"2021-01-13T11:05:29.637475Z","iopub.status.idle":"2021-01-13T11:05:29.773197Z","shell.execute_reply":"2021-01-13T11:05:29.773824Z"},"papermill":{"duration":0.335586,"end_time":"2021-01-13T11:05:29.773985","exception":false,"start_time":"2021-01-13T11:05:29.438399","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"MONET_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/monet_tfrec/*.tfrec'))\nprint(\"Monet TFRecord Files : \", len(MONET_FILENAMES))\nprint(MONET_FILENAMES)\n\nPHOTO_FILENAMES = tf.io.gfile.glob(str(GCS_PATH + '/photo_tfrec/*.tfrec'))\nprint(\"Photo TFRecord Files : \", len(PHOTO_FILENAMES))","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:29.853907Z","iopub.status.busy":"2021-01-13T11:05:29.852919Z","iopub.status.idle":"2021-01-13T11:05:30.009317Z","shell.execute_reply":"2021-01-13T11:05:30.008696Z"},"papermill":{"duration":0.197961,"end_time":"2021-01-13T11:05:30.009453","exception":false,"start_time":"2021-01-13T11:05:29.811492","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"# Version 2\n\nNEW_MONET_FILENAMES = tf.io.gfile.glob(str(GCS_PATH_MONET + '/monet*.tfrec'))\nprint(\"New Monet TFRecord Files : \", len(NEW_MONET_FILENAMES))\n#print(NEW_MONET_FILENAMES)\n\nTOTAL_MONET_FILES = []\nTOTAL_MONET_FILES.extend(MONET_FILENAMES)\nTOTAL_MONET_FILES.extend(NEW_MONET_FILENAMES)\nprint(len(TOTAL_MONET_FILES))","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:30.096833Z","iopub.status.busy":"2021-01-13T11:05:30.09587Z","iopub.status.idle":"2021-01-13T11:05:30.101023Z","shell.execute_reply":"2021-01-13T11:05:30.100345Z"},"papermill":{"duration":0.052256,"end_time":"2021-01-13T11:05:30.101139","exception":false,"start_time":"2021-01-13T11:05:30.048883","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"TOTAL_MONET_FILES","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:30.19139Z","iopub.status.busy":"2021-01-13T11:05:30.190638Z","iopub.status.idle":"2021-01-13T11:05:30.194082Z","shell.execute_reply":"2021-01-13T11:05:30.193555Z"},"papermill":{"duration":0.054125,"end_time":"2021-01-13T11:05:30.194213","exception":false,"start_time":"2021-01-13T11:05:30.140088","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"IMAGE_SIZE = [256,256]\n\ndef decode_image(image):\n    \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\n\n\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    \n    example = tf.io.parse_single_example(example, tfrecord_format)\n    image = decode_image(example['image'])\n    return image","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:30.283451Z","iopub.status.busy":"2021-01-13T11:05:30.282769Z","iopub.status.idle":"2021-01-13T11:05:30.285316Z","shell.execute_reply":"2021-01-13T11:05:30.285857Z"},"papermill":{"duration":0.050753,"end_time":"2021-01-13T11:05:30.286016","exception":false,"start_time":"2021-01-13T11:05:30.235263","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"def load_dataset(filenames, labeled = True, ordered = False):\n    dataset = tf.data.TFRecordDataset(filenames)\n    dataset = dataset.map(read_tfrecord, num_parallel_calls = tf.data.experimental.AUTOTUNE)\n    \n    return dataset","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.038554,"end_time":"2021-01-13T11:05:30.363747","exception":false,"start_time":"2021-01-13T11:05:30.325193","status":"completed"},"tags":[]},"cell_type":"markdown","source":"## Data Augment"},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:30.444745Z","iopub.status.busy":"2021-01-13T11:05:30.44401Z","iopub.status.idle":"2021-01-13T11:05:30.471405Z","shell.execute_reply":"2021-01-13T11:05:30.47223Z"},"papermill":{"duration":0.069891,"end_time":"2021-01-13T11:05:30.472415","exception":false,"start_time":"2021-01-13T11:05:30.402524","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"import random\nimport tensorflow as tf\nimport math\nimport tensorflow.keras.backend as K \n\n\ndef transform_rotation(image, height, rotation):\n    \n    dim = height\n    xdim = height % 2\n    rotation = rotation * tf.random.uniform([1], dtype = 'float32')\n    rotation = math.pi/180 * rotation\n    \n    c1 = tf.math.cos(rotation)\n    s1 = tf.math.sin(rotation)\n    one = tf.constant([1], dtype='float32')\n    zero = tf.constant([0], dtype='float32')\n    \n    rotation_matrix = tf.reshape(tf.concat([c1,s1,zero,-s1,c1,zero,zero,zero,one],axis=0),shape=[3,3])\n\n    \n    x = tf.repeat(tf.range(dim//2,-dim//2,-1), dim) # shape like : [111222333]\n    y = tf.tile(tf.range(-dim//2,dim//2), [dim]) #shape like: [123123123]\n    z = tf.ones([dim*dim], dtype='int32')\n    \n    idx = tf.stack([x,y,z])\n    \n    idx2 = K.dot(rotation_matrix, tf.cast(idx, dtype='float32'))\n    idx2 = K.cast(idx2, dtype='int32')\n    idx2 = K.clip(idx2,-dim//2+1+xdim, dim//2)\n    \n    idx3 = tf.stack([dim//2 -idx2[0], dim//2 -1 + idx2[1,]])\n    \n    \n    d = tf.gather_nd(image, tf.transpose(idx3))\n    return tf.reshape(d, shape = [dim, dim, 3])\n    \n    # rotation matrix\n    \ndef data_augment(image):\n    \n    p_rotate = tf.random.uniform([], 0, 1.0, dtype = tf.float32)\n    p_spatial = tf.random.uniform([], 0, 1.0, dtype = tf.float32)\n    p_crop = tf.random.uniform([], 0, 1.0, dtype = tf.float32)\n    \n    if p_crop > 0.5 : \n        temp_size = random.randint(260, 290)\n        image = tf.image.resize(image, size= [temp_size,temp_size])\n        image = tf.image.random_crop(image, size = [256,256,3])\n        \n        if p_crop > 0.9:\n            temp_size2 = random.randint(290, 310)\n            image = tf.image.resize(image, [temp_size2, temp_size2])\n            image = tf.image.random_crop(image, size = [256,256,3])\n            \n        \n    if p_rotate > 0.75:\n        image = tf.image.rot90(image,k=3)\n    elif p_rotate > .5:\n        image = tf.image.rot90(image,k=2)\n    elif p_rotate > 0.25:\n        image = tf.image.rot90(image,k=1)\n\n    if p_rotate >=0.3:\n        image = transform_rotation(image, height=256,rotation=45.)\n        \n        \n    if p_spatial >0.6:\n        image = tf.image.random_flip_left_right(image)\n        image = tf.image.random_flip_up_down(image)\n        if p_spatial >0.85:\n            image = tf.image.transpose(image)\n\n    return image\n    \n    ","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.039564,"end_time":"2021-01-13T11:05:30.551337","exception":false,"start_time":"2021-01-13T11:05:30.511773","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### GAN Dataset"},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:30.634761Z","iopub.status.busy":"2021-01-13T11:05:30.634134Z","iopub.status.idle":"2021-01-13T11:05:30.644892Z","shell.execute_reply":"2021-01-13T11:05:30.645473Z"},"papermill":{"duration":0.05439,"end_time":"2021-01-13T11:05:30.645642","exception":false,"start_time":"2021-01-13T11:05:30.591252","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"AUTO = tf.data.experimental.AUTOTUNE\n\ndef get_gan_dataset(monet_files, photo_files, augment = None, repeat = True, shuffle = True, batch_size = 1):\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 = AUTO)\n        photo_ds = photo_ds.map(augment, num_parallel_calls = AUTO)\n        \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    \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(AUTO)\n    photo_ds = photo_ds.prefetch(AUTO)\n    \n    gan_ds = tf.data.Dataset.zip((monet_ds, photo_ds))\n    \n    \n    return gan_ds","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:30.733231Z","iopub.status.busy":"2021-01-13T11:05:30.732513Z","iopub.status.idle":"2021-01-13T11:05:30.735979Z","shell.execute_reply":"2021-01-13T11:05:30.736431Z"},"papermill":{"duration":0.047829,"end_time":"2021-01-13T11:05:30.736574","exception":false,"start_time":"2021-01-13T11:05:30.688745","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"#gan_dataset = get_gan_dataset(MONET_FILENAMES, PHOTO_FILENAMES, augment = data_augment, repeat = True, shuffle = True, batch_size = 1)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:30.820421Z","iopub.status.busy":"2021-01-13T11:05:30.819736Z","iopub.status.idle":"2021-01-13T11:05:34.216203Z","shell.execute_reply":"2021-01-13T11:05:34.216745Z"},"papermill":{"duration":3.440375,"end_time":"2021-01-13T11:05:34.216891","exception":false,"start_time":"2021-01-13T11:05:30.776516","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"# Version 2 update\n\ngan_dataset = get_gan_dataset(TOTAL_MONET_FILES, PHOTO_FILENAMES, augment = data_augment, repeat = True, shuffle = True, batch_size = 1)\n","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.039591,"end_time":"2021-01-13T11:05:34.296447","exception":false,"start_time":"2021-01-13T11:05:34.256856","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Visualizing the Dataset\n\nVersion 2 Update"},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:34.378319Z","iopub.status.busy":"2021-01-13T11:05:34.377691Z","iopub.status.idle":"2021-01-13T11:05:34.382456Z","shell.execute_reply":"2021-01-13T11:05:34.383008Z"},"papermill":{"duration":0.047732,"end_time":"2021-01-13T11:05:34.383159","exception":false,"start_time":"2021-01-13T11:05:34.335427","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"import matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:34.470276Z","iopub.status.busy":"2021-01-13T11:05:34.469606Z","iopub.status.idle":"2021-01-13T11:05:44.878484Z","shell.execute_reply":"2021-01-13T11:05:44.879003Z"},"papermill":{"duration":10.454411,"end_time":"2021-01-13T11:05:44.87916","exception":false,"start_time":"2021-01-13T11:05:34.424749","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"a_tuple = None\nfor data in gan_dataset.take(1):\n    a_tuple = data","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:44.967556Z","iopub.status.busy":"2021-01-13T11:05:44.965809Z","iopub.status.idle":"2021-01-13T11:05:45.223733Z","shell.execute_reply":"2021-01-13T11:05:45.223222Z"},"papermill":{"duration":0.304803,"end_time":"2021-01-13T11:05:45.223847","exception":false,"start_time":"2021-01-13T11:05:44.919044","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"# PAINTING\n\nplt.imshow(np.array(a_tuple[0]).squeeze())","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:45.31437Z","iopub.status.busy":"2021-01-13T11:05:45.313667Z","iopub.status.idle":"2021-01-13T11:05:45.505023Z","shell.execute_reply":"2021-01-13T11:05:45.505552Z"},"papermill":{"duration":0.238984,"end_time":"2021-01-13T11:05:45.505694","exception":false,"start_time":"2021-01-13T11:05:45.26671","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"# PHOTO IMAGE\n\nplt.imshow(np.array(a_tuple[1]).squeeze())","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.045122,"end_time":"2021-01-13T11:05:45.59657","exception":false,"start_time":"2021-01-13T11:05:45.551448","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.046296,"end_time":"2021-01-13T11:05:45.688312","exception":false,"start_time":"2021-01-13T11:05:45.642016","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Developing the GAN Structure"},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:45.790092Z","iopub.status.busy":"2021-01-13T11:05:45.78929Z","iopub.status.idle":"2021-01-13T11:05:45.792424Z","shell.execute_reply":"2021-01-13T11:05:45.791914Z"},"papermill":{"duration":0.058687,"end_time":"2021-01-13T11:05:45.792537","exception":false,"start_time":"2021-01-13T11:05:45.73385","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"OUTPUT_CHANNELS = 3\n\ndef downsample(filters, size, apply_instancenorm = True):\n    initializer = tf.random_normal_initializer(0., 0.02)\n    gamma_init = keras.initializers.RandomNormal(mean = 0.0, stddev = 0.02)\n    \n    result = keras.Sequential()\n    result.add(layers.Conv2D(filters, size, strides = 2, padding= 'same', kernel_initializer = initializer, use_bias = False))\n    \n    if apply_instancenorm:\n        result.add(tfa.layers.InstanceNormalization(gamma_initializer = gamma_init))\n        \n    result.add(layers.LeakyReLU())\n    \n    return result","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:45.894672Z","iopub.status.busy":"2021-01-13T11:05:45.893989Z","iopub.status.idle":"2021-01-13T11:05:45.897452Z","shell.execute_reply":"2021-01-13T11:05:45.896809Z"},"papermill":{"duration":0.058872,"end_time":"2021-01-13T11:05:45.897573","exception":false,"start_time":"2021-01-13T11:05:45.838701","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"def upsample(filters, size, apply_dropout = False):\n    \n    initializer = tf.random_normal_initializer(0., 0.02)\n    gamma_init = keras.initializers.RandomNormal(mean = 0.0, stddev = 0.02)\n    \n    result = keras.Sequential()\n    result.add(layers.Conv2DTranspose(filters, size, strides = 2, padding = 'same', kernel_initializer = initializer, use_bias = False))\n    result.add(tfa.layers.InstanceNormalization(gamma_initializer = gamma_init))\n    \n    if apply_dropout:\n        result.add(layers.Dropout(0.5))\n        \n    result.add(layers.ReLU())\n    \n    return result","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:46.005167Z","iopub.status.busy":"2021-01-13T11:05:46.004441Z","iopub.status.idle":"2021-01-13T11:05:46.007727Z","shell.execute_reply":"2021-01-13T11:05:46.007223Z"},"papermill":{"duration":0.064608,"end_time":"2021-01-13T11:05:46.00784","exception":false,"start_time":"2021-01-13T11:05:45.943232","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"def Generator():\n    \n    inputs = layers.Input(shape = [256,256,3])\n    \n    down_stack = [\n        downsample(64, 4, apply_instancenorm = False),\n        downsample(128,4),\n        downsample(256,4),\n        downsample(512,4),\n        downsample(512,4),\n        downsample(512,4),\n        downsample(512,4),\n        downsample(512,4),\n    ]\n    \n    \n    up_stack = [\n        upsample(512, 4, apply_dropout=True),\n        upsample(512, 4, apply_dropout=True),  \n        upsample(512, 4, apply_dropout=True),\n        upsample(512, 4),\n        upsample(256, 4),\n        upsample(128, 4),\n        upsample(64, 4)\n    ]\n    \n    initializer = tf.random_normal_initializer(0., 0.02)\n    last = layers.Conv2DTranspose(OUTPUT_CHANNELS, 4, strides = 2, padding = \"same\",kernel_initializer = initializer, \n                                 activation = 'tanh')\n    \n    x = inputs\n    \n    # Downsampling\n    \n    skips = []\n    for down in down_stack:\n        x = down(x)\n        skips.append(x)\n    \n    skips = reversed(skips[:-1])\n    \n    for up, skip in zip(up_stack, skips):\n        \n        x = up(x)\n        x = layers.Concatenate()([x, skip])\n        \n    x = last(x)\n        \n        \n    return keras.Model(inputs = inputs, outputs = x)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.execute_input":"2021-01-13T11:05:46.112097Z","iopub.status.busy":"2021-01-13T11:05:46.111393Z","iopub.status.idle":"2021-01-13T11:05:46.114761Z","shell.execute_reply":"2021-01-13T11:05:46.114243Z"},"papermill":{"duration":0.061292,"end_time":"2021-01-13T11:05:46.114878","exception":false,"start_time":"2021-01-13T11:05:46.053586","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"def Discriminator():\n    \n    initializer = tf.random_normal_initializer(0., 0.02)\n    gamma_init = keras.initializers.RandomNormal(mean = 0.0, stddev = 0.02)\n    \n    inp = layers.Input(shape = [256, 256, 3], name = 'input_image')\n    \n    x = inp\n    \n    down1 = downsample(64, 4, False)(x)\n    down2 = downsample(128, 4)(down1)\n    down3 = downsample(256, 4)(down2)\n    \n    zero_pad1 = layers.ZeroPadding2D()(down3)\n    conv = layers.Conv2D(512, 4, strides = 1, kernel_initializer = initializer, use_bias = False)(zero_pad1)\n    norm1 = tfa.layers.InstanceNormalization(gamma_initializer = gamma_init)(conv)\n    \n    leaky_relu = layers.LeakyReLU()(norm1)\n    \n    zero_pad2 = layers.ZeroPadding2D()(leaky_relu)\n    \n    last = layers.Conv2D(1, 4, strides = 1, kernel_initializer = initializer)(zero_pad2)\n    \n    return tf.keras.Model(inputs = inp, outputs = last)\n    \n    ","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.045548,"end_time":"2021-01-13T11:05:46.206294","exception":false,"start_time":"2021-01-13T11:05:46.160746","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:46.304964Z","iopub.status.busy":"2021-01-13T11:05:46.304299Z","iopub.status.idle":"2021-01-13T11:05:56.780109Z","shell.execute_reply":"2021-01-13T11:05:56.779495Z"},"papermill":{"duration":10.527041,"end_time":"2021-01-13T11:05:56.780227","exception":false,"start_time":"2021-01-13T11:05:46.253186","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"with strategy.scope():\n    \n    monet_generator = Generator()\n    photo_generator = Generator()\n    \n    monet_discriminator = Discriminator()\n    photo_discriminator = Discriminator()","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.045768,"end_time":"2021-01-13T11:05:56.872122","exception":false,"start_time":"2021-01-13T11:05:56.826354","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.045522,"end_time":"2021-01-13T11:05:56.963495","exception":false,"start_time":"2021-01-13T11:05:56.917973","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.04554,"end_time":"2021-01-13T11:05:57.05697","exception":false,"start_time":"2021-01-13T11:05:57.01143","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.045727,"end_time":"2021-01-13T11:05:57.149145","exception":false,"start_time":"2021-01-13T11:05:57.103418","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Train the CycleGAN tutorial"},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:57.249472Z","iopub.status.busy":"2021-01-13T11:05:57.248803Z","iopub.status.idle":"2021-01-13T11:05:57.252163Z","shell.execute_reply":"2021-01-13T11:05:57.251568Z"},"papermill":{"duration":0.057284,"end_time":"2021-01-13T11:05:57.25229","exception":false,"start_time":"2021-01-13T11:05:57.195006","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"with strategy.scope():\n    \n    monet_generator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1= 0.5)\n    photo_generator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1 = 0.5)\n    \n    monet_discriminator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1 = 0.5)\n    photo_discriminator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1 = 0.5)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:57.37235Z","iopub.status.busy":"2021-01-13T11:05:57.371574Z","iopub.status.idle":"2021-01-13T11:05:57.375321Z","shell.execute_reply":"2021-01-13T11:05:57.374654Z"},"papermill":{"duration":0.076141,"end_time":"2021-01-13T11:05:57.375456","exception":false,"start_time":"2021-01-13T11:05:57.299315","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"class CycleGAN(keras.Model):\n    \n    \n    def __init__(self, monet_generator, photo_generator, monet_discriminator, photo_discriminator, lambda_cycle = 10):\n        \n        super(CycleGAN, self).__init__()\n        \n        self.m_gen = monet_generator\n        self.p_gen = photo_generator\n        self.m_disc = monet_discriminator\n        self.p_disc = photo_discriminator\n        self.lambda_cycle = lambda_cycle\n        \n        \n    def compile(self, m_gen_optimizer, p_gen_optimizer, m_disc_optimizer, p_disc_optimizer, gen_loss_fn, disc_loss_fn, cycle_loss_fn, identity_loss_fn):\n        super(CycleGAN, self).compile()\n        \n        self.m_gen_optimizer = m_gen_optimizer\n        self.p_gen_optimizer = p_gen_optimizer\n        self.m_disc_optimizer = m_disc_optimizer\n        self.p_disc_optimizer = p_disc_optimizer\n        \n        self.gen_loss_fn = gen_loss_fn\n        self.disc_loss_fn = disc_loss_fn\n        self.cycle_loss_fn = cycle_loss_fn\n        self.identity_loss_fn = identity_loss_fn\n        \n        \n    def train_step(self, batch_data):\n        \n        \n        real_monet, real_photo = batch_data\n        \n        with tf.GradientTape(persistent = True) as tape:\n            \n            \n            fake_monet = self.m_gen(real_photo, training = True)\n            cycled_photo = self.p_gen(fake_monet, training = True)\n            \n            fake_photo = self.p_gen(real_monet, training = True)\n            cycled_monet = self.m_gen(fake_photo, training = True)\n            \n            # generating itself\n            same_monet = self.m_gen(real_monet, training = True)\n            same_photo = self.p_gen(real_photo, training = True)\n            \n            # discriminator used to check, inputing real images\n            disc_real_monet = self.m_disc(real_monet, training = True)\n            disc_real_photo = self.p_disc(real_photo, training = True)\n            \n            # discriminator used to check, inputing fake images\n            disc_fake_monet = self.m_disc(fake_monet, training = True)\n            disc_fake_photo = self.p_disc(fake_photo, training = True)\n            \n            # evaluates generator loss\n            monet_gen_loss = self.gen_loss_fn(disc_fake_monet)\n            photo_gen_loss = self.gen_loss_fn(disc_fake_photo)\n            \n            # total cycle consistency loss\n            total_cycle_loss = self.cycle_loss_fn(real_monet, cycled_monet, self.lambda_cycle) + self.cycle_loss_fn(real_photo, cycled_photo, self.lambda_cycle)\n            \n            \n            # total generator loss\n            \n            total_monet_gen_loss = monet_gen_loss + total_cycle_loss + self.identity_loss_fn(real_monet, same_monet, self.lambda_cycle)\n            \n            \n            total_photo_gen_loss = photo_gen_loss + total_cycle_loss + self.identity_loss_fn(real_photo, same_photo, self.lambda_cycle)\n            \n            \n            # total discriminator loss\n            monet_disc_loss = self.disc_loss_fn(disc_real_monet, disc_fake_monet)\n            photo_disc_loss = self.disc_loss_fn(disc_real_photo, disc_fake_photo)\n            \n            # gradients for generator & discriminator\n            monet_generator_gradients = tape.gradient(total_monet_gen_loss,\n                                                  self.m_gen.trainable_variables)\n            photo_generator_gradients = tape.gradient(total_photo_gen_loss,\n                                                  self.p_gen.trainable_variables)\n            monet_discriminator_gradients = tape.gradient(monet_disc_loss,\n                                                      self.m_disc.trainable_variables)\n            photo_discriminator_gradients = tape.gradient(photo_disc_loss,\n                                                      self.p_disc.trainable_variables)\n            # gradients to the optimizer\n            self.m_gen_optimizer.apply_gradients(zip(monet_generator_gradients, self.m_gen.trainable_variables))\n            self.p_gen_optimizer.apply_gradients(zip(photo_generator_gradients, self.p_gen.trainable_variables))\n            self.m_disc_optimizer.apply_gradients(zip(monet_discriminator_gradients, self.m_disc.trainable_variables))\n            self.p_disc_optimizer.apply_gradients(zip(photo_discriminator_gradients, self.p_disc.trainable_variables))\n            \n            \n        \n        return {\n            \"monet_gen_loss\" : total_monet_gen_loss,\n            \"photo_gen_loss\" : total_photo_gen_loss,\n            \"monet_disc_loss\" : monet_disc_loss,\n            \"photo_disc_loss\" : photo_disc_loss\n        }","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:57.477095Z","iopub.status.busy":"2021-01-13T11:05:57.476237Z","iopub.status.idle":"2021-01-13T11:05:57.481323Z","shell.execute_reply":"2021-01-13T11:05:57.480766Z"},"papermill":{"duration":0.057583,"end_time":"2021-01-13T11:05:57.481457","exception":false,"start_time":"2021-01-13T11:05:57.423874","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"\"\"\"class EarlyStopping_custom(tf.keras.callbacks.EarlyStopping):\n    \n    def __init__(self, monitor, mode, patience, restore_best_weights = True):\n        \n        super(EarlyStopping_custom, self).__init__()\n        self.restore_best_weights = restore_best_weights\n        self.monitor = monitor\n        self.mode = mode\n        self.\"\"\"","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.048328,"end_time":"2021-01-13T11:05:57.577232","exception":false,"start_time":"2021-01-13T11:05:57.528904","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.047656,"end_time":"2021-01-13T11:05:57.672317","exception":false,"start_time":"2021-01-13T11:05:57.624661","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.048517,"end_time":"2021-01-13T11:05:57.768185","exception":false,"start_time":"2021-01-13T11:05:57.719668","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:57.872846Z","iopub.status.busy":"2021-01-13T11:05:57.872124Z","iopub.status.idle":"2021-01-13T11:05:57.875247Z","shell.execute_reply":"2021-01-13T11:05:57.874586Z"},"papermill":{"duration":0.059153,"end_time":"2021-01-13T11:05:57.875361","exception":false,"start_time":"2021-01-13T11:05:57.816208","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"with strategy.scope():\n    \n    def discriminator_loss(real, generated):\n        \n        real_loss = tf.keras.losses.BinaryCrossentropy(from_logits = True, reduction = tf.keras.losses.Reduction.NONE)(tf.ones_like(real), real)\n        \n        generated_loss = tf.keras.losses.BinaryCrossentropy(from_logits = True, reduction = tf.keras.losses.Reduction.NONE)(tf.zeros_like(generated), generated)\n        \n        total_disc_loss = real_loss + generated_loss\n        \n        return total_disc_loss * 0.5","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:57.980945Z","iopub.status.busy":"2021-01-13T11:05:57.980244Z","iopub.status.idle":"2021-01-13T11:05:57.98504Z","shell.execute_reply":"2021-01-13T11:05:57.984426Z"},"papermill":{"duration":0.060826,"end_time":"2021-01-13T11:05:57.985155","exception":false,"start_time":"2021-01-13T11:05:57.924329","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"with strategy.scope():\n    \n    def generator_loss(generated):\n        \n        return tf.keras.losses.BinaryCrossentropy(from_logits = True, reduction = tf.keras.losses.Reduction.NONE)(tf.ones_like(generated), generated)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:58.094527Z","iopub.status.busy":"2021-01-13T11:05:58.093638Z","iopub.status.idle":"2021-01-13T11:05:58.097099Z","shell.execute_reply":"2021-01-13T11:05:58.096465Z"},"papermill":{"duration":0.057012,"end_time":"2021-01-13T11:05:58.097218","exception":false,"start_time":"2021-01-13T11:05:58.040206","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"with strategy.scope():\n    \n    def calc_cycle_loss(real_image, cycled_image, LAMBDA):\n        loss1 = tf.reduce_mean(tf.abs(real_image - cycled_image))\n        \n        return LAMBDA * loss1","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:58.200262Z","iopub.status.busy":"2021-01-13T11:05:58.199276Z","iopub.status.idle":"2021-01-13T11:05:58.20213Z","shell.execute_reply":"2021-01-13T11:05:58.20259Z"},"papermill":{"duration":0.057356,"end_time":"2021-01-13T11:05:58.202738","exception":false,"start_time":"2021-01-13T11:05:58.145382","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"with strategy.scope():\n    \n    def identity_loss(real_image, same_image, LAMBDA):\n        \n        loss = tf.reduce_mean(tf.abs(real_image - same_image))\n        return LAMBDA * 0.5 * loss","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:58.3085Z","iopub.status.busy":"2021-01-13T11:05:58.307829Z","iopub.status.idle":"2021-01-13T11:05:58.310854Z","shell.execute_reply":"2021-01-13T11:05:58.310229Z"},"papermill":{"duration":0.060481,"end_time":"2021-01-13T11:05:58.310984","exception":false,"start_time":"2021-01-13T11:05:58.250503","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"with strategy.scope():\n    \n    monet_generator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1 = 0.5)\n    photo_generator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1 = 0.5)\n    \n    monet_discriminator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1 = 0.5)\n    photo_discriminator_optimizer = tf.keras.optimizers.Adam(2e-4, beta_1 = 0.5)\n    \n    ","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:58.42587Z","iopub.status.busy":"2021-01-13T11:05:58.424888Z","iopub.status.idle":"2021-01-13T11:05:58.447135Z","shell.execute_reply":"2021-01-13T11:05:58.447698Z"},"papermill":{"duration":0.089208,"end_time":"2021-01-13T11:05:58.447889","exception":false,"start_time":"2021-01-13T11:05:58.358681","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"with strategy.scope():\n    \n    cycle_gan_model = CycleGAN(\n        monet_generator, photo_generator, monet_discriminator, photo_discriminator\n    )\n    \n    cycle_gan_model.compile(\n        m_gen_optimizer = monet_generator_optimizer, \n        p_gen_optimizer = photo_generator_optimizer, \n        m_disc_optimizer = monet_discriminator_optimizer, \n        p_disc_optimizer = photo_discriminator_optimizer, \n        gen_loss_fn = generator_loss,\n        disc_loss_fn = discriminator_loss,\n        cycle_loss_fn = calc_cycle_loss,\n        identity_loss_fn = identity_loss\n    )","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":0.047208,"end_time":"2021-01-13T11:05:58.54763","exception":false,"start_time":"2021-01-13T11:05:58.500422","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:58.649162Z","iopub.status.busy":"2021-01-13T11:05:58.64826Z","iopub.status.idle":"2021-01-13T11:05:58.650866Z","shell.execute_reply":"2021-01-13T11:05:58.651391Z"},"papermill":{"duration":0.055795,"end_time":"2021-01-13T11:05:58.65154","exception":false,"start_time":"2021-01-13T11:05:58.595745","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"EPOCHS_NUM = 31","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:58.754294Z","iopub.status.busy":"2021-01-13T11:05:58.753478Z","iopub.status.idle":"2021-01-13T11:05:58.756571Z","shell.execute_reply":"2021-01-13T11:05:58.755965Z"},"papermill":{"duration":0.056988,"end_time":"2021-01-13T11:05:58.756688","exception":false,"start_time":"2021-01-13T11:05:58.6997","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"BATCH_SIZE = 2\nsteps_per_epoch = (7038//BATCH_SIZE)\n#gan_dataset = get_gan_dataset(MONET_FILENAMES,PHOTO_FILENAMES,augment=data_augument,repeat=True,shuffle=True,batch_size=BATCH_SIZE)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-13T11:05:58.857799Z","iopub.status.busy":"2021-01-13T11:05:58.857119Z","iopub.status.idle":"2021-01-13T15:03:05.354835Z","shell.execute_reply":"2021-01-13T15:03:05.355385Z"},"papermill":{"duration":14226.550861,"end_time":"2021-01-13T15:03:05.355549","exception":false,"start_time":"2021-01-13T11:05:58.804688","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"cycle_gan_model.fit(\n    #full_dataset,\n    gan_dataset,\n    epochs = EPOCHS_NUM,\n    #steps_per_epoch = (max(n_monet_samples, n_photo_samples) // BATCH_SIZE)\n    steps_per_epoch = steps_per_epoch\n)","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":37.589618,"end_time":"2021-01-13T15:04:20.352405","exception":false,"start_time":"2021-01-13T15:03:42.762787","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":37.357754,"end_time":"2021-01-13T15:05:35.702519","exception":false,"start_time":"2021-01-13T15:04:58.344765","status":"completed"},"tags":[]},"cell_type":"markdown","source":"# Notebook in making..."},{"metadata":{"papermill":{"duration":38.387381,"end_time":"2021-01-13T15:06:51.895997","exception":false,"start_time":"2021-01-13T15:06:13.508616","status":"completed"},"tags":[]},"cell_type":"markdown","source":"### Possible Hyperparameters to tune"},{"metadata":{"papermill":{"duration":37.893074,"end_time":"2021-01-13T15:08:07.069201","exception":false,"start_time":"2021-01-13T15:07:29.176127","status":"completed"},"tags":[]},"cell_type":"markdown","source":"* IMAGE_SIZE\n* NUMBER OF EPOCHS\n* Optimizer Selected"},{"metadata":{"papermill":{"duration":37.678891,"end_time":"2021-01-13T15:09:22.361777","exception":false,"start_time":"2021-01-13T15:08:44.682886","status":"completed"},"tags":[]},"cell_type":"markdown","source":""},{"metadata":{"papermill":{"duration":37.981635,"end_time":"2021-01-13T15:10:38.092909","exception":false,"start_time":"2021-01-13T15:10:00.111274","status":"completed"},"tags":[]},"cell_type":"markdown","source":""},{"metadata":{"papermill":{"duration":37.820385,"end_time":"2021-01-13T15:11:54.360563","exception":false,"start_time":"2021-01-13T15:11:16.540178","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":37.49224,"end_time":"2021-01-13T15:13:11.32683","exception":false,"start_time":"2021-01-13T15:12:33.83459","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":37.553675,"end_time":"2021-01-13T15:14:26.50945","exception":false,"start_time":"2021-01-13T15:13:48.955775","status":"completed"},"tags":[],"trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}