{"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:50:11.916822Z","iopub.execute_input":"2021-08-02T15:50:11.917499Z","iopub.status.idle":"2021-08-02T15:50:30.330153Z","shell.execute_reply.started":"2021-08-02T15:50:11.917401Z","shell.execute_reply":"2021-08-02T15:50:30.329148Z"},"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:50:30.336469Z","iopub.execute_input":"2021-08-02T15:50:30.336893Z","iopub.status.idle":"2021-08-02T15:50:30.470633Z","shell.execute_reply.started":"2021-08-02T15:50:30.336845Z","shell.execute_reply":"2021-08-02T15:50:30.469396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label.breed.unique()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:50:30.474418Z","iopub.execute_input":"2021-08-02T15:50:30.474740Z","iopub.status.idle":"2021-08-02T15:50:30.486737Z","shell.execute_reply.started":"2021-08-02T15:50:30.474707Z","shell.execute_reply":"2021-08-02T15:50:30.485389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label.breed.nunique()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:50:30.488686Z","iopub.execute_input":"2021-08-02T15:50:30.490151Z","iopub.status.idle":"2021-08-02T15:50:30.501488Z","shell.execute_reply.started":"2021-08-02T15:50:30.490006Z","shell.execute_reply":"2021-08-02T15:50:30.500070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:50:30.503711Z","iopub.execute_input":"2021-08-02T15:50:30.504368Z","iopub.status.idle":"2021-08-02T15:50:30.518904Z","shell.execute_reply.started":"2021-08-02T15:50:30.504320Z","shell.execute_reply":"2021-08-02T15:50:30.517226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_label.breed.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:50:30.520891Z","iopub.execute_input":"2021-08-02T15:50:30.521622Z","iopub.status.idle":"2021-08-02T15:50:30.537978Z","shell.execute_reply.started":"2021-08-02T15:50:30.521575Z","shell.execute_reply":"2021-08-02T15:50:30.536434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:50:30.540121Z","iopub.execute_input":"2021-08-02T15:50:30.540611Z","iopub.status.idle":"2021-08-02T15:50:30.725295Z","shell.execute_reply.started":"2021-08-02T15:50:30.540565Z","shell.execute_reply":"2021-08-02T15:50:30.724094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img=cv2.imread('../input/dog-breed-identification/train/a9fafe79f816b5032af49b12fa0cebae.jpg')","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:52:12.405243Z","iopub.execute_input":"2021-08-02T15:52:12.405656Z","iopub.status.idle":"2021-08-02T15:52:12.418964Z","shell.execute_reply.started":"2021-08-02T15:52:12.405625Z","shell.execute_reply":"2021-08-02T15:52:12.417831Z"},"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:52:19.055069Z","iopub.execute_input":"2021-08-02T15:52:19.055796Z","iopub.status.idle":"2021-08-02T15:52:19.255375Z","shell.execute_reply.started":"2021-08-02T15:52:19.055763Z","shell.execute_reply":"2021-08-02T15:52:19.254309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:52:26.359578Z","iopub.execute_input":"2021-08-02T15:52:26.359949Z","iopub.status.idle":"2021-08-02T15:52:26.368233Z","shell.execute_reply.started":"2021-08-02T15:52:26.359917Z","shell.execute_reply":"2021-08-02T15:52:26.366434Z"},"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:50:31.065180Z","iopub.execute_input":"2021-08-02T15:50:31.066095Z","iopub.status.idle":"2021-08-02T15:50:31.102961Z","shell.execute_reply.started":"2021-08-02T15:50:31.065955Z","shell.execute_reply":"2021-08-02T15:50:31.101728Z"},"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 = (128,128))) 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:50:31.104732Z","iopub.execute_input":"2021-08-02T15:50:31.105462Z","iopub.status.idle":"2021-08-02T15:52:05.672489Z","shell.execute_reply.started":"2021-08-02T15:50:31.105416Z","shell.execute_reply":"2021-08-02T15:52:05.670980Z"},"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:52:05.674245Z","iopub.execute_input":"2021-08-02T15:52:05.674690Z","iopub.status.idle":"2021-08-02T15:52:07.269403Z","shell.execute_reply.started":"2021-08-02T15:52:05.674645Z","shell.execute_reply":"2021-08-02T15:52:07.267938Z"},"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-08-02T15:52:07.271204Z","iopub.execute_input":"2021-08-02T15:52:07.271676Z","iopub.status.idle":"2021-08-02T15:52:07.278411Z","shell.execute_reply.started":"2021-08-02T15:52:07.271615Z","shell.execute_reply":"2021-08-02T15:52:07.276889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\n\nmodel.add(Conv2D(32,(3,3),input_shape=(128,128,3)))\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:53:07.097721Z","iopub.execute_input":"2021-08-02T15:53:07.098206Z","iopub.status.idle":"2021-08-02T15:53:07.263696Z","shell.execute_reply.started":"2021-08-02T15:53:07.098169Z","shell.execute_reply":"2021-08-02T15:53:07.262497Z"},"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:53:11.169751Z","iopub.execute_input":"2021-08-02T15:53:11.170215Z","iopub.status.idle":"2021-08-02T15:53:11.190349Z","shell.execute_reply.started":"2021-08-02T15:53:11.170168Z","shell.execute_reply":"2021-08-02T15:53:11.189180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trained_model=model.fit(X_train,Y_train,\n         epochs=20,\n         batch_size=32,\n         steps_per_epoch=X_train.shape[0]//32,\n         validation_steps=X_test.shape[0]//32,\n         validation_data=(X_test,Y_test),\n         verbose=2)","metadata":{"execution":{"iopub.status.busy":"2021-08-02T15:56:15.006519Z","iopub.execute_input":"2021-08-02T15:56:15.007287Z"},"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-08-02T15:54:22.208792Z","iopub.execute_input":"2021-08-02T15:54:22.209316Z","iopub.status.idle":"2021-08-02T15:56:15.004374Z","shell.execute_reply.started":"2021-08-02T15:54:22.209273Z","shell.execute_reply":"2021-08-02T15:56:15.003193Z"},"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:52:09.985392Z","iopub.status.idle":"2021-08-02T15:52:09.986346Z"},"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:52:09.988107Z","iopub.status.idle":"2021-08-02T15:52:09.988913Z"},"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:52:09.990415Z","iopub.status.idle":"2021-08-02T15:52:09.991269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}