{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom zipfile import ZipFile as zf\nimport shutil\nimport cv2\nimport matplotlib.pyplot as plt\n\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\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n        \nIMAGE_SIZE = 150\nCATAGORIES = ['Dog', 'Cat']\nCONV = (64, 3)\nMAXPOOL = 2\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":"2021-08-02T23:47:09.450899Z","iopub.execute_input":"2021-08-02T23:47:09.4513Z","iopub.status.idle":"2021-08-02T23:47:09.763723Z","shell.execute_reply.started":"2021-08-02T23:47:09.451219Z","shell.execute_reply":"2021-08-02T23:47:09.762763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from zipfile import ZipFile as zf\ntrain_zip = zf('/kaggle/input/dogs-vs-cats/train.zip', 'r')\ntest_zip = zf('/kaggle/input/dogs-vs-cats/test1.zip', 'r')\ntrain_zip.extractall()\ntest_zip.extractall()\ntrain_zip.close(), test_zip.close()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T23:47:09.765133Z","iopub.execute_input":"2021-08-02T23:47:09.765413Z","iopub.status.idle":"2021-08-02T23:47:31.074779Z","shell.execute_reply.started":"2021-08-02T23:47:09.765384Z","shell.execute_reply":"2021-08-02T23:47:31.073875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preprocessing\n\n\nDIR_PATH= '/kaggle/input/dogs-vs-cats'\nWORKING_PATH = \"\"\ntry:\n    os.mkdir(os.path.join('cats'))\n    os.mkdir(os.path.join('dogs'))\nexcept:\n    pass\n\nfor img in os.listdir(os.path.join('./train')):\n    if img.startswith('./cat'):\n        if os.path.exists(os.path.join('./cats', img)) is False:\n            shutil.copy(os.path.join('train', img), os.path.join('./cats', img))\n    else:\n        if os.path.exists(os.path.join('./dogs', img)) is False:\n            shutil.copy(os.path.join('./train', img), os.path.join('./dogs', img))\n","metadata":{"execution":{"iopub.status.busy":"2021-08-02T23:47:31.076722Z","iopub.execute_input":"2021-08-02T23:47:31.077026Z","iopub.status.idle":"2021-08-02T23:47:34.469142Z","shell.execute_reply.started":"2021-08-02T23:47:31.076996Z","shell.execute_reply":"2021-08-02T23:47:34.468192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_data = []\n\nif os.path.exists('X.p'):\n    pass\nelse:\n    for img in os.listdir('cats'):\n        img = cv2.resize(cv2.imread(os.path.join('cats', img)), (IMAGE_SIZE, IMAGE_SIZE))\n        training_data.append([img, 1])\n    for img in os.listdir('dogs'):\n        img = cv2.resize(cv2.imread(os.path.join('dogs', img)), (IMAGE_SIZE, IMAGE_SIZE))\n        training_data.append([img, 0])\n","metadata":{"execution":{"iopub.status.busy":"2021-08-02T23:47:34.470645Z","iopub.execute_input":"2021-08-02T23:47:34.47095Z","iopub.status.idle":"2021-08-02T23:48:31.381921Z","shell.execute_reply.started":"2021-08-02T23:47:34.470916Z","shell.execute_reply":"2021-08-02T23:48:31.380882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nimport pickle\n\nrandom.shuffle(training_data)\n\nif os.path.exists('X.p'):\n    pX = open('X.p', 'rb')\n    py = open('y.p', 'rb')\n    X = np.array(pickle.load(pX))\n    y = np.array(pickle.load(py))\n    pX.close(), py.close()\nelse:\n    X,y=[],[]\n    for feature, label in training_data:\n        X.append(feature)\n        y.append(label)\n\nX = np.array(X)\ny = np.array(y)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T23:48:31.383642Z","iopub.execute_input":"2021-08-02T23:48:31.384051Z","iopub.status.idle":"2021-08-02T23:48:32.403879Z","shell.execute_reply.started":"2021-08-02T23:48:31.38401Z","shell.execute_reply":"2021-08-02T23:48:32.402965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_delta = np.array(X).reshape(-1, IMAGE_SIZE, IMAGE_SIZE, 3)\nprint(X_delta.shape)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T23:48:32.405273Z","iopub.execute_input":"2021-08-02T23:48:32.405733Z","iopub.status.idle":"2021-08-02T23:48:33.189667Z","shell.execute_reply.started":"2021-08-02T23:48:32.405673Z","shell.execute_reply":"2021-08-02T23:48:33.188594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle \n\nif os.path.exists('X.p') is False:\n    p = open('X.p', 'wb')\n    pickle.dump(X, p)\n    p.close()\n    p = open('y.p', 'wb')\n    pickle.dump(y, p)\n    p.close()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T23:48:33.190881Z","iopub.execute_input":"2021-08-02T23:48:33.19117Z","iopub.status.idle":"2021-08-02T23:48:37.702349Z","shell.execute_reply.started":"2021-08-02T23:48:33.191141Z","shell.execute_reply":"2021-08-02T23:48:37.701231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow.keras.layers as layers\nfrom tensorflow import keras as ker\n\n# detect and init the TPU\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()\n\n# instantiate a distribution strategy\ntpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)\n\n\nwith tpu_strategy.scope():\n    model = ker.Sequential([\n        layers.Conv2D(CONV[0], (CONV[1],CONV[1]), activation='relu', input_shape = X.shape[1:]),\n        layers.MaxPool2D(pool_size=(MAXPOOL,MAXPOOL)),\n        layers.Conv2D(CONV[0], (CONV[1],CONV[1]), activation='relu', input_shape = X.shape[1:]),\n        layers.MaxPool2D(pool_size=(MAXPOOL,MAXPOOL)),\n        layers.Flatten(),\n        layers.Dense(CONV[0], activation='relu'),\n        layers.Dense(1, activation='sigmoid'),\n    ])\n\n    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate = 0.001), loss='binary_crossentropy', metrics=['binary_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-08-02T23:48:37.706219Z","iopub.execute_input":"2021-08-02T23:48:37.706657Z","iopub.status.idle":"2021-08-02T23:48:51.614072Z","shell.execute_reply.started":"2021-08-02T23:48:37.706598Z","shell.execute_reply":"2021-08-02T23:48:51.61301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\ntry:\n    os.mkdir('./val_set')\n    os.mkdir('./val_set/cats')\n    os.mkdir('./val_set/dogs')\nexcept:\n    pass\ntrain_datagen = ImageDataGenerator(rescale=1./255,\n                                  rotation_range = 40,\n                                  width_shift_range = 0.2,\n                                  height_shift_range = 0.2,\n                                   shear_range = 0.2,\n                                   zoom_range = 0.2,\n                                   horizontal_flip = True,\n                                   fill_mode = 'nearest'\n                                  )\ntrain_generator = train_datagen.flow_from_directory(\n    './train',\n    target_size = (150,150),\n    batch_size = 8,\n    class_mode = 'binary'\n)\nval_datagen = ImageDataGenerator(rescale=1./255)\nval_generator = val_datagen.flow_from_directory(\n    './val_set/',\n    target_size = (150,150),\n    batch_size = 8,\n    class_mode = 'binary'\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T23:48:51.616111Z","iopub.execute_input":"2021-08-02T23:48:51.616389Z","iopub.status.idle":"2021-08-02T23:48:51.990045Z","shell.execute_reply.started":"2021-08-02T23:48:51.616362Z","shell.execute_reply":"2021-08-02T23:48:51.989013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T23:48:51.99139Z","iopub.execute_input":"2021-08-02T23:48:51.991666Z","iopub.status.idle":"2021-08-02T23:48:52.002659Z","shell.execute_reply.started":"2021-08-02T23:48:51.99164Z","shell.execute_reply":"2021-08-02T23:48:52.001672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Start Training\nhistory = model.fit(X, y, epochs=3, batch_size=32, validation_data=val_generator)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T23:48:52.004084Z","iopub.execute_input":"2021-08-02T23:48:52.004368Z","iopub.status.idle":"2021-08-02T23:49:50.16086Z","shell.execute_reply.started":"2021-08-02T23:48:52.004341Z","shell.execute_reply":"2021-08-02T23:49:50.159859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt \nacc = history.history['binary_accuracy']\nval_acc = history.history['val_binary_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs_range = range(len(acc))\n\nplt.figure(figsize=(15, 15))\nplt.subplot(2, 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(2, 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":"2021-08-02T23:49:50.162072Z","iopub.execute_input":"2021-08-02T23:49:50.162339Z","iopub.status.idle":"2021-08-02T23:49:50.504427Z","shell.execute_reply.started":"2021-08-02T23:49:50.162313Z","shell.execute_reply":"2021-08-02T23:49:50.502992Z"},"trusted":true},"execution_count":null,"outputs":[]}]}