{"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":"import numpy as np\nimport pandas as pd\nimport os,cv2,random,time,shutil,csv\nimport tensorflow as tf\nfrom matplotlib import pyplot as plt\nfrom matplotlib import image as mpimg\nfrom tqdm import tqdm\nimport json,os,cv2,keras\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Model\nfrom keras.layers import BatchNormalization,Dense,GlobalAveragePooling2D,Lambda,Dropout,InputLayer,Input\nfrom tensorflow.keras.utils import to_categorical,plot_model\nfrom tensorflow.keras.preprocessing.image import load_img","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:37:26.363875Z","iopub.execute_input":"2022-07-18T09:37:26.364818Z","iopub.status.idle":"2022-07-18T09:37:26.372397Z","shell.execute_reply.started":"2022-07-18T09:37:26.364778Z","shell.execute_reply":"2022-07-18T09:37:26.371337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir='../input/dog-breed-identification/train'\ntest_dir='../input/dog-breed-identification/test'","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:38:00.854826Z","iopub.execute_input":"2022-07-18T09:38:00.855567Z","iopub.status.idle":"2022-07-18T09:38:00.860158Z","shell.execute_reply.started":"2022-07-18T09:38:00.855529Z","shell.execute_reply":"2022-07-18T09:38:00.859103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe=pd.read_csv('../input/dog-breed-identification/labels.csv')\nsample_df=pd.read_csv('../input/dog-breed-identification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T12:39:27.296055Z","iopub.execute_input":"2022-07-18T12:39:27.296429Z","iopub.status.idle":"2022-07-18T12:39:27.596544Z","shell.execute_reply.started":"2022-07-18T12:39:27.296395Z","shell.execute_reply":"2022-07-18T12:39:27.595607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:38:53.409489Z","iopub.execute_input":"2022-07-18T09:38:53.410462Z","iopub.status.idle":"2022-07-18T09:38:53.431385Z","shell.execute_reply.started":"2022-07-18T09:38:53.410412Z","shell.execute_reply":"2022-07-18T09:38:53.429664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:39:49.003798Z","iopub.execute_input":"2022-07-18T09:39:49.004341Z","iopub.status.idle":"2022-07-18T09:39:49.011064Z","shell.execute_reply.started":"2022-07-18T09:39:49.004307Z","shell.execute_reply":"2022-07-18T09:39:49.010117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:40:02.724194Z","iopub.execute_input":"2022-07-18T09:40:02.724661Z","iopub.status.idle":"2022-07-18T09:40:02.744099Z","shell.execute_reply.started":"2022-07-18T09:40:02.724601Z","shell.execute_reply":"2022-07-18T09:40:02.743206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"No of dog's images in train directory\",len(os.listdir(train_dir)))\nprint(\"No of dog's images in test directory\",len(os.listdir(test_dir)))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:42:01.824440Z","iopub.execute_input":"2022-07-18T09:42:01.825117Z","iopub.status.idle":"2022-07-18T09:42:02.118562Z","shell.execute_reply.started":"2022-07-18T09:42:01.825075Z","shell.execute_reply":"2022-07-18T09:42:02.117687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe['breed'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:43:45.742813Z","iopub.execute_input":"2022-07-18T09:43:45.743343Z","iopub.status.idle":"2022-07-18T09:43:45.752944Z","shell.execute_reply.started":"2022-07-18T09:43:45.743311Z","shell.execute_reply":"2022-07-18T09:43:45.751793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe['breed'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:44:43.408348Z","iopub.execute_input":"2022-07-18T09:44:43.408720Z","iopub.status.idle":"2022-07-18T09:44:43.416665Z","shell.execute_reply.started":"2022-07-18T09:44:43.408687Z","shell.execute_reply":"2022-07-18T09:44:43.415718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(action='ignore')\nimport seaborn as sns\nsns.countplot(labels_dataframe['breed'][:10])\nplt.xticks(rotation=60)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:48:08.718309Z","iopub.execute_input":"2022-07-18T09:48:08.718670Z","iopub.status.idle":"2022-07-18T09:48:08.931327Z","shell.execute_reply.started":"2022-07-18T09:48:08.718624Z","shell.execute_reply":"2022-07-18T09:48:08.930258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dog_breeds=sorted(list(set(labels_dataframe['breed'])))\nn_classes=len(dog_breeds)\nprint(n_classes)\nprint(dog_breeds[:15])","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:52:26.144975Z","iopub.execute_input":"2022-07-18T09:52:26.145506Z","iopub.status.idle":"2022-07-18T09:52:26.159457Z","shell.execute_reply.started":"2022-07-18T09:52:26.145465Z","shell.execute_reply":"2022-07-18T09:52:26.158506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes_to_num=dict(zip(dog_breeds,range(n_classes)))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:53:31.275814Z","iopub.execute_input":"2022-07-18T09:53:31.276734Z","iopub.status.idle":"2022-07-18T09:53:31.281804Z","shell.execute_reply.started":"2022-07-18T09:53:31.276695Z","shell.execute_reply":"2022-07-18T09:53:31.280503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes_to_num","metadata":{"execution":{"iopub.status.busy":"2022-07-18T09:53:46.635106Z","iopub.execute_input":"2022-07-18T09:53:46.635443Z","iopub.status.idle":"2022-07-18T09:53:46.646139Z","shell.execute_reply.started":"2022-07-18T09:53:46.635411Z","shell.execute_reply":"2022-07-18T09:53:46.645024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"../input/dog-breed-identification/train","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(train_dir+'/'+labels_dataframe['id']+'.jpg')[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:15:00.597579Z","iopub.execute_input":"2022-07-18T10:15:00.597934Z","iopub.status.idle":"2022-07-18T10:15:00.607294Z","shell.execute_reply.started":"2022-07-18T10:15:00.597904Z","shell.execute_reply":"2022-07-18T10:15:00.606282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv2.imread((train_dir+'/'+labels_dataframe['id']+'.jpg')[0]).shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:34:23.962189Z","iopub.execute_input":"2022-07-18T10:34:23.962812Z","iopub.status.idle":"2022-07-18T10:34:23.979115Z","shell.execute_reply.started":"2022-07-18T10:34:23.962774Z","shell.execute_reply":"2022-07-18T10:34:23.978175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cv2.imread((train_dir+'/'+labels_dataframe['id']+'.jpg')[9]).shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:34:27.452274Z","iopub.execute_input":"2022-07-18T10:34:27.452618Z","iopub.status.idle":"2022-07-18T10:34:27.466564Z","shell.execute_reply.started":"2022-07-18T10:34:27.452588Z","shell.execute_reply":"2022-07-18T10:34:27.465657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(cv2.imread((train_dir+'/'+labels_dataframe.id+'.jpg')[1]))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:16:25.909138Z","iopub.execute_input":"2022-07-18T10:16:25.909753Z","iopub.status.idle":"2022-07-18T10:16:26.143236Z","shell.execute_reply.started":"2022-07-18T10:16:25.909716Z","shell.execute_reply":"2022-07-18T10:16:26.142323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe['file_name']=labels_dataframe['id'].apply(lambda x: train_dir+'/'+f\"{x}.jpg\")\nlabels_dataframe","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:21:59.012078Z","iopub.execute_input":"2022-07-18T10:21:59.013069Z","iopub.status.idle":"2022-07-18T10:21:59.035340Z","shell.execute_reply.started":"2022-07-18T10:21:59.013026Z","shell.execute_reply":"2022-07-18T10:21:59.034043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(cv2.imread(labels_dataframe['file_name'][8000]))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:22:53.891265Z","iopub.execute_input":"2022-07-18T10:22:53.892156Z","iopub.status.idle":"2022-07-18T10:22:54.127505Z","shell.execute_reply.started":"2022-07-18T10:22:53.892118Z","shell.execute_reply":"2022-07-18T10:22:54.126534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe['breed']=labels_dataframe['breed'].map(classes_to_num)\nlabels_dataframe","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:24:14.784009Z","iopub.execute_input":"2022-07-18T10:24:14.784371Z","iopub.status.idle":"2022-07-18T10:24:14.803655Z","shell.execute_reply.started":"2022-07-18T10:24:14.784340Z","shell.execute_reply":"2022-07-18T10:24:14.802794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=to_categorical(labels_dataframe['breed'])\ny","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:25:19.319969Z","iopub.execute_input":"2022-07-18T10:25:19.320413Z","iopub.status.idle":"2022-07-18T10:25:19.332452Z","shell.execute_reply.started":"2022-07-18T10:25:19.320367Z","shell.execute_reply":"2022-07-18T10:25:19.331395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:25:41.211513Z","iopub.execute_input":"2022-07-18T10:25:41.211988Z","iopub.status.idle":"2022-07-18T10:25:41.220199Z","shell.execute_reply.started":"2022-07-18T10:25:41.211944Z","shell.execute_reply":"2022-07-18T10:25:41.219047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Extraction","metadata":{}},{"cell_type":"code","source":"import keras\nfrom keras.applications.inception_resnet_v2 import InceptionResNetV2,preprocess_input as resnet_preprocess\nfrom keras.applications.inception_v3 import InceptionV3,preprocess_input as inception_preprocess\nfrom keras.applications.xception import Xception , preprocess_input as xception_preprocess\nfrom keras.applications.nasnet import NASNetLarge,preprocess_input as nasnet_preprocess\nfrom keras.layers.merge import concatenate","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:32:31.632066Z","iopub.execute_input":"2022-07-18T10:32:31.632416Z","iopub.status.idle":"2022-07-18T10:32:31.638987Z","shell.execute_reply.started":"2022-07-18T10:32:31.632385Z","shell.execute_reply":"2022-07-18T10:32:31.637716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape=(331,331,3)\ninput_layer=Input(shape=(input_shape))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:50:21.392408Z","iopub.execute_input":"2022-07-18T10:50:21.392794Z","iopub.status.idle":"2022-07-18T10:50:21.400128Z","shell.execute_reply.started":"2022-07-18T10:50:21.392762Z","shell.execute_reply":"2022-07-18T10:50:21.399076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_layer","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:50:23.412006Z","iopub.execute_input":"2022-07-18T10:50:23.412364Z","iopub.status.idle":"2022-07-18T10:50:23.418585Z","shell.execute_reply.started":"2022-07-18T10:50:23.412331Z","shell.execute_reply":"2022-07-18T10:50:23.417350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\npreprocess_resnet=Lambda(resnet_preprocess)(input_layer)\ninception_resnet=InceptionResNetV2(weights='imagenet',include_top=False,\n                                   input_shape=input_shape,pooling='avg')(preprocess_resnet)\n\n\n\npreprocessor_inception=Lambda(inception_preprocess)(input_layer)\ninception_v3=InceptionV3(weights='imagenet',include_top=False,\n                                   input_shape=input_shape,pooling='avg')(preprocessor_inception)\n\n\n\npreprocessor_xception=Lambda(xception_preprocess)(input_layer)\nxception=Xception(weights='imagenet',include_top=False,\n                                   input_shape=input_shape,pooling='avg')(preprocessor_xception)\n\n\n\npreprocessor_nasnet=Lambda(nasnet_preprocess)(input_layer)\nnasnet=NASNetLarge(weights='imagenet',include_top=False,\n                                   input_shape=input_shape,pooling='avg')(preprocessor_nasnet)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:53:50.557637Z","iopub.execute_input":"2022-07-18T10:53:50.558329Z","iopub.status.idle":"2022-07-18T10:54:10.963572Z","shell.execute_reply.started":"2022-07-18T10:53:50.558292Z","shell.execute_reply":"2022-07-18T10:54:10.962480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocess_resnet","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:52:08.412428Z","iopub.execute_input":"2022-07-18T10:52:08.412814Z","iopub.status.idle":"2022-07-18T10:52:08.419487Z","shell.execute_reply.started":"2022-07-18T10:52:08.412781Z","shell.execute_reply":"2022-07-18T10:52:08.418551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inception_resnet","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:51:55.822159Z","iopub.execute_input":"2022-07-18T10:51:55.822503Z","iopub.status.idle":"2022-07-18T10:51:55.829968Z","shell.execute_reply.started":"2022-07-18T10:51:55.822474Z","shell.execute_reply":"2022-07-18T10:51:55.828855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nmerge=concatenate([inception_resnet,inception_v3,xception,nasnet])\nmodel=Model(inputs=input_layer,outputs=merge)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:55:02.671724Z","iopub.execute_input":"2022-07-18T10:55:02.672070Z","iopub.status.idle":"2022-07-18T10:55:02.688601Z","shell.execute_reply.started":"2022-07-18T10:55:02.672040Z","shell.execute_reply":"2022-07-18T10:55:02.687352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merge","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:55:10.961850Z","iopub.execute_input":"2022-07-18T10:55:10.962202Z","iopub.status.idle":"2022-07-18T10:55:10.968357Z","shell.execute_reply.started":"2022-07-18T10:55:10.962172Z","shell.execute_reply":"2022-07-18T10:55:10.967471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:55:17.943005Z","iopub.execute_input":"2022-07-18T10:55:17.943762Z","iopub.status.idle":"2022-07-18T10:55:17.952033Z","shell.execute_reply.started":"2022-07-18T10:55:17.943712Z","shell.execute_reply":"2022-07-18T10:55:17.950492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:55:46.591925Z","iopub.execute_input":"2022-07-18T10:55:46.592948Z","iopub.status.idle":"2022-07-18T10:55:46.735786Z","shell.execute_reply.started":"2022-07-18T10:55:46.592901Z","shell.execute_reply":"2022-07-18T10:55:46.734808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(model,show_shapes=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T10:59:21.573341Z","iopub.execute_input":"2022-07-18T10:59:21.573777Z","iopub.status.idle":"2022-07-18T10:59:21.737402Z","shell.execute_reply.started":"2022-07-18T10:59:21.573739Z","shell.execute_reply":"2022-07-18T10:59:21.736246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.input.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:00:13.041402Z","iopub.execute_input":"2022-07-18T11:00:13.041790Z","iopub.status.idle":"2022-07-18T11:00:13.047935Z","shell.execute_reply.started":"2022-07-18T11:00:13.041755Z","shell.execute_reply":"2022-07-18T11:00:13.046963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.output.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:05:42.091067Z","iopub.execute_input":"2022-07-18T11:05:42.091640Z","iopub.status.idle":"2022-07-18T11:05:42.103939Z","shell.execute_reply.started":"2022-07-18T11:05:42.091595Z","shell.execute_reply":"2022-07-18T11:05:42.102634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(model.trainable_weights)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:01:57.276731Z","iopub.execute_input":"2022-07-18T11:01:57.277105Z","iopub.status.idle":"2022-07-18T11:01:57.302531Z","shell.execute_reply.started":"2022-07-18T11:01:57.277076Z","shell.execute_reply":"2022-07-18T11:01:57.301639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature extraction by using pretrained model","metadata":{}},{"cell_type":"code","source":"def feature_extractor(df):\n    img_size = (331,331,3)\n    data_size = len(df)\n    batch_size = 22\n    x = np.zeros([data_size,9664], dtype=np.uint8)\n    datagen = ImageDataGenerator()\n    generator = datagen.flow_from_dataframe(df,x_col = 'file_name', class_mode = None, batch_size=batch_size, \n                                            shuffle = False,target_size = (img_size[:2]),color_mode = 'rgb')\n    \n    i = 0\n    \n    for input_batch in tqdm(generator):\n        input_batch = model.predict(input_batch)\n        x[i * batch_size : (i + 1) * batch_size] = input_batch\n        i += 1\n        if i * batch_size >= data_size:\n            break\n    return x\n        ","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:18:58.347849Z","iopub.execute_input":"2022-07-18T11:18:58.348211Z","iopub.status.idle":"2022-07-18T11:18:58.356026Z","shell.execute_reply.started":"2022-07-18T11:18:58.348180Z","shell.execute_reply":"2022-07-18T11:18:58.355063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=feature_extractor(labels_dataframe)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:19:01.177113Z","iopub.execute_input":"2022-07-18T11:19:01.177550Z","iopub.status.idle":"2022-07-18T11:28:52.869392Z","shell.execute_reply.started":"2022-07-18T11:19:01.177510Z","shell.execute_reply":"2022-07-18T11:28:52.868323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:28:56.486378Z","iopub.execute_input":"2022-07-18T11:28:56.486764Z","iopub.status.idle":"2022-07-18T11:28:56.494110Z","shell.execute_reply.started":"2022-07-18T11:28:56.486729Z","shell.execute_reply":"2022-07-18T11:28:56.493117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:28:59.867001Z","iopub.execute_input":"2022-07-18T11:28:59.867594Z","iopub.status.idle":"2022-07-18T11:28:59.879690Z","shell.execute_reply.started":"2022-07-18T11:28:59.867548Z","shell.execute_reply":"2022-07-18T11:28:59.878672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:29:18.446690Z","iopub.execute_input":"2022-07-18T11:29:18.447279Z","iopub.status.idle":"2022-07-18T11:29:18.455551Z","shell.execute_reply.started":"2022-07-18T11:29:18.447242Z","shell.execute_reply":"2022-07-18T11:29:18.454396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping,ModelCheckpoint,ReduceLROnPlateau\nearlystop=EarlyStopping(monitor='val_loss',patience=20,restore_best_weights=True)\ncheckpoint=ModelCheckpoint('kaggle/working/checkpoint',monitor='val_loss',mode='min',save_best_only=True)\nlr=ReduceLROnPlateau(monitor='val_loss',factor=0.5,patience=3,min_lr=1e-6)\nmy_callback=[earlystop,checkpoint,lr]","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:40:54.856225Z","iopub.execute_input":"2022-07-18T11:40:54.856671Z","iopub.status.idle":"2022-07-18T11:40:54.863852Z","shell.execute_reply.started":"2022-07-18T11:40:54.856610Z","shell.execute_reply":"2022-07-18T11:40:54.862504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Let's create the model","metadata":{}},{"cell_type":"code","source":"x.shape[1:]","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:35:02.766465Z","iopub.execute_input":"2022-07-18T11:35:02.766864Z","iopub.status.idle":"2022-07-18T11:35:02.774067Z","shell.execute_reply.started":"2022-07-18T11:35:02.766830Z","shell.execute_reply":"2022-07-18T11:35:02.773061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nnn=keras.models.Sequential([\n    \n    InputLayer(x.shape[1:]),\n    Dropout(0.7),\n    Dense(n_classes,activation='softmax')\n    \n    \n])\n\nnn.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])\nh=nn.fit(x,y,batch_size=120,epochs=100,validation_split=0.20,callbacks=my_callback)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:41:03.825181Z","iopub.execute_input":"2022-07-18T11:41:03.825538Z","iopub.status.idle":"2022-07-18T11:41:43.277527Z","shell.execute_reply.started":"2022-07-18T11:41:03.825505Z","shell.execute_reply":"2022-07-18T11:41:43.276660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plotting the results","metadata":{}},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12))\n\nax1.plot(h.history['loss'],color = 'b',label = 'loss')\nax1.plot(h.history['val_loss'],color = 'r',label = 'val_loss')\nax1.set_xticks(np.arange(1, 60, 1))\nax1.set_yticks(np.arange(0, 1, 0.1))\nax1.legend(['loss','val_loss'],shadow = True)\n\n\nax2.plot(h.history['accuracy'],color = 'green',label = 'accuracy')\nax2.plot(h.history['val_accuracy'],color = 'red',label = 'val_accuracy')\nax2.legend(['accuracy','val_accuracy'],shadow = True)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:43:46.685671Z","iopub.execute_input":"2022-07-18T11:43:46.686028Z","iopub.status.idle":"2022-07-18T11:43:47.344468Z","shell.execute_reply.started":"2022-07-18T11:43:46.685998Z","shell.execute_reply":"2022-07-18T11:43:47.343527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Working with test data","metadata":{}},{"cell_type":"code","source":"test_data=[]\nids=[]\n\nfor pic in os.listdir(test_dir):\n    ids.append(pic.split('.')[0])\n    test_data.append(test_dir+'/'+pic)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:46:35.745221Z","iopub.execute_input":"2022-07-18T11:46:35.745571Z","iopub.status.idle":"2022-07-18T11:46:35.765716Z","shell.execute_reply.started":"2022-07-18T11:46:35.745540Z","shell.execute_reply":"2022-07-18T11:46:35.764851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:47:07.785867Z","iopub.execute_input":"2022-07-18T11:47:07.786321Z","iopub.status.idle":"2022-07-18T11:47:07.793773Z","shell.execute_reply.started":"2022-07-18T11:47:07.786285Z","shell.execute_reply":"2022-07-18T11:47:07.792669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:47:10.345321Z","iopub.execute_input":"2022-07-18T11:47:10.345738Z","iopub.status.idle":"2022-07-18T11:47:10.352444Z","shell.execute_reply.started":"2022-07-18T11:47:10.345700Z","shell.execute_reply":"2022-07-18T11:47:10.351489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataframe=pd.DataFrame({'file_name':test_data})\ntest_dataframe","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:47:56.590659Z","iopub.execute_input":"2022-07-18T11:47:56.591266Z","iopub.status.idle":"2022-07-18T11:47:56.605078Z","shell.execute_reply.started":"2022-07-18T11:47:56.591228Z","shell.execute_reply":"2022-07-18T11:47:56.603973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_features=feature_extractor(test_dataframe)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:48:44.771052Z","iopub.execute_input":"2022-07-18T11:48:44.771439Z","iopub.status.idle":"2022-07-18T11:59:31.414785Z","shell.execute_reply.started":"2022-07-18T11:48:44.771406Z","shell.execute_reply":"2022-07-18T11:59:31.412904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_features","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:59:35.055958Z","iopub.execute_input":"2022-07-18T11:59:35.056305Z","iopub.status.idle":"2022-07-18T11:59:35.064740Z","shell.execute_reply.started":"2022-07-18T11:59:35.056275Z","shell.execute_reply":"2022-07-18T11:59:35.063719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_features.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-18T11:59:44.327172Z","iopub.execute_input":"2022-07-18T11:59:44.327773Z","iopub.status.idle":"2022-07-18T11:59:44.334982Z","shell.execute_reply.started":"2022-07-18T11:59:44.327710Z","shell.execute_reply":"2022-07-18T11:59:44.333458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=nn.predict(test_features)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2022-07-18T12:36:48.962211Z","iopub.execute_input":"2022-07-18T12:36:48.962863Z","iopub.status.idle":"2022-07-18T12:36:49.407689Z","shell.execute_reply.started":"2022-07-18T12:36:48.962824Z","shell.execute_reply":"2022-07-18T12:36:49.406694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.set_printoptions(suppress=True)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2022-07-18T12:35:57.912161Z","iopub.execute_input":"2022-07-18T12:35:57.912519Z","iopub.status.idle":"2022-07-18T12:35:57.920641Z","shell.execute_reply.started":"2022-07-18T12:35:57.912488Z","shell.execute_reply":"2022-07-18T12:35:57.919673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_key(val):\n    for key, value in classes_to_num.items():\n        if val==value:\n            return key\n        \npred_codes=np.argmax(y_pred,axis=1)\npredictions=[]\n\nfor i in pred_codes:\n    \n    predictions.append(get_key(i))","metadata":{"execution":{"iopub.status.busy":"2022-07-18T12:36:01.371499Z","iopub.execute_input":"2022-07-18T12:36:01.372488Z","iopub.status.idle":"2022-07-18T12:36:01.562355Z","shell.execute_reply.started":"2022-07-18T12:36:01.372441Z","shell.execute_reply":"2022-07-18T12:36:01.561429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_codes","metadata":{"execution":{"iopub.status.busy":"2022-07-18T12:36:05.569369Z","iopub.execute_input":"2022-07-18T12:36:05.569744Z","iopub.status.idle":"2022-07-18T12:36:05.576530Z","shell.execute_reply.started":"2022-07-18T12:36:05.569711Z","shell.execute_reply":"2022-07-18T12:36:05.575353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[:25]","metadata":{"execution":{"iopub.status.busy":"2022-07-18T12:02:06.522279Z","iopub.execute_input":"2022-07-18T12:02:06.522626Z","iopub.status.idle":"2022-07-18T12:02:06.529604Z","shell.execute_reply.started":"2022-07-18T12:02:06.522596Z","shell.execute_reply":"2022-07-18T12:02:06.528709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataframe['breed']=predictions\ntest_dataframe[:10]","metadata":{"execution":{"iopub.status.busy":"2022-07-18T12:36:11.482099Z","iopub.execute_input":"2022-07-18T12:36:11.482495Z","iopub.status.idle":"2022-07-18T12:36:11.493420Z","shell.execute_reply.started":"2022-07-18T12:36:11.482462Z","shell.execute_reply":"2022-07-18T12:36:11.492407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(6,6))\n\nfor index , data in test_dataframe[:10].iterrows():\n    img = data['file_name']\n    label = data['breed']\n    img = cv2.imread(img)\n#     plt.subplot(2,5, index+1)\n    plt.imshow(img)\n    plt.xlabel(label,fontsize = (15))\n    plt.tight_layout()\n    plt.show()    ","metadata":{"execution":{"iopub.status.busy":"2022-07-18T12:36:15.316508Z","iopub.execute_input":"2022-07-18T12:36:15.317200Z","iopub.status.idle":"2022-07-18T12:36:18.056436Z","shell.execute_reply.started":"2022-07-18T12:36:15.317142Z","shell.execute_reply":"2022-07-18T12:36:18.055568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.DataFrame(y_pred,columns=classes_to_num.keys())\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-07-18T12:37:03.201408Z","iopub.execute_input":"2022-07-18T12:37:03.201788Z","iopub.status.idle":"2022-07-18T12:37:03.238990Z","shell.execute_reply.started":"2022-07-18T12:37:03.201752Z","shell.execute_reply":"2022-07-18T12:37:03.237982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.float_format', lambda x: '%.15f' % x)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-07-18T12:38:59.226541Z","iopub.execute_input":"2022-07-18T12:38:59.226900Z","iopub.status.idle":"2022-07-18T12:38:59.252401Z","shell.execute_reply.started":"2022-07-18T12:38:59.226870Z","shell.execute_reply":"2022-07-18T12:38:59.251373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df","metadata":{"execution":{"iopub.status.busy":"2022-07-18T12:39:38.046240Z","iopub.execute_input":"2022-07-18T12:39:38.047311Z","iopub.status.idle":"2022-07-18T12:39:38.074533Z","shell.execute_reply.started":"2022-07-18T12:39:38.047261Z","shell.execute_reply":"2022-07-18T12:39:38.073628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df=sample_df[['id']]\nsample_df","metadata":{"execution":{"iopub.status.busy":"2022-07-18T12:39:41.205309Z","iopub.execute_input":"2022-07-18T12:39:41.205640Z","iopub.status.idle":"2022-07-18T12:39:41.218566Z","shell.execute_reply.started":"2022-07-18T12:39:41.205611Z","shell.execute_reply":"2022-07-18T12:39:41.217562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.merge(sample_df,submission,left_index=True,right_index=True)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-07-18T12:39:45.805829Z","iopub.execute_input":"2022-07-18T12:39:45.806510Z","iopub.status.idle":"2022-07-18T12:39:45.845419Z","shell.execute_reply.started":"2022-07-18T12:39:45.806459Z","shell.execute_reply":"2022-07-18T12:39:45.844492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('dog_breed_classifier2.csv',index=None)","metadata":{"execution":{"iopub.status.busy":"2022-07-18T12:40:12.926729Z","iopub.execute_input":"2022-07-18T12:40:12.927081Z","iopub.status.idle":"2022-07-18T12:40:14.585435Z","shell.execute_reply.started":"2022-07-18T12:40:12.927052Z","shell.execute_reply":"2022-07-18T12:40:14.584459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('dog_breed_classifier2.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-18T12:40:15.965640Z","iopub.execute_input":"2022-07-18T12:40:15.966029Z","iopub.status.idle":"2022-07-18T12:40:16.240332Z","shell.execute_reply.started":"2022-07-18T12:40:15.965999Z","shell.execute_reply":"2022-07-18T12:40:16.239398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}