{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "19099315-19ba-7fe5-45e4-befd5fb6da31"
      },
      "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": "558a2844-7425-0335-e1e1-9e0bf534b2b8"
      },
      "outputs": [],
      "source": [
        "import cv2                 # working with, mainly resizing, images\n",
        "import numpy as np         # dealing with arrays\n",
        "import os                  # dealing with directories\n",
        "from random import shuffle # mixing up or currently ordered data that might lead our network astray in training.\n",
        "from tqdm import tqdm      # a nice pretty percentage bar for tasks. Thanks to viewer Daniel B\u00fchler for this suggestion\n",
        "\n",
        "TRAIN_DIR = '../input/train'\n",
        "TEST_DIR = '../input/test'\n",
        "IMG_SIZE = 50\n",
        "LR = 1e-3\n",
        "\n",
        "MODEL_NAME = 'dogsvscats-{}-{}.model'.format(LR, '2conv-basic') # just so we remember which saved model is which, sizes must match"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3a069b8e-1765-c2ef-3b96-87a348779254"
      },
      "outputs": [],
      "source": [
        "def label_img(img):\n",
        "    word_label = img.split('.')[-3]\n",
        "    # conversion to one-hot array [cat,dog]\n",
        "    #                            [much cat, no dog]\n",
        "    if word_label == 'cat': return [1,0]\n",
        "    #                             [no cat, very doggo]\n",
        "    elif word_label == 'dog': return [0,1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "224c11a3-c952-a070-fe3a-a3f76599cd72"
      },
      "outputs": [],
      "source": [
        "def create_train_data():\n",
        "    training_data = []\n",
        "    for img in tqdm(os.listdir(TRAIN_DIR)):\n",
        "        label = label_img(img)\n",
        "        path = os.path.join(TRAIN_DIR,img)\n",
        "        img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)\n",
        "        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n",
        "        training_data.append([np.array(img),np.array(label)])\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": "da334112-cb96-1d1a-adac-7c2fe7f617c9"
      },
      "outputs": [],
      "source": [
        "def process_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 = cv2.imread(path,cv2.IMREAD_GRAYSCALE)\n",
        "        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))\n",
        "        testing_data.append([np.array(img), img_num])\n",
        "        \n",
        "    shuffle(testing_data)\n",
        "    np.save('test_data.npy', testing_data)\n",
        "    return testing_data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2295a0f1-cf1b-1185-a672-cb03567585f3"
      },
      "outputs": [],
      "source": [
        "train_data = create_train_data()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9ae36e33-8689-e0e4-fba2-93742b93d1e8"
      },
      "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",
        "convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')\n",
        "\n",
        "convnet = conv_2d(convnet, 32, 5, activation='relu')\n",
        "convnet = max_pool_2d(convnet, 5)\n",
        "\n",
        "convnet = conv_2d(convnet, 64, 5, activation='relu')\n",
        "convnet = max_pool_2d(convnet, 5)\n",
        "\n",
        "convnet = fully_connected(convnet, 1024, activation='relu')\n",
        "convnet = dropout(convnet, 0.8)\n",
        "\n",
        "convnet = fully_connected(convnet, 2, activation='softmax')\n",
        "convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')\n",
        "\n",
        "model = tflearn.DNN(convnet, tensorboard_dir='log')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b1699352-da35-b618-c05e-664f2a656de9"
      },
      "outputs": [],
      "source": [
        "\n",
        "\n",
        "if os.path.exists('{}.meta'.format(MODEL_NAME)):\n",
        "    model.load(MODEL_NAME)\n",
        "    print('model loaded!')\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2a928fae-395a-f673-948a-8eb43d630a3a"
      },
      "outputs": [],
      "source": [
        "\n",
        "\n",
        "train = train_data[:-500]\n",
        "test = train_data[-500:]\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "75ad1b7b-3dce-f188-d734-dd2f6c535a56"
      },
      "outputs": [],
      "source": [
        "X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,1)\n",
        "Y = [i[1] for i in train]\n",
        "\n",
        "test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,1)\n",
        "test_y = [i[1] for i in test]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "384667c0-cb30-babc-5be1-0ff40e2bbae6"
      },
      "outputs": [],
      "source": [
        "model.fit({'input': X}, {'targets': Y}, n_epoch=2, validation_set=({'input': test_x}, {'targets': test_y}), \n",
        "    snapshot_step=50000, show_metric=True, run_id=MODEL_NAME)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b81f106c-ff64-e828-24bd-8716cad1143a"
      },
      "outputs": [],
      "source": [
        "\n",
        "\n",
        "import tensorflow as tf\n",
        "tf.reset_default_graph()\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3f30e2d2-bdfa-2779-cad6-cba542be6a77"
      },
      "outputs": [],
      "source": [
        "convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')\n",
        "\n",
        "convnet = conv_2d(convnet, 32, 5, activation='relu')\n",
        "convnet = max_pool_2d(convnet, 5)\n",
        "\n",
        "convnet = conv_2d(convnet, 64, 5, activation='relu')\n",
        "convnet = max_pool_2d(convnet, 5)\n",
        "\n",
        "convnet = conv_2d(convnet, 32, 5, activation='relu')\n",
        "convnet = max_pool_2d(convnet, 5)\n",
        "\n",
        "convnet = conv_2d(convnet, 64, 5, activation='relu')\n",
        "convnet = max_pool_2d(convnet, 5)\n",
        "\n",
        "convnet = conv_2d(convnet, 32, 5, activation='relu')\n",
        "convnet = max_pool_2d(convnet, 5)\n",
        "\n",
        "convnet = conv_2d(convnet, 64, 5, activation='relu')\n",
        "convnet = max_pool_2d(convnet, 5)\n",
        "\n",
        "convnet = fully_connected(convnet, 1024, activation='relu')\n",
        "convnet = dropout(convnet, 0.8)\n",
        "\n",
        "convnet = fully_connected(convnet, 2, activation='softmax')\n",
        "convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')\n",
        "\n",
        "model = tflearn.DNN(convnet, tensorboard_dir='log')\n",
        "\n",
        "\n",
        "\n",
        "if os.path.exists('{}.meta'.format(MODEL_NAME)):\n",
        "    model.load(MODEL_NAME)\n",
        "    print('model loaded!')\n",
        "\n",
        "train = train_data[:-500]\n",
        "test = train_data[-500:]\n",
        "\n",
        "X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,1)\n",
        "Y = [i[1] for i in train]\n",
        "\n",
        "test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,1)\n",
        "test_y = [i[1] for i in test]\n",
        "\n",
        "model.fit({'input': X}, {'targets': Y}, n_epoch=4, validation_set=({'input': test_x}, {'targets': test_y}), \n",
        "    snapshot_step=500, show_metric=True, run_id=MODEL_NAME)\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1fa77981-585a-0a50-c1dc-a4e11d3d18c9"
      },
      "outputs": [],
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "553932a8-95b3-f91f-0116-7c4e01523bad"
      },
      "outputs": [],
      "source": ""
    }
  ],
  "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
}