{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-02T15:43:29.950347Z","iopub.execute_input":"2021-08-02T15:43:29.951155Z","iopub.status.idle":"2021-08-02T15:43:47.041137Z","shell.execute_reply.started":"2021-08-02T15:43:29.951006Z","shell.execute_reply":"2021-08-02T15:43:47.027918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label= pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\ny_label.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:43:47.042411Z","iopub.execute_input":"2021-08-02T15:43:47.042735Z","iopub.status.idle":"2021-08-02T15:43:47.103644Z","shell.execute_reply.started":"2021-08-02T15:43:47.042701Z","shell.execute_reply":"2021-08-02T15:43:47.102726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label.breed.unique()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:43:47.105618Z","iopub.execute_input":"2021-08-02T15:43:47.105954Z","iopub.status.idle":"2021-08-02T15:43:47.115911Z","shell.execute_reply.started":"2021-08-02T15:43:47.105919Z","shell.execute_reply":"2021-08-02T15:43:47.114943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label.breed.nunique()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:43:47.117823Z","iopub.execute_input":"2021-08-02T15:43:47.118326Z","iopub.status.idle":"2021-08-02T15:43:47.127273Z","shell.execute_reply.started":"2021-08-02T15:43:47.118290Z","shell.execute_reply":"2021-08-02T15:43:47.126114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:43:47.128698Z","iopub.execute_input":"2021-08-02T15:43:47.129222Z","iopub.status.idle":"2021-08-02T15:43:47.140213Z","shell.execute_reply.started":"2021-08-02T15:43:47.129187Z","shell.execute_reply":"2021-08-02T15:43:47.139004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label.breed.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:43:47.141364Z","iopub.execute_input":"2021-08-02T15:43:47.141722Z","iopub.status.idle":"2021-08-02T15:43:47.152364Z","shell.execute_reply.started":"2021-08-02T15:43:47.141688Z","shell.execute_reply":"2021-08-02T15:43:47.151287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt\nimg=cv2.imread('../input/dog-breed-identification/test/000621fb3cbb32d8935728e48679680e.jpg')","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:43:47.153890Z","iopub.execute_input":"2021-08-02T15:43:47.154462Z","iopub.status.idle":"2021-08-02T15:43:47.313091Z","shell.execute_reply.started":"2021-08-02T15:43:47.154305Z","shell.execute_reply":"2021-08-02T15:43:47.312360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\nplt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:43:47.315618Z","iopub.execute_input":"2021-08-02T15:43:47.315859Z","iopub.status.idle":"2021-08-02T15:43:47.528309Z","shell.execute_reply.started":"2021-08-02T15:43:47.315835Z","shell.execute_reply":"2021-08-02T15:43:47.527571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:43:47.530939Z","iopub.execute_input":"2021-08-02T15:43:47.531215Z","iopub.status.idle":"2021-08-02T15:43:47.536775Z","shell.execute_reply.started":"2021-08-02T15:43:47.531187Z","shell.execute_reply":"2021-08-02T15:43:47.535992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"H=128\nW=128\nC=3","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:43:47.538382Z","iopub.execute_input":"2021-08-02T15:43:47.538938Z","iopub.status.idle":"2021-08-02T15:43:47.543088Z","shell.execute_reply.started":"2021-08-02T15:43:47.538899Z","shell.execute_reply":"2021-08-02T15:43:47.542199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file_location = '../input/dog-breed-identification/train/' \ntrain_data = y_label.assign(img_path = lambda x : train_file_location + x['id'] + '.jpg')\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:43:47.544432Z","iopub.execute_input":"2021-08-02T15:43:47.544932Z","iopub.status.idle":"2021-08-02T15:43:47.574363Z","shell.execute_reply.started":"2021-08-02T15:43:47.544898Z","shell.execute_reply":"2021-08-02T15:43:47.573652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import load_img,img_to_array, ImageDataGenerator\nX = np.array([img_to_array(load_img(img,target_size = (H,W))) for img in train_data['img_path'].values.tolist()])\nprint(X.shape)\nY = pd.get_dummies(train_data['breed'])\nprint(Y.shape)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:43:47.575423Z","iopub.execute_input":"2021-08-02T15:43:47.575733Z","iopub.status.idle":"2021-08-02T15:45:15.634746Z","shell.execute_reply.started":"2021-08-02T15:43:47.575700Z","shell.execute_reply":"2021-08-02T15:45:15.633894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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.25)\nprint(X_train.shape,Y_train.shape)\nprint(X_test.shape,Y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:45:15.636084Z","iopub.execute_input":"2021-08-02T15:45:15.636446Z","iopub.status.idle":"2021-08-02T15:45:16.746086Z","shell.execute_reply.started":"2021-08-02T15:45:15.636407Z","shell.execute_reply":"2021-08-02T15:45:16.745200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense,Conv2D,MaxPool2D,Dropout,Flatten,Activation","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:45:16.747376Z","iopub.execute_input":"2021-08-02T15:45:16.747854Z","iopub.status.idle":"2021-08-02T15:45:16.752783Z","shell.execute_reply.started":"2021-08-02T15:45:16.747814Z","shell.execute_reply":"2021-08-02T15:45:16.751907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\n\nmodel.add(Conv2D(32,(3,3),input_shape=(H,W,C)))\nmodel.add(Activation('relu'))\n\nmodel.add(MaxPool2D((2,2)))\n\nmodel.add(Conv2D(64,(3,3)))\nmodel.add(Activation('relu'))\n\nmodel.add(MaxPool2D((2,2)))\n\nmodel.add(Conv2D(128,(3,3)))\nmodel.add(Activation('relu'))\n\nmodel.add(MaxPool2D((2,2)))\n\nmodel.add(Conv2D(128,(3,3)))\nmodel.add(Activation('relu'))\n\nmodel.add(MaxPool2D((2,2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(512))\nmodel.add(Activation('relu'))\n\nmodel.add(Dense(Y.shape[1]))\nmodel.add(Activation('softmax'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:45:16.754182Z","iopub.execute_input":"2021-08-02T15:45:16.754572Z","iopub.status.idle":"2021-08-02T15:45:18.660949Z","shell.execute_reply.started":"2021-08-02T15:45:16.754532Z","shell.execute_reply":"2021-08-02T15:45:18.660043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n      optimizer='adam',\n      loss='categorical_crossentropy',\n      metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:45:18.664294Z","iopub.execute_input":"2021-08-02T15:45:18.664612Z","iopub.status.idle":"2021-08-02T15:45:18.680608Z","shell.execute_reply.started":"2021-08-02T15:45:18.664574Z","shell.execute_reply":"2021-08-02T15:45:18.679767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch=32","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:45:18.683822Z","iopub.execute_input":"2021-08-02T15:45:18.684064Z","iopub.status.idle":"2021-08-02T15:45:18.689835Z","shell.execute_reply.started":"2021-08-02T15:45:18.684039Z","shell.execute_reply":"2021-08-02T15:45:18.688957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trained_model=model.fit(X_train,Y_train,\n         epochs=20,\n         batch_size=batch,\n         steps_per_epoch=X_train.shape[0]//batch,\n         validation_steps=X_test.shape[0]//batch,\n         validation_data=(X_test,Y_test),\n         verbose=2)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:45:18.691146Z","iopub.execute_input":"2021-08-02T15:45:18.691532Z","iopub.status.idle":"2021-08-02T15:46:24.721935Z","shell.execute_reply.started":"2021-08-02T15:45:18.691496Z","shell.execute_reply":"2021-08-02T15:46:24.720909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator()\n\ntest_set = test_datagen.flow_from_directory('/kaggle/input/dog-breed-identification',\n                                            target_size = (128,128),\n                                            classes=['test'])\ny_pred = model.predict(test_set)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:46:24.723798Z","iopub.execute_input":"2021-08-02T15:46:24.724264Z","iopub.status.idle":"2021-08-02T15:47:51.545768Z","shell.execute_reply.started":"2021-08-02T15:46:24.724215Z","shell.execute_reply":"2021-08-02T15:47:51.544758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:47:51.549500Z","iopub.execute_input":"2021-08-02T15:47:51.549801Z","iopub.status.idle":"2021-08-02T15:47:52.102895Z","shell.execute_reply.started":"2021-08-02T15:47:51.549770Z","shell.execute_reply":"2021-08-02T15:47:52.102068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\nfile_list = test_set.filenames\nid_list = []\nfor name in file_list:\n    m = re.sub('test/', '', name)\n    m = re.sub('.jpg', '', m)\n    id_list.append(m)\n    \nsubmission['id'] = id_list\nsubmission.iloc[:,1:] =y_pred\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:47:52.104238Z","iopub.execute_input":"2021-08-02T15:47:52.104576Z","iopub.status.idle":"2021-08-02T15:47:52.676375Z","shell.execute_reply.started":"2021-08-02T15:47:52.104524Z","shell.execute_reply":"2021-08-02T15:47:52.675333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df = submission.set_index('id')\nfinal_df.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:47:52.677938Z","iopub.execute_input":"2021-08-02T15:47:52.678407Z","iopub.status.idle":"2021-08-02T15:47:55.683121Z","shell.execute_reply.started":"2021-08-02T15:47:52.678361Z","shell.execute_reply":"2021-08-02T15:47:55.682205Z"},"trusted":true},"execution_count":null,"outputs":[]}]}