{"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","_kg_hide-input":false,"_kg_hide-output":false,"execution":{"iopub.status.busy":"2021-08-04T16:42:03.337395Z","iopub.execute_input":"2021-08-04T16:42:03.337757Z","iopub.status.idle":"2021-08-04T16:42:27.655664Z","shell.execute_reply.started":"2021-08-04T16:42:03.337677Z","shell.execute_reply":"2021-08-04T16:42:27.643943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:42:37.984230Z","iopub.execute_input":"2021-08-04T16:42:37.984596Z","iopub.status.idle":"2021-08-04T16:42:37.990695Z","shell.execute_reply.started":"2021-08-04T16:42:37.984566Z","shell.execute_reply":"2021-08-04T16:42:37.989518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('../input/dog-breed-identification/labels.csv')\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:42:39.787144Z","iopub.execute_input":"2021-08-04T16:42:39.787481Z","iopub.status.idle":"2021-08-04T16:42:39.853138Z","shell.execute_reply.started":"2021-08-04T16:42:39.787450Z","shell.execute_reply":"2021-08-04T16:42:39.852074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:42:42.223898Z","iopub.execute_input":"2021-08-04T16:42:42.224201Z","iopub.status.idle":"2021-08-04T16:42:42.232182Z","shell.execute_reply.started":"2021-08-04T16:42:42.224174Z","shell.execute_reply":"2021-08-04T16:42:42.231044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file_location = '../input/dog-breed-identification/train/' \ntrain_data = labels.assign(img_path = lambda x : train_file_location + x['id'] + '.jpg')\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:42:44.110896Z","iopub.execute_input":"2021-08-04T16:42:44.111217Z","iopub.status.idle":"2021-08-04T16:42:44.141638Z","shell.execute_reply.started":"2021-08-04T16:42:44.111186Z","shell.execute_reply":"2021-08-04T16:42:44.140658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['breed'].nunique()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:42:49.414411Z","iopub.execute_input":"2021-08-04T16:42:49.414717Z","iopub.status.idle":"2021-08-04T16:42:49.420971Z","shell.execute_reply.started":"2021-08-04T16:42:49.414691Z","shell.execute_reply":"2021-08-04T16:42:49.420095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['img_path'][0]","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:42:50.810750Z","iopub.execute_input":"2021-08-04T16:42:50.811080Z","iopub.status.idle":"2021-08-04T16:42:50.817155Z","shell.execute_reply.started":"2021-08-04T16:42:50.811049Z","shell.execute_reply":"2021-08-04T16:42:50.816148Z"},"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()])\nX.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:42:52.326396Z","iopub.execute_input":"2021-08-04T16:42:52.327123Z","iopub.status.idle":"2021-08-04T16:44:00.249342Z","shell.execute_reply.started":"2021-08-04T16:42:52.327076Z","shell.execute_reply":"2021-08-04T16:44:00.248419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = pd.get_dummies(train_data['breed'])\nY.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:44:21.086807Z","iopub.execute_input":"2021-08-04T16:44:21.087145Z","iopub.status.idle":"2021-08-04T16:44:21.098851Z","shell.execute_reply.started":"2021-08-04T16:44:21.087115Z","shell.execute_reply":"2021-08-04T16:44:21.097869Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:44:21.490376Z","iopub.execute_input":"2021-08-04T16:44:21.490648Z","iopub.status.idle":"2021-08-04T16:44:22.742039Z","shell.execute_reply.started":"2021-08-04T16:44:21.490623Z","shell.execute_reply":"2021-08-04T16:44:22.741182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape,y_train.shape)\nprint(X_test.shape,y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:44:23.913682Z","iopub.execute_input":"2021-08-04T16:44:23.914020Z","iopub.status.idle":"2021-08-04T16:44:23.923095Z","shell.execute_reply.started":"2021-08-04T16:44:23.913992Z","shell.execute_reply":"2021-08-04T16:44:23.922197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import normalize\nX_train = normalize(X_train)\nX_test = normalize(X_test)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:44:25.032292Z","iopub.execute_input":"2021-08-04T16:44:25.032640Z","iopub.status.idle":"2021-08-04T16:44:30.184059Z","shell.execute_reply.started":"2021-08-04T16:44:25.032612Z","shell.execute_reply":"2021-08-04T16:44:30.183204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(shear_range = 0.2,\n                                   zoom_range = 0.2,\n                                   horizontal_flip = True)\ntrain_datagen.fit(X_train)\n\ntrain_generator = train_datagen.flow(X_train,y_train,batch_size = 32)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:44:33.563690Z","iopub.execute_input":"2021-08-04T16:44:33.564015Z","iopub.status.idle":"2021-08-04T16:44:34.126476Z","shell.execute_reply.started":"2021-08-04T16:44:33.563987Z","shell.execute_reply":"2021-08-04T16:44:34.125601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Activation, Flatten, Conv2D, MaxPool2D, Dropout, BatchNormalization","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:44:35.736204Z","iopub.execute_input":"2021-08-04T16:44:35.736549Z","iopub.status.idle":"2021-08-04T16:44:35.740795Z","shell.execute_reply.started":"2021-08-04T16:44:35.736518Z","shell.execute_reply":"2021-08-04T16:44:35.739701Z"},"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(BatchNormalization())\nmodel.add(Activation('relu'))\n\nmodel.add(Conv2D(64,(3,3)))\nmodel.add(Activation('relu'))\n\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Activation('relu'))\n\nmodel.add(Conv2D(16,(3,3)))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\n\nmodel.add(Conv2D(8,(3,3)))\nmodel.add(Activation('relu'))\n\nmodel.add(MaxPool2D(pool_size=(2,2)))\nmodel.add(Activation('relu'))\n\nmodel.add(Conv2D(8,(3,3)))\nmodel.add(BatchNormalization())\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(Dense(100))\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-04T16:44:38.783668Z","iopub.execute_input":"2021-08-04T16:44:38.783996Z","iopub.status.idle":"2021-08-04T16:44:40.726966Z","shell.execute_reply.started":"2021-08-04T16:44:38.783968Z","shell.execute_reply":"2021-08-04T16:44:40.726188Z"},"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-08-04T16:44:53.015756Z","iopub.execute_input":"2021-08-04T16:44:53.016070Z","iopub.status.idle":"2021-08-04T16:44:53.032546Z","shell.execute_reply.started":"2021-08-04T16:44:53.016042Z","shell.execute_reply":"2021-08-04T16:44:53.031581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_generator,\n         validation_data=(X_test, y_test),\n         epochs=100,\n         verbose=2)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T16:44:54.098057Z","iopub.execute_input":"2021-08-04T16:44:54.098500Z","iopub.status.idle":"2021-08-04T17:47:34.785089Z","shell.execute_reply.started":"2021-08-04T16:44:54.098467Z","shell.execute_reply":"2021-08-04T17:47:34.784146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-08-04T17:56:22.586796Z","iopub.execute_input":"2021-08-04T17:56:22.587138Z","iopub.status.idle":"2021-08-04T17:56:23.238559Z","shell.execute_reply.started":"2021-08-04T17:56:22.587100Z","shell.execute_reply":"2021-08-04T17:56:23.237241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T17:56:24.836778Z","iopub.execute_input":"2021-08-04T17:56:24.837091Z","iopub.status.idle":"2021-08-04T17:56:24.859263Z","shell.execute_reply.started":"2021-08-04T17:56:24.837063Z","shell.execute_reply":"2021-08-04T17:56:24.858486Z"},"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 = (128,128),\n    batch_size = 128,\n    classes=['test']\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T17:56:28.965780Z","iopub.execute_input":"2021-08-04T17:56:28.966095Z","iopub.status.idle":"2021-08-04T17:56:29.196765Z","shell.execute_reply.started":"2021-08-04T17:56:28.966066Z","shell.execute_reply":"2021-08-04T17:56:29.195857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(test_set)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T17:56:40.558568Z","iopub.execute_input":"2021-08-04T17:56:40.558884Z","iopub.status.idle":"2021-08-04T17:58:12.328726Z","shell.execute_reply.started":"2021-08-04T17:56:40.558856Z","shell.execute_reply":"2021-08-04T17:58:12.327768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-04T17:58:33.528143Z","iopub.execute_input":"2021-08-04T17:58:33.528481Z","iopub.status.idle":"2021-08-04T17:58:33.535678Z","shell.execute_reply.started":"2021-08-04T17:58:33.528448Z","shell.execute_reply":"2021-08-04T17:58:33.534756Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T17:58:36.368215Z","iopub.execute_input":"2021-08-04T17:58:36.368575Z","iopub.status.idle":"2021-08-04T17:58:36.413083Z","shell.execute_reply.started":"2021-08-04T17:58:36.368545Z","shell.execute_reply":"2021-08-04T17:58:36.412347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['id'] = id_list\nsubmission.iloc[:,1:] =y_pred\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-04T17:59:30.503517Z","iopub.execute_input":"2021-08-04T17:59:30.503842Z","iopub.status.idle":"2021-08-04T17:59:30.994340Z","shell.execute_reply.started":"2021-08-04T17:59:30.503810Z","shell.execute_reply":"2021-08-04T17:59:30.993545Z"},"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-04T17:59:45.264338Z","iopub.execute_input":"2021-08-04T17:59:45.264658Z","iopub.status.idle":"2021-08-04T17:59:48.954447Z","shell.execute_reply.started":"2021-08-04T17:59:45.264631Z","shell.execute_reply":"2021-08-04T17:59:48.953314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}