{"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)\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-08T20:54:56.277897Z","iopub.execute_input":"2021-06-08T20:54:56.278278Z","iopub.status.idle":"2021-06-08T20:54:58.611005Z","shell.execute_reply.started":"2021-06-08T20:54:56.278245Z","shell.execute_reply":"2021-06-08T20:54:58.597146Z"},"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-08T20:55:14.772097Z","iopub.execute_input":"2021-06-08T20:55:14.772426Z","iopub.status.idle":"2021-06-08T20:55:14.800031Z","shell.execute_reply.started":"2021-06-08T20:55:14.772395Z","shell.execute_reply":"2021-06-08T20:55:14.799057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_labels.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-08T20:55:18.286819Z","iopub.execute_input":"2021-06-08T20:55:18.287172Z","iopub.status.idle":"2021-06-08T20:55:18.294670Z","shell.execute_reply.started":"2021-06-08T20:55:18.287139Z","shell.execute_reply":"2021-06-08T20:55:18.293672Z"},"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-08T20:55:20.875977Z","iopub.execute_input":"2021-06-08T20:55:20.876329Z","iopub.status.idle":"2021-06-08T20:55:20.899097Z","shell.execute_reply.started":"2021-06-08T20:55:20.876297Z","shell.execute_reply":"2021-06-08T20:55:20.898362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_data['breed'].unique())","metadata":{"execution":{"iopub.status.busy":"2021-06-08T20:55:24.775082Z","iopub.execute_input":"2021-06-08T20:55:24.775434Z","iopub.status.idle":"2021-06-08T20:55:24.785733Z","shell.execute_reply.started":"2021-06-08T20:55:24.775403Z","shell.execute_reply":"2021-06-08T20:55:24.784800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['img_path'][0]","metadata":{"execution":{"iopub.status.busy":"2021-06-08T20:55:27.088115Z","iopub.execute_input":"2021-06-08T20:55:27.088443Z","iopub.status.idle":"2021-06-08T20:55:27.095942Z","shell.execute_reply.started":"2021-06-08T20:55:27.088414Z","shell.execute_reply":"2021-06-08T20:55:27.095013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(train_data['img_path'][0])","metadata":{"execution":{"iopub.status.busy":"2021-06-08T20:55:31.206698Z","iopub.execute_input":"2021-06-08T20:55:31.207030Z","iopub.status.idle":"2021-06-08T20:55:31.212679Z","shell.execute_reply.started":"2021-06-08T20:55:31.206996Z","shell.execute_reply":"2021-06-08T20:55:31.211902Z"},"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-08T20:55:33.637589Z","iopub.execute_input":"2021-06-08T20:55:33.637902Z","iopub.status.idle":"2021-06-08T20:56:09.808269Z","shell.execute_reply.started":"2021-06-08T20:55:33.637871Z","shell.execute_reply":"2021-06-08T20:56:09.807415Z"},"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-08T20:56:12.871303Z","iopub.execute_input":"2021-06-08T20:56:12.871666Z","iopub.status.idle":"2021-06-08T20:56:12.881242Z","shell.execute_reply.started":"2021-06-08T20:56:12.871623Z","shell.execute_reply":"2021-06-08T20:56:12.880140Z"},"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-08T20:56:15.971028Z","iopub.execute_input":"2021-06-08T20:56:15.971348Z","iopub.status.idle":"2021-06-08T20:56:16.881595Z","shell.execute_reply.started":"2021-06-08T20:56:15.971318Z","shell.execute_reply":"2021-06-08T20:56:16.880653Z"},"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-08T20:56:20.249841Z","iopub.execute_input":"2021-06-08T20:56:20.250208Z","iopub.status.idle":"2021-06-08T20:56:20.257889Z","shell.execute_reply.started":"2021-06-08T20:56:20.250176Z","shell.execute_reply":"2021-06-08T20:56:20.256729Z"},"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-08T20:56:23.542556Z","iopub.execute_input":"2021-06-08T20:56:23.543251Z","iopub.status.idle":"2021-06-08T20:56:28.877533Z","shell.execute_reply.started":"2021-06-08T20:56:23.543206Z","shell.execute_reply":"2021-06-08T20:56:28.876621Z"},"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-08T20:56:59.589920Z","iopub.execute_input":"2021-06-08T20:56:59.590294Z","iopub.status.idle":"2021-06-08T20:57:00.137006Z","shell.execute_reply.started":"2021-06-08T20:56:59.590262Z","shell.execute_reply":"2021-06-08T20:57:00.136060Z"},"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","metadata":{"execution":{"iopub.status.busy":"2021-06-08T20:57:06.162900Z","iopub.execute_input":"2021-06-08T20:57:06.163314Z","iopub.status.idle":"2021-06-08T20:57:06.167433Z","shell.execute_reply.started":"2021-06-08T20:57:06.163281Z","shell.execute_reply":"2021-06-08T20:57:06.166390Z"},"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(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(Activation('elu'))\n\n# model.add(Conv2D(32,(3,3)))\n# model.add(Activation('elu'))\n\n# model.add(Conv2D(16,(3,3)))\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(Activation('elu'))\n\n# model.add(Conv2D(8,(3,3)))\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(Activation('elu'))\n\n# model.add(Dense(100))\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(Y.shape[1]))\n# model.add(Activation('softmax'))\n\n# model.summary()","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('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(BatchNormalization())\nmodel.add(Activation('elu'))\n\nmodel.add(Conv2D(32,(3,3)))\nmodel.add(BatchNormalization())\nmodel.add(Activation('elu'))\n\nmodel.add(Conv2D(32,(3,3)))\nmodel.add(BatchNormalization())\nmodel.add(Activation('elu'))\n\nmodel.add(MaxPool2D(pool_size = (2,2)))\n\nmodel.add(Conv2D(16,(3,3)))\nmodel.add(BatchNormalization())\nmodel.add(Activation('elu'))\n\n# model.add(MaxPool2D(pool_size = (2,2)))\n\nmodel.add(Conv2D(8,(3,3)))\nmodel.add(BatchNormalization())\nmodel.add(Activation('elu'))\n\nmodel.add(Conv2D(8,(3,3)))\nmodel.add(BatchNormalization())\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(BatchNormalization())\nmodel.add(Activation('elu'))\n\nmodel.add(Dense(100))\nmodel.add(BatchNormalization())\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(100))\nmodel.add(BatchNormalization())\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-08T21:53:38.596047Z","iopub.execute_input":"2021-06-08T21:53:38.596444Z","iopub.status.idle":"2021-06-08T21:53:38.901543Z","shell.execute_reply.started":"2021-06-08T21:53:38.596410Z","shell.execute_reply":"2021-06-08T21:53:38.900724Z"},"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-08T21:53:46.783344Z","iopub.execute_input":"2021-06-08T21:53:46.783771Z","iopub.status.idle":"2021-06-08T21:53:46.803053Z","shell.execute_reply.started":"2021-06-08T21:53:46.783730Z","shell.execute_reply":"2021-06-08T21:53:46.802115Z"},"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-08T21:53:49.525179Z","iopub.execute_input":"2021-06-08T21:53:49.525499Z","iopub.status.idle":"2021-06-08T22:15:46.259554Z","shell.execute_reply.started":"2021-06-08T21:53:49.525470Z","shell.execute_reply":"2021-06-08T22:15:46.258743Z"},"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":{"execution":{"iopub.status.busy":"2021-06-08T22:25:57.393079Z","iopub.execute_input":"2021-06-08T22:25:57.393437Z","iopub.status.idle":"2021-06-08T22:25:57.553479Z","shell.execute_reply.started":"2021-06-08T22:25:57.393406Z","shell.execute_reply":"2021-06-08T22:25:57.552509Z"},"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":{"execution":{"iopub.status.busy":"2021-06-08T22:26:00.658067Z","iopub.execute_input":"2021-06-08T22:26:00.658391Z","iopub.status.idle":"2021-06-08T22:26:00.805090Z","shell.execute_reply.started":"2021-06-08T22:26:00.658360Z","shell.execute_reply":"2021-06-08T22:26:00.804197Z"},"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-06-08T22:57:12.615023Z","iopub.execute_input":"2021-06-08T22:57:12.615438Z","iopub.status.idle":"2021-06-08T22:57:12.996129Z","shell.execute_reply.started":"2021-06-08T22:57:12.615401Z","shell.execute_reply":"2021-06-08T22:57:12.995223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-08T22:57:16.832531Z","iopub.execute_input":"2021-06-08T22:57:16.832861Z","iopub.status.idle":"2021-06-08T22:57:16.859497Z","shell.execute_reply.started":"2021-06-08T22:57:16.832831Z","shell.execute_reply":"2021-06-08T22:57:16.858408Z"},"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-06-08T22:34:09.161737Z","iopub.execute_input":"2021-06-08T22:34:09.162082Z","iopub.status.idle":"2021-06-08T22:34:09.393173Z","shell.execute_reply.started":"2021-06-08T22:34:09.162049Z","shell.execute_reply":"2021-06-08T22:34:09.392053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(test_set)","metadata":{"execution":{"iopub.status.busy":"2021-06-08T22:34:24.204579Z","iopub.execute_input":"2021-06-08T22:34:24.204894Z","iopub.status.idle":"2021-06-08T22:35:50.260221Z","shell.execute_reply.started":"2021-06-08T22:34:24.204862Z","shell.execute_reply":"2021-06-08T22:35:50.259328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-08T22:36:07.686130Z","iopub.execute_input":"2021-06-08T22:36:07.686550Z","iopub.status.idle":"2021-06-08T22:36:07.696812Z","shell.execute_reply.started":"2021-06-08T22:36:07.686513Z","shell.execute_reply":"2021-06-08T22:36:07.695894Z"},"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-06-08T22:38:11.751718Z","iopub.execute_input":"2021-06-08T22:38:11.752062Z","iopub.status.idle":"2021-06-08T22:38:11.797504Z","shell.execute_reply.started":"2021-06-08T22:38:11.752030Z","shell.execute_reply":"2021-06-08T22:38:11.796648Z"},"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-06-08T23:00:52.686349Z","iopub.execute_input":"2021-06-08T23:00:52.686786Z","iopub.status.idle":"2021-06-08T23:00:53.237645Z","shell.execute_reply.started":"2021-06-08T23:00:52.686745Z","shell.execute_reply":"2021-06-08T23:00:53.236812Z"},"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-06-08T23:01:32.881060Z","iopub.execute_input":"2021-06-08T23:01:32.881390Z","iopub.status.idle":"2021-06-08T23:01:36.118985Z","shell.execute_reply.started":"2021-06-08T23:01:32.881360Z","shell.execute_reply":"2021-06-08T23:01:36.118147Z"},"trusted":true},"execution_count":null,"outputs":[]}]}