{"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 os\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport shutil\nimport matplotlib\nimport matplotlib.pyplot as plt\n\nimport tensorflow as tf\nfrom tensorflow.keras import layers, callbacks, regularizers\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers.experimental import preprocessing\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\n\n# for 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":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2022-02-01T00:47:01.824807Z","iopub.execute_input":"2022-02-01T00:47:01.825073Z","iopub.status.idle":"2022-02-01T00:47:01.832217Z","shell.execute_reply.started":"2022-02-01T00:47:01.825042Z","shell.execute_reply":"2022-02-01T00:47:01.831342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input/'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-02-01T00:47:01.833821Z","iopub.execute_input":"2022-02-01T00:47:01.834468Z","iopub.status.idle":"2022-02-01T00:47:01.850550Z","shell.execute_reply.started":"2022-02-01T00:47:01.834421Z","shell.execute_reply":"2022-02-01T00:47:01.849670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract zip files\nimport zipfile\ndef extract_images(filePath):\n    with zipfile.ZipFile(filePath,\"r\") as z:\n        z.extractall(\"/kaggle/working\")\n# Test\nextract_images('/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip')\nextract_images('/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip')","metadata":{"execution":{"iopub.status.busy":"2022-02-01T00:47:01.852408Z","iopub.execute_input":"2022-02-01T00:47:01.852756Z","iopub.status.idle":"2022-02-01T00:47:14.272597Z","shell.execute_reply.started":"2022-02-01T00:47:01.852721Z","shell.execute_reply":"2022-02-01T00:47:14.271854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cats_folder = '/kaggle/working/train/cats/'\ndogs_folder = '/kaggle/working/train/dogs/'\n\nif os.path.exists(cats_folder):\n    shutil.rmtree(cats_folder)\n    \nif os.path.exists(dogs_folder):\n    shutil.rmtree(dogs_folder)\n\nos.mkdir(cats_folder)\nos.mkdir(dogs_folder)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T00:47:14.275859Z","iopub.execute_input":"2022-02-01T00:47:14.276077Z","iopub.status.idle":"2022-02-01T00:47:14.928285Z","shell.execute_reply.started":"2022-02-01T00:47:14.276051Z","shell.execute_reply":"2022-02-01T00:47:14.927519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"source = '/kaggle/working/train/'\n  \nallfiles = os.listdir(source)\n  \n# print(len(allfiles))\nfor f in allfiles:\n    if os.path.isfile(source + f):\n        if 'cat' in f:\n            shutil.move(source + f, cats_folder + f)\n        if 'dog' in f:\n            shutil.move(source + f, dogs_folder + f)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T00:47:14.929947Z","iopub.execute_input":"2022-02-01T00:47:14.930294Z","iopub.status.idle":"2022-02-01T00:47:15.927569Z","shell.execute_reply.started":"2022-02-01T00:47:14.930225Z","shell.execute_reply":"2022-02-01T00:47:15.926827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_folder = '/kaggle/working/valid'\nvalid_cats_folder = '/kaggle/working/valid/cats'\nvalid_dogs_folder = '/kaggle/working/valid/dogs'\n\nif os.path.exists(valid_folder):\n    shutil.rmtree(valid_folder)\n\nos.mkdir(valid_folder)\nos.mkdir(valid_cats_folder)\nos.mkdir(valid_dogs_folder)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T00:47:15.931872Z","iopub.execute_input":"2022-02-01T00:47:15.932512Z","iopub.status.idle":"2022-02-01T00:47:16.134463Z","shell.execute_reply.started":"2022-02-01T00:47:15.932473Z","shell.execute_reply":"2022-02-01T00:47:16.133772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for f in os.listdir(cats_folder)[10000:]:\n    file_path = cats_folder + '/' + f\n    if os.path.isfile(file_path):\n        shutil.move(file_path, valid_cats_folder + '/' + f)\n        \nfor f in os.listdir(dogs_folder)[10000:]:\n    file_path = dogs_folder + '/' + f\n    if os.path.isfile(file_path):\n        shutil.move(file_path, valid_dogs_folder + '/' + f)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T00:47:16.135670Z","iopub.execute_input":"2022-02-01T00:47:16.135938Z","iopub.status.idle":"2022-02-01T00:47:16.285555Z","shell.execute_reply.started":"2022-02-01T00:47:16.135894Z","shell.execute_reply":"2022-02-01T00:47:16.284903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '/kaggle/working/train'\nvalidation_dir = '/kaggle/working/valid'\ntest_dir = '/kaggle/working/test'\n\nbatch_size = 64\nimg_height = 256\nimg_width = 256\n\ntrain_ds = tf.keras.utils.image_dataset_from_directory(\n  train_dir,\n  seed=123,\n  labels='inferred',\n  label_mode='binary',\n  image_size=(img_height, img_width),\n  batch_size=batch_size,\n  interpolation='nearest',\n  shuffle=True)\n    \n\nval_ds = tf.keras.utils.image_dataset_from_directory(\n  validation_dir,\n  seed=123,\n  labels='inferred',\n  label_mode='binary',\n  image_size=(img_height, img_width),\n  batch_size=batch_size,\n  interpolation='nearest',\n  shuffle=False)\n\ntest_ds = tf.keras.utils.image_dataset_from_directory(\n  test_dir,\n  seed=123,\n  labels=None,\n  image_size=(img_height, img_width),\n  batch_size=batch_size,\n  interpolation='nearest',\n  shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T00:47:16.286775Z","iopub.execute_input":"2022-02-01T00:47:16.287019Z","iopub.status.idle":"2022-02-01T00:47:17.587780Z","shell.execute_reply.started":"2022-02-01T00:47:16.286983Z","shell.execute_reply":"2022-02-01T00:47:17.587100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_ds.class_names\nprint(class_names)\n\ntest_class_names = test_ds.class_names\nprint(test_class_names)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T00:47:17.588985Z","iopub.execute_input":"2022-02-01T00:47:17.589248Z","iopub.status.idle":"2022-02-01T00:47:17.594342Z","shell.execute_reply.started":"2022-02-01T00:47:17.589212Z","shell.execute_reply":"2022-02-01T00:47:17.593557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = len(class_names)\n\n# early_stopping = callbacks.EarlyStopping(\n#     min_delta=0.001, # minimium amount of change to count as an improvement\n#     patience=7, # how many epochs to wait before stopping\n#     restore_best_weights=True,\n# )\n#, kernel_regularizer=regularizers.l2(0.01)\n\nmodel = Sequential([\n    layers.InputLayer(input_shape=[img_height, img_width, 3]),\n    preprocessing.RandomContrast(factor=0.5),\n    preprocessing.RandomFlip(mode='horizontal_and_vertical'),\n    preprocessing.RandomRotation(0.20),\n    layers.Rescaling(1./255),\n    layers.BatchNormalization(),\n    layers.Conv2D(filters=32, kernel_size=5, activation=\"relu\", padding='same'),\n    layers.BatchNormalization(),\n    layers.MaxPool2D(),\n    layers.Dropout(0.1),    \n    layers.Conv2D(filters=64, kernel_size=3, activation=\"relu\", padding='same'),\n    layers.BatchNormalization(),\n    layers.MaxPool2D(),\n    layers.Dropout(0.1),    \n    layers.Conv2D(filters=128, kernel_size=3, activation=\"relu\", padding='same'),\n    layers.Conv2D(filters=128, kernel_size=3, activation=\"relu\", padding='same'),\n    layers.BatchNormalization(),\n    layers.MaxPool2D(),\n    layers.Dropout(0.1), \n    layers.Conv2D(filters=256, kernel_size=3, activation=\"relu\", padding='same'),\n    layers.BatchNormalization(),\n    layers.MaxPool2D(),\n    layers.Dropout(0.1), \n    layers.Flatten(),\n    layers.Dense(units=8, activation=\"relu\"),\n    layers.Dense(units=1, activation=\"sigmoid\")\n])\n\nmodel.compile(\n    optimizer=tf.keras.optimizers.Adam(epsilon=0.01),\n    loss='binary_crossentropy',\n    metrics=['binary_accuracy']\n)\n\n\nmodel.summary()\n\nepochs=50\nhistory = model.fit(\n  train_ds,\n  validation_data=val_ds,\n  epochs=epochs,\n#   callbacks=[early_stopping]\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T00:47:17.597052Z","iopub.execute_input":"2022-02-01T00:47:17.597808Z","iopub.status.idle":"2022-02-01T00:54:25.677964Z","shell.execute_reply.started":"2022-02-01T00:47:17.597769Z","shell.execute_reply":"2022-02-01T00:54:25.675422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history_frame = pd.DataFrame(history.history)\n# history_frame\nmodel.evaluate(val_ds)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T00:54:25.680472Z","iopub.status.idle":"2022-02-01T00:54:25.685440Z","shell.execute_reply.started":"2022-02-01T00:54:25.685201Z","shell.execute_reply":"2022-02-01T00:54:25.685230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['binary_accuracy']\nval_acc = history.history['val_binary_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(epochs)\n\nplt.figure(figsize=(8, 8))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-01T00:54:25.686523Z","iopub.status.idle":"2022-02-01T00:54:25.686905Z","shell.execute_reply.started":"2022-02-01T00:54:25.686697Z","shell.execute_reply":"2022-02-01T00:54:25.686720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(300,300))\n# for img in test_ds.take(1):\n#     for i in range(64):\n#         ax = plt.subplot(64, 1, i + 1)\n#         plt.imshow(img[i].numpy().astype(\"uint8\"))\n#         plt.title(pred_str_labels[i])\n#         plt.axis(\"off\")\n#     plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-01T00:54:25.687733Z","iopub.status.idle":"2022-02-01T00:54:25.688124Z","shell.execute_reply.started":"2022-02-01T00:54:25.687892Z","shell.execute_reply":"2022-02-01T00:54:25.687913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_labels = model.predict(test_ds, batch_size=batch_size)\npred_str_labels = ['dog' if num > 0.5 else 'cat' for num in pred_labels]","metadata":{"execution":{"iopub.status.busy":"2022-02-01T00:54:25.689763Z","iopub.status.idle":"2022-02-01T00:54:25.693386Z","shell.execute_reply.started":"2022-02-01T00:54:25.693154Z","shell.execute_reply":"2022-02-01T00:54:25.693179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = os.listdir(\"/kaggle/working/test\")\nsubmission_df = pd.read_csv('/kaggle/input/dogs-vs-cats-redux-kernels-edition/sample_submission.csv')\nfor i, fname in enumerate(test_filenames):\n    index = int(fname[fname.rfind('/')+1:fname.rfind('.')])\n    submission_df.at[index-1, 'label'] = pred_labels[index-1]\nsubmission_df.to_csv('/kaggle/working/submission.csv', index=False)\nsubmission_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-01T00:54:25.696621Z","iopub.status.idle":"2022-02-01T00:54:25.697156Z","shell.execute_reply.started":"2022-02-01T00:54:25.696893Z","shell.execute_reply":"2022-02-01T00:54:25.696920Z"},"trusted":true},"execution_count":null,"outputs":[]}]}