{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<center style=\"font-family:verdana;\"><h1 style=\"font-size:200%; padding: 10px; background: #008080;\"><b style=\"color:white;\">Crotalus horridus. A RattleSnake!</b></h1></center>","metadata":{}},{"cell_type":"markdown","source":"\"The timber rattlesnake, canebrake rattlesnake, or banded rattlesnake (Crotalus horridus) is a species of venomous, sometimes highly venomous, pit viper endemic to eastern North America. This is the only rattlesnake species in most of the populous Northeastern United States and is second only to its relatives to the west, the prairie rattlesnake, as the most northerly distributed venomous snake in North America. No subspecies are currently recognized.\"\n\n\"The timber rattlesnake was one of the many reptile species originally described by Carl Linnaeus in the landmark 1758 10th edition of his Systema Naturae, and still bears its original name Crotalus horridus.\"\n\nhttps://en.wikipedia.org/wiki/Timber_rattlesnake","metadata":{}},{"cell_type":"markdown","source":"#All script by Ting Liu https://www.kaggle.com/code/tingtingliuliu/kernel59f18a2057","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nimport os\nimport glob\nfrom matplotlib import pyplot as plt\n%matplotlib inline\nimport time","metadata":{"execution":{"iopub.status.busy":"2022-04-11T21:35:02.562660Z","iopub.execute_input":"2022-04-11T21:35:02.563446Z","iopub.status.idle":"2022-04-11T21:35:08.178199Z","shell.execute_reply.started":"2022-04-11T21:35:02.563339Z","shell.execute_reply":"2022-04-11T21:35:08.177398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.__version__","metadata":{"execution":{"iopub.status.busy":"2022-04-11T21:43:40.931131Z","iopub.execute_input":"2022-04-11T21:43:40.932075Z","iopub.status.idle":"2022-04-11T21:43:40.947460Z","shell.execute_reply.started":"2022-04-11T21:43:40.931953Z","shell.execute_reply":"2022-04-11T21:43:40.946794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs_path = glob.glob('../input/snakeclef2022/SnakeCLEF2022-medium_size/SnakeCLEF2022-medium_size/1999/Crotalus_horridus/*.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-04-11T21:58:13.909208Z","iopub.execute_input":"2022-04-11T21:58:13.909646Z","iopub.status.idle":"2022-04-11T21:58:13.943760Z","shell.execute_reply.started":"2022-04-11T21:58:13.909606Z","shell.execute_reply":"2022-04-11T21:58:13.943007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(imgs_path)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T21:58:33.420710Z","iopub.execute_input":"2022-04-11T21:58:33.421888Z","iopub.status.idle":"2022-04-11T21:58:33.427520Z","shell.execute_reply.started":"2022-04-11T21:58:33.421838Z","shell.execute_reply":"2022-04-11T21:58:33.426599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nimg = tf.keras.preprocessing.image.load_img(random.choice(imgs_path))\nimg","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:01:10.397705Z","iopub.execute_input":"2022-04-11T22:01:10.398491Z","iopub.status.idle":"2022-04-11T22:01:11.710482Z","shell.execute_reply.started":"2022-04-11T22:01:10.398443Z","shell.execute_reply":"2022-04-11T22:01:11.708002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs_path[:3]","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:01:41.023193Z","iopub.execute_input":"2022-04-11T22:01:41.023472Z","iopub.status.idle":"2022-04-11T22:01:41.029983Z","shell.execute_reply.started":"2022-04-11T22:01:41.023446Z","shell.execute_reply":"2022-04-11T22:01:41.028996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_jpg(path):\n    img = tf.io.read_file(path)\n    img = tf.image.decode_jpeg(img, channels=3)\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:02:08.014862Z","iopub.execute_input":"2022-04-11T22:02:08.015951Z","iopub.status.idle":"2022-04-11T22:02:08.020874Z","shell.execute_reply.started":"2022-04-11T22:02:08.015900Z","shell.execute_reply":"2022-04-11T22:02:08.019881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize(input_image, input_mask):\n    input_image = tf.cast(input_image, tf.float32)/127.5 - 1\n    input_mask = tf.cast(input_mask, tf.float32)/127.5 - 1\n    return input_image, input_mask","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:02:24.809179Z","iopub.execute_input":"2022-04-11T22:02:24.809706Z","iopub.status.idle":"2022-04-11T22:02:24.815254Z","shell.execute_reply.started":"2022-04-11T22:02:24.809660Z","shell.execute_reply":"2022-04-11T22:02:24.814385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image(image_path):\n    image = read_jpg(image_path)\n    w = tf.shape(image)[1]\n    w = w // 2\n    input_image = image[:, :w, :]\n    input_mask = image[:, w:, :]\n    input_image = tf.image.resize(input_image, (256, 256))\n    input_mask = tf.image.resize(input_mask, (256, 256))\n    \n    if tf.random.uniform(()) > 0.5:\n        input_image = tf.image.flip_left_right(input_image)\n        input_mask = tf.image.flip_left_right(input_mask)\n\n    input_image, input_mask = normalize(input_image, input_mask)\n\n    return input_mask, input_image","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:02:42.246700Z","iopub.execute_input":"2022-04-11T22:02:42.247028Z","iopub.status.idle":"2022-04-11T22:02:42.253540Z","shell.execute_reply.started":"2022-04-11T22:02:42.246994Z","shell.execute_reply":"2022-04-11T22:02:42.252924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = tf.data.Dataset.from_tensor_slices(imgs_path)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:02:58.884369Z","iopub.execute_input":"2022-04-11T22:02:58.884643Z","iopub.status.idle":"2022-04-11T22:02:58.935989Z","shell.execute_reply.started":"2022-04-11T22:02:58.884613Z","shell.execute_reply":"2022-04-11T22:02:58.935188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = dataset.map(load_image, num_parallel_calls=tf.data.experimental.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:03:19.372607Z","iopub.execute_input":"2022-04-11T22:03:19.372952Z","iopub.status.idle":"2022-04-11T22:03:19.655245Z","shell.execute_reply.started":"2022-04-11T22:03:19.372912Z","shell.execute_reply":"2022-04-11T22:03:19.654383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:03:35.210083Z","iopub.execute_input":"2022-04-11T22:03:35.210395Z","iopub.status.idle":"2022-04-11T22:03:35.219860Z","shell.execute_reply.started":"2022-04-11T22:03:35.210365Z","shell.execute_reply":"2022-04-11T22:03:35.218791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 32\nBUFFER_SIZE = 200","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:03:56.104022Z","iopub.execute_input":"2022-04-11T22:03:56.104862Z","iopub.status.idle":"2022-04-11T22:03:56.110118Z","shell.execute_reply.started":"2022-04-11T22:03:56.104809Z","shell.execute_reply":"2022-04-11T22:03:56.109163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = train.shuffle(BUFFER_SIZE).batch(BATCH_SIZE)\ntrain_dataset = train_dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:04:12.099626Z","iopub.execute_input":"2022-04-11T22:04:12.099925Z","iopub.status.idle":"2022-04-11T22:04:12.111956Z","shell.execute_reply.started":"2022-04-11T22:04:12.099896Z","shell.execute_reply":"2022-04-11T22:04:12.111015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 5))\nfor img, musk in train_dataset.take(1):\n    plt.subplot(1,2,1)\n    plt.imshow(tf.keras.preprocessing.image.array_to_img(img[0]))\n    plt.subplot(1,2,2)\n    plt.imshow(tf.keras.preprocessing.image.array_to_img(musk[0]));","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:04:51.704615Z","iopub.execute_input":"2022-04-11T22:04:51.705352Z","iopub.status.idle":"2022-04-11T22:04:52.196056Z","shell.execute_reply.started":"2022-04-11T22:04:51.705293Z","shell.execute_reply":"2022-04-11T22:04:52.194781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#We don't have validation file\n#imgs_path_test = glob.glob('../input/anime-sketch-colorization-pair/data/val/*.png')\nimgs_path_test = glob.glob('../input/snakeclef2022/SnakeCLEF2022-small_size/SnakeCLEF2022-small_size/1999/Crotalus_horridus/*.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:09:51.441718Z","iopub.execute_input":"2022-04-11T22:09:51.442042Z","iopub.status.idle":"2022-04-11T22:09:51.473913Z","shell.execute_reply.started":"2022-04-11T22:09:51.442012Z","shell.execute_reply":"2022-04-11T22:09:51.472943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(imgs_path_test)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:10:06.208319Z","iopub.execute_input":"2022-04-11T22:10:06.208658Z","iopub.status.idle":"2022-04-11T22:10:06.215111Z","shell.execute_reply.started":"2022-04-11T22:10:06.208582Z","shell.execute_reply":"2022-04-11T22:10:06.214189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_test = tf.data.Dataset.from_tensor_slices(imgs_path_test)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:10:25.282334Z","iopub.execute_input":"2022-04-11T22:10:25.282897Z","iopub.status.idle":"2022-04-11T22:10:25.289363Z","shell.execute_reply.started":"2022-04-11T22:10:25.282859Z","shell.execute_reply":"2022-04-11T22:10:25.288334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_image_test(image_path):\n    image = read_jpg(image_path)\n    w = tf.shape(image)[1]\n    w = w // 2\n    input_image = image[:, :w, :]\n    input_mask = image[:, w:, :]\n    input_image = tf.image.resize(input_image, (256, 256))\n    input_mask = tf.image.resize(input_mask, (256, 256))\n    \n    input_image, input_mask = normalize(input_image, input_mask)\n\n    return input_mask, input_image","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:10:43.495896Z","iopub.execute_input":"2022-04-11T22:10:43.496880Z","iopub.status.idle":"2022-04-11T22:10:43.502965Z","shell.execute_reply.started":"2022-04-11T22:10:43.496838Z","shell.execute_reply":"2022-04-11T22:10:43.502118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_test = dataset_test.map(load_image_test)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:10:59.264803Z","iopub.execute_input":"2022-04-11T22:10:59.265064Z","iopub.status.idle":"2022-04-11T22:10:59.364308Z","shell.execute_reply.started":"2022-04-11T22:10:59.265038Z","shell.execute_reply":"2022-04-11T22:10:59.363452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_test = dataset_test.batch(BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:11:14.594477Z","iopub.execute_input":"2022-04-11T22:11:14.595009Z","iopub.status.idle":"2022-04-11T22:11:14.600036Z","shell.execute_reply.started":"2022-04-11T22:11:14.594972Z","shell.execute_reply":"2022-04-11T22:11:14.599058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img, musk in dataset_test.take(1):\n    plt.subplot(1,2,1)\n    plt.imshow(tf.keras.preprocessing.image.array_to_img(img[0]))\n    plt.subplot(1,2,2)\n    plt.imshow(tf.keras.preprocessing.image.array_to_img(musk[0]))","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:11:31.338384Z","iopub.execute_input":"2022-04-11T22:11:31.338734Z","iopub.status.idle":"2022-04-11T22:11:31.797849Z","shell.execute_reply.started":"2022-04-11T22:11:31.338688Z","shell.execute_reply":"2022-04-11T22:11:31.797214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"OUTPUT_CHANNELS = 3","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:11:49.987280Z","iopub.execute_input":"2022-04-11T22:11:49.987746Z","iopub.status.idle":"2022-04-11T22:11:49.992091Z","shell.execute_reply.started":"2022-04-11T22:11:49.987686Z","shell.execute_reply":"2022-04-11T22:11:49.991282Z"},"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                               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":"2022-04-11T22:12:08.101549Z","iopub.execute_input":"2022-04-11T22:12:08.102173Z","iopub.status.idle":"2022-04-11T22:12:08.108910Z","shell.execute_reply.started":"2022-04-11T22:12:08.102114Z","shell.execute_reply":"2022-04-11T22:12:08.108218Z"},"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                                        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":"2022-04-11T22:12:27.336600Z","iopub.execute_input":"2022-04-11T22:12:27.337204Z","iopub.status.idle":"2022-04-11T22:12:27.343583Z","shell.execute_reply.started":"2022-04-11T22:12:27.337146Z","shell.execute_reply":"2022-04-11T22:12:27.342961Z"},"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), # (bs, 128, 128, 64)\n        downsample(128, 4), # (bs, 64, 64, 128)\n        downsample(256, 4), # (bs, 32, 32, 256)\n        downsample(512, 4), # (bs, 16, 16, 512)\n        downsample(512, 4), # (bs, 8, 8, 512)\n        downsample(512, 4), # (bs, 4, 4, 512)\n        downsample(512, 4), # (bs, 2, 2, 512)\n        downsample(512, 4), # (bs, 1, 1, 512)\n    ]\n\n    up_stack = [\n        upsample(512, 4, apply_dropout=True), # (bs, 2, 2, 1024)\n        upsample(512, 4, apply_dropout=True), # (bs, 4, 4, 1024)\n        upsample(512, 4, apply_dropout=True), # (bs, 8, 8, 1024)\n        upsample(512, 4), # (bs, 16, 16, 1024)\n        upsample(256, 4), # (bs, 32, 32, 512)\n        upsample(128, 4), # (bs, 64, 64, 256)\n        upsample(64, 4), # (bs, 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                                         activation='tanh') # (bs, 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":"2022-04-11T22:13:17.471065Z","iopub.execute_input":"2022-04-11T22:13:17.471509Z","iopub.status.idle":"2022-04-11T22:13:17.481131Z","shell.execute_reply.started":"2022-04-11T22:13:17.471464Z","shell.execute_reply":"2022-04-11T22:13:17.480305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generator = Generator()\n#tf.keras.utils.plot_model(generator, show_shapes=True, dpi=64)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:13:34.150152Z","iopub.execute_input":"2022-04-11T22:13:34.150815Z","iopub.status.idle":"2022-04-11T22:13:35.162805Z","shell.execute_reply.started":"2022-04-11T22:13:34.150781Z","shell.execute_reply":"2022-04-11T22:13:35.161901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LAMBDA = 10","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:13:49.671771Z","iopub.execute_input":"2022-04-11T22:13:49.672052Z","iopub.status.idle":"2022-04-11T22:13:49.676524Z","shell.execute_reply.started":"2022-04-11T22:13:49.672022Z","shell.execute_reply":"2022-04-11T22:13:49.675648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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":"2022-04-11T22:14:06.239987Z","iopub.execute_input":"2022-04-11T22:14:06.240479Z","iopub.status.idle":"2022-04-11T22:14:06.247015Z","shell.execute_reply.started":"2022-04-11T22:14:06.240429Z","shell.execute_reply":"2022-04-11T22:14:06.245830Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Discriminator():\n#    initializer = tf.random_normal_initializer(0., 0.02)\n\n    inp = tf.keras.layers.Input(shape=[256, 256, 3], name='input_image')\n    tar = tf.keras.layers.Input(shape=[256, 256, 3], name='target_image')\n\n    x = tf.keras.layers.concatenate([inp, tar]) # (bs, 256, 256, channels*2)\n\n    down1 = downsample(64, 4, False)(x) # (bs, 128, 128, 64)\n    down2 = downsample(128, 4)(down1) # (bs, 64, 64, 128)\n    down3 = downsample(256, 4)(down2) # (bs, 32, 32, 256)\n\n    conv = tf.keras.layers.Conv2D(512, 4, strides=1,\n                                  padding='same',\n                                  use_bias=False)(down3) # (bs, 32, 32, 512)\n\n    batchnorm1 = tf.keras.layers.BatchNormalization()(conv)\n\n    leaky_relu = tf.keras.layers.LeakyReLU()(batchnorm1)\n\n    last = tf.keras.layers.Conv2D(1, 4, strides=1, padding='same')(leaky_relu) # (bs, 30, 30, 1)\n\n    return tf.keras.Model(inputs=[inp, tar], outputs=last)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:14:30.661321Z","iopub.execute_input":"2022-04-11T22:14:30.661653Z","iopub.status.idle":"2022-04-11T22:14:30.670004Z","shell.execute_reply.started":"2022-04-11T22:14:30.661611Z","shell.execute_reply":"2022-04-11T22:14:30.668981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"discriminator = Discriminator()\n#tf.keras.utils.plot_model(discriminator, show_shapes=True, dpi=64)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:14:48.483218Z","iopub.execute_input":"2022-04-11T22:14:48.483837Z","iopub.status.idle":"2022-04-11T22:14:48.618403Z","shell.execute_reply.started":"2022-04-11T22:14:48.483787Z","shell.execute_reply":"2022-04-11T22:14:48.617570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_object = tf.keras.losses.BinaryCrossentropy(from_logits=True)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:15:04.328131Z","iopub.execute_input":"2022-04-11T22:15:04.329986Z","iopub.status.idle":"2022-04-11T22:15:04.334199Z","shell.execute_reply.started":"2022-04-11T22:15:04.329929Z","shell.execute_reply":"2022-04-11T22:15:04.333509Z"},"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":"2022-04-11T22:15:21.130174Z","iopub.execute_input":"2022-04-11T22:15:21.130801Z","iopub.status.idle":"2022-04-11T22:15:21.136109Z","shell.execute_reply.started":"2022-04-11T22:15:21.130765Z","shell.execute_reply":"2022-04-11T22:15:21.135241Z"},"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":"2022-04-11T22:15:39.436986Z","iopub.execute_input":"2022-04-11T22:15:39.437537Z","iopub.status.idle":"2022-04-11T22:15:39.442207Z","shell.execute_reply.started":"2022-04-11T22:15:39.437501Z","shell.execute_reply":"2022-04-11T22:15:39.441536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_images(model, test_input, tar):\n    prediction = model(test_input, training=True)\n    plt.figure(figsize=(15,15))\n\n    display_list = [test_input[0], tar[0], prediction[0]]\n    title = ['Input Image', '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 between [0, 1] to plot it.\n        plt.imshow(display_list[i] * 0.5 + 0.5)\n        plt.axis('off')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:15:56.734651Z","iopub.execute_input":"2022-04-11T22:15:56.735209Z","iopub.status.idle":"2022-04-11T22:15:56.741029Z","shell.execute_reply.started":"2022-04-11T22:15:56.735174Z","shell.execute_reply":"2022-04-11T22:15:56.740338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for example_input, example_target in dataset_test.take(1):\n    generate_images(generator, example_input, example_target)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:16:13.016040Z","iopub.execute_input":"2022-04-11T22:16:13.016351Z","iopub.status.idle":"2022-04-11T22:16:15.606768Z","shell.execute_reply.started":"2022-04-11T22:16:13.016318Z","shell.execute_reply":"2022-04-11T22:16:15.605676Z"},"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":"2022-04-11T22:16:33.783239Z","iopub.execute_input":"2022-04-11T22:16:33.784006Z","iopub.status.idle":"2022-04-11T22:16:33.789864Z","shell.execute_reply.started":"2022-04-11T22:16:33.783961Z","shell.execute_reply":"2022-04-11T22:16:33.788907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EPOCHS = 1","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:16:48.520445Z","iopub.execute_input":"2022-04-11T22:16:48.520743Z","iopub.status.idle":"2022-04-11T22:16:48.525027Z","shell.execute_reply.started":"2022-04-11T22:16:48.520696Z","shell.execute_reply":"2022-04-11T22:16:48.524074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@tf.function\ndef train_step(input_image, target, epoch):\n    with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:\n        gen_output = generator(input_image, training=True)\n\n        disc_real_output = discriminator([input_image, target], training=True)\n        disc_generated_output = discriminator([input_image, gen_output], training=True)\n\n        gen_total_loss, gen_gan_loss, gen_l1_loss = generator_loss(disc_generated_output, gen_output, target)\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))","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:17:08.725275Z","iopub.execute_input":"2022-04-11T22:17:08.725565Z","iopub.status.idle":"2022-04-11T22:17:08.733181Z","shell.execute_reply.started":"2022-04-11T22:17:08.725532Z","shell.execute_reply":"2022-04-11T22:17:08.732278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fit(train_ds, epochs, test_ds):\n    for epoch in range(epochs+1):\n        if epoch%10 == 0:\n            for example_input, example_target in test_ds.take(1):\n                generate_images(generator, example_input, example_target)\n        print(\"Epoch: \", epoch)\n\n        for n, (input_image, target) in train_ds.enumerate():\n            print('.', end='')\n            train_step(input_image, target, epoch)\n        print()","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:17:25.983217Z","iopub.execute_input":"2022-04-11T22:17:25.983818Z","iopub.status.idle":"2022-04-11T22:17:25.990547Z","shell.execute_reply.started":"2022-04-11T22:17:25.983752Z","shell.execute_reply":"2022-04-11T22:17:25.989714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fit(train_dataset, EPOCHS, dataset_test)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T22:17:45.382141Z","iopub.execute_input":"2022-04-11T22:17:45.382842Z","iopub.status.idle":"2022-04-11T22:18:08.512310Z","shell.execute_reply.started":"2022-04-11T22:17:45.382792Z","shell.execute_reply":"2022-04-11T22:18:08.511311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Since Ting Liu didn't provide any (any at all) information about the steps we've performed, I (with my very poor skills) can only suppose that is a TF/Keras/ReLU/tanh activation/Adam Optimizer/CNN???  And RattleSnakes!","metadata":{}},{"cell_type":"markdown","source":"#Acknowledgement:\n\nTing Liu https://www.kaggle.com/code/tingtingliuliu/kernel59f18a2057","metadata":{}}]}