{"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":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-25T17:57:01.739589Z","iopub.execute_input":"2022-01-25T17:57:01.739988Z","iopub.status.idle":"2022-01-25T17:57:06.533105Z","shell.execute_reply.started":"2022-01-25T17:57:01.739877Z","shell.execute_reply":"2022-01-25T17:57:06.532365Z"},"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-01-25T17:57:06.534937Z","iopub.execute_input":"2022-01-25T17:57:06.535221Z","iopub.status.idle":"2022-01-25T17:57:06.548576Z","shell.execute_reply.started":"2022-01-25T17:57:06.535185Z","shell.execute_reply":"2022-01-25T17:57:06.547803Z"},"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-01-25T17:57:06.549899Z","iopub.execute_input":"2022-01-25T17:57:06.55103Z","iopub.status.idle":"2022-01-25T17:57:24.607335Z","shell.execute_reply.started":"2022-01-25T17:57:06.550992Z","shell.execute_reply":"2022-01-25T17:57:24.60651Z"},"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-01-25T17:57:24.609208Z","iopub.execute_input":"2022-01-25T17:57:24.60946Z","iopub.status.idle":"2022-01-25T17:57:24.617322Z","shell.execute_reply.started":"2022-01-25T17:57:24.609425Z","shell.execute_reply":"2022-01-25T17:57:24.61669Z"},"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-01-25T17:57:24.61957Z","iopub.execute_input":"2022-01-25T17:57:24.61989Z","iopub.status.idle":"2022-01-25T17:57:25.315649Z","shell.execute_reply.started":"2022-01-25T17:57:24.619854Z","shell.execute_reply":"2022-01-25T17:57:25.314826Z"},"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-01-25T17:57:25.316938Z","iopub.execute_input":"2022-01-25T17:57:25.317581Z","iopub.status.idle":"2022-01-25T17:57:25.322982Z","shell.execute_reply.started":"2022-01-25T17:57:25.317541Z","shell.execute_reply":"2022-01-25T17:57:25.32236Z"},"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-01-25T17:57:25.324048Z","iopub.execute_input":"2022-01-25T17:57:25.324513Z","iopub.status.idle":"2022-01-25T17:57:25.474362Z","shell.execute_reply.started":"2022-01-25T17:57:25.324476Z","shell.execute_reply":"2022-01-25T17:57:25.473719Z"},"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-01-25T17:57:25.485466Z","iopub.execute_input":"2022-01-25T17:57:25.485969Z","iopub.status.idle":"2022-01-25T17:57:29.307703Z","shell.execute_reply.started":"2022-01-25T17:57:25.485933Z","shell.execute_reply":"2022-01-25T17:57:29.306921Z"},"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-01-25T17:57:29.310577Z","iopub.execute_input":"2022-01-25T17:57:29.310824Z","iopub.status.idle":"2022-01-25T17:57:29.315869Z","shell.execute_reply.started":"2022-01-25T17:57:29.310793Z","shell.execute_reply":"2022-01-25T17:57:29.314759Z"},"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.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-01-25T17:57:29.528663Z","iopub.execute_input":"2022-01-25T17:57:29.529351Z","iopub.status.idle":"2022-01-25T19:00:31.410348Z","shell.execute_reply.started":"2022-01-25T17:57:29.529312Z","shell.execute_reply":"2022-01-25T19:00:31.409581Z"},"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-01-25T19:00:31.413319Z","iopub.execute_input":"2022-01-25T19:00:31.413516Z","iopub.status.idle":"2022-01-25T19:00:31.87058Z","shell.execute_reply.started":"2022-01-25T19:00:31.413491Z","shell.execute_reply":"2022-01-25T19:00:31.86986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# history_frame = pd.DataFrame(history.history)\n# history_frame\n\n#test processing\npred_labels = model.predict(test_ds, batch_size=batch_size)\npred_str_labels = ['dog' if num > 0.5 else 'cat' for num in pred_labels]\n# pred_str_labels[:10]","metadata":{"execution":{"iopub.status.busy":"2022-01-25T19:05:44.840883Z","iopub.execute_input":"2022-01-25T19:05:44.841172Z","iopub.status.idle":"2022-01-25T19:06:11.26399Z","shell.execute_reply.started":"2022-01-25T19:05:44.841115Z","shell.execute_reply":"2022-01-25T19:06:11.263313Z"},"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-01-25T19:03:50.955404Z","iopub.execute_input":"2022-01-25T19:03:50.955674Z","iopub.status.idle":"2022-01-25T19:03:50.959743Z","shell.execute_reply.started":"2022-01-25T19:03:50.95563Z","shell.execute_reply":"2022-01-25T19:03:50.958973Z"},"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-01-25T19:03:56.790755Z","iopub.execute_input":"2022-01-25T19:03:56.791053Z","iopub.status.idle":"2022-01-25T19:03:56.795034Z","shell.execute_reply.started":"2022-01-25T19:03:56.791023Z","shell.execute_reply":"2022-01-25T19:03:56.794091Z"},"trusted":true},"execution_count":null,"outputs":[]}]}