{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "caba1cb8-bc24-4270-b575-a9c462d80247"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "import matplotlib.pylab as plt\n",
        "from random import shuffle\n",
        "\n",
        "import os\n",
        "import cv2"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c53108c4-a6ca-6291-74dc-148f078b7d0d"
      },
      "outputs": [],
      "source": [
        "train_path = \"../input/train\"\n",
        "test_path = \"../input/test\"\n",
        "\n",
        "ROWS = 64\n",
        "COLS = 64\n",
        "CHANNELS = 3\n",
        "\n",
        "images      = [img for img in os.listdir(train_path)]\n",
        "images_dog  = [img for img in os.listdir(train_path) if \"dog\" in img]\n",
        "images_cat  = [img for img in os.listdir(train_path) if \"cat\" in img]\n",
        "\n",
        "images_test = [img for img in os.listdir(test_path)]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8ad221ed-1d7e-c066-ff7e-56188966f195"
      },
      "outputs": [],
      "source": [
        "#only taking a subset (less accuracy but faster training)\n",
        "train_dog = images_dog[:1000]\n",
        "train_cat = images_cat[:1000]\n",
        "valid_dog = images_dog[1000:1100]\n",
        "valid_cat = images_cat[1000:1100]\n",
        "\n",
        "train_list = train_dog + train_cat\n",
        "valid_list = valid_dog + valid_cat\n",
        "test_list  = images_test[0:]\n",
        "\n",
        "shuffle(train_list)\n",
        "\n",
        "train = np.ndarray(shape=(len(train_list),ROWS, COLS))\n",
        "train_color = np.ndarray(shape=(len(train_list), ROWS, COLS, CHANNELS), dtype=np.uint8)\n",
        "test = np.ndarray(shape=(len(test_list),ROWS, COLS))\n",
        "test_color = np.ndarray(shape=(len(images_test), ROWS, COLS, CHANNELS), dtype=np.uint8)\n",
        "valid = np.ndarray(shape=(len(valid_list), ROWS, COLS))\n",
        "valid_color = np.ndarray(shape=(len(valid_list), ROWS, COLS, CHANNELS), dtype=np.uint8)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5b14b17f-fe2c-93fd-e26a-6f60ff59e27d"
      },
      "outputs": [],
      "source": [
        "labels = np.ndarray(len(train_list))\n",
        "\n",
        "for i, img_path in enumerate(train_list):\n",
        "    img_color = cv2.imread(os.path.join(train_path, img_path), 1)\n",
        "    img_color = cv2.resize(img_color, (ROWS, COLS), interpolation=cv2.INTER_CUBIC)\n",
        "    img = cv2.cvtColor(img_color, cv2.COLOR_BGR2GRAY)\n",
        "    \n",
        "    train[i] = img\n",
        "    train_color[i] = img_color\n",
        "   \n",
        "    if \"dog\" in img_path:\n",
        "        labels[i] = 0\n",
        "    else:\n",
        "        labels[i] = 1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "202d968e-f8db-b90f-86c3-c95fbc7a3648"
      },
      "outputs": [],
      "source": [
        "valid_labels = np.ndarray(len(valid_list))\n",
        "\n",
        "for i, img_path in enumerate(valid_list):\n",
        "    img_color = cv2.imread(os.path.join(train_path, img_path), 1)\n",
        "    img_color = cv2.resize(img_color, (ROWS, COLS), interpolation=cv2.INTER_CUBIC)\n",
        "    img = cv2.cvtColor(img_color, cv2.COLOR_BGR2GRAY)\n",
        "    \n",
        "    valid[i] = img\n",
        "    valid_color[i] = img_color\n",
        "    \n",
        "    if \"dog\" in img_path:\n",
        "        valid_labels[i] = 0\n",
        "    else:\n",
        "        valid_labels[i] = 1"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2bfe4962-7c61-4726-8b3f-2b1fcd27a5a8"
      },
      "outputs": [],
      "source": [
        "for i, img_path in enumerate(test_list):\n",
        "    img_color = cv2.imread(os.path.join(test_path, img_path), 1)\n",
        "    img_color = cv2.resize(img_color, (ROWS, COLS), interpolation=cv2.INTER_CUBIC)\n",
        "    img = cv2.cvtColor(img_color, cv2.COLOR_BGR2GRAY)\n",
        "    \n",
        "    test[i] = img\n",
        "    test_color[i] = img_color"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7b30acff-a610-92a1-ed19-5b71774bd45a"
      },
      "outputs": [],
      "source": [
        "for i, img_path in enumerate(test_list):\n",
        "    img_color = cv2.imread(os.path.join(test_path, img_path), 1)\n",
        "    img_color = cv2.resize(img_color, (ROWS, COLS), interpolation=cv2.INTER_CUBIC)\n",
        "    img = cv2.cvtColor(img_color, cv2.COLOR_BGR2GRAY)\n",
        "    \n",
        "    test[i] = img\n",
        "    test_color[i] = img_color"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e146f35a-270b-8362-7e54-39665590f9fa"
      },
      "outputs": [],
      "source": [
        "from keras.utils import np_utils\n",
        "\n",
        "X_train = train_color / 255\n",
        "X_valid = valid_color / 255\n",
        "X_test  = test_color  / 255\n",
        "# one hot encode outputs\n",
        "y_train = np_utils.to_categorical(labels)\n",
        "y_valid = np_utils.to_categorical(valid_labels)\n",
        "num_classes = y_valid.shape[1]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8098369f-a542-7bbf-b23b-075009d7f5ac"
      },
      "outputs": [],
      "source": [
        "def larger_model():\n",
        "\t# create model\n",
        "\tmodel = Sequential()\n",
        "\tmodel.add(Convolution2D(30, 5, 5, border_mode='valid', input_shape=(64, 64, 3), activation='relu'))\n",
        "\tmodel.add(MaxPooling2D(pool_size=(2, 2)))\n",
        "\tmodel.add(Convolution2D(15, 3, 3, activation='relu'))\n",
        "\tmodel.add(MaxPooling2D(pool_size=(2, 2)))\n",
        "\tmodel.add(Dropout(0.2))\n",
        "\tmodel.add(Flatten())\n",
        "\tmodel.add(Dense(128, activation='relu'))\n",
        "\tmodel.add(Dense(50, activation='relu'))\n",
        "\tmodel.add(Dense(num_classes, activation='softmax'))\n",
        "\t# Compile model\n",
        "\tmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n",
        "\treturn model"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7f0761b0-b71e-4b89-29fe-18681ff97a23"
      },
      "outputs": [],
      "source": [
        "from keras.models import Sequential\n",
        "from keras.layers import Dense\n",
        "from keras.layers import Dropout\n",
        "from keras.layers import Flatten\n",
        "from keras.layers.convolutional import Convolution2D\n",
        "from keras.layers.convolutional import MaxPooling2D\n",
        "# build the model\n",
        "model = larger_model()\n",
        "# Fit the model\n",
        "model.fit(X_train, y_train, validation_data=(X_valid, y_valid), nb_epoch=10, batch_size=200, verbose=2)\n",
        "# Final evaluation of the model\n",
        "scores = model.evaluate(X_valid, y_valid, verbose=0)\n",
        "print(\"Classification Error: %.2f%%\" % (100-scores[1]*100))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "11e1d8cf-7b09-ecc5-2077-4a24bd588db8"
      },
      "outputs": [],
      "source": [
        "submission = model.predict_classes(X_test, verbose=2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "352b61c1-569f-a072-4a90-901599e28223"
      },
      "outputs": [],
      "source": [
        "pd.DataFrame({\"id\": list(range(1,len(test_color)+1)), \n",
        "              \"label\": submission}).to_csv('submission.csv', index=False,header=True)"
      ]
    }
  ],
  "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
}