{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":15062,"databundleVersionId":545987,"sourceType":"competition"}],"dockerImageVersionId":30627,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport zipfile\nimport os\nimport glob\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mimage\n\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.utils import image_dataset_from_directory\nfrom tensorflow.keras.layers import Conv2D, MaxPool2D, UpSampling2D, Input\nfrom tensorflow.keras.layers import RandomBrightness, RandomContrast, RandomCrop, RandomFlip, RandomHeight, RandomRotation, RandomTranslation, RandomWidth, RandomZoom\nfrom tensorflow.keras.models import Model, Sequential\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-15T17:59:36.271062Z","iopub.execute_input":"2023-12-15T17:59:36.271415Z","iopub.status.idle":"2023-12-15T17:59:52.889441Z","shell.execute_reply.started":"2023-12-15T17:59:36.271387Z","shell.execute_reply":"2023-12-15T17:59:52.888653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_data_path = \"/kaggle/input/generative-dog-images\"\nwith zipfile.ZipFile(os.path.join(full_data_path, \"all-dogs.zip\"), 'r') as f:\n    f.extractall(os.path.join(\"/kaggle/working/\", \"all-dogs\"))","metadata":{"execution":{"iopub.status.busy":"2023-12-15T17:59:52.891346Z","iopub.execute_input":"2023-12-15T17:59:52.89201Z","iopub.status.idle":"2023-12-15T18:00:08.49256Z","shell.execute_reply.started":"2023-12-15T17:59:52.891975Z","shell.execute_reply":"2023-12-15T18:00:08.491599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_images_path = \"/kaggle/working/all-dogs/all-dogs\"\n\nprint(f\"The dog folder has {len(os.listdir(dog_images_path))} images\")","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:00:08.493988Z","iopub.execute_input":"2023-12-15T18:00:08.494602Z","iopub.status.idle":"2023-12-15T18:00:08.515135Z","shell.execute_reply.started":"2023-12-15T18:00:08.494567Z","shell.execute_reply":"2023-12-15T18:00:08.514257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(dog_images_path)[0]\nimage = mimage.imread(os.path.join(dog_images_path, \"n02112137_2998.jpg\"))\nimage.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:00:08.517475Z","iopub.execute_input":"2023-12-15T18:00:08.517737Z","iopub.status.idle":"2023-12-15T18:00:08.959722Z","shell.execute_reply.started":"2023-12-15T18:00:08.517714Z","shell.execute_reply":"2023-12-15T18:00:08.958937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_data = image_dataset_from_directory(\n    dog_images_path,\n    labels=None,\n    label_mode=None,\n    color_mode=\"rgb\",\n    batch_size=32,\n    image_size=(64, 64),\n    shuffle=True,\n    seed=39\n)","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:00:08.960903Z","iopub.execute_input":"2023-12-15T18:00:08.961292Z","iopub.status.idle":"2023-12-15T18:00:15.443023Z","shell.execute_reply.started":"2023-12-15T18:00:08.961254Z","shell.execute_reply":"2023-12-15T18:00:15.442083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(image_data.cardinality().numpy())","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:00:15.444216Z","iopub.execute_input":"2023-12-15T18:00:15.444579Z","iopub.status.idle":"2023-12-15T18:00:15.451441Z","shell.execute_reply.started":"2023-12-15T18:00:15.444539Z","shell.execute_reply":"2023-12-15T18:00:15.450559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_images(images):\n    return images / 255.0\n\nimage_data = image_data.map(resize_images)","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:00:15.452754Z","iopub.execute_input":"2023-12-15T18:00:15.453091Z","iopub.status.idle":"2023-12-15T18:00:15.494398Z","shell.execute_reply.started":"2023-12-15T18:00:15.453059Z","shell.execute_reply":"2023-12-15T18:00:15.493733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_augmentation = Sequential([\n    Input((64, 64, 3)),\n    RandomBrightness(0.1, value_range=(0, 1), seed=39),\n    RandomContrast(0.1, seed=40),\n    RandomFlip(mode=\"horizontal\", seed=34),\n    RandomRotation(0.2, seed=233),\n    RandomZoom(0.2, 0.2, seed=100)\n])\n\ndata_augmentation.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:00:15.495463Z","iopub.execute_input":"2023-12-15T18:00:15.495825Z","iopub.status.idle":"2023-12-15T18:00:15.934237Z","shell.execute_reply.started":"2023-12-15T18:00:15.495792Z","shell.execute_reply":"2023-12-15T18:00:15.933335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def adding_target(image):\n    image = data_augmentation(image)\n    return (image, image)\n\nimage_data = image_data.map(adding_target)","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:00:15.935411Z","iopub.execute_input":"2023-12-15T18:00:15.935663Z","iopub.status.idle":"2023-12-15T18:00:16.195466Z","shell.execute_reply.started":"2023-12-15T18:00:15.935641Z","shell.execute_reply":"2023-12-15T18:00:16.194658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cardinality = image_data.cardinality().numpy()\ntrain_split = 0.7\ntest_valid_split = 0.15\n\ntrain_size = round(train_split * cardinality)\ntest_size = round(test_valid_split * cardinality) \nvalid_size = round(test_valid_split * cardinality)\n\ntrain_data = image_data.take(train_size)\ntest_data = image_data.skip(train_size)\nvalid_data = test_data.skip(valid_size)\ntest_data = test_data.take(test_size)\n\ndel image_data\n\nprint(\"Cardinality of total data is \", cardinality)\nprint(\"Cardinality of train data is \", train_data.cardinality().numpy())\nprint(\"Cardinality of test data is \", test_data.cardinality().numpy())\nprint(\"Cardinality of validation data is \", valid_data.cardinality().numpy())","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:00:16.198089Z","iopub.execute_input":"2023-12-15T18:00:16.198375Z","iopub.status.idle":"2023-12-15T18:00:16.219235Z","shell.execute_reply.started":"2023-12-15T18:00:16.198351Z","shell.execute_reply":"2023-12-15T18:00:16.218319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder_inp = Input((64, 64, 3))\nenc = Conv2D(\n    64,\n    kernel_size=(3, 3),\n    strides=(1, 1),\n    padding=\"same\",\n    activation=\"relu\"\n) (encoder_inp)\nenc = MaxPool2D() (enc)\nenc = Conv2D(\n    128,\n    kernel_size=(3, 3),\n    strides=(1, 1),\n    padding=\"same\",\n    activation=\"relu\"\n) (enc)\nenc = MaxPool2D() (enc)\nencoder_out = Conv2D(\n    256,\n    kernel_size=(3, 3),\n    strides=(1, 1),\n    padding=\"same\",\n    activation=\"relu\"\n) (enc)\n\nencoder = Model(encoder_inp, encoder_out)\nencoder.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:00:16.220282Z","iopub.execute_input":"2023-12-15T18:00:16.220546Z","iopub.status.idle":"2023-12-15T18:00:16.322111Z","shell.execute_reply.started":"2023-12-15T18:00:16.220524Z","shell.execute_reply":"2023-12-15T18:00:16.32095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"decoder_inp = Input((16, 16, 256))\ndec = Conv2D(\n    128,\n    kernel_size=(3, 3),\n    strides=(1, 1),\n    padding=\"same\",\n    activation=\"relu\"\n) (decoder_inp)\ndec = UpSampling2D() (dec)\ndec = Conv2D(\n    64,\n    kernel_size=(3, 3),\n    strides=(1, 1),\n    padding=\"same\",\n    activation=\"relu\"\n) (dec)\ndec = UpSampling2D() (dec)\ndec = Conv2D(\n    3,\n    kernel_size=(3, 3),\n    strides=(1, 1),\n    padding=\"same\",\n    activation=\"sigmoid\"\n) (dec)\n\ndecoder = Model(decoder_inp, dec)\ndecoder.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:00:16.323235Z","iopub.execute_input":"2023-12-15T18:00:16.323501Z","iopub.status.idle":"2023-12-15T18:00:16.400584Z","shell.execute_reply.started":"2023-12-15T18:00:16.323478Z","shell.execute_reply":"2023-12-15T18:00:16.399704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"autoencoder = Sequential([\n    Input((64, 64, 3)),\n    encoder,\n    decoder\n])\n\nautoencoder.summary(expand_nested=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:00:16.40162Z","iopub.execute_input":"2023-12-15T18:00:16.40189Z","iopub.status.idle":"2023-12-15T18:00:16.483902Z","shell.execute_reply.started":"2023-12-15T18:00:16.401867Z","shell.execute_reply":"2023-12-15T18:00:16.483068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@tf.function\ndef PSNR(y_true, y_pred):\n    return tf.image.psnr(y_true, y_pred, max_val=1.0)","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:00:16.485187Z","iopub.execute_input":"2023-12-15T18:00:16.485446Z","iopub.status.idle":"2023-12-15T18:00:16.489832Z","shell.execute_reply.started":"2023-12-15T18:00:16.485423Z","shell.execute_reply":"2023-12-15T18:00:16.488996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"autoencoder.compile(\n    optimizer=\"adam\",\n    loss=\"binary_crossentropy\",\n    metrics=[PSNR, \"accuracy\"]\n)","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:00:16.490998Z","iopub.execute_input":"2023-12-15T18:00:16.49127Z","iopub.status.idle":"2023-12-15T18:00:16.51927Z","shell.execute_reply.started":"2023-12-15T18:00:16.491247Z","shell.execute_reply":"2023-12-15T18:00:16.518494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callbacks = [\n    EarlyStopping(monitor=\"val_loss\", mode=\"max\", patience=5, start_from_epoch=3, restore_best_weights=True),\n    ReduceLROnPlateau(monitor=\"val_loss\", factor=0.2, patience=3, min_lr=1e-5)\n]","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:00:16.520226Z","iopub.execute_input":"2023-12-15T18:00:16.52047Z","iopub.status.idle":"2023-12-15T18:00:16.525263Z","shell.execute_reply.started":"2023-12-15T18:00:16.520449Z","shell.execute_reply":"2023-12-15T18:00:16.524344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = autoencoder.fit(\n    train_data,\n    validation_data=valid_data,\n    epochs=50,\n    shuffle=True,\n    callbacks=callbacks\n).history","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:00:16.526176Z","iopub.execute_input":"2023-12-15T18:00:16.526424Z","iopub.status.idle":"2023-12-15T18:11:55.056499Z","shell.execute_reply.started":"2023-12-15T18:00:16.526403Z","shell.execute_reply":"2023-12-15T18:11:55.055531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"autoencoder.evaluate(test_data)","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:14:16.925666Z","iopub.execute_input":"2023-12-15T18:14:16.926561Z","iopub.status.idle":"2023-12-15T18:14:34.635348Z","shell.execute_reply.started":"2023-12-15T18:14:16.92652Z","shell.execute_reply":"2023-12-15T18:14:34.634508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image1, image2 = next(test_data.unbatch().as_numpy_iterator())\nprediction = autoencoder.predict(np.expand_dims(image1, 0))","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:22:58.872845Z","iopub.execute_input":"2023-12-15T18:22:58.87318Z","iopub.status.idle":"2023-12-15T18:23:13.17213Z","shell.execute_reply.started":"2023-12-15T18:22:58.873157Z","shell.execute_reply":"2023-12-15T18:23:13.171158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 6))\nplt.subplot(1, 2, 1)\nplt.imshow(image1)\nplt.axis(False)\nplt.subplot(1, 2, 2)\nplt.imshow(np.squeeze(prediction))\nplt.axis(False)\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:24:10.223909Z","iopub.execute_input":"2023-12-15T18:24:10.224278Z","iopub.status.idle":"2023-12-15T18:24:10.369515Z","shell.execute_reply.started":"2023-12-15T18:24:10.224249Z","shell.execute_reply":"2023-12-15T18:24:10.368613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"half_pred = encoder.predict((np.expand_dims(image1, 0)))\nfinal_pred = decoder.predict(half_pred)\nplt.imshow(np.squeeze(_pred))\nplt.axis(False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-15T18:28:55.310219Z","iopub.execute_input":"2023-12-15T18:28:55.311116Z","iopub.status.idle":"2023-12-15T18:28:55.542938Z","shell.execute_reply.started":"2023-12-15T18:28:55.311073Z","shell.execute_reply":"2023-12-15T18:28:55.541784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}