{"cells":[{"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":"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":"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":"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":"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":"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":"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":"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":"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":{"_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":"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":"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":{"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}