{"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-12-01T13:41:12.687400Z","iopub.execute_input":"2021-12-01T13:41:12.687956Z","iopub.status.idle":"2021-12-01T13:41:22.013984Z","shell.execute_reply.started":"2021-12-01T13:41:12.687843Z","shell.execute_reply":"2021-12-01T13:41:22.013233Z"},"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-12-01T13:41:58.882019Z","iopub.execute_input":"2021-12-01T13:41:58.882299Z","iopub.status.idle":"2021-12-01T13:41:59.323087Z","shell.execute_reply.started":"2021-12-01T13:41:58.882267Z","shell.execute_reply":"2021-12-01T13:41:59.322360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-01T13:42:10.915919Z","iopub.execute_input":"2021-12-01T13:42:10.916181Z","iopub.status.idle":"2021-12-01T13:42:10.935849Z","shell.execute_reply.started":"2021-12-01T13:42:10.916154Z","shell.execute_reply":"2021-12-01T13:42:10.935186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-01T13:42:23.840046Z","iopub.execute_input":"2021-12-01T13:42:23.840635Z","iopub.status.idle":"2021-12-01T13:42:23.869254Z","shell.execute_reply.started":"2021-12-01T13:42:23.840596Z","shell.execute_reply":"2021-12-01T13:42:23.868451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def jpg(id):\n    return id+\".jpg\"","metadata":{"execution":{"iopub.status.busy":"2021-12-01T13:42:37.518668Z","iopub.execute_input":"2021-12-01T13:42:37.519133Z","iopub.status.idle":"2021-12-01T13:42:37.523250Z","shell.execute_reply.started":"2021-12-01T13:42:37.519098Z","shell.execute_reply":"2021-12-01T13:42:37.522329Z"},"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-12-01T13:42:49.572245Z","iopub.execute_input":"2021-12-01T13:42:49.572761Z","iopub.status.idle":"2021-12-01T13:42:49.589204Z","shell.execute_reply.started":"2021-12-01T13:42:49.572723Z","shell.execute_reply":"2021-12-01T13:42:49.588363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.count()","metadata":{"execution":{"iopub.status.busy":"2021-12-01T13:43:01.077827Z","iopub.execute_input":"2021-12-01T13:43:01.078625Z","iopub.status.idle":"2021-12-01T13:43:01.096041Z","shell.execute_reply.started":"2021-12-01T13:43:01.078568Z","shell.execute_reply":"2021-12-01T13:43:01.095396Z"},"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-12-01T13:43:17.556309Z","iopub.execute_input":"2021-12-01T13:43:17.556603Z","iopub.status.idle":"2021-12-01T13:43:22.232421Z","shell.execute_reply.started":"2021-12-01T13:43:17.556573Z","shell.execute_reply":"2021-12-01T13:43:22.231691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2021-12-01T13:43:40.267847Z","iopub.execute_input":"2021-12-01T13:43:40.268123Z","iopub.status.idle":"2021-12-01T13:43:40.274494Z","shell.execute_reply.started":"2021-12-01T13:43:40.268092Z","shell.execute_reply":"2021-12-01T13:43:40.273704Z"},"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-12-01T13:44:14.511346Z","iopub.execute_input":"2021-12-01T13:44:14.512192Z","iopub.status.idle":"2021-12-01T13:44:14.813640Z","shell.execute_reply.started":"2021-12-01T13:44:14.512152Z","shell.execute_reply":"2021-12-01T13:44:14.812973Z"},"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)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-01T13:44:57.860594Z","iopub.execute_input":"2021-12-01T13:44:57.861143Z","iopub.status.idle":"2021-12-01T13:45:01.733874Z","shell.execute_reply.started":"2021-12-01T13:44:57.861106Z","shell.execute_reply":"2021-12-01T13:45:01.733102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names = train.class_indices","metadata":{"execution":{"iopub.status.busy":"2021-12-01T13:45:25.547175Z","iopub.execute_input":"2021-12-01T13:45:25.547973Z","iopub.status.idle":"2021-12-01T13:45:25.552225Z","shell.execute_reply.started":"2021-12-01T13:45:25.547926Z","shell.execute_reply":"2021-12-01T13:45:25.551399Z"},"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-12-01T13:45:36.766156Z","iopub.execute_input":"2021-12-01T13:45:36.766440Z","iopub.status.idle":"2021-12-01T13:45:36.770244Z","shell.execute_reply.started":"2021-12-01T13:45:36.766410Z","shell.execute_reply":"2021-12-01T13:45:36.769483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names[:10]","metadata":{"execution":{"iopub.status.busy":"2021-12-01T13:45:50.384785Z","iopub.execute_input":"2021-12-01T13:45:50.385047Z","iopub.status.idle":"2021-12-01T13:45:50.390692Z","shell.execute_reply.started":"2021-12-01T13:45:50.385020Z","shell.execute_reply":"2021-12-01T13:45:50.390008Z"},"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-12-01T13:46:03.242080Z","iopub.execute_input":"2021-12-01T13:46:03.242330Z","iopub.status.idle":"2021-12-01T13:46:04.079086Z","shell.execute_reply.started":"2021-12-01T13:46:03.242302Z","shell.execute_reply":"2021-12-01T13:46:04.078305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y[0]","metadata":{"execution":{"iopub.status.busy":"2021-12-01T13:46:16.762860Z","iopub.execute_input":"2021-12-01T13:46:16.763529Z","iopub.status.idle":"2021-12-01T13:46:16.770089Z","shell.execute_reply.started":"2021-12-01T13:46:16.763471Z","shell.execute_reply":"2021-12-01T13:46:16.769417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-12-01T13:46:30.057392Z","iopub.execute_input":"2021-12-01T13:46:30.057901Z","iopub.status.idle":"2021-12-01T13:46:30.061772Z","shell.execute_reply.started":"2021-12-01T13:46:30.057868Z","shell.execute_reply":"2021-12-01T13:46:30.060840Z"},"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-12-01T13:46:41.341011Z","iopub.execute_input":"2021-12-01T13:46:41.341274Z","iopub.status.idle":"2021-12-01T13:46:41.347036Z","shell.execute_reply.started":"2021-12-01T13:46:41.341245Z","shell.execute_reply":"2021-12-01T13:46:41.346196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(x,y)","metadata":{"execution":{"iopub.status.busy":"2021-12-01T13:46:55.418375Z","iopub.execute_input":"2021-12-01T13:46:55.418923Z","iopub.status.idle":"2021-12-01T13:46:57.998159Z","shell.execute_reply.started":"2021-12-01T13:46:55.418884Z","shell.execute_reply":"2021-12-01T13:46:57.997526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.inception_resnet_v2 import InceptionResNetV2","metadata":{"execution":{"iopub.status.busy":"2021-12-01T13:47:13.905190Z","iopub.execute_input":"2021-12-01T13:47:13.905466Z","iopub.status.idle":"2021-12-01T13:47:13.909474Z","shell.execute_reply.started":"2021-12-01T13:47:13.905436Z","shell.execute_reply":"2021-12-01T13:47:13.908482Z"},"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-12-01T13:47:28.535790Z","iopub.execute_input":"2021-12-01T13:47:28.536636Z","iopub.status.idle":"2021-12-01T13:47:36.919467Z","shell.execute_reply.started":"2021-12-01T13:47:28.536596Z","shell.execute_reply":"2021-12-01T13:47:36.918733Z"},"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-12-01T13:48:20.190283Z","iopub.execute_input":"2021-12-01T13:48:20.190575Z","iopub.status.idle":"2021-12-01T13:48:20.194943Z","shell.execute_reply.started":"2021-12-01T13:48:20.190544Z","shell.execute_reply":"2021-12-01T13:48:20.194276Z"},"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()","metadata":{"execution":{"iopub.status.busy":"2021-12-01T13:48:34.304756Z","iopub.execute_input":"2021-12-01T13:48:34.305013Z","iopub.status.idle":"2021-12-01T13:48:35.647481Z","shell.execute_reply.started":"2021-12-01T13:48:34.304985Z","shell.execute_reply":"2021-12-01T13:48:35.646718Z"},"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-12-01T13:48:51.078122Z","iopub.execute_input":"2021-12-01T13:48:51.078379Z","iopub.status.idle":"2021-12-01T13:48:51.102982Z","shell.execute_reply.started":"2021-12-01T13:48:51.078351Z","shell.execute_reply":"2021-12-01T13:48:51.102261Z"},"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-12-01T13:49:04.495257Z","iopub.execute_input":"2021-12-01T13:49:04.495538Z","iopub.status.idle":"2021-12-01T13:49:04.499636Z","shell.execute_reply.started":"2021-12-01T13:49:04.495499Z","shell.execute_reply":"2021-12-01T13:49:04.498753Z"},"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-12-01T13:49:18.909266Z","iopub.execute_input":"2021-12-01T13:49:18.909804Z","iopub.status.idle":"2021-12-01T14:08:16.019573Z","shell.execute_reply.started":"2021-12-01T13:49:18.909766Z","shell.execute_reply":"2021-12-01T14:08:16.018780Z"},"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-12-01T14:08:38.680074Z","iopub.execute_input":"2021-12-01T14:08:38.680650Z","iopub.status.idle":"2021-12-01T14:08:38.940967Z","shell.execute_reply.started":"2021-12-01T14:08:38.680611Z","shell.execute_reply":"2021-12-01T14:08:38.940268Z"},"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-12-01T14:08:53.308908Z","iopub.execute_input":"2021-12-01T14:08:53.309162Z","iopub.status.idle":"2021-12-01T14:08:53.518040Z","shell.execute_reply.started":"2021-12-01T14:08:53.309135Z","shell.execute_reply":"2021-12-01T14:08:53.517350Z"},"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-12-01T14:09:09.720100Z","iopub.execute_input":"2021-12-01T14:09:09.720693Z","iopub.status.idle":"2021-12-01T14:09:16.896630Z","shell.execute_reply.started":"2021-12-01T14:09:09.720655Z","shell.execute_reply":"2021-12-01T14:09:16.895875Z"},"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-12-01T14:09:33.080412Z","iopub.execute_input":"2021-12-01T14:09:33.080709Z","iopub.status.idle":"2021-12-01T14:09:33.085162Z","shell.execute_reply.started":"2021-12-01T14:09:33.080678Z","shell.execute_reply":"2021-12-01T14:09:33.084194Z"},"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-12-01T14:09:48.240954Z","iopub.execute_input":"2021-12-01T14:09:48.241726Z","iopub.status.idle":"2021-12-01T14:09:52.319123Z","shell.execute_reply.started":"2021-12-01T14:09:48.241672Z","shell.execute_reply":"2021-12-01T14:09:52.318361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test,verbose =2)","metadata":{"execution":{"iopub.status.busy":"2021-12-01T14:10:11.648341Z","iopub.execute_input":"2021-12-01T14:10:11.648618Z","iopub.status.idle":"2021-12-01T14:12:03.540074Z","shell.execute_reply.started":"2021-12-01T14:10:11.648587Z","shell.execute_reply":"2021-12-01T14:12:03.539319Z"},"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-12-01T14:12:23.596506Z","iopub.execute_input":"2021-12-01T14:12:23.597351Z","iopub.status.idle":"2021-12-01T14:12:23.873045Z","shell.execute_reply.started":"2021-12-01T14:12:23.597310Z","shell.execute_reply":"2021-12-01T14:12:23.872199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.iloc[:,1:] =pred","metadata":{"execution":{"iopub.status.busy":"2021-12-01T14:12:38.035128Z","iopub.execute_input":"2021-12-01T14:12:38.035557Z","iopub.status.idle":"2021-12-01T14:12:38.476833Z","shell.execute_reply.started":"2021-12-01T14:12:38.035519Z","shell.execute_reply":"2021-12-01T14:12:38.476062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-01T14:12:52.606916Z","iopub.execute_input":"2021-12-01T14:12:52.607621Z","iopub.status.idle":"2021-12-01T14:12:52.637454Z","shell.execute_reply.started":"2021-12-01T14:12:52.607584Z","shell.execute_reply":"2021-12-01T14:12:52.636780Z"},"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-12-01T14:13:06.971019Z","iopub.execute_input":"2021-12-01T14:13:06.971789Z","iopub.status.idle":"2021-12-01T14:13:09.461227Z","shell.execute_reply.started":"2021-12-01T14:13:06.971748Z","shell.execute_reply":"2021-12-01T14:13:09.460500Z"},"trusted":true},"execution_count":null,"outputs":[]}]}