{"metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"version": "3.6.3", "file_extension": ".py", "mimetype": "text/x-python", "codemirror_mode": {"version": 3, "name": "ipython"}, "pygments_lexer": "ipython3", "name": "python", "nbconvert_exporter": "python"}}, "nbformat": 4, "cells": [{"metadata": {"_cell_guid": "6ebff5fd-d842-403b-9aa3-5d1eb6a54832", "_uuid": "273231aada43e457046bbb8cca71fc566404cd75"}, "cell_type": "code", "execution_count": null, "source": ["import numpy as np \n", "import pandas as pd\n", "from sklearn.model_selection import train_test_split\n", "import io\n", "import bson\n", "import matplotlib\n", "import matplotlib.pyplot as plt\n", "from skimage.data import imread   \n", "import multiprocessing as mp\n", "import numpy as np\n", "import pandas as pd\n", "from collections import Counter\n", "from keras.utils import np_utils\n", "from tflearn.data_utils import to_categorical\n", "from tflearn.layers.core import input_data, dropout, fully_connected\n", "from tflearn.layers.conv import conv_2d, max_pool_2d\n", "from tflearn.layers.estimator import regression\n", "import tensorflow as tf\n", "import tflearn\n", "import skimage\n", "from skimage.transform import resize, rescale\n", "from tqdm import tqdm_notebook\n", "from sklearn.metrics import roc_curve, auc\n", "import itertools"], "outputs": []}, {"metadata": {"collapsed": true, "_cell_guid": "68f83dd8-698b-431f-a060-b19682253c09", "_uuid": "35dbb9923bc40be6758d8bbff67891ad558cd48c"}, "cell_type": "code", "execution_count": null, "source": ["categories_names = pd.read_csv('../input/category_names.csv', index_col='category_id')"], "outputs": []}, {"metadata": {"_cell_guid": "e1fa16be-3868-41dc-85ac-ac1129d70bb5", "_uuid": "5f4bfb38c70d77a21ee7deed890d122d196567fe"}, "cell_type": "code", "execution_count": null, "source": ["idProduct = []\n", "categoryProduct = []\n", "countProductImgs = []\n", "totalDict = 7069896\n", "trainPath = '../input/train.bson'\n", "\n", "with open(trainPath, 'rb') as file, tqdm_notebook(total=totalDict) as bar:\n", "        \n", "    productDict = bson.decode_file_iter(file)\n", "\n", "    for c, myDict in enumerate(productDict):\n", "        bar.update()\n", "        idProduct.append(myDict['_id'])\n", "        categoryProduct.append(str(myDict['category_id']))\n", "        countProductImgs.append(len(myDict['imgs']))"], "outputs": []}, {"metadata": {"collapsed": true, "_cell_guid": "8ec53063-3842-47a8-b4cd-d84b0c66643d", "_uuid": "a02b1f2d662e1062ee3241ab67c5b2cc3cbf37e9"}, "cell_type": "code", "execution_count": null, "source": ["myDataframe = pd.DataFrame({'categoryID': categoryProduct, 'countImgs': countProductImgs}, index=idProduct)"], "outputs": []}, {"metadata": {"_cell_guid": "db468fa2-d8a1-4426-9628-22680d588bd6", "_uuid": "5be1029e802075c184c6da418a430282b598c537"}, "cell_type": "code", "execution_count": null, "source": ["myDataframe.countImgs.value_counts().plot(kind='bar')\n", "print(\"Total Images in Train set:\", myDataframe.countImgs.sum())\n", "print(\"Total Categories:\", len(pd.unique(myDataframe.categoryID)))"], "outputs": []}, {"metadata": {"_cell_guid": "0830036b-9028-40d6-b42a-b9d94191f1f7", "_uuid": "faef119be7a9de7f0e651be68f68f0ee7aec13ff"}, "cell_type": "code", "execution_count": null, "source": ["classLabels, count = zip(*Counter(categoryProduct).items())\n", "indexes = np.arange(len(classLabels))\n", "width = 1\n", "\n", "plt.bar(indexes, count, width)\n", "plt.xticks(indexes + width * 0.5, classLabels)\n", "plt.xticks(rotation=90)\n", "plt.show()"], "outputs": []}, {"metadata": {"_cell_guid": "4e8325e6-8536-4d2a-9913-e140fa17159c", "_uuid": "6ca56b2fe776613dfe020b36a9c070a7731721af"}, "cell_type": "code", "execution_count": null, "source": ["#visualize training sample\n", "idProductTrain = []\n", "categoryProductTrain = []\n", "countProductImgsTrain = []\n", "totalDict = 82\n", "trainPath = '../input/train_example.bson'\n", "picturesTrain = []\n", "catergoryIdArray = []\n", "count = 0\n", "with open(trainPath, 'rb') as file, tqdm_notebook(total=totalDict) as bar:\n", "        \n", "    productDictTrain = bson.decode_file_iter(file)\n", "\n", "    for c, myDict in enumerate(productDictTrain):\n", "        if(count>82):\n", "            break\n", "        count = count + 1\n", "        bar.update()\n", "        idProductTrain.append(myDict['_id'])\n", "        categoryProductTrain.append(str(myDict['category_id']))\n", "        countProductImgsTrain.append(len(myDict['imgs']))\n", "        \n", "        for e, myPic in enumerate(myDict['imgs']):\n", "        #display all images\n", "            picture = imread(io.BytesIO(myPic['picture']))\n", "            image_rescaled = rescale(picture, 1.0 / 4.0)\n", "            pictures.append(image_rescaled)\n", "#             picturesTrain.append(picture)\n", "    #         print(\"PRODUCT ID:\", product_id, \"NUMBER\", e)\n", "            plt.imshow(image_rescaled)\n", "            plt.show()\n", "            catergoryIdArray.append(str(myDict['category_id']))"], "outputs": []}, {"metadata": {"collapsed": true, "_cell_guid": "296f91f6-0057-4315-955b-10224c0ae6e3", "_uuid": "b46eb42ad0e204852c3a2ee8c87f182a844988d4"}, "cell_type": "code", "execution_count": null, "source": ["def getRawFeatures(picture):\n", "    red = [] #red channel\n", "    green = [] #green channel\n", "    blue = [] #blue channel\n", "    for row in range(picture.shape[0]):\n", "        for col in range(picture.shape[1]):\n", "            red.append(picture[row][col][0])\n", "            green.append(picture[row][col][1])\n", "            blue.append(picture[row][col][2])\n", "    feature = red\n", "    feature.extend(green)\n", "    feature.extend(blue)\n", "    return feature"], "outputs": []}, {"metadata": {"_cell_guid": "6e853ef1-7ed0-4749-90bb-66cd386ebfe6", "_uuid": "640f6597cedccdf1fabcf7879784c337908ae493"}, "cell_type": "code", "execution_count": null, "source": ["classLabels, count = zip(*Counter(categoryProductTrain).items())\n", "indexes = np.arange(len(classLabels))\n", "width = 1\n", "\n", "plt.bar(indexes, count, width)\n", "plt.xticks(indexes + width * 0.5, classLabels)\n", "plt.xticks(rotation=90)\n", "plt.show()"], "outputs": []}, {"metadata": {"_cell_guid": "3ef71b1a-406a-45c3-9c8a-af460da9fd19", "_uuid": "5dc52a1134240e802ac0f655095902144b52239c"}, "cell_type": "code", "execution_count": null, "source": ["idProduct = []\n", "categoryProduct = []\n", "countProductImgs = []\n", "pictures = []\n", "catergoryIdArray = []\n", "\n", "# totalDict = 7069896\n", "totalDict = 82\n", "# trainPath = '../input/train.bson'\n", "trainPath = \"../input/train_example.bson\"\n", "count = 0\n", "with open(trainPath, 'rb') as file, tqdm_notebook(total=totalDict) as bar:\n", "        \n", "    productDict = bson.decode_file_iter(file)\n", "\n", "    for c, myDict in enumerate(productDict):\n", "        if(count > 82):\n", "            break\n", "        count = count + 1\n", "        \n", "        bar.update()\n", "        idProduct.append(myDict['_id'])\n", "        categoryProduct.append(str(myDict['category_id']))\n", "        countProductImgs.append(len(myDict['imgs']))\n", "        \n", "        for e, myPic in enumerate(myDict['imgs']):\n", "        #display all images\n", "            picture = imread(io.BytesIO(myPic['picture']))\n", "            image_rescaled = rescale(picture, 1.0 / 4.0)\n", "            pictures.append(image_rescaled)\n", "    #         print(\"PRODUCT ID:\", product_id, \"NUMBER\", e)\n", "#             plt.imshow(image_rescaled)\n", "#             plt.show()\n", "            catergoryIdArray.append(str(myDict['category_id']))"], "outputs": []}, {"metadata": {"_cell_guid": "f97d4fba-22d5-4daf-b987-07e8871fb344", "_uuid": "cac51c15687c3c126864fd5155e337eb5237a93f"}, "cell_type": "code", "execution_count": null, "source": ["X = np.asarray(pictures)\n", "y = np.asarray(catergoryIdArray)\n", "print(X.shape, y.shape)\n", "\n", "#reshaping X in proper format\n", "X = X.reshape(X.shape[0], 3, 45, 45).astype('float32')\n", "#normalizing X\n", "X = X - np.mean(X) / X.std()"], "outputs": []}, {"metadata": {"collapsed": true, "_cell_guid": "00556f9d-ef35-4ed9-b5ec-7f9bb92eeef7", "_uuid": "42b971def8681c1ac8244e318d944106541c0a7a"}, "cell_type": "code", "execution_count": null, "source": ["#converting class labels to One hot encoding\n", "b,y = np.unique(y, return_inverse=True) #returns the unique values\n", "y = np_utils.to_categorical(y) \n", "#y is converted to one hot representation                 "], "outputs": []}, {"metadata": {"_cell_guid": "2a353968-80bb-4b55-81e9-0a54edafd47d", "_uuid": "e4e81477bf6399d025297117cbf99aa4667ce7f8"}, "cell_type": "code", "execution_count": null, "source": ["numClasses = len(Counter(np.asarray(catergoryIdArray)))\n", "print(numClasses)\n", "print(X.shape)\n", "print(y.shape)"], "outputs": []}, {"metadata": {"_cell_guid": "0c4770ca-5fdb-44e8-8e4a-cac7ead11f6b", "_uuid": "3377e4b08e34ac57bb19360ee12b68a7347b4b3f"}, "cell_type": "code", "execution_count": null, "source": ["#lets split\n", "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1234) \n", "#spliting the data\n", "\n", "print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)"], "outputs": []}, {"metadata": {"_cell_guid": "f6f03dfa-a0f4-474c-81f4-d4a966a58e0f", "_uuid": "3b2925866d73f53c4e115734f66444ecd3258bfb"}, "cell_type": "code", "execution_count": null, "source": ["myConvModel = input_data(shape=[None,3,45,45])\n", "\n", "#Layer 1: Convolution and Pooling\n", "myConvModel = conv_2d(myConvModel,45,3,activation='elu')\n", "myConvModel = max_pool_2d(myConvModel,2)\n", "\n", "#Layer 2 Convolution and Pooling on important features\n", "myConvModel = conv_2d(myConvModel,100,2,activation='relu')\n", "myConvModel = max_pool_2d(myConvModel,2)\n", "\n", "#setting the dropout parameter to 30%\n", "myConvModel = dropout(myConvModel,0.3)\n", "\n", "#Layer 3 Convolution and Pooling with activation function elu\n", "myConvModel = conv_2d(myConvModel,100,2, activation='elu')\n", "myConvModel = max_pool_2d(myConvModel,2)\n", "\n", "#Layer 4\n", "myConvModel = fully_connected(myConvModel,512,activation='sigmoid')\n", "myConvModel = dropout(myConvModel,0.3)\n", "#Layer 5\n", "myConvModel = fully_connected(myConvModel,numClasses,activation='softmax')\n", "myConvModel = regression(myConvModel,optimizer='adagrad',loss='categorical_crossentropy',learning_rate=0.05)"], "outputs": []}, {"metadata": {"_cell_guid": "8eabe5fe-1edc-40a1-a276-c048e92fbff3", "_uuid": "1de324af49921fb1461d47e4851f4e9b8a79e884"}, "cell_type": "code", "execution_count": null, "source": ["with tf.device('cpu:0'):\n", "   myConvModel = tflearn.DNN(myConvModel) #Training using DNN model class\n", "# batch size =100\n", "   myConvModel.fit(X_train , y_train, n_epoch=15, validation_set = (X_test, y_test), batch_size = 10) #fitting the data"], "outputs": []}, {"metadata": {"_cell_guid": "403e7a70-b3a3-402e-ad87-4a718d2de798", "_uuid": "4f610e1fc16827b0273dd58b32a68d3ca5842279"}, "cell_type": "code", "execution_count": null, "source": ["print(\"Accuracy:\")\n", "myConvModel.evaluate(X_test, y_test) #Finding accuracy"], "outputs": []}, {"metadata": {"collapsed": true, "_cell_guid": "d6c785c2-0fcf-4fa2-8e50-0c3180688dd7", "_uuid": "c5733d68f750d7a30fdce2bcb87e201e76826a9e"}, "cell_type": "code", "execution_count": null, "source": ["y_predict = myConvModel.predict(X_test)"], "outputs": []}, {"metadata": {"_cell_guid": "f7f6a845-407d-495e-bc4c-e24b7050fbfd", "_uuid": "7963aa2d73cd80792427340ab9628b3aa3bbe4a2"}, "cell_type": "code", "execution_count": null, "source": ["\n", "n_classes = numClasses\n", "y_score = y_predict\n", "\n", "# Compute ROC curve and ROC area for each class\n", "falsePositiveRate = dict()\n", "truePositiveRate = dict()\n", "roc_auc = dict()\n", "for i in range(n_classes):\n", "    falsePositiveRate[i], truePositiveRate[i], _ = roc_curve(y_test[:, i], y_score[:, i])\n", "    roc_auc[i] = auc(falsePositiveRate[i], truePositiveRate[i])\n", "\n", "# Compute micro-average ROC curve and ROC area\n", "falsePositiveRate[\"micro\"], truePositiveRate[\"micro\"], _ = roc_curve(y_test.ravel(), y_score.ravel())\n", "roc_auc[\"micro\"] = auc(falsePositiveRate[\"micro\"], truePositiveRate[\"micro\"])\n", "\n", "# plt.figure()\n", "# lw = 2\n", "# plt.plot(falsePositiveRate[2], truePositiveRate[2], color='darkorange',\n", "#          lw=lw, label='ROC curve (area = %0.2f)' % roc_auc[2])\n", "# plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')\n", "# plt.xlim([0.0, 1.0])\n", "# plt.ylim([0.0, 1.05])\n", "# plt.xlabel('False Positive Rate')\n", "# plt.ylabel('True Positive Rate')\n", "# plt.title('Receiver operating characteristic example')\n", "# plt.legend(loc=\"lower right\")\n", "# plt.show()\n", "\n", "all_falsePositiveRate = np.unique(np.concatenate([falsePositiveRate[i] for i in range(n_classes)]))\n", "\n", "mean_truePositiveRate = np.zeros_like(all_falsePositiveRate)\n", "for i in range(n_classes):\n", "    mean_truePositiveRate += np.interp(all_falsePositiveRate, falsePositiveRate[i], truePositiveRate[i])\n", "\n", "mean_truePositiveRate /= n_classes\n", "\n", "falsePositiveRate[\"macro\"] = all_falsePositiveRate\n", "truePositiveRate[\"macro\"] = mean_truePositiveRate\n", "roc_auc[\"macro\"] = auc(falsePositiveRate[\"macro\"], truePositiveRate[\"macro\"])\n", "\n", "# Plot all ROC curves\n", "plt.figure()\n", "plt.plot(falsePositiveRate[\"micro\"], truePositiveRate[\"micro\"],\n", "         label='micro-average ROC curve (area = {0:0.2f})'\n", "               ''.format(roc_auc[\"micro\"]),\n", "         color='deeppink', linestyle=':', linewidth=4)\n", "\n", "colors = itertools.cycle(['aqua', 'darkorange', 'cornflowerblue'])\n", "\n", "plt.plot([0, 1], [0, 1], 'k--', lw=lw)\n", "plt.xlim([0.0, 1.0])\n", "plt.ylim([0.0, 1.05])\n", "plt.xlabel('FPR')\n", "plt.ylabel('TPR')\n", "plt.title('ROC Curve')\n", "plt.show()"], "outputs": []}, {"metadata": {"_cell_guid": "a1d25a06-5a94-4c0d-aaa4-4bc51ad7ca5e", "_uuid": "d980b42365ce387137ff661b411ca39db4f21617"}, "cell_type": "code", "execution_count": null, "source": ["from sklearn import metrics\n", "print(\"Area under the Curve:\")\n", "metrics.auc(falsePositiveRate[\"micro\"],truePositiveRate[\"micro\"]) #Area under the curve"], "outputs": []}, {"metadata": {"_cell_guid": "5db0e733-df49-4a99-bf3e-0f47b3bd153a", "_uuid": "2986afe98a864f8dae6f32d122c5c277557f653d"}, "cell_type": "code", "execution_count": null, "source": ["print('Log loss:')\n", "metrics.log_loss(y_test, y_predict)"], "outputs": []}, {"metadata": {"collapsed": true, "_cell_guid": "5c027d09-9a71-4606-83b6-e479017ff522", "_uuid": "4278a8aedc33f0d00e12231f86fdf2c36da0a322"}, "cell_type": "code", "execution_count": null, "source": [], "outputs": []}, {"metadata": {"collapsed": true, "_cell_guid": "5fd60ec6-3446-4045-b178-2f56ad7ebc71", "_uuid": "aa9b17122d138846ac17e2ccf01645ac4c08b8ac"}, "cell_type": "code", "execution_count": null, "source": [], "outputs": []}], "nbformat_minor": 1}