{"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-07-29T10:26:06.544262Z","iopub.execute_input":"2021-07-29T10:26:06.544623Z","iopub.status.idle":"2021-07-29T10:26:21.673018Z","shell.execute_reply.started":"2021-07-29T10:26:06.544547Z","shell.execute_reply":"2021-07-29T10:26:21.662448Z"},"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-07-29T10:26:46.485173Z","iopub.execute_input":"2021-07-29T10:26:46.485495Z","iopub.status.idle":"2021-07-29T10:26:46.998107Z","shell.execute_reply.started":"2021-07-29T10:26:46.485465Z","shell.execute_reply":"2021-07-29T10:26:46.997260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T10:26:56.077253Z","iopub.execute_input":"2021-07-29T10:26:56.077574Z","iopub.status.idle":"2021-07-29T10:26:56.100787Z","shell.execute_reply.started":"2021-07-29T10:26:56.077545Z","shell.execute_reply":"2021-07-29T10:26:56.099607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T10:27:00.316248Z","iopub.execute_input":"2021-07-29T10:27:00.316569Z","iopub.status.idle":"2021-07-29T10:27:00.345270Z","shell.execute_reply.started":"2021-07-29T10:27:00.316539Z","shell.execute_reply":"2021-07-29T10:27:00.344275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def jpg(id):\n    return id+\".jpg\"","metadata":{"execution":{"iopub.status.busy":"2021-07-29T10:27:13.238389Z","iopub.execute_input":"2021-07-29T10:27:13.238722Z","iopub.status.idle":"2021-07-29T10:27:13.243453Z","shell.execute_reply.started":"2021-07-29T10:27:13.238676Z","shell.execute_reply":"2021-07-29T10:27:13.242434Z"},"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-07-29T10:27:15.756751Z","iopub.execute_input":"2021-07-29T10:27:15.757076Z","iopub.status.idle":"2021-07-29T10:27:15.771779Z","shell.execute_reply.started":"2021-07-29T10:27:15.757046Z","shell.execute_reply":"2021-07-29T10:27:15.771016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.count()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T10:27:22.797715Z","iopub.execute_input":"2021-07-29T10:27:22.798105Z","iopub.status.idle":"2021-07-29T10:27:22.815798Z","shell.execute_reply.started":"2021-07-29T10:27:22.798067Z","shell.execute_reply":"2021-07-29T10:27:22.814893Z"},"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-07-29T10:27:39.677902Z","iopub.execute_input":"2021-07-29T10:27:39.678207Z","iopub.status.idle":"2021-07-29T10:27:43.561716Z","shell.execute_reply.started":"2021-07-29T10:27:39.678180Z","shell.execute_reply":"2021-07-29T10:27:43.560795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2021-07-29T10:27:52.677516Z","iopub.execute_input":"2021-07-29T10:27:52.677848Z","iopub.status.idle":"2021-07-29T10:27:52.683067Z","shell.execute_reply.started":"2021-07-29T10:27:52.677817Z","shell.execute_reply":"2021-07-29T10:27:52.681968Z"},"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-07-29T10:27:55.229928Z","iopub.execute_input":"2021-07-29T10:27:55.230301Z","iopub.status.idle":"2021-07-29T10:27:55.237593Z","shell.execute_reply.started":"2021-07-29T10:27:55.230271Z","shell.execute_reply":"2021-07-29T10:27:55.236599Z"},"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-07-29T10:28:08.510098Z","iopub.execute_input":"2021-07-29T10:28:08.510477Z","iopub.status.idle":"2021-07-29T10:28:11.793183Z","shell.execute_reply.started":"2021-07-29T10:28:08.510444Z","shell.execute_reply":"2021-07-29T10:28:11.792310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"names = train.class_indices","metadata":{"execution":{"iopub.status.busy":"2021-07-29T10:28:23.877659Z","iopub.execute_input":"2021-07-29T10:28:23.878019Z","iopub.status.idle":"2021-07-29T10:28:23.882085Z","shell.execute_reply.started":"2021-07-29T10:28:23.877988Z","shell.execute_reply":"2021-07-29T10:28:23.880952Z"},"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-07-29T10:28:29.157012Z","iopub.execute_input":"2021-07-29T10:28:29.157315Z","iopub.status.idle":"2021-07-29T10:28:29.160853Z","shell.execute_reply.started":"2021-07-29T10:28:29.157288Z","shell.execute_reply":"2021-07-29T10:28:29.160039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names[:10]","metadata":{"execution":{"iopub.status.busy":"2021-07-29T10:28:37.709571Z","iopub.execute_input":"2021-07-29T10:28:37.709923Z","iopub.status.idle":"2021-07-29T10:28:37.715397Z","shell.execute_reply.started":"2021-07-29T10:28:37.709893Z","shell.execute_reply":"2021-07-29T10:28:37.714598Z"},"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-07-29T10:29:00.508857Z","iopub.execute_input":"2021-07-29T10:29:00.509187Z","iopub.status.idle":"2021-07-29T10:29:01.528057Z","shell.execute_reply.started":"2021-07-29T10:29:00.509147Z","shell.execute_reply":"2021-07-29T10:29:01.527068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y[0]","metadata":{"execution":{"iopub.status.busy":"2021-07-29T10:29:06.317252Z","iopub.execute_input":"2021-07-29T10:29:06.317602Z","iopub.status.idle":"2021-07-29T10:29:06.327587Z","shell.execute_reply.started":"2021-07-29T10:29:06.317570Z","shell.execute_reply":"2021-07-29T10:29:06.326153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2021-07-29T10:29:37.869246Z","iopub.execute_input":"2021-07-29T10:29:37.869567Z","iopub.status.idle":"2021-07-29T10:29:37.873513Z","shell.execute_reply.started":"2021-07-29T10:29:37.869537Z","shell.execute_reply":"2021-07-29T10:29:37.872456Z"},"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-07-29T10:29:54.838505Z","iopub.execute_input":"2021-07-29T10:29:54.838868Z","iopub.status.idle":"2021-07-29T10:29:54.844836Z","shell.execute_reply.started":"2021-07-29T10:29:54.838835Z","shell.execute_reply":"2021-07-29T10:29:54.843433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot(x,y)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T10:29:57.718180Z","iopub.execute_input":"2021-07-29T10:29:57.718536Z","iopub.status.idle":"2021-07-29T10:30:00.302586Z","shell.execute_reply.started":"2021-07-29T10:29:57.718505Z","shell.execute_reply":"2021-07-29T10:30:00.301706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.applications.inception_resnet_v2 import InceptionResNetV2","metadata":{"execution":{"iopub.status.busy":"2021-07-29T10:30:09.077399Z","iopub.execute_input":"2021-07-29T10:30:09.077813Z","iopub.status.idle":"2021-07-29T10:30:09.082752Z","shell.execute_reply.started":"2021-07-29T10:30:09.077774Z","shell.execute_reply":"2021-07-29T10:30:09.081672Z"},"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-07-29T10:30:16.877026Z","iopub.execute_input":"2021-07-29T10:30:16.877368Z","iopub.status.idle":"2021-07-29T10:30:24.462612Z","shell.execute_reply.started":"2021-07-29T10:30:16.877336Z","shell.execute_reply":"2021-07-29T10:30:24.461715Z"},"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-07-29T10:30:42.677890Z","iopub.execute_input":"2021-07-29T10:30:42.678196Z","iopub.status.idle":"2021-07-29T10:30:42.684423Z","shell.execute_reply.started":"2021-07-29T10:30:42.678170Z","shell.execute_reply":"2021-07-29T10:30:42.683647Z"},"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-07-29T10:30:50.109146Z","iopub.execute_input":"2021-07-29T10:30:50.109468Z","iopub.status.idle":"2021-07-29T10:30:51.448117Z","shell.execute_reply.started":"2021-07-29T10:30:50.109438Z","shell.execute_reply":"2021-07-29T10:30:51.447339Z"},"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-07-29T10:31:07.438350Z","iopub.execute_input":"2021-07-29T10:31:07.438677Z","iopub.status.idle":"2021-07-29T10:31:07.671941Z","shell.execute_reply.started":"2021-07-29T10:31:07.438648Z","shell.execute_reply":"2021-07-29T10:31:07.670949Z"},"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-07-29T10:31:10.957392Z","iopub.execute_input":"2021-07-29T10:31:10.957746Z","iopub.status.idle":"2021-07-29T10:31:10.961994Z","shell.execute_reply.started":"2021-07-29T10:31:10.957710Z","shell.execute_reply":"2021-07-29T10:31:10.961147Z"},"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-07-29T10:31:18.956920Z","iopub.execute_input":"2021-07-29T10:31:18.957236Z","iopub.status.idle":"2021-07-29T11:11:02.509196Z","shell.execute_reply.started":"2021-07-29T10:31:18.957208Z","shell.execute_reply":"2021-07-29T11:11:02.508252Z"},"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-07-29T11:13:19.824465Z","iopub.execute_input":"2021-07-29T11:13:19.824791Z","iopub.status.idle":"2021-07-29T11:13:19.986516Z","shell.execute_reply.started":"2021-07-29T11:13:19.824761Z","shell.execute_reply":"2021-07-29T11:13:19.985740Z"},"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()\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T11:13:34.416667Z","iopub.execute_input":"2021-07-29T11:13:34.416997Z","iopub.status.idle":"2021-07-29T11:13:34.560854Z","shell.execute_reply.started":"2021-07-29T11:13:34.416968Z","shell.execute_reply":"2021-07-29T11:13:34.560078Z"},"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-07-29T11:13:46.775947Z","iopub.execute_input":"2021-07-29T11:13:46.776278Z","iopub.status.idle":"2021-07-29T11:13:52.860655Z","shell.execute_reply.started":"2021-07-29T11:13:46.776250Z","shell.execute_reply":"2021-07-29T11:13:52.859664Z"},"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-07-29T11:13:57.487849Z","iopub.execute_input":"2021-07-29T11:13:57.488176Z","iopub.status.idle":"2021-07-29T11:13:57.493335Z","shell.execute_reply.started":"2021-07-29T11:13:57.488147Z","shell.execute_reply":"2021-07-29T11:13:57.492327Z"},"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-07-29T11:14:13.080299Z","iopub.execute_input":"2021-07-29T11:14:13.080669Z","iopub.status.idle":"2021-07-29T11:14:16.651037Z","shell.execute_reply.started":"2021-07-29T11:14:13.080635Z","shell.execute_reply":"2021-07-29T11:14:16.650112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test,verbose =2)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T11:14:22.207195Z","iopub.execute_input":"2021-07-29T11:14:22.207512Z","iopub.status.idle":"2021-07-29T11:16:16.423021Z","shell.execute_reply.started":"2021-07-29T11:14:22.207483Z","shell.execute_reply":"2021-07-29T11:16:16.422208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')\nsubmission.head()\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T11:16:43.297763Z","iopub.execute_input":"2021-07-29T11:16:43.298143Z","iopub.status.idle":"2021-07-29T11:16:43.604629Z","shell.execute_reply.started":"2021-07-29T11:16:43.298112Z","shell.execute_reply":"2021-07-29T11:16:43.603654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.iloc[:,1:] =pred","metadata":{"execution":{"iopub.status.busy":"2021-07-29T11:16:56.528230Z","iopub.execute_input":"2021-07-29T11:16:56.528555Z","iopub.status.idle":"2021-07-29T11:16:56.955586Z","shell.execute_reply.started":"2021-07-29T11:16:56.528526Z","shell.execute_reply":"2021-07-29T11:16:56.954637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T11:16:59.975266Z","iopub.execute_input":"2021-07-29T11:16:59.975748Z","iopub.status.idle":"2021-07-29T11:17:00.012214Z","shell.execute_reply.started":"2021-07-29T11:16:59.975678Z","shell.execute_reply":"2021-07-29T11:17:00.011375Z"},"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-07-29T11:17:21.369493Z","iopub.execute_input":"2021-07-29T11:17:21.369923Z","iopub.status.idle":"2021-07-29T11:17:23.861020Z","shell.execute_reply.started":"2021-07-29T11:17:21.369885Z","shell.execute_reply":"2021-07-29T11:17:23.860137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}