{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "468d3800-299c-b067-dad9-85ee3e9d3b9f"
      },
      "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": "d8bbfeb9-0cd1-2f76-6372-13b901b59922"
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n",
        "import seaborn as sns\n",
        "from skimage.io import imread, imshow\n",
        "import cv2\n",
        "import keras\n",
        "from keras.models import Sequential\n",
        "from keras.layers import Convolution2D, MaxPooling2D, Flatten, Dense\n",
        "from keras.preprocessing.image import ImageDataGenerator\n",
        "from keras import backend as K \n",
        "K.set_image_dim_ordering('th')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "09aaa259-5079-a632-a1ec-68f1b217d693"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "from glob import glob\n",
        "TRAIN_DATA = \"../input/train\"\n",
        "type_1_files = glob(os.path.join(TRAIN_DATA, \"Type_1\", \"*.jpg\"))\n",
        "type_1_ids = np.array([s[len(os.path.join(TRAIN_DATA, \"Type_1\"))+1:-4] for s in type_1_files])\n",
        "type_2_files = glob(os.path.join(TRAIN_DATA, \"Type_2\", \"*.jpg\"))\n",
        "type_2_ids = np.array([s[len(os.path.join(TRAIN_DATA, \"Type_2\"))+1:-4] for s in type_2_files])\n",
        "type_3_files = glob(os.path.join(TRAIN_DATA, \"Type_3\", \"*.jpg\"))\n",
        "type_3_ids = np.array([s[len(os.path.join(TRAIN_DATA, \"Type_3\"))+1:-4] for s in type_3_files])\n",
        "\n",
        "\n",
        "TEST_DATA = \"../input/test\"\n",
        "test_files = glob(os.path.join(TEST_DATA, \"*.jpg\"))\n",
        "test_ids = np.array([s[len(TEST_DATA)+1:-4] for s in test_files])\n",
        "\n",
        "\n",
        "ADDITIONAL_DATA = \"../input/additional\"\n",
        "additional_type_1_files = glob(os.path.join(ADDITIONAL_DATA, \"Type_1\", \"*.jpg\"))\n",
        "additional_type_1_ids = np.array([s[len(os.path.join(ADDITIONAL_DATA, \"Type_1\"))+1:-4] for s in additional_type_1_files])\n",
        "additional_type_2_files = glob(os.path.join(ADDITIONAL_DATA, \"Type_2\", \"*.jpg\"))\n",
        "additional_type_2_ids = np.array([s[len(os.path.join(ADDITIONAL_DATA, \"Type_2\"))+1:-4] for s in additional_type_2_files])\n",
        "additional_type_3_files = glob(os.path.join(ADDITIONAL_DATA, \"Type_3\", \"*.jpg\"))\n",
        "additional_type_3_ids = np.array([s[len(os.path.join(ADDITIONAL_DATA, \"Type_3\"))+1:-4] for s in additional_type_3_files])\n",
        "\n",
        "def get_filename(image_id, image_type):\n",
        "    \"\"\"\n",
        "    Method to get image file path from its id and type   \n",
        "    \"\"\"\n",
        "    if image_type == \"Type_1\" or \\\n",
        "        image_type == \"Type_2\" or \\\n",
        "        image_type == \"Type_3\":\n",
        "        data_path = os.path.join(TRAIN_DATA, image_type)\n",
        "    elif image_type == \"Test\":\n",
        "        data_path = TEST_DATA\n",
        "    elif image_type == \"AType_1\" or \\\n",
        "          image_type == \"AType_2\" or \\\n",
        "          image_type == \"AType_3\":\n",
        "        data_path = os.path.join(ADDITIONAL_DATA, image_type)\n",
        "    else:\n",
        "        raise Exception(\"Image type '%s' is not recognized\" % image_type)\n",
        "\n",
        "    ext = 'jpg'\n",
        "    return os.path.join(data_path, \"{}.{}\".format(image_id, ext))\n",
        "\n",
        "def get_image_data(image_id, image_type):\n",
        "    \"\"\"\n",
        "    Method to get image data as np.array specifying image id and type\n",
        "    \"\"\"\n",
        "    fname = get_filename(image_id, image_type)\n",
        "    img = cv2.imread(fname)\n",
        "    assert img is not None, \"Failed to read image : %s, %s\" % (image_id, image_type)\n",
        "    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
        "    return img\n",
        "\n",
        "print(len(additional_type_1_files), len(additional_type_2_files), len(additional_type_2_files))\n",
        "print(\"Type 1\", additional_type_1_ids[:10])\n",
        "print(\"Type 2\", additional_type_2_ids[:10])\n",
        "print(\"Type 3\", additional_type_3_ids[:10])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "bd4632be-6017-3eb6-4394-3f25ec7f8d06"
      },
      "outputs": [],
      "source": [
        "def plt_st(l1,l2):\n",
        "    plt.figure(figsize=(l1,l2))\n",
        "\n",
        "def apply_image_clustering(img):\n",
        "    Z = img.reshape((-1,3))\n",
        "    \n",
        "    # convert to np.float32\n",
        "    Z = np.float32(Z)\n",
        "\n",
        "    # define criteria, number of clusters(K) and apply kmeans()\n",
        "    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)\n",
        "    \n",
        "    \"\"\"\n",
        "    Right now the mask has an either in or out policy \n",
        "    \"\"\"\n",
        "    K = 4\n",
        "    ret,label,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)\n",
        "    \n",
        "    # Now convert back into uint8, and make original image\n",
        "    center = np.uint8(center)\n",
        "    res = center[label.flatten()]\n",
        "    res2 = res.reshape((img.shape))\n",
        "    return res2\n",
        "\n",
        "tile_size = (256, 256)\n",
        "n = 15\n",
        "\n",
        "complete_images = []\n",
        "for k, type_ids in enumerate([type_1_ids, type_2_ids, type_3_ids]):\n",
        "    m = int(np.floor(len(type_ids) / n))\n",
        "    complete_image = np.zeros((m*(tile_size[0]+2), n*(tile_size[1]+2), 3), dtype=np.uint8)\n",
        "    train_ids = sorted(type_ids)\n",
        "    counter = 0\n",
        "    for i in range(m):\n",
        "        ys = i*(tile_size[1] + 2)\n",
        "        ye = ys + tile_size[1]\n",
        "        for j in range(n):\n",
        "            xs = j*(tile_size[0] + 2)\n",
        "            xe = xs + tile_size[0]\n",
        "            image_id = train_ids[counter]; counter+=1\n",
        "            img = get_image_data(image_id, 'Type_%i' % (k+1))\n",
        "            img = cv2.resize(img, dsize=tile_size)\n",
        "            img = cv2.putText(img, image_id, (5,img.shape[0] - 5), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (255, 255, 255), thickness=3)\n",
        "            complete_image[ys:ye, xs:xe] = img[:,:,:]\n",
        "    complete_images.append(complete_image)\n",
        "    \n",
        "plt_st(20, 20)\n",
        "plt.imshow(complete_images[0])\n",
        "plt.title(\"Training dataset of type %i\" % (1))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e2e7d3c8-b2de-a028-6304-9749929063fc"
      },
      "outputs": [],
      "source": [
        "plt_st(20, 20)\n",
        "plt.imshow(complete_images[2])\n",
        "plt.title(\"Training dataset of type %i\" % (3))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "31e123b3-340b-357e-d479-3705b16aaaf7"
      },
      "outputs": [],
      "source": [
        "from PIL import Image\n",
        "import os, sys\n",
        "#image = Image.open('../kaggle/input/train/Type_1/10.jpg', \"rb\")\n",
        "#image.show()\n",
        "image_id = 10\n",
        "image_type = 'Type_1'\n",
        "fname = get_filename(image_id, image_type)\n",
        "print(fname)\n",
        "jpgfile = Image.open(fname)\n",
        "print(jpgfile.bits, jpgfile.size, jpgfile.format)\n",
        "plt.imshow(jpgfile)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a4f1bf83-0d6a-5d75-66da-14109f86d9ae"
      },
      "outputs": [],
      "source": [
        "def plt_st(l1,l2):\n",
        "    plt.figure(figsize=(l1,l2))\n",
        "\n",
        "tile_size = (256, 256)\n",
        "n = 15\n",
        "\n",
        "complete_images = []\n",
        "for k, type_ids in enumerate([type_1_ids, type_2_ids, type_3_ids]):\n",
        "    m = int(np.floor(len(type_ids) / n))\n",
        "    complete_image = np.zeros((m*(tile_size[0]+2), n*(tile_size[1]+2), 3), dtype=np.uint8)\n",
        "    train_ids = sorted(type_ids)\n",
        "    counter = 0\n",
        "    for i in range(m):\n",
        "        ys = i*(tile_size[1] + 2)\n",
        "        ye = ys + tile_size[1]\n",
        "        for j in range(n):\n",
        "            xs = j*(tile_size[0] + 2)\n",
        "            xe = xs + tile_size[0]\n",
        "            image_id = train_ids[counter]; counter+=1\n",
        "            img = get_image_data(image_id, 'Type_%i' % (k+1))\n",
        "            img = cv2.resize(img, dsize=tile_size)\n",
        "            img = cv2.putText(img, image_id, (5,img.shape[0] - 5), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (255, 255, 255), thickness=3)\n",
        "            complete_image[ys:ye, xs:xe] = img[:,:,:]\n",
        "    complete_images.append(complete_image)\n",
        "    \n",
        "plt_st(20, 20)\n",
        "plt.imshow(complete_images[0])\n",
        "plt.title(\"Training dataset of type %i\" % (1))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "698d619d-a0f5-de4d-7c35-e184a7d56a05"
      },
      "outputs": [],
      "source": [
        "classifier = Sequential()\n",
        "classifier.add(Convolution2D(32,(3,3), input_shape = (3, 64, 64), activation = 'relu'))\n",
        "classifier.add(MaxPooling2D(pool_size=(2,2)))\n",
        "classifier.add(Flatten())\n",
        "classifier.add(Dense(units=128, activation='relu'))\n",
        "classifier.add(Dense(units=3, activation='sigmoid'))\n",
        "classifier.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f1cf50b4-f3db-4de1-98eb-16d5ce4c1e25"
      },
      "outputs": [],
      "source": [
        "#fitting the train data\n",
        "train_datagen = ImageDataGenerator(\n",
        "        rescale=1./255)\n",
        "\n",
        "#        shear_range=0.2,\n",
        "#       zoom_range=0.2,\n",
        "#      horizontal_flip=True)\n",
        "\n",
        "test_datagen = ImageDataGenerator(rescale=1./255)\n",
        "\n",
        "training_set = train_datagen.flow_from_directory(\n",
        "        TRAIN_DATA,\n",
        "        target_size=(64, 64),\n",
        "        batch_size=32,\n",
        "        class_mode='categorical')\n",
        "\n",
        "test_set = test_datagen.flow_from_directory(\n",
        "        TEST_DATA,\n",
        "        target_size=(64, 64),\n",
        "        batch_size=32,\n",
        "        class_mode='categorical')\n",
        "\n",
        "classifier.fit_generator(\n",
        "        training_set,\n",
        "        steps_per_epoch=40,\n",
        "        epochs=2,\n",
        "        validation_data=test_set,\n",
        "        nb_val_samples=4)"
      ]
    }
  ],
  "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
}