{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "da839170-82ae-f033-5778-c687652814b6"
      },
      "outputs": [],
      "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 in \n",
        "\n",
        "import numpy as np # linear algebra\n",
        "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
        "\n",
        "# Input data files are available in the \"../input/\" directory.\n",
        "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n",
        "\n",
        "from subprocess import check_output\n",
        "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n",
        "\n",
        "# Any results you write to the current directory are saved as output."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "187ae0c2-bcc5-0326-7f66-93edb0de3e96"
      },
      "outputs": [],
      "source": [
        "import tflearn\n",
        "from tflearn.layers.conv import conv_2d, max_pool_2d\n",
        "from tflearn.layers.core import input_data, dropout, fully_connected\n",
        "from tflearn.layers.estimator import regression\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "fb8fe5c3-ef5e-82f2-fcf7-3286a8501091"
      },
      "outputs": [],
      "source": [
        "import cv2\n",
        "import numpy as np\n",
        "import os         \n",
        "from random import shuffle \n",
        "from tqdm import tqdm      \n",
        "import tensorflow as tf\n",
        "import matplotlib.pyplot as plt\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f700d843-c0c0-f40c-c34c-8263c616f214"
      },
      "outputs": [],
      "source": [
        "%matplotlib inline\n",
        "\n",
        "TRAIN_DIR = 'train'\n",
        "TEST_DIR = 'test'\n",
        "IMG_SIZE = 50\n",
        "LR = 1e-3\n",
        "MODEL_NAME = 'dogs-vs-cats-convnet'\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c8e96167-9a5a-dedc-5519-47350bd895db"
      },
      "outputs": [],
      "source": [
        "def create_label(image_name):\n",
        "    \"\"\" Create an one-hot encoded vector from image name \"\"\"\n",
        "    word_label = image_name.split('.')[-3]\n",
        "    if word_label == 'cat':\n",
        "        return np.array([1,0])\n",
        "    elif word_label == 'dog':\n",
        "        return np.array([0,1])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c4a6dd03-8f8b-7603-1d40-da377a48e190"
      },
      "outputs": [],
      "source": [
        "def create_train_data():\n",
        "    training_data = []\n",
        "    for img in tqdm(os.listdir(TRAIN_DIR)):\n",
        "        path = os.path.join(TRAIN_DIR, img)\n",
        "        img_data = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n",
        "        img_data = cv2.resize(img_data, (IMG_SIZE, IMG_SIZE))\n",
        "        training_data.append([np.array(img_data), create_label(img)])\n",
        "    shuffle(training_data)\n",
        "    np.save('train_data.npy', training_data)\n",
        "    return training_data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0bbd7057-acf9-3d96-7641-5af153a1c253"
      },
      "outputs": [],
      "source": [
        "def create_test_data():\n",
        "    testing_data = []\n",
        "    for img in tqdm(os.listdir(TEST_DIR)):\n",
        "        path = os.path.join(TEST_DIR,img)\n",
        "        img_num = img.split('.')[0]\n",
        "        img_data = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n",
        "        img_data = cv2.resize(img_data, (IMG_SIZE, IMG_SIZE))\n",
        "        testing_data.append([np.array(img_data), img_num])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "57a61a43-a9f5-8c91-4212-6183ac563220"
      },
      "outputs": [],
      "source": [
        "# If you have already created the dataset:\n",
        "# train_data = np.load('train_data.npy')\n",
        "# test_data = np.load('test_data.npy')shuffle(testing_data)\n",
        "    np.save('test_data.npy', testing_data\n",
        "    return testing_data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "97e821d5-9f30-e04c-e3ff-bc6956aa07fc"
      },
      "outputs": [],
      "source": [
        "train = 'train_data'[:-500]\n",
        "test = 'train_data'[-500:]\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1b6d50c0-715c-56ad-c471-bbb0696015dc"
      },
      "outputs": [],
      "source": [
        "\n",
        "X_train = np.array([i[0] for i in train]).reshape(-1, IMG_SIZE, IMG_SIZE, 1)\n",
        "y_train = [i[1] for i in train]\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "62a0a64f-6637-f4c5-0fbb-7be9d37f5d44"
      },
      "outputs": [],
      "source": [
        "X_test = np.array([i[0] for i in test]).reshape(-1, IMG_SIZE, IMG_SIZE, 1)\n",
        "y_test = [i[1] for i in test]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a8ffd39a-4d22-00f1-99a8-3c0704415c3f"
      },
      "outputs": [],
      "source": [
        "tf.reset_default_graph()\n"
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}