{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "33f48235-a740-eab6-d4cc-996b474c17f4"
      },
      "source": [
        "### Goal\n",
        "\n",
        "- Environment exploration\n",
        "- Sample visualization\n",
        "- Use Xception to predict ImageNet class (cannot load the weight file)\n",
        "- Test loading the data (5/14 added)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5fc5cf5e-07b1-29e1-b06f-6323fe3e2889"
      },
      "outputs": [],
      "source": [
        "# Check docker environment\n",
        "import os\n",
        "os.getcwd()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bf9b5670-57d6-ea31-9d50-f1a7c9932baf"
      },
      "outputs": [],
      "source": [
        "os.listdir()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "75d3605c-84f7-2ca4-aaf1-85557b473a4b"
      },
      "outputs": [],
      "source": [
        "os.listdir('../')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e42cd05b-f5a0-15be-ac17-724352a7a9bb"
      },
      "outputs": [],
      "source": [
        "os.listdir('../input/')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "47f64a49-d946-fdb6-5c03-fbbc8a894c83"
      },
      "outputs": [],
      "source": [
        "os.listdir('../input/test')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "78c38304-7fe8-e537-60d9-31fe9c34dd11"
      },
      "outputs": [],
      "source": [
        "os.listdir('../input/train/')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2780f63a-7eda-47d3-86e9-ad76845a781e"
      },
      "outputs": [],
      "source": [
        "len(os.listdir('../input/train/')), len(os.listdir('../input/test/'))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a8ceb97f-507a-0e68-ce19-a0192cc549b0"
      },
      "outputs": [],
      "source": [
        "os.listdir('../lib')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "54550556-089d-67a8-a385-49f415467771"
      },
      "outputs": [],
      "source": [
        "os.listdir('../lib/kagglegym/')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f2ce1557-32a7-6e82-6994-b57e9e49c3a9"
      },
      "outputs": [],
      "source": [
        "os.listdir('../config/')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a9f2495a-c82e-b60d-3cd5-5926cd812d98"
      },
      "outputs": [],
      "source": [
        "from keras.preprocessing import image"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ca1f5a18-a58b-9af2-cd68-220853e7a901"
      },
      "outputs": [],
      "source": [
        "files = os.listdir('../input/train')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "05f44883-aa4f-9241-5e75-85fb6d49aed6"
      },
      "outputs": [],
      "source": [
        "for j,f in enumerate(files):\n",
        "    img = image.load_img('../input/train/'+f)\n",
        "    print(j,f,img.height,img.width)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0e6593b7-49fa-dec3-c6fc-9b98e9b96833"
      },
      "outputs": [],
      "source": [
        "img = image.load_img('../input/train/'+files[11])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "067b4369-6db0-82b4-ac9c-31779878247c"
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n",
        "plt.imshow(img)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c5b6a547-efc7-897c-c8aa-874309ff57c9"
      },
      "outputs": [],
      "source": [
        "x = image.img_to_array(img)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "825f79e2-f83e-be10-1902-3138087a0f62"
      },
      "outputs": [],
      "source": [
        "x.min(),x.max()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "55f5dde9-8560-c8a7-0058-b7927a56a319"
      },
      "outputs": [],
      "source": [
        "x.dtype"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "4d7f800c-c427-ac21-ddd5-a11ae64f8acb"
      },
      "outputs": [],
      "source": [
        "x = (x-x.min())/(x.max()-x.min())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3165b3de-7f0f-08a3-9285-1f4cdaa289fd"
      },
      "outputs": [],
      "source": [
        "x.dtype,x.min(),x.max()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c98fcce3-2ed3-90a9-43db-b77517f2f9cf"
      },
      "outputs": [],
      "source": [
        "sample_dog = image.load_img('../input/train/dog.11987.jpg')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "41e73c6c-9db5-99ff-07d1-46820112edb1"
      },
      "outputs": [],
      "source": [
        "plt.imshow(sample_dog)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6c3fc3f1-af4a-0d5c-a94d-bef8c95a584b"
      },
      "outputs": [],
      "source": [
        "sample_cat = image.load_img('../input/train/cat.12253.jpg')\n",
        "plt.imshow(sample_cat)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "25e30f9a-ee21-7aba-7a2f-fdbdaabeea70"
      },
      "outputs": [],
      "source": [
        "sample_dog.height,sample_dog.width,sample_cat.height,sample_cat.width"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d69ce7a9-e01d-17b3-6c14-b9ec9bc77d9c"
      },
      "outputs": [],
      "source": [
        "import numpy as np"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6b5ce538-d315-de47-abd9-ace038c1ae96"
      },
      "outputs": [],
      "source": [
        "np.array(sample_dog).shape,np.array(sample_cat).shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7e0ea7b7-708b-9a09-da54-622dc928b394"
      },
      "outputs": [],
      "source": [
        "# With resizing\n",
        "sample_dog = image.load_img('../input/train/dog.10041.jpg',target_size=(150,150))\n",
        "plt.imshow(sample_dog)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d68f80d8-e57c-0d0a-496c-337e5360c313"
      },
      "outputs": [],
      "source": [
        "sample_cat = image.load_img('../input/train/cat.9265.jpg',target_size=(150,150))\n",
        "plt.imshow(sample_cat)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8e9eb972-a239-7581-ab15-184225ba8309"
      },
      "outputs": [],
      "source": [
        "from keras.applications.xception import Xception\n",
        "from keras.applications.xception import preprocess_input\n",
        "from keras.applications.imagenet_utils import decode_predictions\n",
        "from keras.preprocessing import image\n",
        "\n",
        "model = Xception()\n",
        "\n",
        "\n",
        "def pred (m,n):\n",
        "\tfor j in range(m,n):\n",
        "\t\tfile = files[j]\n",
        "\t\timg = image.load_img('../input/train/'+file,target_size=(299,299))\n",
        "\t\tx = image.img_to_array(img)\n",
        "\t\tx = np.expand_dims(x, axis=0)\n",
        "\t\tx = preprocess_input(x)\n",
        "\t\tpreds = model.predict(x)\n",
        "\t\tprint (decode_predictions(preds)[0][0][1], ', ', file.split('.')[0])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "d99ed887-f102-5db5-5d7b-249d96063aab"
      },
      "outputs": [],
      "source": [
        "# Cannot download pretrained model\n",
        "len(files)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e737549c-7325-8a52-2a52-ba78becfffa2"
      },
      "outputs": [],
      "source": [
        "import keras"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "8b52656e-eb03-76ac-bb0a-cd3593a53b06"
      },
      "outputs": [],
      "source": [
        "# Build from scratch\n",
        "model = keras.models.Sequential()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "88ff55f8-b351-f0bf-fb12-10803160d977"
      },
      "outputs": [],
      "source": [
        "model.add()"
      ]
    }
  ],
  "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
}