{"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":"# ATTRIBUTION: Modified Code from Kautsub Mishra\n\n# 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 matplotlib.pyplot as plt\n%matplotlib inline\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-06-25T17:51:44.144139Z","iopub.execute_input":"2021-06-25T17:51:44.144632Z","iopub.status.idle":"2021-06-25T17:51:55.803751Z","shell.execute_reply.started":"2021-06-25T17:51:44.144587Z","shell.execute_reply":"2021-06-25T17:51:55.801185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_labels = pd.read_csv('../input/dog-breed-identification/labels.csv')\ndf_labels.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T17:51:55.808141Z","iopub.execute_input":"2021-06-25T17:51:55.808413Z","iopub.status.idle":"2021-06-25T17:51:55.852548Z","shell.execute_reply.started":"2021-06-25T17:51:55.808389Z","shell.execute_reply":"2021-06-25T17:51:55.851915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_labels.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-25T17:51:55.854133Z","iopub.execute_input":"2021-06-25T17:51:55.854349Z","iopub.status.idle":"2021-06-25T17:51:55.85963Z","shell.execute_reply.started":"2021-06-25T17:51:55.854323Z","shell.execute_reply":"2021-06-25T17:51:55.858365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_file_location = '../input/dog-breed-identification/train/' \ntrain_data = df_labels.assign(img_path = lambda x : train_file_location + x['id'] + '.jpg')\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T18:55:53.049866Z","iopub.execute_input":"2021-06-25T18:55:53.050297Z","iopub.status.idle":"2021-06-25T18:55:53.124554Z","shell.execute_reply.started":"2021-06-25T18:55:53.050215Z","shell.execute_reply":"2021-06-25T18:55:53.123254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_data['breed'].unique())","metadata":{"execution":{"iopub.status.busy":"2021-06-25T17:51:55.887692Z","iopub.execute_input":"2021-06-25T17:51:55.887925Z","iopub.status.idle":"2021-06-25T17:51:55.905834Z","shell.execute_reply.started":"2021-06-25T17:51:55.887869Z","shell.execute_reply":"2021-06-25T17:51:55.905017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['img_path'][0]","metadata":{"execution":{"iopub.status.busy":"2021-06-25T17:51:55.906895Z","iopub.execute_input":"2021-06-25T17:51:55.907142Z","iopub.status.idle":"2021-06-25T17:51:55.923567Z","shell.execute_reply.started":"2021-06-25T17:51:55.90712Z","shell.execute_reply":"2021-06-25T17:51:55.92257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(train_data['img_path'][0])","metadata":{"execution":{"iopub.status.busy":"2021-06-25T17:51:55.924484Z","iopub.execute_input":"2021-06-25T17:51:55.924719Z","iopub.status.idle":"2021-06-25T17:51:55.942539Z","shell.execute_reply.started":"2021-06-25T17:51:55.924682Z","shell.execute_reply":"2021-06-25T17:51:55.941566Z"},"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-06-25T17:51:55.944262Z","iopub.execute_input":"2021-06-25T17:51:55.944581Z","iopub.status.idle":"2021-06-25T17:52:38.10904Z","shell.execute_reply.started":"2021-06-25T17:51:55.944556Z","shell.execute_reply":"2021-06-25T17:52:38.107772Z"},"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-06-25T17:52:38.112518Z","iopub.execute_input":"2021-06-25T17:52:38.112757Z","iopub.status.idle":"2021-06-25T17:52:38.122526Z","shell.execute_reply.started":"2021-06-25T17:52:38.112733Z","shell.execute_reply":"2021-06-25T17:52:38.121098Z"},"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.25)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T17:52:38.124086Z","iopub.execute_input":"2021-06-25T17:52:38.124317Z","iopub.status.idle":"2021-06-25T17:52:38.458729Z","shell.execute_reply.started":"2021-06-25T17:52:38.124295Z","shell.execute_reply":"2021-06-25T17:52:38.457219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape,y_train.shape)\nprint(X_val.shape,y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T17:52:38.460468Z","iopub.execute_input":"2021-06-25T17:52:38.46084Z","iopub.status.idle":"2021-06-25T17:52:38.467718Z","shell.execute_reply.started":"2021-06-25T17:52:38.460804Z","shell.execute_reply":"2021-06-25T17:52:38.466102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import normalize\nX_train = normalize(X_train)\nX_val = normalize(X_val)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T17:52:38.469166Z","iopub.execute_input":"2021-06-25T17:52:38.469503Z","iopub.status.idle":"2021-06-25T17:52:43.362546Z","shell.execute_reply.started":"2021-06-25T17:52:38.469472Z","shell.execute_reply":"2021-06-25T17:52:43.361316Z"},"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-06-25T17:52:43.363832Z","iopub.execute_input":"2021-06-25T17:52:43.364224Z","iopub.status.idle":"2021-06-25T17:52:43.616003Z","shell.execute_reply.started":"2021-06-25T17:52:43.36419Z","shell.execute_reply":"2021-06-25T17:52:43.614394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense,Conv2D,MaxPool2D,Dropout,Flatten,Activation,BatchNormalization\nfrom keras.models import model_from_json\nfrom keras.models import load_model","metadata":{"execution":{"iopub.status.busy":"2021-06-25T17:52:43.617533Z","iopub.execute_input":"2021-06-25T17:52:43.617937Z","iopub.status.idle":"2021-06-25T17:52:43.623328Z","shell.execute_reply.started":"2021-06-25T17:52:43.617895Z","shell.execute_reply":"2021-06-25T17:52:43.621936Z"},"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('elu'))\n\nmodel.add(Conv2D(64,(3,3)))\nmodel.add(Activation('elu'))\n\nmodel.add(MaxPool2D(pool_size = (2,2)))\n\nmodel.add(Conv2D(64,(3,3)))\nmodel.add(Activation('elu'))\n\nmodel.add(Conv2D(32,(3,3)))\nmodel.add(Activation('elu'))\n\nmodel.add(Conv2D(16,(3,3)))\nmodel.add(Activation('elu'))\n\nmodel.add(MaxPool2D(pool_size = (2,2)))\n\nmodel.add(Conv2D(8,(3,3)))\nmodel.add(Activation('elu'))\n\nmodel.add(Conv2D(8,(3,3)))\nmodel.add(Activation('elu'))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(128))\nmodel.add(BatchNormalization())\nmodel.add(Activation('elu'))\n\nmodel.add(Dense(100))\nmodel.add(Activation('elu'))\n\nmodel.add(Dense(100))\nmodel.add(Activation('elu'))\n\nmodel.add(Dense(100))\nmodel.add(Activation('elu'))\n\nmodel.add(Dropout(0.25))\n\nmodel.add(Dense(100))\nmodel.add(Activation('elu'))\n\nmodel.add(Dense(Y.shape[1]))\nmodel.add(Activation('softmax'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T17:52:43.624835Z","iopub.execute_input":"2021-06-25T17:52:43.625244Z","iopub.status.idle":"2021-06-25T17:52:43.845293Z","shell.execute_reply.started":"2021-06-25T17:52:43.62521Z","shell.execute_reply":"2021-06-25T17:52:43.844042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = Sequential()\n\n# model.add(Conv2D(32,(3,3),input_shape = (128,128,3)))\n# model.add(BatchNormalization())\n# model.add(Activation('elu'))\n\n# model.add(Conv2D(64,(3,3)))\n# model.add(Activation('elu'))\n\n# model.add(MaxPool2D(pool_size = (2,2)))\n\n# model.add(Conv2D(64,(3,3)))\n# model.add(BatchNormalization())\n# model.add(Activation('elu'))\n\n# model.add(Conv2D(32,(3,3)))\n# model.add(BatchNormalization())\n# model.add(Activation('elu'))\n\n# model.add(Conv2D(32,(3,3)))\n# model.add(BatchNormalization())\n# model.add(Activation('elu'))\n\n# model.add(MaxPool2D(pool_size = (2,2)))\n\n# model.add(Conv2D(16,(3,3)))\n# model.add(BatchNormalization())\n# model.add(Activation('elu'))\n\n# # model.add(MaxPool2D(pool_size = (2,2)))\n\n# model.add(Conv2D(8,(3,3)))\n# model.add(BatchNormalization())\n# model.add(Activation('elu'))\n\n# model.add(Conv2D(8,(3,3)))\n# model.add(BatchNormalization())\n# model.add(Activation('elu'))\n\n# model.add(Flatten())\n\n# model.add(Dense(128))\n# model.add(BatchNormalization())\n# model.add(Activation('elu'))\n\n# model.add(Dense(100))\n# model.add(BatchNormalization())\n# model.add(Activation('elu'))\n\n# model.add(Dense(100))\n# model.add(BatchNormalization())\n# model.add(Activation('elu'))\n\n# model.add(Dense(100))\n# model.add(Activation('elu'))\n\n# model.add(Dropout(0.25))\n\n# model.add(Dense(100))\n# model.add(Activation('elu'))\n\n# model.add(Dense(100))\n# model.add(BatchNormalization())\n# model.add(Activation('elu'))\n\n# model.add(Dense(Y.shape[1]))\n# model.add(Activation('softmax'))\n\n# model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T17:52:43.846493Z","iopub.execute_input":"2021-06-25T17:52:43.846728Z","iopub.status.idle":"2021-06-25T17:52:43.851934Z","shell.execute_reply.started":"2021-06-25T17:52:43.8467Z","shell.execute_reply":"2021-06-25T17:52:43.85075Z"},"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-06-25T17:52:43.853266Z","iopub.execute_input":"2021-06-25T17:52:43.853516Z","iopub.status.idle":"2021-06-25T17:52:43.882043Z","shell.execute_reply.started":"2021-06-25T17:52:43.853491Z","shell.execute_reply":"2021-06-25T17:52:43.881177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tr_history = model.fit_generator(train_generator,\n         validation_data=(X_val,y_val),\n         epochs=50,\n         verbose=2)","metadata":{"execution":{"iopub.status.busy":"2021-06-25T17:52:43.882956Z","iopub.execute_input":"2021-06-25T17:52:43.883293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = tr_history.history['loss']\nval_loss = tr_history.history['val_loss']\nepochs = range(1, len(loss) + 1)\nplt.plot(epochs, loss, 'y', label='Training loss')\nplt.plot(epochs, val_loss, 'r', label='Validation loss')\nplt.title('Training and validation loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = tr_history.history['accuracy']\nval_acc = tr_history.history['val_accuracy']\nepochs = range(1,len(acc)+1)\nplt.plot(epochs,acc,'y',label = 'Training_acc')\nplt.plot(epochs,val_acc,'r',label = 'Validation_acc')\nplt.title('Training and Validation accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","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":"model_json = model.to_json()\n\n# serialize model to JSON\nmodel_json = model.to_json()\nwith open(\"np-model.json\", \"w\") as json_file:\n    json_file.write(model_json)\n# serialize weights to HDF5\nmodel.save(\"np-model.h5\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}