{"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":"2022-11-30T07:31:40.297433Z","iopub.execute_input":"2022-11-30T07:31:40.297671Z","iopub.status.idle":"2022-11-30T07:32:07.204267Z","shell.execute_reply.started":"2022-11-30T07:31:40.297606Z","shell.execute_reply":"2022-11-30T07:32:07.183257Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"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":"2022-11-30T07:32:07.214733Z","iopub.execute_input":"2022-11-30T07:32:07.214943Z","iopub.status.idle":"2022-11-30T07:32:07.701907Z","shell.execute_reply.started":"2022-11-30T07:32:07.214917Z","shell.execute_reply":"2022-11-30T07:32:07.701190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-30T07:32:07.703138Z","iopub.execute_input":"2022-11-30T07:32:07.703402Z","iopub.status.idle":"2022-11-30T07:32:07.722579Z","shell.execute_reply.started":"2022-11-30T07:32:07.703368Z","shell.execute_reply":"2022-11-30T07:32:07.721954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-30T07:32:07.724526Z","iopub.execute_input":"2022-11-30T07:32:07.724831Z","iopub.status.idle":"2022-11-30T07:32:07.749799Z","shell.execute_reply.started":"2022-11-30T07:32:07.724798Z","shell.execute_reply":"2022-11-30T07:32:07.749080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def jpg(id):\n    return id+\".jpg\"","metadata":{"execution":{"iopub.status.busy":"2022-11-30T07:32:07.750847Z","iopub.execute_input":"2022-11-30T07:32:07.751157Z","iopub.status.idle":"2022-11-30T07:32:07.755160Z","shell.execute_reply.started":"2022-11-30T07:32:07.751119Z","shell.execute_reply":"2022-11-30T07:32:07.754335Z"},"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":"2022-11-30T07:32:07.756664Z","iopub.execute_input":"2022-11-30T07:32:07.756966Z","iopub.status.idle":"2022-11-30T07:32:07.772617Z","shell.execute_reply.started":"2022-11-30T07:32:07.756933Z","shell.execute_reply":"2022-11-30T07:32:07.771715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.count()","metadata":{"execution":{"iopub.status.busy":"2022-11-30T07:32:07.773917Z","iopub.execute_input":"2022-11-30T07:32:07.774212Z","iopub.status.idle":"2022-11-30T07:32:07.792721Z","shell.execute_reply.started":"2022-11-30T07:32:07.774179Z","shell.execute_reply":"2022-11-30T07:32:07.791757Z"},"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":"2022-11-30T07:32:07.793951Z","iopub.execute_input":"2022-11-30T07:32:07.794192Z","iopub.status.idle":"2022-11-30T07:32:12.487923Z","shell.execute_reply.started":"2022-11-30T07:32:07.794160Z","shell.execute_reply":"2022-11-30T07:32:12.487136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2022-11-30T07:32:12.489069Z","iopub.execute_input":"2022-11-30T07:32:12.489336Z","iopub.status.idle":"2022-11-30T07:32:12.494216Z","shell.execute_reply.started":"2022-11-30T07:32:12.489303Z","shell.execute_reply":"2022-11-30T07:32:12.493298Z"},"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":"2022-11-30T07:32:12.495770Z","iopub.execute_input":"2022-11-30T07:32:12.496197Z","iopub.status.idle":"2022-11-30T07:32:12.774933Z","shell.execute_reply.started":"2022-11-30T07:32:12.496161Z","shell.execute_reply":"2022-11-30T07:32:12.774162Z"},"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":"2022-11-30T07:32:12.778263Z","iopub.execute_input":"2022-11-30T07:32:12.778539Z","iopub.status.idle":"2022-11-30T07:32:17.029215Z","shell.execute_reply.started":"2022-11-30T07:32:12.778506Z","shell.execute_reply":"2022-11-30T07:32:17.028429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names = train.class_indices","metadata":{"execution":{"iopub.status.busy":"2022-11-30T07:32:17.030368Z","iopub.execute_input":"2022-11-30T07:32:17.031033Z","iopub.status.idle":"2022-11-30T07:32:17.035468Z","shell.execute_reply.started":"2022-11-30T07:32:17.030991Z","shell.execute_reply":"2022-11-30T07:32:17.034234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = [k for k,v in names.items()]","metadata":{"execution":{"iopub.status.busy":"2022-11-30T07:32:17.036798Z","iopub.execute_input":"2022-11-30T07:32:17.037543Z","iopub.status.idle":"2022-11-30T07:32:17.045873Z","shell.execute_reply.started":"2022-11-30T07:32:17.037510Z","shell.execute_reply":"2022-11-30T07:32:17.045105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names[:10]","metadata":{"execution":{"iopub.status.busy":"2022-11-30T07:32:17.047213Z","iopub.execute_input":"2022-11-30T07:32:17.047540Z","iopub.status.idle":"2022-11-30T07:32:17.058442Z","shell.execute_reply.started":"2022-11-30T07:32:17.047508Z","shell.execute_reply":"2022-11-30T07:32:17.057686Z"},"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":"2022-11-30T07:32:17.059649Z","iopub.execute_input":"2022-11-30T07:32:17.059945Z","iopub.status.idle":"2022-11-30T07:32:17.932733Z","shell.execute_reply.started":"2022-11-30T07:32:17.059913Z","shell.execute_reply":"2022-11-30T07:32:17.931915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y[0]","metadata":{"execution":{"iopub.status.busy":"2022-11-30T07:32:17.933894Z","iopub.execute_input":"2022-11-30T07:32:17.934149Z","iopub.status.idle":"2022-11-30T07:32:17.940886Z","shell.execute_reply.started":"2022-11-30T07:32:17.934111Z","shell.execute_reply":"2022-11-30T07:32:17.940188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-11-30T07:32:17.942120Z","iopub.execute_input":"2022-11-30T07:32:17.942572Z","iopub.status.idle":"2022-11-30T07:32:17.951013Z","shell.execute_reply.started":"2022-11-30T07:32:17.942537Z","shell.execute_reply":"2022-11-30T07:32:17.950115Z"},"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":"2022-11-30T07:32:17.952322Z","iopub.execute_input":"2022-11-30T07:32:17.952620Z","iopub.status.idle":"2022-11-30T07:32:17.961117Z","shell.execute_reply.started":"2022-11-30T07:32:17.952584Z","shell.execute_reply":"2022-11-30T07:32:17.960407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(x,y)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T07:32:17.962088Z","iopub.execute_input":"2022-11-30T07:32:17.962475Z","iopub.status.idle":"2022-11-30T07:32:20.496727Z","shell.execute_reply.started":"2022-11-30T07:32:17.962443Z","shell.execute_reply":"2022-11-30T07:32:20.496136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.inception_resnet_v2 import InceptionResNetV2","metadata":{"execution":{"iopub.status.busy":"2022-11-30T07:32:20.497822Z","iopub.execute_input":"2022-11-30T07:32:20.498601Z","iopub.status.idle":"2022-11-30T07:32:20.502095Z","shell.execute_reply.started":"2022-11-30T07:32:20.498563Z","shell.execute_reply":"2022-11-30T07:32:20.501273Z"},"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":"2022-11-30T07:32:20.503461Z","iopub.execute_input":"2022-11-30T07:32:20.504523Z","iopub.status.idle":"2022-11-30T07:32:36.468588Z","shell.execute_reply.started":"2022-11-30T07:32:20.504488Z","shell.execute_reply":"2022-11-30T07:32:36.467792Z"},"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":"2022-11-30T07:32:36.469923Z","iopub.execute_input":"2022-11-30T07:32:36.470192Z","iopub.status.idle":"2022-11-30T07:32:36.474820Z","shell.execute_reply.started":"2022-11-30T07:32:36.470157Z","shell.execute_reply":"2022-11-30T07:32:36.473882Z"},"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":"2022-11-30T07:32:36.476387Z","iopub.execute_input":"2022-11-30T07:32:36.476857Z","iopub.status.idle":"2022-11-30T07:32:37.839153Z","shell.execute_reply.started":"2022-11-30T07:32:36.476821Z","shell.execute_reply":"2022-11-30T07:32:37.838426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='categorical_crossentropy',metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-11-30T07:32:37.840328Z","iopub.execute_input":"2022-11-30T07:32:37.840595Z","iopub.status.idle":"2022-11-30T07:32:37.860307Z","shell.execute_reply.started":"2022-11-30T07:32:37.840553Z","shell.execute_reply":"2022-11-30T07:32:37.859435Z"},"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":"2022-11-30T07:32:37.862970Z","iopub.execute_input":"2022-11-30T07:32:37.863184Z","iopub.status.idle":"2022-11-30T07:32:37.870377Z","shell.execute_reply.started":"2022-11-30T07:32:37.863154Z","shell.execute_reply":"2022-11-30T07:32:37.869599Z"},"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":"2022-11-30T07:32:37.871580Z","iopub.execute_input":"2022-11-30T07:32:37.871911Z","iopub.status.idle":"2022-11-30T07:53:12.261102Z","shell.execute_reply.started":"2022-11-30T07:32:37.871873Z","shell.execute_reply":"2022-11-30T07:53:12.260449Z"},"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":"2022-11-30T07:53:12.264545Z","iopub.execute_input":"2022-11-30T07:53:12.267372Z","iopub.status.idle":"2022-11-30T07:53:53.928915Z","shell.execute_reply.started":"2022-11-30T07:53:12.267333Z","shell.execute_reply":"2022-11-30T07:53:53.928091Z"},"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":"2022-11-30T07:53:53.930436Z","iopub.execute_input":"2022-11-30T07:53:53.930721Z","iopub.status.idle":"2022-11-30T07:53:54.155843Z","shell.execute_reply.started":"2022-11-30T07:53:53.930683Z","shell.execute_reply":"2022-11-30T07:53:54.155098Z"},"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":"2022-11-30T07:53:54.157178Z","iopub.execute_input":"2022-11-30T07:53:54.157468Z","iopub.status.idle":"2022-11-30T07:54:00.722013Z","shell.execute_reply.started":"2022-11-30T07:53:54.157434Z","shell.execute_reply":"2022-11-30T07:54:00.721283Z"},"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":"2022-11-30T07:54:00.723794Z","iopub.execute_input":"2022-11-30T07:54:00.724060Z","iopub.status.idle":"2022-11-30T07:54:00.729047Z","shell.execute_reply.started":"2022-11-30T07:54:00.724025Z","shell.execute_reply":"2022-11-30T07:54:00.728045Z"},"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":"2022-11-30T07:54:00.730630Z","iopub.execute_input":"2022-11-30T07:54:00.731036Z","iopub.status.idle":"2022-11-30T07:54:07.521164Z","shell.execute_reply.started":"2022-11-30T07:54:00.731000Z","shell.execute_reply":"2022-11-30T07:54:07.520382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test,verbose =2)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T07:54:07.525154Z","iopub.execute_input":"2022-11-30T07:54:07.525796Z","iopub.status.idle":"2022-11-30T07:56:07.132278Z","shell.execute_reply.started":"2022-11-30T07:54:07.525764Z","shell.execute_reply":"2022-11-30T07:56:07.131537Z"},"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":"2022-11-30T07:56:07.133644Z","iopub.execute_input":"2022-11-30T07:56:07.134507Z","iopub.status.idle":"2022-11-30T07:56:07.378361Z","shell.execute_reply.started":"2022-11-30T07:56:07.134469Z","shell.execute_reply":"2022-11-30T07:56:07.377364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.iloc[:,1:] =pred","metadata":{"execution":{"iopub.status.busy":"2022-11-30T07:56:07.379862Z","iopub.execute_input":"2022-11-30T07:56:07.380217Z","iopub.status.idle":"2022-11-30T07:56:07.743405Z","shell.execute_reply.started":"2022-11-30T07:56:07.380179Z","shell.execute_reply":"2022-11-30T07:56:07.742646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-30T07:56:07.744532Z","iopub.execute_input":"2022-11-30T07:56:07.745173Z","iopub.status.idle":"2022-11-30T07:56:07.773357Z","shell.execute_reply.started":"2022-11-30T07:56:07.745137Z","shell.execute_reply":"2022-11-30T07:56:07.772215Z"},"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":"2022-11-30T07:56:07.774779Z","iopub.execute_input":"2022-11-30T07:56:07.775036Z","iopub.status.idle":"2022-11-30T07:56:09.481826Z","shell.execute_reply.started":"2022-11-30T07:56:07.775001Z","shell.execute_reply":"2022-11-30T07:56:09.481089Z"},"trusted":true},"execution_count":null,"outputs":[]}]}