{"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\nimport tensorflow_hub as hub\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-02-01T20:36:09.542102Z","iopub.execute_input":"2022-02-01T20:36:09.542472Z","iopub.status.idle":"2022-02-01T20:36:14.687053Z","shell.execute_reply.started":"2022-02-01T20:36:09.542390Z","shell.execute_reply":"2022-02-01T20:36:14.686336Z"},"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-01T20:36:14.688856Z","iopub.execute_input":"2022-02-01T20:36:14.689111Z","iopub.status.idle":"2022-02-01T20:36:14.697068Z","shell.execute_reply.started":"2022-02-01T20:36:14.689078Z","shell.execute_reply":"2022-02-01T20:36:14.696109Z"},"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-01T20:36:14.698718Z","iopub.execute_input":"2022-02-01T20:36:14.698990Z","iopub.status.idle":"2022-02-01T20:36:31.883707Z","shell.execute_reply.started":"2022-02-01T20:36:14.698936Z","shell.execute_reply":"2022-02-01T20:36:31.882992Z"},"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-01T20:36:31.886075Z","iopub.execute_input":"2022-02-01T20:36:31.886589Z","iopub.status.idle":"2022-02-01T20:36:31.892105Z","shell.execute_reply.started":"2022-02-01T20:36:31.886551Z","shell.execute_reply":"2022-02-01T20:36:31.891355Z"},"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-01T20:36:31.894910Z","iopub.execute_input":"2022-02-01T20:36:31.895310Z","iopub.status.idle":"2022-02-01T20:36:32.694772Z","shell.execute_reply.started":"2022-02-01T20:36:31.895280Z","shell.execute_reply":"2022-02-01T20:36:32.691934Z"},"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-01T20:36:32.696004Z","iopub.execute_input":"2022-02-01T20:36:32.699448Z","iopub.status.idle":"2022-02-01T20:36:32.714446Z","shell.execute_reply.started":"2022-02-01T20:36:32.699409Z","shell.execute_reply":"2022-02-01T20:36:32.710595Z"},"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-01T20:36:32.719279Z","iopub.execute_input":"2022-02-01T20:36:32.719821Z","iopub.status.idle":"2022-02-01T20:36:32.959759Z","shell.execute_reply.started":"2022-02-01T20:36:32.719757Z","shell.execute_reply":"2022-02-01T20:36:32.958834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def list_files(startpath):\n#     for root, dirs, files in os.walk(startpath):\n#         level = root.replace(startpath, '').count(os.sep)\n#         indent = ' ' * 4 * (level)\n#         print('{}{}/'.format(indent, os.path.basename(root)))\n#         subindent = ' ' * 4 * (level + 1)\n#         for f in files:\n#             print('{}{}'.format(subindent, f))\n       \n    \n# src_dir = '/kaggle/working/train'\n# val_dir = '/kaggle/working/valid'\n# valid_cats_dir = '/kaggle/working/valid/cats'\n# valid_dogs_dir = '/kaggle/working/valid/dogs'\n# cats_dir = '/kaggle/working/train/cats'\n# dogs_dir = '/kaggle/working/train/dogs'\n# test_dir = '/kaggle/working/test'\n# cats_test_dir = '/kaggle/working/test/cats'\n# dogs_test_dir = '/kaggle/working/test/dogs'\n    \n# list_files(test_dir)\n\n# allfiles = os.listdir(cats_dir)\n# print(len(allfiles))\n  \n# for f in allfiles:\n#     if os.path.isfile(f):\n#         os.remove(source + f)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T20:36:32.964648Z","iopub.execute_input":"2022-02-01T20:36:32.964925Z","iopub.status.idle":"2022-02-01T20:36:32.985635Z","shell.execute_reply.started":"2022-02-01T20:36:32.964890Z","shell.execute_reply":"2022-02-01T20:36:32.978617Z"},"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 = 16\nimg_height = 224\nimg_width = 224\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-01T20:36:32.987788Z","iopub.execute_input":"2022-02-01T20:36:32.988546Z","iopub.status.idle":"2022-02-01T20:36:36.911980Z","shell.execute_reply.started":"2022-02-01T20:36:32.988507Z","shell.execute_reply":"2022-02-01T20:36:36.910441Z"},"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-01T20:36:36.913322Z","iopub.execute_input":"2022-02-01T20:36:36.916514Z","iopub.status.idle":"2022-02-01T20:36:36.930739Z","shell.execute_reply.started":"2022-02-01T20:36:36.913806Z","shell.execute_reply":"2022-02-01T20:36:36.929281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# AUTOTUNE = tf.data.AUTOTUNE\n\n# train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)\n# val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T20:36:36.932714Z","iopub.execute_input":"2022-02-01T20:36:36.933006Z","iopub.status.idle":"2022-02-01T20:36:36.938338Z","shell.execute_reply.started":"2022-02-01T20:36:36.932968Z","shell.execute_reply":"2022-02-01T20:36:36.937201Z"},"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\n# model = 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\n# model.compile(\n#     optimizer=tf.keras.optimizers.Adam(epsilon=0.01),\n#     loss='binary_crossentropy',\n#     metrics=['binary_accuracy']\n# )\n\n\n# model.summary()\n\n# epochs=50\n# history = 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-01T20:36:36.939796Z","iopub.execute_input":"2022-02-01T20:36:36.940226Z","iopub.status.idle":"2022-02-01T20:36:36.947798Z","shell.execute_reply.started":"2022-02-01T20:36:36.940189Z","shell.execute_reply":"2022-02-01T20:36:36.947101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_incepion_v3_url = 'https://tfhub.dev/google/imagenet/inception_v3/feature_vector/5'\ndo_fine_tuning = False\n\nmodel = tf.keras.Sequential([\n    tf.keras.layers.InputLayer(input_shape=(img_height, img_width, 3)),\n    hub.KerasLayer(model_incepion_v3_url, trainable=do_fine_tuning),\n    tf.keras.layers.Dropout(0.2),\n    tf.keras.layers.Dense(64, activation='relu'),\n    tf.keras.layers.Dense(32, activation='relu'),\n    tf.keras.layers.Dense(1, activation='sigmoid')\n])\nmodel.build((None, img_height, img_width, 3))\nmodel.summary()\n\n# for layer in model.layers:\n#     print(layer)","metadata":{"execution":{"iopub.status.busy":"2022-02-01T20:36:36.948990Z","iopub.execute_input":"2022-02-01T20:36:36.949396Z","iopub.status.idle":"2022-02-01T20:36:38.886792Z","shell.execute_reply.started":"2022-02-01T20:36:36.949343Z","shell.execute_reply":"2022-02-01T20:36:38.884820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n  optimizer=tf.keras.optimizers.Adam(epsilon=3e-4), \n  loss='binary_crossentropy',\n  metrics=['binary_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-02-01T20:36:38.888168Z","iopub.status.idle":"2022-02-01T20:36:38.888848Z","shell.execute_reply.started":"2022-02-01T20:36:38.888598Z","shell.execute_reply":"2022-02-01T20:36:38.888626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# steps_per_epoch = 50 #len(train_ds) // batch_size\n# validation_steps = 3 #len(val_ds) // batch_size\nhistory = model.fit(\n    train_ds,\n    epochs=30, \n#     steps_per_epoch=steps_per_epoch,\n#     validation_steps=validation_steps,\n    validation_data=val_ds).history","metadata":{"execution":{"iopub.status.busy":"2022-02-01T20:36:38.890320Z","iopub.status.idle":"2022-02-01T20:36:38.891024Z","shell.execute_reply.started":"2022-02-01T20:36:38.890748Z","shell.execute_reply":"2022-02-01T20:36:38.890776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history['binary_accuracy']\nval_acc = history['val_binary_accuracy']\n\nloss = history['loss']\nval_loss = history['val_loss']\n\nepochs_range = range(30)\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-01T20:36:38.892341Z","iopub.status.idle":"2022-02-01T20:36:38.893052Z","shell.execute_reply.started":"2022-02-01T20:36:38.892777Z","shell.execute_reply":"2022-02-01T20:36:38.892805Z"},"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]\npred_str_labels[:10]","metadata":{"execution":{"iopub.status.busy":"2022-02-01T20:36:38.894411Z","iopub.status.idle":"2022-02-01T20:36:38.895089Z","shell.execute_reply.started":"2022-02-01T20:36:38.894814Z","shell.execute_reply":"2022-02-01T20:36:38.894840Z"},"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-01T20:36:38.896446Z","iopub.status.idle":"2022-02-01T20:36:38.897156Z","shell.execute_reply.started":"2022-02-01T20:36:38.896884Z","shell.execute_reply":"2022-02-01T20:36:38.896911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_ids = list()\n\n# allfiles = os.listdir(test_dir)\n# for f in allfiles:\n#     test_ids.append(f)\n\n# np.savetxt(\n#     '/kaggle/working/submission.csv',\n#     np.rec.fromarrays([test_ids, pred_str_labels]),\n#     fmt=['%s', '%d'],\n#     delimiter=',',\n#     header='id,label',\n#     comments='',\n# )\n\ntest_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-01T20:36:38.898503Z","iopub.status.idle":"2022-02-01T20:36:38.899199Z","shell.execute_reply.started":"2022-02-01T20:36:38.898925Z","shell.execute_reply":"2022-02-01T20:36:38.898967Z"},"trusted":true},"execution_count":null,"outputs":[]}]}