{"cells":[{"metadata":{"_uuid":"c6deaf05316125e44af90d253b415e6e13a24954","_cell_guid":"5eb9cb21-3232-4803-b586-a5161324b4f4"},"cell_type":"markdown","source":"# Dog Breed Identification with Keras\n### This notebook only uses the top 20 breeds due to memory limitations.\nDataset=https://www.kaggle.com/c/dog-breed-identification/data","execution_count":null},{"metadata":{"_uuid":"b357ed74056d39353bad382301868e6d91faffcc","_cell_guid":"a7b4dd9a-778e-4cba-ae75-98afaca61cd7"},"cell_type":"markdown","source":"### 1. Loading Libraries","execution_count":null},{"metadata":{"_uuid":"38c88f049dcbc4f1979943cd75eb57ff2dc0d10f","_cell_guid":"7185c1bf-9f5e-4ec9-b00a-1067bd7d5e22","trusted":true},"cell_type":"code","source":"from keras.layers import Dense,Dropout,Input,MaxPooling2D,ZeroPadding2D,Conv2D,Flatten\nfrom keras.models import Sequential\nfrom keras.losses import categorical_crossentropy\nfrom keras.optimizers import Adam,SGD\nfrom keras.preprocessing.image import img_to_array,load_img,ImageDataGenerator\nfrom keras.utils import to_categorical\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\npd.options.display.max_colwidth=150","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a0196495f9645b524277150367b44deaf2ffdffb","_cell_guid":"8e951182-9e6a-47da-8ac0-a95f0d504e34"},"cell_type":"markdown","source":"### 2. Loading Dataset","execution_count":null},{"metadata":{"_uuid":"96210507e7da39e731634555361bd54205dd7c1e","_cell_guid":"3ead4b24-7088-4096-9812-3904dc5f168c"},"cell_type":"markdown","source":"Loading dog id and breed name","execution_count":null},{"metadata":{"_uuid":"3ccc15147b48f95d8b56ec1473eb7fd7599967c3","_cell_guid":"ee523639-fd01-4895-9629-23be52aaedb3","trusted":true},"cell_type":"code","source":"df1=pd.read_csv('../input/labels.csv')\ndf1.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c5b42c3fbc2ad5c3f4f1a94deab96fdb2cfa63ee","_cell_guid":"7b8e9ab2-5937-4edf-ac25-c421b7c5e666","trusted":true},"cell_type":"code","source":"# path of the dogs images\nimg_file='../input/train/'","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1eacae5f383ecc732c97f06818357c49a701cba6","_cell_guid":"f48146fa-a3ea-4162-be49-50d23d6c0a00"},"cell_type":"markdown","source":"Adding path of the dog image to its id and breed","execution_count":null},{"metadata":{"_uuid":"b2be82861e7d2b5755de9857de184e705689a66e","_cell_guid":"19b2911f-3d2e-4bf6-9f49-65952e057150","trusted":true},"cell_type":"code","source":"df=df1.assign(img_path=lambda x: img_file + x['id'] +'.jpg')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"212b6fa70fdecb5a4be7e13b40403687f7e31f83","_cell_guid":"04ab44ec-ef5e-4a01-9a63-3a7edac4147e"},"cell_type":"markdown","source":"### 3. Basic Visualization","execution_count":null},{"metadata":{"_uuid":"b021952def17149058bcb93f8dd1e42b094494f7","_cell_guid":"547748d1-9bb6-45f0-87df-9bc9ad12263b","trusted":true},"cell_type":"code","source":"#Number of different breed type\ndf.breed.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e0cfdb158dbbb06d721711e3e800c60f39051336","_cell_guid":"e605219f-25f1-400b-8bd9-0dbb2cf1ff8f","trusted":true},"cell_type":"code","source":"# Take a look at the class/breed distribution\nax=pd.value_counts(df['breed'],ascending=True).plot(kind='barh',fontsize=\"40\",title=\"Class Distribution\",figsize=(50,100))\nax.set(xlabel=\"Images per class\", ylabel=\"Classes\")\nax.xaxis.label.set_size(40)\nax.yaxis.label.set_size(40)\nax.title.set_size(60)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3571737b956a6fe3bf20be4f84fdeae01eb693f1","_cell_guid":"bc247aba-ea37-498b-b6f2-4268fe941975"},"cell_type":"markdown","source":"#### Selecting only Top 20 breed due to system limitation","execution_count":null},{"metadata":{"_uuid":"02affb251115f836c48bb2197497a4a80cbbaf98","_cell_guid":"bddd1260-4ee7-481b-95f0-c2b7741e0e04","trusted":true},"cell_type":"code","source":"#Top 20 breed\ntop_20=list(df.breed.value_counts()[0:20].index)\ntop_20","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"897ff3dc34305e962c5a86c700183e4c254363b7","_cell_guid":"9d21c76b-b54d-450e-8f01-2f07c42bd08c","trusted":true},"cell_type":"code","source":"df2=df[df.breed.isin(top_20)]\ndf2.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a2f0935da483ef302a59508d7c31c8e6ce765148","_cell_guid":"69cf5f3d-7001-47c7-bcb7-a222302d0f13"},"cell_type":"markdown","source":"### 4. Loading Images and converting it to pixel\nFor Machine Learning Operation, we are loading the images and converting it to numpy array of pixel ","execution_count":null},{"metadata":{"_uuid":"d855b750e836c47f48d29f0b3b3fa9dfe197fa54","_cell_guid":"d5045e83-e641-4155-998c-a43757aa03e2","trusted":true},"cell_type":"code","source":"img_pixel=np.array([img_to_array(load_img(img, target_size=(299, 299))) for img in df2['img_path'].values.tolist()])\nimg_pixel.shape","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"677fb438fc3c4e5612b8c6832a1deafc4a0dbdde","_cell_guid":"f2054e55-44cf-40f4-9641-7a8e7cc28387"},"cell_type":"markdown","source":"### 5. Label encoding Dogs breed name for prediction\n","execution_count":null},{"metadata":{"_uuid":"294a89d462ae6e69fde106b7637d3c81f9744fdf","_cell_guid":"c01abe59-b65e-46b6-8241-ddb4b62d0a99","trusted":true},"cell_type":"code","source":"img_label=df2.breed\nimg_label=pd.get_dummies(df2.breed)\nimg_label.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"2516066ddfe39b2343c4e1c1419a008cae1ce627","_cell_guid":"9ef1559c-e749-4326-bfc8-4f280a22fe8a"},"cell_type":"markdown","source":"### 6. Final X,y Matrix for Deep learning prediction","execution_count":null},{"metadata":{"_uuid":"5c1d77752e11d938c35dfa0f89225e0583a30138","_cell_guid":"feb22b6d-36c1-4dea-acf0-3c6afe42b109","trusted":true},"cell_type":"code","source":"X=img_pixel\ny=img_label.values\nprint(X.shape)\nprint(y.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ca768034967d6c58e424366e0d5ec7b7c443bb6c","_cell_guid":"5049fae8-267b-4dd8-a4ee-6eaa87d2ce73"},"cell_type":"markdown","source":"### 7. Train test Split","execution_count":null},{"metadata":{"_uuid":"b1f48d7f8d806edbbab0c613f6cb1f48c686b054","_cell_guid":"577a3464-a5bd-4b8c-a1ec-86d2a9dfc4de","trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2)\nprint(X_train.shape)\nprint(y_train.shape)\nprint(X_test.shape)\nprint(y_test.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"15a2692c328a84a0fdc1bbf4f92664e5366d1dc7","_cell_guid":"da3c745e-7ba3-4be1-b954-7dc837956472"},"cell_type":"markdown","source":"### 8. Data pre-processing and data augmentation<br>\nImage generator, to generate rotated,shifted,flipped images etc.\nTo over come Translational invariance","execution_count":null},{"metadata":{"collapsed":true,"_uuid":"8c1b44b42f585b3d82653191453a1a2e075d3dd5","_cell_guid":"9a3f929a-9a90-4192-8989-7d731ad7173c","trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n    rotation_range=30,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest')\n\ntest_datagen=ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"e250b0b52c12bce40206690378fd7bcea45cbc8c","_cell_guid":"dbe33f07-9184-4c1e-9ab9-4e19406b7e39","trusted":true},"cell_type":"code","source":"training_set=train_datagen.flow(X_train,y=y_train,batch_size=32)\ntesting_set=test_datagen.flow(X_test,y=y_test,batch_size=32)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e02bb76f9f08b3f8d9c11131237b8b25da1f2a71","_cell_guid":"768a6509-eda0-419a-b8ce-a9a7a5ae14f9"},"cell_type":"markdown","source":"### 9. Defining Deep Learning Model","execution_count":null},{"metadata":{"_uuid":"ed852e3f180e84e00163014e32833260fe14a76d","_cell_guid":"0a79f5ef-c24a-4566-8151-415e8ea6bc7a","trusted":true},"cell_type":"code","source":"model=Sequential()\n\nmodel.add(ZeroPadding2D((1,1),input_shape=(299,299,3)))\nmodel.add(Conv2D(32,kernel_size=(3,3),activation='relu'))\nmodel.add(ZeroPadding2D(padding=(1,1)))\nmodel.add(Conv2D(32,kernel_size=(3,3),activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(64,activation='relu'))\nmodel.add(Dropout(0.2))\n\nmodel.add(Dense(20,activation='softmax'))\n\nmodel.compile(loss=categorical_crossentropy,optimizer='adam',metrics=['accuracy'])\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b38cf4bebb6ab24d9bb2c7f32015321770486bd0","_cell_guid":"adf499e9-03bd-45ce-85ae-10eb54d5eaec"},"cell_type":"markdown","source":"### 10. Fitting the Deep Learning Model","execution_count":null},{"metadata":{"_uuid":"d51628a4729fdac16939f18746798c3c649dd17c","_cell_guid":"64022374-2e16-4bc0-b2e2-fc0038215435","trusted":true},"cell_type":"code","source":"history=model.fit_generator(training_set,\n                    steps_per_epoch = 16,\n                    validation_data = testing_set,\n                    validation_steps = 4,\n                    epochs = 2,\n                    verbose = 1)","execution_count":null,"outputs":[]},{"metadata":{"collapsed":true,"_uuid":"873a605d076bfb4caaf20307219fc39e96cb1a80","_cell_guid":"2d04241b-7bbe-4b1a-8152-853e26124f5c","trusted":false},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}