{"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 cv2\nimport re\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\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":"2022-01-16T09:04:06.453287Z","iopub.execute_input":"2022-01-16T09:04:06.453711Z","iopub.status.idle":"2022-01-16T09:04:41.329709Z","shell.execute_reply.started":"2022-01-16T09:04:06.453601Z","shell.execute_reply":"2022-01-16T09:04:41.322042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y= pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\ny.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-16T09:04:41.331854Z","iopub.execute_input":"2022-01-16T09:04:41.332363Z","iopub.status.idle":"2022-01-16T09:04:41.381476Z","shell.execute_reply.started":"2022-01-16T09:04:41.332298Z","shell.execute_reply":"2022-01-16T09:04:41.380494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y.breed.unique())\nprint(\"\\n\")\nprint(y.breed.nunique())\nprint(\"\\n\")\nprint(y.isna().sum())\nprint(\"\\n\")\nprint(y.breed.value_counts())\nprint(\"\\n\")\n\nimg=cv2.imread('../input/dog-breed-identification/test/ddc0134def0da0472e55271da8b57c44.jpg')\n#('../input/dog-breed-identification/test/000621fb3cbb32d8935728e48679680e.jpg')\n\nimg=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\nplt.imshow(img)\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-16T09:04:41.382915Z","iopub.execute_input":"2022-01-16T09:04:41.383150Z","iopub.status.idle":"2022-01-16T09:04:41.723264Z","shell.execute_reply.started":"2022-01-16T09:04:41.383119Z","shell.execute_reply":"2022-01-16T09:04:41.722365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(img.shape)\nH=128\nW=128\nC=3\n\ntrain_file_location = '../input/dog-breed-identification/train/' \ntrain_data = y.assign(img_path = lambda x : train_file_location + x['id'] + '.jpg')\ntrain_data.head()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-16T09:05:31.002417Z","iopub.execute_input":"2022-01-16T09:05:31.002767Z","iopub.status.idle":"2022-01-16T09:05:31.022783Z","shell.execute_reply.started":"2022-01-16T09:05:31.002726Z","shell.execute_reply":"2022-01-16T09:05:31.021882Z"},"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 = (H,W))) for img in train_data['img_path'].values.tolist()])\nprint(X.shape)\nY = pd.get_dummies(train_data['breed'])\nprint(Y.shape)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-16T09:05:39.414528Z","iopub.execute_input":"2022-01-16T09:05:39.414940Z","iopub.status.idle":"2022-01-16T09:07:08.962489Z","shell.execute_reply.started":"2022-01-16T09:05:39.414911Z","shell.execute_reply":"2022-01-16T09:07:08.961564Z"},"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)\nprint(X_train.shape,Y_train.shape)\nprint(X_test.shape,Y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T09:07:36.129389Z","iopub.execute_input":"2022-01-16T09:07:36.129650Z","iopub.status.idle":"2022-01-16T09:07:37.536546Z","shell.execute_reply.started":"2022-01-16T09:07:36.129624Z","shell.execute_reply":"2022-01-16T09:07:37.535644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom keras.models import Sequential\nfrom keras.layers import Dense,Conv2D,MaxPool2D,Dropout,Flatten,Activation\nmodel=Sequential()\n\nmodel.add(Conv2D(32,(3,3),input_shape=(H,W,C)))\nmodel.add(Activation('relu'))\n\nmodel.add(MaxPool2D((2,2)))\n\nmodel.add(Conv2D(64,(3,3)))\nmodel.add(Activation('relu'))\n\nmodel.add(MaxPool2D((2,2)))\n\nmodel.add(Conv2D(128,(3,3)))\nmodel.add(Activation('relu'))\n\nmodel.add(MaxPool2D((2,2)))\n\nmodel.add(Conv2D(128,(3,3)))\nmodel.add(Activation('relu'))\n\nmodel.add(MaxPool2D((2,2)))\n\nmodel.add(Flatten())\n\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(512))\nmodel.add(Activation('relu'))\n\nmodel.add(Dense(Y.shape[1]))\nmodel.add(Activation('softmax'))\n\nmodel.summary()\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-16T09:07:51.098408Z","iopub.execute_input":"2022-01-16T09:07:51.098704Z","iopub.status.idle":"2022-01-16T09:07:51.357347Z","shell.execute_reply.started":"2022-01-16T09:07:51.098675Z","shell.execute_reply":"2022-01-16T09:07:51.356399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch=32\n\nmodel.compile(\n      optimizer='adam',\n      loss='categorical_crossentropy',\n      metrics=['accuracy'])\n\ntrained_model=model.fit(X_train,Y_train,\n         epochs=20,\n         batch_size=batch,\n         steps_per_epoch=X_train.shape[0]//batch,\n         validation_steps=X_test.shape[0]//batch,\n         validation_data=(X_test,Y_test),\n         verbose=2)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-16T09:08:01.202392Z","iopub.execute_input":"2022-01-16T09:08:01.202667Z","iopub.status.idle":"2022-01-16T09:38:01.112810Z","shell.execute_reply.started":"2022-01-16T09:08:01.202639Z","shell.execute_reply":"2022-01-16T09:38:01.111700Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen = ImageDataGenerator()\n\ntest_set = test_datagen.flow_from_directory(\n    '/kaggle/input/dog-breed-identification',\n    target_size = (128,128),\n    classes=['test']\n)\ny_pred = model.predict(test_set)","metadata":{"execution":{"iopub.status.busy":"2022-01-16T09:38:26.151271Z","iopub.execute_input":"2022-01-16T09:38:26.151629Z","iopub.status.idle":"2022-01-16T09:40:12.527474Z","shell.execute_reply.started":"2022-01-16T09:38:26.151596Z","shell.execute_reply":"2022-01-16T09:40:12.526443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/dog-breed-identification/sample_submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-16T09:40:28.175465Z","iopub.execute_input":"2022-01-16T09:40:28.175763Z","iopub.status.idle":"2022-01-16T09:40:28.884340Z","shell.execute_reply.started":"2022-01-16T09:40:28.175731Z","shell.execute_reply":"2022-01-16T09:40:28.883524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\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    \nsubmission['id'] = id_list\nsubmission.iloc[:,1:] =y_pred\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-16T09:40:35.429647Z","iopub.execute_input":"2022-01-16T09:40:35.429912Z","iopub.status.idle":"2022-01-16T09:40:36.133055Z","shell.execute_reply.started":"2022-01-16T09:40:35.429885Z","shell.execute_reply":"2022-01-16T09:40:36.132084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"r = submission.set_index('id')\nr.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-01-16T09:40:42.144862Z","iopub.execute_input":"2022-01-16T09:40:42.145134Z","iopub.status.idle":"2022-01-16T09:40:45.085589Z","shell.execute_reply.started":"2022-01-16T09:40:42.145101Z","shell.execute_reply":"2022-01-16T09:40:45.084489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}