{"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\n# for 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-08-08T17:55:02.837907Z","iopub.execute_input":"2021-08-08T17:55:02.838304Z","iopub.status.idle":"2021-08-08T17:55:02.850473Z","shell.execute_reply.started":"2021-08-08T17:55:02.838217Z","shell.execute_reply":"2021-08-08T17:55:02.849457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport os\nfrom tkinter import filedialog,Text\nfrom PIL import Image, ImageOps\nimport tkinter as tk\nimport io","metadata":{"execution":{"iopub.status.busy":"2021-08-08T17:55:02.852234Z","iopub.execute_input":"2021-08-08T17:55:02.852761Z","iopub.status.idle":"2021-08-08T17:55:02.980506Z","shell.execute_reply.started":"2021-08-08T17:55:02.852717Z","shell.execute_reply":"2021-08-08T17:55:02.979668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten\nimport tensorflow_hub as hub\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dot, Lambda, Input\nfrom tensorflow.keras.models import Sequential,Model\nimport keras\nfrom tensorflow.keras import datasets, layers, models","metadata":{"execution":{"iopub.status.busy":"2021-08-08T17:55:02.982376Z","iopub.execute_input":"2021-08-08T17:55:02.982756Z","iopub.status.idle":"2021-08-08T17:55:07.236892Z","shell.execute_reply.started":"2021-08-08T17:55:02.982718Z","shell.execute_reply":"2021-08-08T17:55:07.235980Z"},"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-08T17:55:07.238633Z","iopub.execute_input":"2021-08-08T17:55:07.238995Z","iopub.status.idle":"2021-08-08T17:55:07.286430Z","shell.execute_reply.started":"2021-08-08T17:55:07.238952Z","shell.execute_reply":"2021-08-08T17:55:07.285425Z"},"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-08T17:55:07.287981Z","iopub.execute_input":"2021-08-08T17:55:07.288344Z","iopub.status.idle":"2021-08-08T17:55:07.318944Z","shell.execute_reply.started":"2021-08-08T17:55:07.288305Z","shell.execute_reply":"2021-08-08T17:55:07.318156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.iloc[3,2][2:]","metadata":{"execution":{"iopub.status.busy":"2021-08-08T17:55:07.320313Z","iopub.execute_input":"2021-08-08T17:55:07.320689Z","iopub.status.idle":"2021-08-08T17:55:07.328973Z","shell.execute_reply.started":"2021-08-08T17:55:07.320624Z","shell.execute_reply":"2021-08-08T17:55:07.327814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_preocessing(path):\n    image = Image.open(path)\n    size = (128, 128)\n    image = ImageOps.fit(image, size, Image.ANTIALIAS)\n    #image= image.transpose(Image.FLIP_LEFT_RIGHT)\n    image_array = np.asarray(image)\n    a,b,c = (np.shape(image_array))\n    if c==4:\n        image_array = image_array[:,:,:3]\n    image_array = (image_array.astype(np.float32) / 255.0) \n    \n    return image_array","metadata":{"execution":{"iopub.status.busy":"2021-08-08T17:55:07.330758Z","iopub.execute_input":"2021-08-08T17:55:07.331379Z","iopub.status.idle":"2021-08-08T17:55:07.337193Z","shell.execute_reply.started":"2021-08-08T17:55:07.331342Z","shell.execute_reply":"2021-08-08T17:55:07.336278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir='../input/dog-breed-identification/train'\nprevfolder='none'\nlabel=0\ndogs = []\nlabel_names=[]\n\nfor i in range(0,len(train_data)):\n    label=train_data.iloc[i,1]\n    filename=train_data.iloc[i,2]\n    #print(filename)\n    image = image_preocessing(filename)\n    dogs.append(image)\n    label_names.append(label)\n\nprint(labels)\nprint(len(labels),\"images\")","metadata":{"execution":{"iopub.status.busy":"2021-08-08T17:55:07.340714Z","iopub.execute_input":"2021-08-08T17:55:07.341017Z","iopub.status.idle":"2021-08-08T17:56:43.447554Z","shell.execute_reply.started":"2021-08-08T17:55:07.340992Z","shell.execute_reply":"2021-08-08T17:56:43.446663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(np.shape(label_names))\nprint(np.shape(dogs))","metadata":{"execution":{"iopub.status.busy":"2021-08-08T17:56:43.449683Z","iopub.execute_input":"2021-08-08T17:56:43.450045Z","iopub.status.idle":"2021-08-08T17:56:44.075148Z","shell.execute_reply.started":"2021-08-08T17:56:43.450006Z","shell.execute_reply":"2021-08-08T17:56:44.073431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = pd.get_dummies(label_names)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T17:56:44.076546Z","iopub.execute_input":"2021-08-08T17:56:44.076939Z","iopub.status.idle":"2021-08-08T17:56:44.087898Z","shell.execute_reply.started":"2021-08-08T17:56:44.076897Z","shell.execute_reply":"2021-08-08T17:56:44.086997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train=np.array(dogs)\ndogs=5\nY_train=Y.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2021-08-08T17:56:44.089468Z","iopub.execute_input":"2021-08-08T17:56:44.089873Z","iopub.status.idle":"2021-08-08T17:56:44.679023Z","shell.execute_reply.started":"2021-08-08T17:56:44.089830Z","shell.execute_reply":"2021-08-08T17:56:44.678078Z"},"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_train,Y_train,test_size = 0.25)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T18:00:32.678915Z","iopub.execute_input":"2021-08-08T18:00:32.679300Z","iopub.status.idle":"2021-08-08T18:00:33.244628Z","shell.execute_reply.started":"2021-08-08T18:00:32.679266Z","shell.execute_reply":"2021-08-08T18:00:33.243702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape,Y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T18:00:37.877978Z","iopub.execute_input":"2021-08-08T18:00:37.878333Z","iopub.status.idle":"2021-08-08T18:00:37.883778Z","shell.execute_reply.started":"2021-08-08T18:00:37.878300Z","shell.execute_reply":"2021-08-08T18:00:37.882520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train[4]","metadata":{"execution":{"iopub.status.busy":"2021-08-08T18:00:48.110147Z","iopub.execute_input":"2021-08-08T18:00:48.110511Z","iopub.status.idle":"2021-08-08T18:00:48.117389Z","shell.execute_reply.started":"2021-08-08T18:00:48.110475Z","shell.execute_reply":"2021-08-08T18:00:48.116277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\ntrain_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-08T18:00:51.195296Z","iopub.execute_input":"2021-08-08T18:00:51.195637Z","iopub.status.idle":"2021-08-08T18:00:51.770563Z","shell.execute_reply.started":"2021-08-08T18:00:51.195601Z","shell.execute_reply":"2021-08-08T18:00:51.769307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(128,128, 3)))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((4, 4)))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(len(np.unique(label_names)), activation=\"softmax\"))\n\n  \n    \n    \nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-08T18:00:53.245587Z","iopub.execute_input":"2021-08-08T18:00:53.245979Z","iopub.status.idle":"2021-08-08T18:00:53.319501Z","shell.execute_reply.started":"2021-08-08T18:00:53.245943Z","shell.execute_reply":"2021-08-08T18:00:53.317938Z"},"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-08T18:00:58.169798Z","iopub.execute_input":"2021-08-08T18:00:58.170207Z","iopub.status.idle":"2021-08-08T18:00:58.185050Z","shell.execute_reply.started":"2021-08-08T18:00:58.170169Z","shell.execute_reply":"2021-08-08T18:00:58.183672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_generator,\n         validation_data=(X_test, Y_test),\n         epochs=1,\n         verbose=2)","metadata":{"execution":{"iopub.status.busy":"2021-08-08T18:01:25.028442Z","iopub.execute_input":"2021-08-08T18:01:25.028877Z","iopub.status.idle":"2021-08-08T18:01:51.341762Z","shell.execute_reply.started":"2021-08-08T18:01:25.028838Z","shell.execute_reply":"2021-08-08T18:01:51.340903Z"},"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-08T18:01:57.692352Z","iopub.execute_input":"2021-08-08T18:01:57.692834Z","iopub.status.idle":"2021-08-08T18:01:58.419851Z","shell.execute_reply.started":"2021-08-08T18:01:57.692782Z","shell.execute_reply":"2021-08-08T18:01:58.418877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-08T18:02:00.189454Z","iopub.execute_input":"2021-08-08T18:02:00.189857Z","iopub.status.idle":"2021-08-08T18:02:00.220530Z","shell.execute_reply.started":"2021-08-08T18:02:00.189820Z","shell.execute_reply":"2021-08-08T18:02:00.219423Z"},"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-08T18:02:02.452347Z","iopub.execute_input":"2021-08-08T18:02:02.452729Z","iopub.status.idle":"2021-08-08T18:02:20.572578Z","shell.execute_reply.started":"2021-08-08T18:02:02.452687Z","shell.execute_reply":"2021-08-08T18:02:20.571566Z"},"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-08T18:02:23.022704Z","iopub.execute_input":"2021-08-08T18:02:23.023051Z","iopub.status.idle":"2021-08-08T18:02:23.360699Z","shell.execute_reply.started":"2021-08-08T18:02:23.023015Z","shell.execute_reply":"2021-08-08T18:02:23.359655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(test_set)\ny_pred.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-08T18:04:15.986347Z","iopub.execute_input":"2021-08-08T18:04:15.986753Z","iopub.status.idle":"2021-08-08T18:06:13.161318Z","shell.execute_reply.started":"2021-08-08T18:04:15.986718Z","shell.execute_reply":"2021-08-08T18:06:13.160431Z"},"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-08T18:06:15.869850Z","iopub.execute_input":"2021-08-08T18:06:15.870252Z","iopub.status.idle":"2021-08-08T18:06:15.915167Z","shell.execute_reply.started":"2021-08-08T18:06:15.870218Z","shell.execute_reply":"2021-08-08T18:06:15.914245Z"},"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-08T18:06:18.392368Z","iopub.execute_input":"2021-08-08T18:06:18.392752Z","iopub.status.idle":"2021-08-08T18:06:18.902902Z","shell.execute_reply.started":"2021-08-08T18:06:18.392712Z","shell.execute_reply":"2021-08-08T18:06:18.901900Z"},"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-08T18:06:23.006210Z","iopub.execute_input":"2021-08-08T18:06:23.006556Z","iopub.status.idle":"2021-08-08T18:06:25.385547Z","shell.execute_reply.started":"2021-08-08T18:06:23.006523Z","shell.execute_reply":"2021-08-08T18:06:25.384627Z"},"trusted":true},"execution_count":null,"outputs":[]}]}