{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "b3cb88ac-4138-f5b6-827b-55590ad937b7"
      },
      "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",
        "from PIL import Image\n",
        "import tensorflow as tf\n",
        "import tflearn\n",
        "from tflearn import *\n",
        "from tflearn.data_utils import image_preloader\n",
        "import os\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",
        "#Image.open(glob(\"../input/test/*\")[1])\n",
        "# Any results you write to the current directory are saved as output."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1419021f-a057-e35a-4378-1c5679fdaceb"
      },
      "outputs": [],
      "source": [
        "# Generate our index file for reading and processing\n",
        "TRAIN_DIR = '../input/train/'\n",
        "TEST_DIR = '../input/test/'\n",
        "labels = [1 if 'cat' in f else 0 for f in os.listdir(TRAIN_DIR)]\n",
        "files = os.listdir(TRAIN_DIR)\n",
        "\n",
        "with open('temp.txt', 'w') as f:\n",
        "    for i in range(0, len(labels)):\n",
        "        f.write(files[i] + ' ' + str(labels[i]) + '\\n')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "9bae97f2-6fa8-e7ed-a058-ca5734ee9a03"
      },
      "outputs": [],
      "source": [
        "x, y = image_preloader(target_path='temp.txt', mode='file', \n",
        "                       image_shape=(64,64), categorical_labels=True, normalize=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "24ed7cbc-6c7d-2e1f-ee8b-f546c7126108"
      },
      "outputs": [],
      "source": [
        "def buildNetwork():\n",
        "    network = input_data(shape=[None, 64, 64, 3])\n",
        "    network = conv_2d(network, 32, 3, activation='relu')\n",
        "    network = max_pool_2d(network, 2)\n",
        "    network = conv_2d(network, 64, 3, activation='relu')\n",
        "    network = conv_2d(network, 64, 3, activation='relu')\n",
        "    network = max_pool_2d(network, 2)\n",
        "    network = fully_connected(network, 512, activation='relu')\n",
        "    network = dropout(network, 0.5)\n",
        "    network = fully_connected(network, 2, activation='softmax')\n",
        "    network = regression(network, optimizer='adam',\n",
        "                         loss='categorical_crossentropy',\n",
        "                         learning_rate=0.001)\n",
        "    return network"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "54ecaf37-d428-40e5-89fa-e4aa786ef1ce"
      },
      "outputs": [],
      "source": [
        "network = buildNetwork()\n",
        "model = tflearn.DNN(network, tensorboard_verbose=0)\n",
        "\n",
        "col = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)\n",
        "for x in col:\n",
        "    tf.add_to_collection(tf.GraphKeys.VARIABLES, x )    \n",
        "    \n",
        "model.fit(x, y, n_epoch=50, shuffle=True, validation_set=0.1,\n",
        "          show_metric=True, batch_size=96, run_id='testrun1')\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.5.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}