{"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","execution":{"iopub.status.busy":"2021-06-22T16:26:06.796553Z","iopub.execute_input":"2021-06-22T16:26:06.797060Z","iopub.status.idle":"2021-06-22T16:26:37.619401Z","shell.execute_reply.started":"2021-06-22T16:26:06.796981Z","shell.execute_reply":"2021-06-22T16:26:37.618023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\nsample = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:37.635128Z","iopub.execute_input":"2021-06-22T16:26:37.635448Z","iopub.status.idle":"2021-06-22T16:26:38.190214Z","shell.execute_reply.started":"2021-06-22T16:26:37.635419Z","shell.execute_reply":"2021-06-22T16:26:38.189194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:38.193227Z","iopub.execute_input":"2021-06-22T16:26:38.193635Z","iopub.status.idle":"2021-06-22T16:26:38.219537Z","shell.execute_reply.started":"2021-06-22T16:26:38.193592Z","shell.execute_reply":"2021-06-22T16:26:38.218032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:38.221734Z","iopub.execute_input":"2021-06-22T16:26:38.222242Z","iopub.status.idle":"2021-06-22T16:26:38.255639Z","shell.execute_reply.started":"2021-06-22T16:26:38.222196Z","shell.execute_reply":"2021-06-22T16:26:38.254349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def jpg(id):\n    return id+\".jpg\"","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:38.261932Z","iopub.execute_input":"2021-06-22T16:26:38.262297Z","iopub.status.idle":"2021-06-22T16:26:38.267042Z","shell.execute_reply.started":"2021-06-22T16:26:38.262268Z","shell.execute_reply":"2021-06-22T16:26:38.265765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels[\"id\"] = labels[\"id\"].apply(jpg)\nsample[\"id\"] = sample[\"id\"].apply(jpg)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:38.272044Z","iopub.execute_input":"2021-06-22T16:26:38.272503Z","iopub.status.idle":"2021-06-22T16:26:38.290999Z","shell.execute_reply.started":"2021-06-22T16:26:38.272458Z","shell.execute_reply":"2021-06-22T16:26:38.289835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.count()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:38.292349Z","iopub.execute_input":"2021-06-22T16:26:38.292858Z","iopub.status.idle":"2021-06-22T16:26:38.310042Z","shell.execute_reply.started":"2021-06-22T16:26:38.292815Z","shell.execute_reply":"2021-06-22T16:26:38.308601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator as Imgen\nfrom keras.preprocessing import image","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:38.311646Z","iopub.execute_input":"2021-06-22T16:26:38.312122Z","iopub.status.idle":"2021-06-22T16:26:44.492180Z","shell.execute_reply.started":"2021-06-22T16:26:38.312076Z","shell.execute_reply":"2021-06-22T16:26:44.491150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:44.494201Z","iopub.execute_input":"2021-06-22T16:26:44.494718Z","iopub.status.idle":"2021-06-22T16:26:44.501403Z","shell.execute_reply.started":"2021-06-22T16:26:44.494664Z","shell.execute_reply":"2021-06-22T16:26:44.500070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagenerated = Imgen(preprocessing_function = keras.applications.nasnet.preprocess_input,\n                      shear_range = 0.2,\n                     horizontal_flip = True,\n                     validation_split = 0.1)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:44.503695Z","iopub.execute_input":"2021-06-22T16:26:44.504237Z","iopub.status.idle":"2021-06-22T16:26:44.519699Z","shell.execute_reply.started":"2021-06-22T16:26:44.504192Z","shell.execute_reply":"2021-06-22T16:26:44.518621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = datagenerated.flow_from_dataframe(\nlabels,\ndirectory = '../input/dog-breed-identification/train',\nx_col = 'id',\ny_col = 'breed',\nsubset = 'training',\ncolor_mode = 'rgb',\nclass_mode = 'categorical',\ntarget_size = (331,331),\nbatch_size = 32,\nshuffle =True,\nseed = 123)\n\nval_ds = datagenerated.flow_from_dataframe(\nlabels,\ndirectory = '../input/dog-breed-identification/train',\nx_col = 'id',\ny_col = 'breed',\nsubset = 'validation',\ncolor_mode = 'rgb',\nclass_mode = 'categorical',\ntarget_size = (331,331),\nbatch_size = 32,\nshuffle =True,\nseed = 123)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:44.522337Z","iopub.execute_input":"2021-06-22T16:26:44.522666Z","iopub.status.idle":"2021-06-22T16:26:48.800628Z","shell.execute_reply.started":"2021-06-22T16:26:44.522637Z","shell.execute_reply":"2021-06-22T16:26:48.798670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names = train.class_indices","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:48.802066Z","iopub.execute_input":"2021-06-22T16:26:48.802474Z","iopub.status.idle":"2021-06-22T16:26:48.812405Z","shell.execute_reply.started":"2021-06-22T16:26:48.802427Z","shell.execute_reply":"2021-06-22T16:26:48.808514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = [k for k,v in names.items()]","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:48.814397Z","iopub.execute_input":"2021-06-22T16:26:48.814898Z","iopub.status.idle":"2021-06-22T16:26:48.820859Z","shell.execute_reply.started":"2021-06-22T16:26:48.814852Z","shell.execute_reply":"2021-06-22T16:26:48.819641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names[:10]","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:48.823485Z","iopub.execute_input":"2021-06-22T16:26:48.823976Z","iopub.status.idle":"2021-06-22T16:26:48.835844Z","shell.execute_reply.started":"2021-06-22T16:26:48.823931Z","shell.execute_reply":"2021-06-22T16:26:48.834245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x,y = next(train)\nprint(y.shape)\nprint(x.shape)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:48.837992Z","iopub.execute_input":"2021-06-22T16:26:48.838718Z","iopub.status.idle":"2021-06-22T16:26:49.838249Z","shell.execute_reply.started":"2021-06-22T16:26:48.838674Z","shell.execute_reply":"2021-06-22T16:26:49.837078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y[0]","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:49.839793Z","iopub.execute_input":"2021-06-22T16:26:49.840286Z","iopub.status.idle":"2021-06-22T16:26:49.851545Z","shell.execute_reply.started":"2021-06-22T16:26:49.840243Z","shell.execute_reply":"2021-06-22T16:26:49.850124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:49.854305Z","iopub.execute_input":"2021-06-22T16:26:49.854834Z","iopub.status.idle":"2021-06-22T16:26:49.860998Z","shell.execute_reply.started":"2021-06-22T16:26:49.854789Z","shell.execute_reply":"2021-06-22T16:26:49.859645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot(images,labels):\n    plt.figure(figsize = [20,15])\n    for i in range(30):\n        plt.subplot(5,6,i+1)\n        plt.imshow(images[i])\n        plt.title(class_names[np.argmax(labels[i])])\n        plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:49.863498Z","iopub.execute_input":"2021-06-22T16:26:49.863955Z","iopub.status.idle":"2021-06-22T16:26:49.873076Z","shell.execute_reply.started":"2021-06-22T16:26:49.863910Z","shell.execute_reply":"2021-06-22T16:26:49.871526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(x,y)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:49.875118Z","iopub.execute_input":"2021-06-22T16:26:49.875759Z","iopub.status.idle":"2021-06-22T16:26:52.452003Z","shell.execute_reply.started":"2021-06-22T16:26:49.875550Z","shell.execute_reply":"2021-06-22T16:26:52.450885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.inception_resnet_v2 import InceptionResNetV2","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:52.454245Z","iopub.execute_input":"2021-06-22T16:26:52.454653Z","iopub.status.idle":"2021-06-22T16:26:52.460309Z","shell.execute_reply.started":"2021-06-22T16:26:52.454605Z","shell.execute_reply":"2021-06-22T16:26:52.458831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transfer_model = InceptionResNetV2(include_top = False,\n                                   weights ='imagenet',\n                                  input_shape = (331,331,3)\n                                  )\ntransfer_model.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:26:52.462346Z","iopub.execute_input":"2021-06-22T16:26:52.463008Z","iopub.status.idle":"2021-06-22T16:27:08.603910Z","shell.execute_reply.started":"2021-06-22T16:26:52.462944Z","shell.execute_reply":"2021-06-22T16:27:08.602696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense,MaxPooling2D,Dropout,Conv2D,GlobalAveragePooling2D,Flatten","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:27:08.609413Z","iopub.execute_input":"2021-06-22T16:27:08.612154Z","iopub.status.idle":"2021-06-22T16:27:08.619668Z","shell.execute_reply.started":"2021-06-22T16:27:08.612089Z","shell.execute_reply":"2021-06-22T16:27:08.618522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(transfer_model)\nmodel.add(GlobalAveragePooling2D())\nmodel.add(Dense(256,activation='relu'))\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(120,activation = 'softmax'))\n\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:27:08.625272Z","iopub.execute_input":"2021-06-22T16:27:08.628753Z","iopub.status.idle":"2021-06-22T16:27:10.602793Z","shell.execute_reply.started":"2021-06-22T16:27:08.628490Z","shell.execute_reply":"2021-06-22T16:27:10.601827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='categorical_crossentropy',metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:27:10.606862Z","iopub.execute_input":"2021-06-22T16:27:10.607166Z","iopub.status.idle":"2021-06-22T16:27:10.847820Z","shell.execute_reply.started":"2021-06-22T16:27:10.607136Z","shell.execute_reply":"2021-06-22T16:27:10.846498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback = [keras.callbacks.EarlyStopping(monitor='val_accuracy',patience=2),\n           keras.callbacks.ModelCheckpoint(\"MyModel.h5\",save_best_only =True,verbose =2)]","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:27:33.647710Z","iopub.execute_input":"2021-06-22T16:27:33.648131Z","iopub.status.idle":"2021-06-22T16:27:33.653646Z","shell.execute_reply.started":"2021-06-22T16:27:33.648098Z","shell.execute_reply":"2021-06-22T16:27:33.652457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train,epochs = 25,validation_data = val_ds,callbacks = callback)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T16:36:09.357632Z","iopub.execute_input":"2021-06-22T16:36:09.358079Z","iopub.status.idle":"2021-06-22T16:58:17.124844Z","shell.execute_reply.started":"2021-06-22T16:36:09.357988Z","shell.execute_reply":"2021-06-22T16:58:17.123180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(7,6))\nplt.plot(history.epoch,history.history['accuracy'],label =\"training_acc\")\nplt.plot(history.epoch,history.history['val_accuracy'],label =\"validation_acc\")\nplt.title(\"Accuracy\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T17:04:34.699720Z","iopub.execute_input":"2021-06-22T17:04:34.700305Z","iopub.status.idle":"2021-06-22T17:04:35.200484Z","shell.execute_reply.started":"2021-06-22T17:04:34.700272Z","shell.execute_reply":"2021-06-22T17:04:35.198974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.epoch,history.history[\"loss\"],label = \"training_loss\")\nplt.plot(history.epoch,history.history[\"val_loss\"],label = \"validation_loss\")\nplt.title(\"Loss\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T17:04:07.643651Z","iopub.execute_input":"2021-06-22T17:04:07.644098Z","iopub.status.idle":"2021-06-22T17:04:07.840845Z","shell.execute_reply.started":"2021-06-22T17:04:07.644050Z","shell.execute_reply":"2021-06-22T17:04:07.839814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\nmodel = load_model(\"./MyModel.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-06-22T17:06:05.703233Z","iopub.execute_input":"2021-06-22T17:06:05.703625Z","iopub.status.idle":"2021-06-22T17:06:14.164084Z","shell.execute_reply.started":"2021-06-22T17:06:05.703591Z","shell.execute_reply":"2021-06-22T17:06:14.162778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testgen = Imgen(preprocessing_function=keras.applications.inception_resnet_v2.preprocess_input)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T17:06:19.954626Z","iopub.execute_input":"2021-06-22T17:06:19.955022Z","iopub.status.idle":"2021-06-22T17:06:19.960733Z","shell.execute_reply.started":"2021-06-22T17:06:19.954972Z","shell.execute_reply":"2021-06-22T17:06:19.959418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = testgen.flow_from_dataframe(\nsample,\ndirectory='/kaggle/input/dog-breed-identification/test',\n    x_col = 'id',\n    y_col = None,\n    color_mode = 'rgb',\n    target_size = (331,331),\n    class_mode = None,\n    batch_size = 32,\n    shuffle = False\n    \n)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T17:06:43.440551Z","iopub.execute_input":"2021-06-22T17:06:43.440943Z","iopub.status.idle":"2021-06-22T17:06:47.205447Z","shell.execute_reply.started":"2021-06-22T17:06:43.440910Z","shell.execute_reply":"2021-06-22T17:06:47.204155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test,verbose =2)","metadata":{"execution":{"iopub.status.busy":"2021-06-22T17:07:06.082641Z","iopub.execute_input":"2021-06-22T17:07:06.083310Z","iopub.status.idle":"2021-06-22T17:09:14.666026Z","shell.execute_reply.started":"2021-06-22T17:07:06.083242Z","shell.execute_reply":"2021-06-22T17:09:14.665164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T17:09:48.344537Z","iopub.execute_input":"2021-06-22T17:09:48.344965Z","iopub.status.idle":"2021-06-22T17:09:48.646478Z","shell.execute_reply.started":"2021-06-22T17:09:48.344927Z","shell.execute_reply":"2021-06-22T17:09:48.645250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.iloc[:,1:] =pred","metadata":{"execution":{"iopub.status.busy":"2021-06-22T17:09:59.261921Z","iopub.execute_input":"2021-06-22T17:09:59.262366Z","iopub.status.idle":"2021-06-22T17:09:59.666921Z","shell.execute_reply.started":"2021-06-22T17:09:59.262327Z","shell.execute_reply":"2021-06-22T17:09:59.665750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-22T17:10:08.916234Z","iopub.execute_input":"2021-06-22T17:10:08.916601Z","iopub.status.idle":"2021-06-22T17:10:08.952243Z","shell.execute_reply.started":"2021-06-22T17:10:08.916569Z","shell.execute_reply":"2021-06-22T17:10:08.950601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_sub = submission.set_index('id')\nfinal_sub.to_csv('Submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-06-22T17:10:43.789382Z","iopub.execute_input":"2021-06-22T17:10:43.789741Z","iopub.status.idle":"2021-06-22T17:10:47.344356Z","shell.execute_reply.started":"2021-06-22T17:10:43.789709Z","shell.execute_reply":"2021-06-22T17:10:47.343002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}