{"cells": [{"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 io import BytesIO\n", "import cv2\n", "import bson\n", "from skimage.data import imread\n", "import matplotlib.pyplot as plt\n", "import keras\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", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "2175a5c3-8b06-4112-995c-155d45ee76b7", "_uuid": "6766ab6a1a7e0d985b96a43260e59bc56dafa396"}}, {"source": ["category_data=pd.read_csv(\"../input/category_names.csv\")\n", "print(\"Total categories are:\", len(category_data))\n", "category_data.head(0)"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "3e8352b2-a4a7-41b8-aa46-9cdf54657cdc", "_uuid": "d8557ce6e662352c85594dbe362ec1476bad1592"}}, {"source": ["def get_the_data(path):\n", "    data = bson.decode_file_iter(open(path, 'rb'))\n", "    images=[]\n", "    category=[]\n", "    for c, d in enumerate(data):\n", "        product_id = d['_id']\n", "        category_id = d['category_id'] # This won't be in Test data\n", "        #prod_to_category[product_id] = category_id\n", "        for e, pic in enumerate(d['imgs']):\n", "            category.append(category_id)\n", "            picture = imread(BytesIO(pic['picture']))\n", "            #picture=pic['picture']\n", "            images.append(picture)\n", "            #break\n", "        if(len(set(category))==1500):\n", "            break\n", "    return category, images"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "afcb8630-ca9c-4fbd-ba57-5a6a0f60151e", "collapsed": true, "_uuid": "d64be65bcc52f6d7107e027aff75d0b8ec9003c8"}}, {"source": ["product_category_train,image_train=get_the_data('../input/train_example.bson')"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "1fc178ea-7470-4f06-b5ea-02cc3b185d2e", "collapsed": true, "_uuid": "2222609c065aa349f80025c799575c1d8130d317"}}, {"source": ["product_category_train,image_train=get_the_data('../input/train.bson')"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "2505c6c1-577a-4b5c-aced-91258705c6c5", "collapsed": true, "_uuid": "bd04247033350593d6d985ada8948f5773f6b707"}}, {"source": ["def img2feat(im):\n", "    return np.float32(im) / 255"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "e4704de8-d11f-4812-9b4e-dd6f6f9a4975", "collapsed": true, "_uuid": "bedcad6e9c2f74f03a8a8004e04791bdfaaa97e0"}}, {"source": ["final=np.array(image_train)"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "ee73fa3b-800c-45fe-9823-dcb327dc5dcd", "collapsed": true, "_uuid": "65b803954fc2477f9ba405b1c0bf6fdbcd99b534"}}, {"source": ["final_train=img2feat(final)"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "266ccc9f-6e75-4fbf-8c1b-c3ac342dfc60", "collapsed": true, "_uuid": "889c8d3ea52ae65cf08e097746bb802a73ab0b35"}}, {"source": ["y, rev_labels = pd.factorize(product_category_train)"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "958dd304-7595-4af4-8380-6a664d41d5aa", "collapsed": true, "_uuid": "f90442b42230e597cb4307482a7e82181e5ddf29"}}, {"source": ["from sklearn.utils import shuffle\n", "im_train,lab_train=shuffle(final_train,y)\n", "test_im=im_train[20000:]\n", "test_lab=lab_train[20000:]\n", "image_train=im_train[:20000]\n", "label_train=lab_train[:20000]"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {}}, {"source": ["from keras.layers import Conv2D, MaxPooling2D,Dropout,Dense, Flatten\n", "from keras.models import Sequential\n", "from keras.optimizers import Adam"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "4e9001e6-5d31-4e11-aec0-f5822acba860", "collapsed": true, "_uuid": "724b7df3a22a53264f411c94ca62aa6cc8f324fe"}}, {"source": ["model=Sequential()\n", "model.add(Conv2D(16,3,activation='relu',input_shape=(180,180,3)))\n", "model.add(Conv2D(32,3,activation='relu'))\n", "model.add(MaxPooling2D(2))\n", "model.add(Dropout(0.2))\n", "model.add(Conv2D(32,3,activation='relu'))\n", "model.add(Conv2D(32,3,activation='relu'))\n", "model.add(MaxPooling2D(2))\n", "model.add(Dropout(0.2))\n", "model.add(Conv2D(64,3,activation='relu'))\n", "model.add(Conv2D(64,3,activation='relu'))\n", "model.add(MaxPooling2D(2))\n", "model.add(Dropout(0.2))\n", "model.add(Conv2D(32,3,activation='relu'))\n", "model.add(Conv2D(16,3,activation='relu'))\n", "model.add(MaxPooling2D(2))\n", "model.add(Dropout(0.2))\n", "model.add(Flatten())\n", "model.add(Dense(len(rev_labels),activation='softmax'))"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "9df4ba12-3b12-4dd6-b757-2cc568a9f840", "collapsed": true, "_uuid": "a480fc0bfabdd2d7365d0e6729f8a8864c53019d"}}, {"source": ["model.compile('Adam','sparse_categorical_crossentropy', metrics=['accuracy'])"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "a0e572a0-0a98-492a-8fd4-2e4fdcbb2ca9", "collapsed": true, "_uuid": "f040dfadb01d5a3573c84770789e678701473c7d"}}, {"source": ["model.summary()"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "25195995-f3a1-48d8-a847-22d2c482cc32", "_uuid": "936ba4ddbaeee32383d8e403f4234cc9a49d9ad2"}}, {"source": ["model.fit(final_train,y,validation_split=0.2,epochs=2,batch_size=50)"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "95f7e520-8354-4202-960d-5b3d77469053", "_uuid": "4ad6d56cb8b6b89d9dca4dcd2641ae1cec96d597"}}, {"source": ["test=np.array(image)\n", "test_image=np.float32(test)/255\n"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "1f5778df-2e74-437b-b229-761ef57a68c4", "collapsed": true, "_uuid": "1b0217ac540e2a917a672f9bb4ea3faa0dd34f7a"}}, {"source": ["pred=model.predict(test_image)"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "3e5538a7-ec20-48a8-b7fb-99aea3094ef3", "collapsed": true, "_uuid": "66ea109bdc8ac774911db8cf8895046c71937a39"}}, {"source": ["acc=[]\n", "for i in pred:\n", "    acc.append(np.argmax(i))"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "3cebb40d-8773-4c7a-a5b6-d25b421ccc4b", "collapsed": true, "_uuid": "f6a3181a6b9ceb750d37e31e82925e28b255a9c8"}}, {"source": ["rev_labels[acc[104]]"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "5fbff5a7-04ca-4179-8713-c6ec25d1d229", "collapsed": true, "_uuid": "af4384b046d1528c4ca0f51cccb2bb5ab041bff8"}}, {"source": ["product_category[104]"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "2a3565ed-4624-4765-822c-542032c6e131", "collapsed": true, "_uuid": "640306e9385c7ee469d50525b1b6785828e4ccff"}}, {"source": ["label_acc=[]\n", "for i in acc:\n", "    label_acc.append(rev_labels[i])"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "b28926cc-ac33-4696-bd83-56f7423ac55e", "collapsed": true, "_uuid": "cbb9b43f0b94eeccce5f4962c78d7aa1f4cdfa90"}}, {"source": ["from sklearn.metrics import accuracy_score\n", "accuracy=accuracy_score(product_category,label_acc)"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "52b62d76-a3b9-42b4-8c97-be5c25e2c247", "collapsed": true, "_uuid": "d83a0ba8b5430b64cb4320f30b323e3b3a9fb623"}}, {"source": ["accuracy"], "cell_type": "code", "outputs": [], "execution_count": null, "metadata": {"_cell_guid": "e5b8712b-9fa4-4893-8cdc-7f8cbfd75b1b", "collapsed": true, "_uuid": "36565d20e11a0c200e1c32a5b5fc5cf91f5e56e7"}}], "nbformat": 4, "nbformat_minor": 1, "metadata": {"language_info": {"codemirror_mode": {"version": 3, "name": "ipython"}, "name": "python", "mimetype": "text/x-python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3", "file_extension": ".py"}, "kernelspec": {"display_name": "Python 3", "name": "python3", "language": "python"}}}