{"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-05-27T10:54:46.235284Z","iopub.execute_input":"2021-05-27T10:54:46.235700Z","iopub.status.idle":"2021-05-27T10:55:34.969522Z","shell.execute_reply.started":"2021-05-27T10:54:46.235602Z","shell.execute_reply":"2021-05-27T10:55:34.968460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1 = pd.read_csv('../input/dog-breed-identification/labels.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-27T10:55:34.972290Z","iopub.execute_input":"2021-05-27T10:55:34.972736Z","iopub.status.idle":"2021-05-27T10:55:35.007048Z","shell.execute_reply.started":"2021-05-27T10:55:34.972693Z","shell.execute_reply":"2021-05-27T10:55:35.006092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-27T10:55:35.010275Z","iopub.execute_input":"2021-05-27T10:55:35.010721Z","iopub.status.idle":"2021-05-27T10:55:35.023548Z","shell.execute_reply.started":"2021-05-27T10:55:35.010675Z","shell.execute_reply":"2021-05-27T10:55:35.022502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T10:55:35.026043Z","iopub.execute_input":"2021-05-27T10:55:35.026781Z","iopub.status.idle":"2021-05-27T10:55:35.048250Z","shell.execute_reply.started":"2021-05-27T10:55:35.026736Z","shell.execute_reply":"2021-05-27T10:55:35.046882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_file='/kaggle/input/dog-breed-identification/train/'\n\ndf=df1.assign(img_path=lambda x: img_file + x['id'] +'.jpg')\nprint(df.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T10:55:35.050208Z","iopub.execute_input":"2021-05-27T10:55:35.050664Z","iopub.status.idle":"2021-05-27T10:55:35.079809Z","shell.execute_reply.started":"2021-05-27T10:55:35.050622Z","shell.execute_reply":"2021-05-27T10:55:35.078772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T10:55:35.083087Z","iopub.execute_input":"2021-05-27T10:55:35.083423Z","iopub.status.idle":"2021-05-27T10:55:35.098749Z","shell.execute_reply.started":"2021-05-27T10:55:35.083395Z","shell.execute_reply":"2021-05-27T10:55:35.097492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['img_path'][0]","metadata":{"execution":{"iopub.status.busy":"2021-05-27T10:55:35.102361Z","iopub.execute_input":"2021-05-27T10:55:35.102652Z","iopub.status.idle":"2021-05-27T10:55:35.112672Z","shell.execute_reply.started":"2021-05-27T10:55:35.102625Z","shell.execute_reply":"2021-05-27T10:55:35.111541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import img_to_array,load_img,ImageDataGenerator\nX=np.array([img_to_array(load_img(img, target_size=(96, 96))) for img in df['img_path'].values.tolist()])\nX.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-27T10:55:35.117413Z","iopub.execute_input":"2021-05-27T10:55:35.117797Z","iopub.status.idle":"2021-05-27T10:58:03.318523Z","shell.execute_reply.started":"2021-05-27T10:55:35.117761Z","shell.execute_reply":"2021-05-27T10:58:03.317324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y=pd.get_dummies(df['breed'])\nY.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-27T10:58:03.320651Z","iopub.execute_input":"2021-05-27T10:58:03.321076Z","iopub.status.idle":"2021-05-27T10:58:03.337292Z","shell.execute_reply.started":"2021-05-27T10:58:03.321029Z","shell.execute_reply":"2021-05-27T10:58:03.336159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_val,Y_train,Y_val = train_test_split(X,Y,test_size=0.3)\n\nprint(X_train.shape,Y_train.shape)\nprint(X_val.shape,Y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T10:58:03.339026Z","iopub.execute_input":"2021-05-27T10:58:03.339820Z","iopub.status.idle":"2021-05-27T10:58:04.777765Z","shell.execute_reply.started":"2021-05-27T10:58:03.339775Z","shell.execute_reply":"2021-05-27T10:58:04.776544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense,Activation,Conv2D,Flatten,MaxPool2D,Dropout","metadata":{"execution":{"iopub.status.busy":"2021-05-27T10:58:04.779537Z","iopub.execute_input":"2021-05-27T10:58:04.780231Z","iopub.status.idle":"2021-05-27T10:58:04.786250Z","shell.execute_reply.started":"2021-05-27T10:58:04.780185Z","shell.execute_reply":"2021-05-27T10:58:04.784684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\n\nmodel.add(Conv2D(64,(3,3),input_shape=(96,96,3)))\nmodel.add(Activation('relu'))\n\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(32,(3,3)))\nmodel.add(Activation('relu'))\n\nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(16,(3,3)))\nmodel.add(Activation('relu'))\n\n# model.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Conv2D(8,(3,3)))\nmodel.add(Activation('relu'))\n\nmodel.add(Flatten())\n\nmodel.add(Dropout(0.25))\n\nmodel.add(Dense(100))\nmodel.add(Activation('relu'))\n\nmodel.add(Dense(100))\nmodel.add(Activation('relu'))\n\nmodel.add(Dense(100))\nmodel.add(Activation('relu'))\n\nmodel.add(Dropout(0.25))\n\nmodel.add(Dense(Y.shape[1]))\nmodel.add(Activation('softmax'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T10:58:04.787863Z","iopub.execute_input":"2021-05-27T10:58:04.788621Z","iopub.status.idle":"2021-05-27T10:58:07.302963Z","shell.execute_reply.started":"2021-05-27T10:58:04.788576Z","shell.execute_reply":"2021-05-27T10:58:07.301862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n             loss='categorical_crossentropy',\n             metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-05-27T10:58:07.306520Z","iopub.execute_input":"2021-05-27T10:58:07.306813Z","iopub.status.idle":"2021-05-27T10:58:07.325353Z","shell.execute_reply.started":"2021-05-27T10:58:07.306783Z","shell.execute_reply":"2021-05-27T10:58:07.324111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train,Y_train,\n         validation_data=(X_val,Y_val),\n         batch_size=32,\n         epochs=75,\n         verbose=2)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T10:58:07.327070Z","iopub.execute_input":"2021-05-27T10:58:07.327524Z","iopub.status.idle":"2021-05-27T11:05:45.780059Z","shell.execute_reply.started":"2021-05-27T10:58:07.327478Z","shell.execute_reply":"2021-05-27T11:05:45.778847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator ( \n    rescale=1/255.\n)\n\ntest_set = test_datagen.flow_from_directory (\n    '/kaggle/input/dog-breed-identification',\n    target_size = (96,96),\n    batch_size = 128,\n    classes=['test']\n)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T11:25:02.771182Z","iopub.execute_input":"2021-05-27T11:25:02.771558Z","iopub.status.idle":"2021-05-27T11:25:03.113443Z","shell.execute_reply.started":"2021-05-27T11:25:02.771527Z","shell.execute_reply":"2021-05-27T11:25:03.112355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(test_set)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T11:25:05.544867Z","iopub.execute_input":"2021-05-27T11:25:05.545250Z","iopub.status.idle":"2021-05-27T11:26:32.111978Z","shell.execute_reply.started":"2021-05-27T11:25:05.545221Z","shell.execute_reply":"2021-05-27T11:26:32.110611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred.shape","metadata":{"execution":{"iopub.status.busy":"2021-05-27T11:28:41.533055Z","iopub.execute_input":"2021-05-27T11:28:41.533518Z","iopub.status.idle":"2021-05-27T11:28:41.542488Z","shell.execute_reply.started":"2021-05-27T11:28:41.533471Z","shell.execute_reply":"2021-05-27T11:28:41.541052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')\ndf_submission","metadata":{"execution":{"iopub.status.busy":"2021-05-27T11:26:48.026476Z","iopub.execute_input":"2021-05-27T11:26:48.026819Z","iopub.status.idle":"2021-05-27T11:26:48.351647Z","shell.execute_reply.started":"2021-05-27T11:26:48.026789Z","shell.execute_reply":"2021-05-27T11:26:48.350290Z"},"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# print(id_list)","metadata":{"execution":{"iopub.status.busy":"2021-05-27T11:27:40.346226Z","iopub.execute_input":"2021-05-27T11:27:40.346618Z","iopub.status.idle":"2021-05-27T11:27:40.400370Z","shell.execute_reply.started":"2021-05-27T11:27:40.346586Z","shell.execute_reply":"2021-05-27T11:27:40.399262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission['id'] = id_list\ndf_submission.iloc[:,1:] =y_pred\ndf_submission.head()","metadata":{"execution":{"iopub.status.busy":"2021-05-27T11:30:59.353864Z","iopub.execute_input":"2021-05-27T11:30:59.354245Z","iopub.status.idle":"2021-05-27T11:30:59.816408Z","shell.execute_reply.started":"2021-05-27T11:30:59.354213Z","shell.execute_reply":"2021-05-27T11:30:59.814988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df = df_submission.set_index('id')\nfinal_df.to_csv('Submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-27T11:31:22.890295Z","iopub.execute_input":"2021-05-27T11:31:22.890687Z","iopub.status.idle":"2021-05-27T11:31:26.572432Z","shell.execute_reply.started":"2021-05-27T11:31:22.890646Z","shell.execute_reply":"2021-05-27T11:31:26.571107Z"},"trusted":true},"execution_count":null,"outputs":[]}]}