{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nfrom pathlib import Path\nfrom zipfile import ZipFile\nimport shutil\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-29T11:14:42.314142Z","iopub.execute_input":"2021-09-29T11:14:42.314412Z","iopub.status.idle":"2021-09-29T11:14:42.318822Z","shell.execute_reply.started":"2021-09-29T11:14:42.314383Z","shell.execute_reply":"2021-09-29T11:14:42.318003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_dir = Path('/kaggle/working/data')\ndata_dir.mkdir()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:17.008621Z","iopub.execute_input":"2021-09-29T08:51:17.008908Z","iopub.status.idle":"2021-09-29T08:51:17.015831Z","shell.execute_reply.started":"2021-09-29T08:51:17.008872Z","shell.execute_reply":"2021-09-29T08:51:17.014539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip') as train_zip:\n    train_zip.extractall(path=data_dir)\n\ntrain_dir = data_dir / 'train'\nlen(list(train_dir.iterdir()))","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:17.017588Z","iopub.execute_input":"2021-09-29T08:51:17.017891Z","iopub.status.idle":"2021-09-29T08:51:29.470209Z","shell.execute_reply.started":"2021-09-29T08:51:17.017855Z","shell.execute_reply":"2021-09-29T08:51:29.469544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with ZipFile('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip') as test_zip:\n    test_zip.extractall(path=data_dir)\n\ntest_dir = data_dir / 'test'\nlen(list(test_dir.iterdir()))","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:29.472075Z","iopub.execute_input":"2021-09-29T08:51:29.472404Z","iopub.status.idle":"2021-09-29T08:51:35.475630Z","shell.execute_reply.started":"2021-09-29T08:51:29.472368Z","shell.execute_reply":"2021-09-29T08:51:35.474945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_dir = train_dir / 'cat'\ndog_dir = train_dir / 'dog'\n\ncat_dir.mkdir()\ndog_dir.mkdir()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:35.476966Z","iopub.execute_input":"2021-09-29T08:51:35.477211Z","iopub.status.idle":"2021-09-29T08:51:35.481907Z","shell.execute_reply.started":"2021-09-29T08:51:35.477178Z","shell.execute_reply":"2021-09-29T08:51:35.481134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image_path in train_dir.glob('*.jpg'):\n    src = str(image_path)\n    if 'cat' in src:\n        shutil.move(src, str(cat_dir))\n    else:\n        shutil.move(src, str(dog_dir))","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:35.483363Z","iopub.execute_input":"2021-09-29T08:51:35.483650Z","iopub.status.idle":"2021-09-29T08:51:36.521796Z","shell.execute_reply.started":"2021-09-29T08:51:35.483614Z","shell.execute_reply":"2021-09-29T08:51:36.521055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(list(cat_dir.iterdir())))\nprint(len(list(dog_dir.iterdir())))","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:36.523298Z","iopub.execute_input":"2021-09-29T08:51:36.523581Z","iopub.status.idle":"2021-09-29T08:51:36.585954Z","shell.execute_reply.started":"2021-09-29T08:51:36.523545Z","shell.execute_reply":"2021-09-29T08:51:36.585294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 32\nIMAGE_SIZE = (160, 160)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:36.587792Z","iopub.execute_input":"2021-09-29T08:51:36.587988Z","iopub.status.idle":"2021-09-29T08:51:36.591795Z","shell.execute_reply.started":"2021-09-29T08:51:36.587965Z","shell.execute_reply":"2021-09-29T08:51:36.590975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = tf.keras.preprocessing.image_dataset_from_directory(\n    str(train_dir),\n    validation_split=0.2,\n    subset=\"training\",\n    seed=33,\n    image_size=IMAGE_SIZE,\n    batch_size=BATCH_SIZE,)\n\nvalidation_dataset = tf.keras.preprocessing.image_dataset_from_directory(\n    str(train_dir),\n    validation_split=0.2,\n    subset=\"validation\",\n    seed=33,\n    image_size=IMAGE_SIZE,\n    batch_size=BATCH_SIZE,)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:36.593174Z","iopub.execute_input":"2021-09-29T08:51:36.593410Z","iopub.status.idle":"2021-09-29T08:51:39.976189Z","shell.execute_reply.started":"2021-09-29T08:51:36.593377Z","shell.execute_reply":"2021-09-29T08:51:39.974122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_dataset.class_names\n\nplt.figure(figsize=(10, 10))\nfor images, labels in train_dataset.take(1):\n    for i in range(9):\n        ax = plt.subplot(3, 3, i + 1)\n        plt.imshow(images[i].numpy().astype(\"uint8\"))\n        plt.title(class_names[labels[i]])\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:40.002486Z","iopub.execute_input":"2021-09-29T08:51:40.003269Z","iopub.status.idle":"2021-09-29T08:51:41.666109Z","shell.execute_reply.started":"2021-09-29T08:51:40.003218Z","shell.execute_reply":"2021-09-29T08:51:41.662707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = train_dataset.cache().prefetch(buffer_size=tf.data.AUTOTUNE)\nvalidation_dataset = validation_dataset.cache().prefetch(buffer_size=tf.data.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:41.667111Z","iopub.execute_input":"2021-09-29T08:51:41.667346Z","iopub.status.idle":"2021-09-29T08:51:41.674658Z","shell.execute_reply.started":"2021-09-29T08:51:41.667316Z","shell.execute_reply":"2021-09-29T08:51:41.673980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_augmentation = tf.keras.Sequential(\n  [\n    tf.keras.layers.experimental.preprocessing.RandomFlip(\"horizontal\"),\n    tf.keras.layers.experimental.preprocessing.RandomRotation(0.1),\n    tf.keras.layers.experimental.preprocessing.RandomZoom(0.1),\n  ]\n)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:41.675787Z","iopub.execute_input":"2021-09-29T08:51:41.676566Z","iopub.status.idle":"2021-09-29T08:51:41.706676Z","shell.execute_reply.started":"2021-09-29T08:51:41.676524Z","shell.execute_reply":"2021-09-29T08:51:41.705898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nfor image_batch, _ in train_dataset.take(1):\n    first_image = image_batch[0]\n    for i in range(9):\n        ax = plt.subplot(3, 3, i + 1)\n        augmented_image = data_augmentation(tf.expand_dims(first_image, 0))\n        plt.imshow(augmented_image[0].numpy().astype('uint8'))\n        plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:41.708027Z","iopub.execute_input":"2021-09-29T08:51:41.708266Z","iopub.status.idle":"2021-09-29T08:51:42.911163Z","shell.execute_reply.started":"2021-09-29T08:51:41.708236Z","shell.execute_reply":"2021-09-29T08:51:42.904609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Transfer Learning","metadata":{}},{"cell_type":"code","source":"base_model = tf.keras.applications.MobileNetV2(\n    input_shape=IMAGE_SIZE + (3,),\n    include_top=False,\n    weights='imagenet')","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:42.912484Z","iopub.execute_input":"2021-09-29T08:51:42.912943Z","iopub.status.idle":"2021-09-29T08:51:44.023352Z","shell.execute_reply.started":"2021-09-29T08:51:42.912908Z","shell.execute_reply":"2021-09-29T08:51:44.022654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_batch, label_batch = next(iter(train_dataset))\nfeature_batch = base_model(image_batch)\nprint(feature_batch.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:44.024555Z","iopub.execute_input":"2021-09-29T08:51:44.024837Z","iopub.status.idle":"2021-09-29T08:51:50.248785Z","shell.execute_reply.started":"2021-09-29T08:51:44.024804Z","shell.execute_reply":"2021-09-29T08:51:50.247867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:50.250266Z","iopub.execute_input":"2021-09-29T08:51:50.250618Z","iopub.status.idle":"2021-09-29T08:51:50.263236Z","shell.execute_reply.started":"2021-09-29T08:51:50.250567Z","shell.execute_reply":"2021-09-29T08:51:50.262419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.summary()","metadata":{"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2021-09-29T08:51:50.264668Z","iopub.execute_input":"2021-09-29T08:51:50.264933Z","iopub.status.idle":"2021-09-29T08:51:50.341335Z","shell.execute_reply.started":"2021-09-29T08:51:50.264899Z","shell.execute_reply":"2021-09-29T08:51:50.340683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"global_average_layer = tf.keras.layers.GlobalAveragePooling2D()\nfeature_batch_average = global_average_layer(feature_batch)\nprint(feature_batch_average.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:50.342679Z","iopub.execute_input":"2021-09-29T08:51:50.342940Z","iopub.status.idle":"2021-09-29T08:51:50.354464Z","shell.execute_reply.started":"2021-09-29T08:51:50.342910Z","shell.execute_reply":"2021-09-29T08:51:50.353468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_layer = tf.keras.layers.Dense(1)\nprediction_batch = prediction_layer(feature_batch_average)\nprint(prediction_batch.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:50.355887Z","iopub.execute_input":"2021-09-29T08:51:50.356129Z","iopub.status.idle":"2021-09-29T08:51:50.375977Z","shell.execute_reply.started":"2021-09-29T08:51:50.356098Z","shell.execute_reply":"2021-09-29T08:51:50.375186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocess_input = tf.keras.applications.mobilenet_v2.preprocess_input","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:50.377351Z","iopub.execute_input":"2021-09-29T08:51:50.377609Z","iopub.status.idle":"2021-09-29T08:51:50.381499Z","shell.execute_reply.started":"2021-09-29T08:51:50.377565Z","shell.execute_reply":"2021-09-29T08:51:50.380843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = tf.keras.Input(shape=IMAGE_SIZE + (3,))\nx = data_augmentation(inputs)\nx = preprocess_input(x)\nx = base_model(x, training=False)\nx = global_average_layer(x)\nx = tf.keras.layers.Dropout(0.2)(x)\noutputs = prediction_layer(x)\nmodel = tf.keras.Model(inputs, outputs)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:50.382917Z","iopub.execute_input":"2021-09-29T08:51:50.383531Z","iopub.status.idle":"2021-09-29T08:51:50.880244Z","shell.execute_reply.started":"2021-09-29T08:51:50.383496Z","shell.execute_reply":"2021-09-29T08:51:50.879547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:50.881609Z","iopub.execute_input":"2021-09-29T08:51:50.881867Z","iopub.status.idle":"2021-09-29T08:51:50.898480Z","shell.execute_reply.started":"2021-09-29T08:51:50.881836Z","shell.execute_reply":"2021-09-29T08:51:50.897669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_learning_rate = 0.0001\nmodel.compile(optimizer=tf.keras.optimizers.Adam(lr=base_learning_rate),\n              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:50.899838Z","iopub.execute_input":"2021-09-29T08:51:50.900458Z","iopub.status.idle":"2021-09-29T08:51:50.923627Z","shell.execute_reply.started":"2021-09-29T08:51:50.900418Z","shell.execute_reply":"2021-09-29T08:51:50.923049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss0, accuracy0 = model.evaluate(validation_dataset)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:50.924894Z","iopub.execute_input":"2021-09-29T08:51:50.925136Z","iopub.status.idle":"2021-09-29T08:51:59.353356Z","shell.execute_reply.started":"2021-09-29T08:51:50.925105Z","shell.execute_reply":"2021-09-29T08:51:59.352638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"initial loss: {:.2f}\".format(loss0))\nprint(\"initial accuracy: {:.2f}\".format(accuracy0))","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:59.357114Z","iopub.execute_input":"2021-09-29T08:51:59.357318Z","iopub.status.idle":"2021-09-29T08:51:59.368503Z","shell.execute_reply.started":"2021-09-29T08:51:59.357291Z","shell.execute_reply":"2021-09-29T08:51:59.367805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"EPOCHS = 20\nhistory = model.fit(train_dataset,\n                    epochs=EPOCHS,\n                    validation_data=validation_dataset)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:51:59.369596Z","iopub.execute_input":"2021-09-29T08:51:59.369890Z","iopub.status.idle":"2021-09-29T08:56:57.320966Z","shell.execute_reply.started":"2021-09-29T08:51:59.369858Z","shell.execute_reply":"2021-09-29T08:56:57.320238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nplt.figure(figsize=(8, 8))\nplt.subplot(2, 1, 1)\nplt.plot(acc, label='Training Accuracy')\nplt.plot(val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.ylabel('Accuracy')\nplt.ylim([min(plt.ylim()),1])\nplt.xticks(list(range(20)))\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(2, 1, 2)\nplt.plot(loss, label='Training Loss')\nplt.plot(val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.ylabel('Cross Entropy')\nplt.ylim([0,1.0])\nplt.title('Training and Validation Loss')\nplt.xticks(list(range(20)))\nplt.xlabel('Epoch')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:56:57.322248Z","iopub.execute_input":"2021-09-29T08:56:57.322590Z","iopub.status.idle":"2021-09-29T08:56:57.778154Z","shell.execute_reply.started":"2021-09-29T08:56:57.322553Z","shell.execute_reply":"2021-09-29T08:56:57.777496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Fine Tuning","metadata":{}},{"cell_type":"code","source":"base_model.trainable = True","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:56:57.782722Z","iopub.execute_input":"2021-09-29T08:56:57.783443Z","iopub.status.idle":"2021-09-29T08:56:57.794793Z","shell.execute_reply.started":"2021-09-29T08:56:57.783404Z","shell.execute_reply":"2021-09-29T08:56:57.794026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Number of layers in the base model: \", len(base_model.layers))","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:56:57.796028Z","iopub.execute_input":"2021-09-29T08:56:57.796703Z","iopub.status.idle":"2021-09-29T08:56:57.805540Z","shell.execute_reply.started":"2021-09-29T08:56:57.796669Z","shell.execute_reply":"2021-09-29T08:56:57.804665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fine_tune_from = 100\n\n# Freeze all the layers before the `fine_tune_at` layer\nfor layer in base_model.layers[:fine_tune_from]:\n    layer.trainable =  False","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:56:57.806786Z","iopub.execute_input":"2021-09-29T08:56:57.807246Z","iopub.status.idle":"2021-09-29T08:56:57.817093Z","shell.execute_reply.started":"2021-09-29T08:56:57.807211Z","shell.execute_reply":"2021-09-29T08:56:57.816303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n              optimizer = tf.keras.optimizers.RMSprop(lr=base_learning_rate/10),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:56:57.818424Z","iopub.execute_input":"2021-09-29T08:56:57.819337Z","iopub.status.idle":"2021-09-29T08:56:57.839620Z","shell.execute_reply.started":"2021-09-29T08:56:57.819292Z","shell.execute_reply":"2021-09-29T08:56:57.838985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:56:57.840715Z","iopub.execute_input":"2021-09-29T08:56:57.841119Z","iopub.status.idle":"2021-09-29T08:56:57.862328Z","shell.execute_reply.started":"2021-09-29T08:56:57.841085Z","shell.execute_reply":"2021-09-29T08:56:57.861747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fine_tune_epochs = 10\ntotal_epochs = EPOCHS + fine_tune_epochs\n\nhistory_fine = model.fit(train_dataset,\n                         epochs=total_epochs,\n                         initial_epoch=history.epoch[-1],\n                         validation_data=validation_dataset)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T08:56:57.863503Z","iopub.execute_input":"2021-09-29T08:56:57.863768Z","iopub.status.idle":"2021-09-29T09:00:46.632193Z","shell.execute_reply.started":"2021-09-29T08:56:57.863737Z","shell.execute_reply":"2021-09-29T09:00:46.631493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc += history_fine.history['accuracy']\nval_acc += history_fine.history['val_accuracy']\n\nloss += history_fine.history['loss']\nval_loss += history_fine.history['val_loss']","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:00:46.636346Z","iopub.execute_input":"2021-09-29T09:00:46.636554Z","iopub.status.idle":"2021-09-29T09:00:46.640673Z","shell.execute_reply.started":"2021-09-29T09:00:46.636529Z","shell.execute_reply":"2021-09-29T09:00:46.639649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\nplt.subplot(2, 1, 1)\nplt.plot(acc, label='Training Accuracy')\nplt.plot(val_acc, label='Validation Accuracy')\nplt.ylim([0.8, 1])\nplt.plot([EPOCHS-1,EPOCHS-1],\n          plt.ylim(), label='Start Fine Tuning')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(2, 1, 2)\nplt.plot(loss, label='Training Loss')\nplt.plot(val_loss, label='Validation Loss')\nplt.ylim([0, 1.0])\nplt.plot([EPOCHS-1,EPOCHS-1],\n         plt.ylim(), label='Start Fine Tuning')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.xlabel('epoch')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T09:00:46.641815Z","iopub.execute_input":"2021-09-29T09:00:46.642477Z","iopub.status.idle":"2021-09-29T09:00:47.227281Z","shell.execute_reply.started":"2021-09-29T09:00:46.642441Z","shell.execute_reply":"2021-09-29T09:00:47.226603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = tf.data.Dataset.list_files(str(test_dir / '*.jpg'))","metadata":{"execution":{"iopub.status.busy":"2021-09-29T10:25:12.358671Z","iopub.execute_input":"2021-09-29T10:25:12.359373Z","iopub.status.idle":"2021-09-29T10:25:12.429866Z","shell.execute_reply.started":"2021-09-29T10:25:12.359335Z","shell.execute_reply":"2021-09-29T10:25:12.429184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.data.Dataset.list_files(str(test_dir / '*.jpg'))","metadata":{"execution":{"iopub.status.busy":"2021-09-29T10:40:35.419011Z","iopub.execute_input":"2021-09-29T10:40:35.419902Z","iopub.status.idle":"2021-09-29T10:40:35.497438Z","shell.execute_reply.started":"2021-09-29T10:40:35.419789Z","shell.execute_reply":"2021-09-29T10:40:35.496778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reads an image from a file, decodes it into a dense tensor, and resizes it\n# to a fixed shape.\ndef parse_image(filename):\n    parts = tf.strings.split(filename, os.sep)\n    label = tf.strings.split(parts[-1], '.')[0]\n\n    image = tf.io.read_file(filename)\n    image = tf.image.decode_jpeg(image)\n    image = tf.image.convert_image_dtype(image, tf.float32)\n    image = tf.image.resize(image, IMAGE_SIZE)\n    \n    return image, label","metadata":{"execution":{"iopub.status.busy":"2021-09-29T10:25:12.902492Z","iopub.execute_input":"2021-09-29T10:25:12.903025Z","iopub.status.idle":"2021-09-29T10:25:12.912461Z","shell.execute_reply.started":"2021-09-29T10:25:12.902987Z","shell.execute_reply":"2021-09-29T10:25:12.911726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = test_dataset.map(parse_image).batch(BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T10:25:20.146875Z","iopub.execute_input":"2021-09-29T10:25:20.147651Z","iopub.status.idle":"2021-09-29T10:25:20.225988Z","shell.execute_reply.started":"2021-09-29T10:25:20.147589Z","shell.execute_reply":"2021-09-29T10:25:20.225326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nfor images, labels in test_dataset.as_numpy_iterator():\n    for i in range(9):\n        ax = plt.subplot(3, 3, i + 1)\n        plt.imshow(images[i])\n        plt.title(int(labels[i].decode('UTF-8')))\n        plt.axis(\"off\")\n    break","metadata":{"execution":{"iopub.status.busy":"2021-09-29T11:01:17.308636Z","iopub.execute_input":"2021-09-29T11:01:17.309356Z","iopub.status.idle":"2021-09-29T11:01:18.777210Z","shell.execute_reply.started":"2021-09-29T11:01:17.309318Z","shell.execute_reply":"2021-09-29T11:01:18.776517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"[] + [1,2,3] + [5,5]","metadata":{"execution":{"iopub.status.busy":"2021-09-29T11:06:39.441509Z","iopub.execute_input":"2021-09-29T11:06:39.441982Z","iopub.status.idle":"2021-09-29T11:06:39.448277Z","shell.execute_reply.started":"2021-09-29T11:06:39.441947Z","shell.execute_reply":"2021-09-29T11:06:39.447290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_ids = []\nlogits = []\npredictions = []\n\nfor image_batch, id_batch in test_dataset.as_numpy_iterator():\n    batch_predictions = model.predict_on_batch(image_batch)\n    batch_predictions = batch_predictions.flatten()\n    \n    logits += batch_predictions.tolist()\n    batch_predictions = tf.nn.sigmoid(batch_predictions)\n    \n    predictions += batch_predictions.numpy().tolist()\n    image_ids += id_batch.tolist()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T11:22:14.208482Z","iopub.execute_input":"2021-09-29T11:22:14.209182Z","iopub.status.idle":"2021-09-29T11:22:41.198676Z","shell.execute_reply.started":"2021-09-29T11:22:14.209146Z","shell.execute_reply":"2021-09-29T11:22:41.197936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'id': image_ids, 'label': predictions, 'logits': logits})","metadata":{"execution":{"iopub.status.busy":"2021-09-29T11:22:41.201015Z","iopub.execute_input":"2021-09-29T11:22:41.201265Z","iopub.status.idle":"2021-09-29T11:22:41.213633Z","shell.execute_reply.started":"2021-09-29T11:22:41.201231Z","shell.execute_reply":"2021-09-29T11:22:41.212790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.describe()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T11:22:42.278764Z","iopub.execute_input":"2021-09-29T11:22:42.279302Z","iopub.status.idle":"2021-09-29T11:22:42.303493Z","shell.execute_reply.started":"2021-09-29T11:22:42.279269Z","shell.execute_reply":"2021-09-29T11:22:42.302614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T11:24:21.176402Z","iopub.execute_input":"2021-09-29T11:24:21.176683Z","iopub.status.idle":"2021-09-29T11:24:21.188117Z","shell.execute_reply.started":"2021-09-29T11:24:21.176651Z","shell.execute_reply":"2021-09-29T11:24:21.187386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.id = submission.id.astype(int)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T11:25:06.615351Z","iopub.execute_input":"2021-09-29T11:25:06.615648Z","iopub.status.idle":"2021-09-29T11:25:06.624123Z","shell.execute_reply.started":"2021-09-29T11:25:06.615604Z","shell.execute_reply":"2021-09-29T11:25:06.623295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.info()","metadata":{"execution":{"iopub.status.busy":"2021-09-29T11:25:10.207066Z","iopub.execute_input":"2021-09-29T11:25:10.207762Z","iopub.status.idle":"2021-09-29T11:25:10.227007Z","shell.execute_reply.started":"2021-09-29T11:25:10.207722Z","shell.execute_reply":"2021-09-29T11:25:10.226251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission[['id', 'label']].to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-29T11:25:38.715559Z","iopub.execute_input":"2021-09-29T11:25:38.716080Z","iopub.status.idle":"2021-09-29T11:25:38.773215Z","shell.execute_reply.started":"2021-09-29T11:25:38.716038Z","shell.execute_reply":"2021-09-29T11:25:38.772503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}