{"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":"2022-02-27T10:19:31.028724Z","iopub.execute_input":"2022-02-27T10:19:31.029261Z","iopub.status.idle":"2022-02-27T10:19:46.101751Z","shell.execute_reply.started":"2022-02-27T10:19:31.029157Z","shell.execute_reply":"2022-02-27T10:19:46.095919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-02-27T10:19:46.103953Z","iopub.execute_input":"2022-02-27T10:19:46.104292Z","iopub.status.idle":"2022-02-27T10:19:46.122551Z","shell.execute_reply.started":"2022-02-27T10:19:46.104260Z","shell.execute_reply":"2022-02-27T10:19:46.113001Z"},"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":"2022-02-27T10:19:46.124855Z","iopub.execute_input":"2022-02-27T10:19:46.125249Z","iopub.status.idle":"2022-02-27T10:19:46.184971Z","shell.execute_reply.started":"2022-02-27T10:19:46.125212Z","shell.execute_reply":"2022-02-27T10:19:46.183969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-27T10:19:46.186772Z","iopub.execute_input":"2022-02-27T10:19:46.187136Z","iopub.status.idle":"2022-02-27T10:19:46.192564Z","shell.execute_reply.started":"2022-02-27T10:19:46.187101Z","shell.execute_reply":"2022-02-27T10:19:46.191864Z"},"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":"2022-02-27T10:19:46.193589Z","iopub.execute_input":"2022-02-27T10:19:46.193864Z","iopub.status.idle":"2022-02-27T10:19:46.238303Z","shell.execute_reply.started":"2022-02-27T10:19:46.193837Z","shell.execute_reply":"2022-02-27T10:19:46.237472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['breed'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-02-27T10:19:46.239452Z","iopub.execute_input":"2022-02-27T10:19:46.239875Z","iopub.status.idle":"2022-02-27T10:19:46.250950Z","shell.execute_reply.started":"2022-02-27T10:19:46.239842Z","shell.execute_reply":"2022-02-27T10:19:46.249581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['img_path'][0]","metadata":{"execution":{"iopub.status.busy":"2022-02-27T10:19:46.252389Z","iopub.execute_input":"2022-02-27T10:19:46.252703Z","iopub.status.idle":"2022-02-27T10:19:46.263354Z","shell.execute_reply.started":"2022-02-27T10:19:46.252675Z","shell.execute_reply":"2022-02-27T10:19:46.262520Z"},"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":"2022-02-27T10:19:46.265574Z","iopub.execute_input":"2022-02-27T10:19:46.266103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = pd.get_dummies(train_data['breed'])\nY.shape","metadata":{"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":{"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":{"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":{"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":{"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":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n             loss='categorical_crossentropy',\n             metrics=['accuracy'])","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(test_set)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred.shape","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['id'] = id_list\nsubmission.iloc[:,1:] =y_pred\nsubmission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df = submission.set_index('id')\nfinal_df.to_csv('Submission.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}