{"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 matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings(\"ignore\")\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# 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-06-27T21:46:08.055011Z","iopub.execute_input":"2021-06-27T21:46:08.055757Z","iopub.status.idle":"2021-06-27T21:46:08.067565Z","shell.execute_reply.started":"2021-06-27T21:46:08.055710Z","shell.execute_reply":"2021-06-27T21:46:08.066203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Importing Libraries","metadata":{}},{"cell_type":"code","source":"import os # to get the file paths and everything\nimport zipfile # to unzip the folder\nimport tensorflow as tf # for everything\nimport random # To shufle the images\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator # to automatically label the images\nfrom tensorflow.keras.optimizers import RMSprop # Optimizer\nfrom shutil import copyfile # to copy the files from one folder to another folder\n","metadata":{"execution":{"iopub.status.busy":"2021-06-27T21:46:08.069148Z","iopub.execute_input":"2021-06-27T21:46:08.069442Z","iopub.status.idle":"2021-06-27T21:46:08.081909Z","shell.execute_reply.started":"2021-06-27T21:46:08.069412Z","shell.execute_reply":"2021-06-27T21:46:08.080732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Extracting the files from the zipped folder","metadata":{}},{"cell_type":"code","source":"local_zip1 = '/kaggle/input/dogs-vs-cats/'\ndirs = [\"train.zip\", \"test1.zip\"]\nfor directory in dirs:\n    local_zip = os.path.join(local_zip1, directory)\n    zip_ref = zipfile.ZipFile(local_zip, 'r')\n    zip_ref.extractall('/kaggle/working/dogs-vs-cats/')\n    zip_ref.close()","metadata":{"execution":{"iopub.status.busy":"2021-06-27T21:46:08.083582Z","iopub.execute_input":"2021-06-27T21:46:08.084019Z","iopub.status.idle":"2021-06-27T21:46:22.049488Z","shell.execute_reply.started":"2021-06-27T21:46:08.083989Z","shell.execute_reply":"2021-06-27T21:46:22.048464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Sanity Check for the images in the directories","metadata":{}},{"cell_type":"code","source":"# Sanity check to get all the file names in the train  and test directory\nfor directory in [\"train\", \"test1\"]:\n    path = os.path.join(\"/kaggle/working/dogs-vs-cats/\", directory)\n    for dirname, _, filenames in os.walk(path):\n        for filename in filenames:\n            print(os.path.join(dirname, filename))\n            break","metadata":{"execution":{"iopub.status.busy":"2021-06-27T21:46:22.051166Z","iopub.execute_input":"2021-06-27T21:46:22.051544Z","iopub.status.idle":"2021-06-27T21:46:22.125469Z","shell.execute_reply.started":"2021-06-27T21:46:22.051498Z","shell.execute_reply":"2021-06-27T21:46:22.124369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Getting the length of files in directories","metadata":{}},{"cell_type":"code","source":"print(len(os.listdir('/kaggle/working/dogs-vs-cats/train')))\nprint(len(os.listdir('/kaggle/working/dogs-vs-cats/test1')))","metadata":{"execution":{"iopub.status.busy":"2021-06-27T21:46:22.126759Z","iopub.execute_input":"2021-06-27T21:46:22.127017Z","iopub.status.idle":"2021-06-27T21:46:22.151177Z","shell.execute_reply.started":"2021-06-27T21:46:22.126992Z","shell.execute_reply":"2021-06-27T21:46:22.150053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Creating new directories so to make life easier for labeling","metadata":{}},{"cell_type":"code","source":"# Creating the directory\ntry:\n    os.mkdir('/kaggle/working/dogs-vs-cats/train/cat')\n    os.mkdir('/kaggle/working/dogs-vs-cats/train/dog')\nexcept OSError:\n    pass","metadata":{"execution":{"iopub.status.busy":"2021-06-27T21:46:22.152562Z","iopub.execute_input":"2021-06-27T21:46:22.152923Z","iopub.status.idle":"2021-06-27T21:46:22.157028Z","shell.execute_reply.started":"2021-06-27T21:46:22.152883Z","shell.execute_reply":"2021-06-27T21:46:22.156194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### These are the directories","metadata":{}},{"cell_type":"code","source":"CATDIR = '/kaggle/working/dogs-vs-cats/train/cat'\nDOGDIR = '/kaggle/working/dogs-vs-cats/train/dog'","metadata":{"execution":{"iopub.status.busy":"2021-06-27T21:46:22.158440Z","iopub.execute_input":"2021-06-27T21:46:22.158769Z","iopub.status.idle":"2021-06-27T21:46:22.169983Z","shell.execute_reply.started":"2021-06-27T21:46:22.158679Z","shell.execute_reply":"2021-06-27T21:46:22.168656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Move the images from the original dataset to cat and dogs directory","metadata":{}},{"cell_type":"code","source":"for file in os.listdir(\"/kaggle/working/dogs-vs-cats/train\"):\n    if \"cat\" in file:\n        copyfile((os.path.join(dirname, filename)),(os.path.join(CATDIR, file)))\n    elif \"dog\" in file:\n        copyfile((os.path.join(dirname, filename)),(os.path.join(DOGDIR, file)))","metadata":{"execution":{"iopub.status.busy":"2021-06-27T21:46:22.171299Z","iopub.execute_input":"2021-06-27T21:46:22.171675Z","iopub.status.idle":"2021-06-27T21:46:26.970854Z","shell.execute_reply.started":"2021-06-27T21:46:22.171641Z","shell.execute_reply":"2021-06-27T21:46:26.969947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Sanity check for lengths","metadata":{}},{"cell_type":"code","source":"print(len(os.listdir(CATDIR)))\nprint(len(os.listdir(DOGDIR)))","metadata":{"execution":{"iopub.status.busy":"2021-06-27T21:46:26.972979Z","iopub.execute_input":"2021-06-27T21:46:26.973311Z","iopub.status.idle":"2021-06-27T21:46:26.989882Z","shell.execute_reply.started":"2021-06-27T21:46:26.973279Z","shell.execute_reply":"2021-06-27T21:46:26.988974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Creating Directories for validation, training and further cats and dogs directories inside the earlier two directories.","metadata":{}},{"cell_type":"code","source":"DIRS = ['/kaggle/working/cats-v-dogs',\n        '/kaggle/working/cats-v-dogs/training',\n        '/kaggle/working/cats-v-dogs/validation',\n        '/kaggle/working/cats-v-dogs/training/cats',\n        '/kaggle/working/cats-v-dogs/training/dogs',\n        '/kaggle/working/cats-v-dogs/validation/cats',\n        '/kaggle/working/cats-v-dogs/validation/dogs']","metadata":{"execution":{"iopub.status.busy":"2021-06-27T21:50:46.157683Z","iopub.execute_input":"2021-06-27T21:50:46.158017Z","iopub.status.idle":"2021-06-27T21:50:46.163217Z","shell.execute_reply.started":"2021-06-27T21:50:46.157989Z","shell.execute_reply":"2021-06-27T21:50:46.161706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for directory in DIRS:\n    try:\n        os.mkdir(directory)\n    except OSError:\n        pass","metadata":{"execution":{"iopub.status.busy":"2021-06-27T21:50:46.617217Z","iopub.execute_input":"2021-06-27T21:50:46.617625Z","iopub.status.idle":"2021-06-27T21:50:46.622942Z","shell.execute_reply.started":"2021-06-27T21:50:46.617590Z","shell.execute_reply":"2021-06-27T21:50:46.621739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Splitting the data","metadata":{}},{"cell_type":"code","source":"#split some part of traindata into validation set\ndef split_data(SOURCE, TRAINING, TESTING, SPLIT_SIZE):\n    files = []\n    \n    for file_name in os.listdir(SOURCE):\n        file_path = os.path.join(SOURCE, file_name)\n        \n        if os.path.getsize(file_path):\n            files.append(file_name)\n    \n    length_of_files = len(files)\n    split_point = int(length_of_files * SPLIT_SIZE)\n    \n    shuffled = random.sample(files, length_of_files)\n    \n    train_set = shuffled[:split_point]\n    test_set = shuffled[split_point:]\n    \n    for file_name in train_set:\n        copyfile(SOURCE + file_name, TRAINING + file_name)\n        \n    for file_name in test_set:\n        copyfile(SOURCE + file_name, TESTING + file_name)","metadata":{"execution":{"iopub.status.busy":"2021-06-27T22:04:31.650117Z","iopub.execute_input":"2021-06-27T22:04:31.650447Z","iopub.status.idle":"2021-06-27T22:04:31.657063Z","shell.execute_reply.started":"2021-06-27T22:04:31.650417Z","shell.execute_reply":"2021-06-27T22:04:31.655887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CAT_SOURCE_DIR = \"/kaggle/working/dogs-vs-cats/train/cat/\"\nTRAINING_CATS_DIR = \"/kaggle/working/cats-v-dogs/training/cats/\"\nTESTING_CATS_DIR = \"/kaggle/working/cats-v-dogs/validation/cats/\"\nDOG_SOURCE_DIR = \"/kaggle/working/dogs-vs-cats/train/dog/\"\nTRAINING_DOGS_DIR = \"/kaggle/working/cats-v-dogs/training/dogs/\"\nTESTING_DOGS_DIR = \"/kaggle/working/cats-v-dogs/validation/dogs/\"\n\nsplit_size = .9\nsplit_data(CAT_SOURCE_DIR, TRAINING_CATS_DIR, TESTING_CATS_DIR, split_size)\nsplit_data(DOG_SOURCE_DIR, TRAINING_DOGS_DIR, TESTING_DOGS_DIR, split_size)","metadata":{"execution":{"iopub.status.busy":"2021-06-27T22:04:32.526893Z","iopub.execute_input":"2021-06-27T22:04:32.527357Z","iopub.status.idle":"2021-06-27T22:04:35.171308Z","shell.execute_reply.started":"2021-06-27T22:04:32.527325Z","shell.execute_reply":"2021-06-27T22:04:35.170541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Sanity Check for the number of files in training set","metadata":{}},{"cell_type":"code","source":"print(len(os.listdir('/kaggle/working/cats-v-dogs/training/cats/')))\nprint(len(os.listdir('/kaggle/working/cats-v-dogs/training/dogs/')))\nprint(len(os.listdir('/kaggle/working/cats-v-dogs/validation/cats/')))\nprint(len(os.listdir('/kaggle/working/cats-v-dogs/validation/dogs/')))","metadata":{"execution":{"iopub.status.busy":"2021-06-27T22:04:44.914491Z","iopub.execute_input":"2021-06-27T22:04:44.915088Z","iopub.status.idle":"2021-06-27T22:04:44.935607Z","shell.execute_reply.started":"2021-06-27T22:04:44.915052Z","shell.execute_reply":"2021-06-27T22:04:44.934346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Model","metadata":{}},{"cell_type":"code","source":"model  = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(16, (3,3), activation = \"relu\", input_shape =(150, 150, 3)),\n    tf.keras.layers.MaxPool2D(2,2),\n    tf.keras.layers.Conv2D(32, (3,3), activation = \"relu\"),\n    tf.keras.layers.MaxPool2D(2,2),\n    tf.keras.layers.Conv2D(64, (3,3), activation = \"relu\"),\n    tf.keras.layers.MaxPool2D(2,2),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(512, activation = \"relu\"),\n    tf.keras.layers.Dense(1, activation = \"sigmoid\")\n])","metadata":{"execution":{"iopub.status.busy":"2021-06-27T22:10:11.728986Z","iopub.execute_input":"2021-06-27T22:10:11.729506Z","iopub.status.idle":"2021-06-27T22:10:12.035542Z","shell.execute_reply.started":"2021-06-27T22:10:11.729458Z","shell.execute_reply":"2021-06-27T22:10:12.034209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Compile the Model","metadata":{}},{"cell_type":"code","source":"model.compile(\n    optimizer=RMSprop(learning_rate=0.001),\n    loss='binary_crossentropy',\n    metrics=['accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2021-06-27T22:10:12.864482Z","iopub.execute_input":"2021-06-27T22:10:12.864864Z","iopub.status.idle":"2021-06-27T22:10:12.882010Z","shell.execute_reply.started":"2021-06-27T22:10:12.864819Z","shell.execute_reply":"2021-06-27T22:10:12.880611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Making use of Imagegenerator","metadata":{}},{"cell_type":"code","source":"TRAINING_DIR = '/kaggle/working/cats-v-dogs/training/'\ntrain_datagen = ImageDataGenerator( rescale = 1.0/255. )\n\ntrain_generator = train_datagen.flow_from_directory(TRAINING_DIR,\n                                                    batch_size=20,\n                                                    class_mode='binary',\n                                                    target_size=(150, 150))\n\nVALIDATION_DIR = '/kaggle/working/cats-v-dogs/validation/'\nvalidation_datagen = ImageDataGenerator( rescale = 1.0/255. )\n\nvalidation_generator = validation_datagen.flow_from_directory(VALIDATION_DIR,\n                                                         batch_size=20,\n                                                         class_mode  = 'binary',\n                                                         target_size = (150, 150))","metadata":{"execution":{"iopub.status.busy":"2021-06-27T22:10:58.004177Z","iopub.execute_input":"2021-06-27T22:10:58.004899Z","iopub.status.idle":"2021-06-27T22:10:58.563325Z","shell.execute_reply.started":"2021-06-27T22:10:58.004847Z","shell.execute_reply":"2021-06-27T22:10:58.562100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Fit the Model","metadata":{}},{"cell_type":"code","source":"history = model.fit(train_generator,\n                    epochs=5,\n                    verbose=1,\n                    validation_data=validation_generator)","metadata":{"execution":{"iopub.status.busy":"2021-06-27T22:11:30.853891Z","iopub.execute_input":"2021-06-27T22:11:30.854285Z","iopub.status.idle":"2021-06-27T22:39:39.920522Z","shell.execute_reply.started":"2021-06-27T22:11:30.854246Z","shell.execute_reply":"2021-06-27T22:39:39.919774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Predicting images in testset\n%matplotlib inline\n\nimport matplotlib.image as mpimg\nimport matplotlib.pyplot as plt\nfrom keras.preprocessing import image\n\ncount = 10\nfor filename in os.listdir('/kaggle/working/dogs-vs-cats/test1'):\n    #Get the path of the images in the test directory\n    print(os.path.join('/kaggle/working/dogs-vs-cats/test1', filename))\n    \n    path = os.path.join('/kaggle/working/dogs-vs-cats/test1', filename)\n    img = image.load_img(path, target_size=(150, 150))\n    x = image.img_to_array(img)\n    x = np.expand_dims(x, axis=0)\n    \n    img = mpimg.imread(path)\n    plt.imshow(img)\n    plt.show()\n    \n    images = np.vstack([x])\n    classes = model.predict(images, batch_size=10)\n    if classes[0]>0.5:\n        print(filename + \" is a dog\")\n    else:\n        print(filename + \" is a cat\")\n    print(\"--\"*30)\n    count -= 1\n    if(count < 0):\n        break","metadata":{"execution":{"iopub.status.busy":"2021-06-27T22:39:44.645252Z","iopub.execute_input":"2021-06-27T22:39:44.645663Z","iopub.status.idle":"2021-06-27T22:39:47.613320Z","shell.execute_reply.started":"2021-06-27T22:39:44.645622Z","shell.execute_reply":"2021-06-27T22:39:47.612314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}