{"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) \nimport cv2\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\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-07-04T13:32:20.430237Z","iopub.execute_input":"2021-07-04T13:32:20.430646Z","iopub.status.idle":"2021-07-04T13:32:39.532348Z","shell.execute_reply.started":"2021-07-04T13:32:20.430566Z","shell.execute_reply":"2021-07-04T13:32:39.531086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"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-07-04T13:32:39.542351Z","iopub.execute_input":"2021-07-04T13:32:39.542631Z","iopub.status.idle":"2021-07-04T13:32:39.599896Z","shell.execute_reply.started":"2021-07-04T13:32:39.542602Z","shell.execute_reply":"2021-07-04T13:32:39.598724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label.breed.unique()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T13:32:39.603892Z","iopub.execute_input":"2021-07-04T13:32:39.604152Z","iopub.status.idle":"2021-07-04T13:32:39.619915Z","shell.execute_reply.started":"2021-07-04T13:32:39.604126Z","shell.execute_reply":"2021-07-04T13:32:39.618745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label.breed.nunique()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T13:32:39.623959Z","iopub.execute_input":"2021-07-04T13:32:39.624234Z","iopub.status.idle":"2021-07-04T13:32:39.63841Z","shell.execute_reply.started":"2021-07-04T13:32:39.624208Z","shell.execute_reply":"2021-07-04T13:32:39.636956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T13:32:39.646071Z","iopub.execute_input":"2021-07-04T13:32:39.646345Z","iopub.status.idle":"2021-07-04T13:32:39.65858Z","shell.execute_reply.started":"2021-07-04T13:32:39.646318Z","shell.execute_reply":"2021-07-04T13:32:39.657677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label.breed.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T13:32:39.662397Z","iopub.execute_input":"2021-07-04T13:32:39.662673Z","iopub.status.idle":"2021-07-04T13:32:39.676813Z","shell.execute_reply.started":"2021-07-04T13:32:39.662646Z","shell.execute_reply":"2021-07-04T13:32:39.676081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img=cv2.imread('../input/dog-breed-identification/test/000621fb3cbb32d8935728e48679680e.jpg')","metadata":{"execution":{"iopub.status.busy":"2021-07-04T13:32:39.680157Z","iopub.execute_input":"2021-07-04T13:32:39.680446Z","iopub.status.idle":"2021-07-04T13:32:39.717646Z","shell.execute_reply.started":"2021-07-04T13:32:39.680421Z","shell.execute_reply":"2021-07-04T13:32:39.716781Z"},"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-07-04T13:32:39.719363Z","iopub.execute_input":"2021-07-04T13:32:39.719639Z","iopub.status.idle":"2021-07-04T13:32:39.953899Z","shell.execute_reply.started":"2021-07-04T13:32:39.719614Z","shell.execute_reply":"2021-07-04T13:32:39.953192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-04T13:32:39.955317Z","iopub.execute_input":"2021-07-04T13:32:39.955663Z","iopub.status.idle":"2021-07-04T13:32:39.961218Z","shell.execute_reply.started":"2021-07-04T13:32:39.955624Z","shell.execute_reply":"2021-07-04T13:32:39.96039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"H=128\nW=128\nC=3","metadata":{"execution":{"iopub.status.busy":"2021-07-04T13:32:39.962549Z","iopub.execute_input":"2021-07-04T13:32:39.963102Z","iopub.status.idle":"2021-07-04T13:32:39.969766Z","shell.execute_reply.started":"2021-07-04T13:32:39.963064Z","shell.execute_reply":"2021-07-04T13:32:39.968815Z"},"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-07-04T13:32:39.971726Z","iopub.execute_input":"2021-07-04T13:32:39.972399Z","iopub.status.idle":"2021-07-04T13:32:40.00075Z","shell.execute_reply.started":"2021-07-04T13:32:39.972284Z","shell.execute_reply":"2021-07-04T13:32:40.000123Z"},"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-07-04T13:32:40.00301Z","iopub.execute_input":"2021-07-04T13:32:40.003362Z","iopub.status.idle":"2021-07-04T13:33:41.554484Z","shell.execute_reply.started":"2021-07-04T13:32:40.00333Z","shell.execute_reply":"2021-07-04T13:33:41.552777Z"},"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-07-04T13:33:41.555807Z","iopub.execute_input":"2021-07-04T13:33:41.556158Z","iopub.status.idle":"2021-07-04T13:33:42.669782Z","shell.execute_reply.started":"2021-07-04T13:33:41.556119Z","shell.execute_reply":"2021-07-04T13:33:42.669032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Conv2D,MaxPool2D,Dropout,Flatten,Activation","metadata":{"execution":{"iopub.status.busy":"2021-07-04T13:33:42.670972Z","iopub.execute_input":"2021-07-04T13:33:42.671298Z","iopub.status.idle":"2021-07-04T13:33:42.675407Z","shell.execute_reply.started":"2021-07-04T13:33:42.671262Z","shell.execute_reply":"2021-07-04T13:33:42.674585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"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-07-04T13:33:42.676611Z","iopub.execute_input":"2021-07-04T13:33:42.677103Z","iopub.status.idle":"2021-07-04T13:33:45.057254Z","shell.execute_reply.started":"2021-07-04T13:33:42.677063Z","shell.execute_reply":"2021-07-04T13:33:45.056422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch=32","metadata":{"execution":{"iopub.status.busy":"2021-07-04T13:33:45.061025Z","iopub.execute_input":"2021-07-04T13:33:45.061439Z","iopub.status.idle":"2021-07-04T13:33:45.069961Z","shell.execute_reply.started":"2021-07-04T13:33:45.061407Z","shell.execute_reply":"2021-07-04T13:33:45.068814Z"},"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-07-04T13:33:45.081964Z","iopub.execute_input":"2021-07-04T13:33:45.082451Z","iopub.status.idle":"2021-07-04T13:33:45.100137Z","shell.execute_reply.started":"2021-07-04T13:33:45.082411Z","shell.execute_reply":"2021-07-04T13:33:45.099091Z"},"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-07-04T13:33:45.101853Z","iopub.execute_input":"2021-07-04T13:33:45.102277Z","iopub.status.idle":"2021-07-04T13:34:44.620649Z","shell.execute_reply.started":"2021-07-04T13:33:45.102231Z","shell.execute_reply":"2021-07-04T13:34:44.619856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator()\n\ntest_set = test_datagen.flow_from_directory(\n    '/kaggle/input/dog-breed-identification',\n    target_size = (128,128),\n    classes=['test']\n)\ny_pred = model.predict(test_set)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T13:37:50.294814Z","iopub.execute_input":"2021-07-04T13:37:50.295143Z","iopub.status.idle":"2021-07-04T13:39:16.390571Z","shell.execute_reply.started":"2021-07-04T13:37:50.295112Z","shell.execute_reply":"2021-07-04T13:39:16.389746Z"},"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-07-04T13:39:16.392207Z","iopub.execute_input":"2021-07-04T13:39:16.392546Z","iopub.status.idle":"2021-07-04T13:39:16.850329Z","shell.execute_reply.started":"2021-07-04T13:39:16.392509Z","shell.execute_reply":"2021-07-04T13:39:16.849541Z"},"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-07-04T13:40:18.018897Z","iopub.execute_input":"2021-07-04T13:40:18.019228Z","iopub.status.idle":"2021-07-04T13:40:18.48025Z","shell.execute_reply.started":"2021-07-04T13:40:18.019199Z","shell.execute_reply":"2021-07-04T13:40:18.47926Z"},"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-07-04T13:40:55.280908Z","iopub.execute_input":"2021-07-04T13:40:55.281239Z","iopub.status.idle":"2021-07-04T13:40:58.084964Z","shell.execute_reply.started":"2021-07-04T13:40:55.281209Z","shell.execute_reply":"2021-07-04T13:40:58.084165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}