{"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\n#for 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-22T12:22:43.885712Z","iopub.execute_input":"2021-07-22T12:22:43.886054Z","iopub.status.idle":"2021-07-22T12:22:43.889724Z","shell.execute_reply.started":"2021-07-22T12:22:43.886026Z","shell.execute_reply":"2021-07-22T12:22:43.888917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Importing Modules","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport random\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom shutil import copyfile\nfrom shutil import move\nfrom tensorflow.keras.utils import plot_model\n%matplotlib inline\nimport matplotlib.image as mpimg\nimport matplotlib.pyplot as plt\nimport shutil","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:22:51.215821Z","iopub.execute_input":"2021-07-22T12:22:51.216166Z","iopub.status.idle":"2021-07-22T12:22:51.223551Z","shell.execute_reply.started":"2021-07-22T12:22:51.216138Z","shell.execute_reply":"2021-07-22T12:22:51.222618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Understanding Dataset","metadata":{}},{"cell_type":"code","source":"len(os.listdir('../input/dog-breed-identification/test'))","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:21:00.092541Z","iopub.execute_input":"2021-07-22T12:21:00.092935Z","iopub.status.idle":"2021-07-22T12:21:00.308019Z","shell.execute_reply.started":"2021-07-22T12:21:00.092899Z","shell.execute_reply":"2021-07-22T12:21:00.306977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/dog-breed-identification/labels.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:21:03.018185Z","iopub.execute_input":"2021-07-22T12:21:03.018653Z","iopub.status.idle":"2021-07-22T12:21:03.058161Z","shell.execute_reply.started":"2021-07-22T12:21:03.01861Z","shell.execute_reply":"2021-07-22T12:21:03.057244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From initial analysis of the dataset we got to know that the test contains images with the name of the image as its id and the file labels.csv contains the classes related to each file.\nWe also get that the dataset is unbalanced","metadata":{}},{"cell_type":"markdown","source":"# Making Directory Flowchart","metadata":{}},{"cell_type":"code","source":"breeds = df.groupby('breed')","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:21:09.778169Z","iopub.execute_input":"2021-07-22T12:21:09.77849Z","iopub.status.idle":"2021-07-22T12:21:09.78874Z","shell.execute_reply.started":"2021-07-22T12:21:09.77846Z","shell.execute_reply":"2021-07-22T12:21:09.787715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('dogbreeds_in')\nos.mkdir('dogbreeds_in/train')\nos.mkdir('dogbreeds_in/validation')","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:21:12.362353Z","iopub.execute_input":"2021-07-22T12:21:12.362669Z","iopub.status.idle":"2021-07-22T12:21:12.36722Z","shell.execute_reply.started":"2021-07-22T12:21:12.362639Z","shell.execute_reply":"2021-07-22T12:21:12.366345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"split_size = 0.7","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:21:18.322283Z","iopub.execute_input":"2021-07-22T12:21:18.322593Z","iopub.status.idle":"2021-07-22T12:21:18.326657Z","shell.execute_reply.started":"2021-07-22T12:21:18.322564Z","shell.execute_reply":"2021-07-22T12:21:18.325186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for breed,group in breeds:\n    files = list(group['id'])\n    ln = len(files)\n    train_p = 'dogbreeds/train/'+breed\n    val_p = 'dogbreeds/validation/'+breed\n    os.mkdir(train_p)\n    os.mkdir(val_p)\n    random.shuffle(files)\n    for ind,fn in enumerate(files):\n        fn_path = '../input/dog-breed-identification/train/' + fn + '.jpg'\n        if ind<split_size*ln:\n            tar_path = train_p +'/'+ fn + '.jpg'\n            copyfile(fn_path,tar_path)\n        else:\n            tar_path = val_p  +'/'+ fn + '.jpg'\n            copyfile(fn_path,tar_path)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:21:21.007599Z","iopub.execute_input":"2021-07-22T12:21:21.007969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"In this subsection we devide the train data into train and validation. We also made a directory graph so that the ImageDataGenerator can use it to distrubte the data in it's various classes ","metadata":{}},{"cell_type":"markdown","source":"# Generating Databatches for Training","metadata":{}},{"cell_type":"code","source":"train_dir = './dogbreeds_in/train'\n\ntrain_datagen = ImageDataGenerator(\n    rotation_range = 20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range = 0.2,\n    zoom_range = 0.2,\n    horizontal_flip = True,\n    fill_mode = 'nearest' \n\n)\n\ntrain_generator = train_datagen.flow_from_directory(\n    train_dir,\n    target_size = (331,331),\n    batch_size = 64,\n    class_mode = 'categorical',\n    shuffle = True\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T07:43:54.913807Z","iopub.execute_input":"2021-07-22T07:43:54.914192Z","iopub.status.idle":"2021-07-22T07:43:55.446047Z","shell.execute_reply.started":"2021-07-22T07:43:54.914159Z","shell.execute_reply":"2021-07-22T07:43:55.444793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_dir = './dogbreeds_in/validation'\n\nval_datagen = ImageDataGenerator()\n\nval_generator = val_datagen.flow_from_directory(\n    val_dir,\n    target_size = (331,331),\n    batch_size = 64,\n    class_mode = 'categorical'\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T07:43:58.408446Z","iopub.execute_input":"2021-07-22T07:43:58.408898Z","iopub.status.idle":"2021-07-22T07:43:58.727774Z","shell.execute_reply.started":"2021-07-22T07:43:58.408868Z","shell.execute_reply":"2021-07-22T07:43:58.726767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We generate data in a batch size of 64 and use image augmentation on the test data","metadata":{}},{"cell_type":"markdown","source":"# Training Using Pretrained Models","metadata":{}},{"cell_type":"markdown","source":"## Single Trained DeepNN","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras.applications.inception_v3 import preprocess_input as inceptpp","metadata":{"execution":{"iopub.status.busy":"2021-07-21T20:37:42.122901Z","iopub.execute_input":"2021-07-21T20:37:42.123241Z","iopub.status.idle":"2021-07-21T20:37:42.127147Z","shell.execute_reply.started":"2021-07-21T20:37:42.123204Z","shell.execute_reply":"2021-07-21T20:37:42.126041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dnn_model = InceptionV3(\n      input_shape=(331,331,3),\n      include_top = False,\n      weights = 'imagenet'\n)\n\nfor layer in dnn_model.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-07-21T20:37:42.128459Z","iopub.execute_input":"2021-07-21T20:37:42.129056Z","iopub.status.idle":"2021-07-21T20:37:47.266137Z","shell.execute_reply.started":"2021-07-21T20:37:42.129019Z","shell.execute_reply":"2021-07-21T20:37:47.265278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    tf.keras.layers.Lambda(inceptpp,input_shape=(331,331,3)),\n    dnn_model,\n    tf.keras.layers.GlobalAveragePooling2D(),\n    tf.keras.layers.Dense(120,activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2021-07-21T20:37:47.267458Z","iopub.execute_input":"2021-07-21T20:37:47.2678Z","iopub.status.idle":"2021-07-21T20:37:47.862562Z","shell.execute_reply.started":"2021-07-21T20:37:47.267765Z","shell.execute_reply":"2021-07-21T20:37:47.861747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-21T20:37:47.863761Z","iopub.execute_input":"2021-07-21T20:37:47.864103Z","iopub.status.idle":"2021-07-21T20:37:47.889229Z","shell.execute_reply.started":"2021-07-21T20:37:47.864069Z","shell.execute_reply":"2021-07-21T20:37:47.888412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(model,show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T20:40:17.936879Z","iopub.execute_input":"2021-07-21T20:40:17.937269Z","iopub.status.idle":"2021-07-21T20:40:18.340907Z","shell.execute_reply.started":"2021-07-21T20:40:17.937241Z","shell.execute_reply":"2021-07-21T20:40:18.337919Z"},"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-21T20:41:06.263117Z","iopub.execute_input":"2021-07-21T20:41:06.263457Z","iopub.status.idle":"2021-07-21T20:41:06.280454Z","shell.execute_reply.started":"2021-07-21T20:41:06.263425Z","shell.execute_reply":"2021-07-21T20:41:06.27954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    epochs = 10,\n    steps_per_epoch = 7213//64,\n    validation_data = val_generator,\n    validation_steps = 3009//64\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T20:41:09.796665Z","iopub.execute_input":"2021-07-21T20:41:09.797062Z","iopub.status.idle":"2021-07-21T21:16:42.032275Z","shell.execute_reply.started":"2021-07-21T20:41:09.797031Z","shell.execute_reply":"2021-07-21T21:16:42.03133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n%matplotlib inline\n\nimport matplotlib.image  as mpimg\nimport matplotlib.pyplot as plt\n\nacc=history.history['accuracy']\nval_acc=history.history['val_accuracy']\nloss=history.history['loss']\nval_loss=history.history['val_loss']\n\nepochs=range(len(acc)) # Get number of epochs\n\nplt.plot(epochs, acc, 'r', \"Training Accuracy\")\nplt.plot(epochs, val_acc, 'b', \"Validation Accuracy\")\nplt.title('Training and validation accuracy')\nplt.figure()\n\nplt.plot(epochs, loss, 'r', \"Training Loss\")\nplt.plot(epochs, val_loss, 'b', \"Validation Loss\")\n\n\nplt.title('Training and validation loss')","metadata":{"execution":{"iopub.status.busy":"2021-07-21T21:16:42.035815Z","iopub.execute_input":"2021-07-21T21:16:42.036144Z","iopub.status.idle":"2021-07-21T21:16:42.289325Z","shell.execute_reply.started":"2021-07-21T21:16:42.036113Z","shell.execute_reply":"2021-07-21T21:16:42.288448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Ensembling 5 Trained Deep NN","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras.applications.inception_v3 import preprocess_input as inceptpp\nfrom tensorflow.keras.applications.vgg19 import VGG19\nfrom tensorflow.keras.applications.vgg19 import preprocess_input as vggpp\nfrom tensorflow.keras.applications.resnet import ResNet101\nfrom tensorflow.keras.applications.resnet import preprocess_input as respp\nfrom tensorflow.keras.applications.xception import Xception\nfrom tensorflow.keras.applications.xception import preprocess_input as xceptpp\nfrom tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2\nfrom tensorflow.keras.applications.inception_resnet_v2 import preprocess_input as inceptrespp","metadata":{"execution":{"iopub.status.busy":"2021-07-21T21:16:42.291423Z","iopub.execute_input":"2021-07-21T21:16:42.291838Z","iopub.status.idle":"2021-07-21T21:16:42.298925Z","shell.execute_reply.started":"2021-07-21T21:16:42.291795Z","shell.execute_reply":"2021-07-21T21:16:42.297753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pretrained_model_prediction(MODEL,Pre,data):\n  dnn_model = MODEL(\n      input_shape=(300,300,3),\n      include_top = False,\n      weights = 'imagenet'\n  )\n  for layer in dnn_model.layers:\n    layer.trainable = False\n  x = data\n  x = tf.keras.layers.Lambda(Pre)(x)\n  x = dnn_model(x)\n  x = tf.keras.layers.GlobalAveragePooling2D()(x)\n  return x","metadata":{"execution":{"iopub.status.busy":"2021-07-21T21:16:42.300902Z","iopub.execute_input":"2021-07-21T21:16:42.301324Z","iopub.status.idle":"2021-07-21T21:16:42.310173Z","shell.execute_reply.started":"2021-07-21T21:16:42.301283Z","shell.execute_reply":"2021-07-21T21:16:42.309398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = tf.keras.layers.Input((300,300,3))\ninception_prediction = pretrained_model_prediction(InceptionV3,inceptpp,inputs)\nvgg_prediction = pretrained_model_prediction(VGG19,vggpp,inputs)\nxception_prediction = pretrained_model_prediction(Xception,xceptpp,inputs)\nresnet_prediction = pretrained_model_prediction(ResNet101,respp,inputs)\ninceptionres_prediction = pretrained_model_prediction(InceptionResNetV2,inceptrespp,inputs)\nnew_features = tf.keras.layers.Concatenate()([inception_prediction,vgg_prediction,xception_prediction,resnet_prediction,inceptionres_prediction])\nconcatenated_pretrained_model = tf.keras.Model(inputs,new_features)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-21T21:16:42.311604Z","iopub.execute_input":"2021-07-21T21:16:42.312069Z","iopub.status.idle":"2021-07-21T21:17:00.286249Z","shell.execute_reply.started":"2021-07-21T21:16:42.312025Z","shell.execute_reply":"2021-07-21T21:17:00.285411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"concatenated_pretrained_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-21T21:17:00.287432Z","iopub.execute_input":"2021-07-21T21:17:00.287792Z","iopub.status.idle":"2021-07-21T21:17:00.377654Z","shell.execute_reply.started":"2021-07-21T21:17:00.287755Z","shell.execute_reply":"2021-07-21T21:17:00.376892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(concatenated_pretrained_model,show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T21:17:00.379631Z","iopub.execute_input":"2021-07-21T21:17:00.380011Z","iopub.status.idle":"2021-07-21T21:17:00.644511Z","shell.execute_reply.started":"2021-07-21T21:17:00.379961Z","shell.execute_reply":"2021-07-21T21:17:00.643626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n    concatenated_pretrained_model,\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(120,activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2021-07-21T21:17:00.647612Z","iopub.execute_input":"2021-07-21T21:17:00.647895Z","iopub.status.idle":"2021-07-21T21:17:04.144637Z","shell.execute_reply.started":"2021-07-21T21:17:00.647865Z","shell.execute_reply":"2021-07-21T21:17:04.143797Z"},"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-21T21:17:04.387902Z","iopub.execute_input":"2021-07-21T21:17:04.388183Z","iopub.status.idle":"2021-07-21T21:17:04.430127Z","shell.execute_reply.started":"2021-07-21T21:17:04.388156Z","shell.execute_reply":"2021-07-21T21:17:04.429361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-21T21:17:04.146027Z","iopub.execute_input":"2021-07-21T21:17:04.146336Z","iopub.status.idle":"2021-07-21T21:17:04.232626Z","shell.execute_reply.started":"2021-07-21T21:17:04.146302Z","shell.execute_reply":"2021-07-21T21:17:04.231776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(model,show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T21:17:04.233934Z","iopub.execute_input":"2021-07-21T21:17:04.234417Z","iopub.status.idle":"2021-07-21T21:17:04.386439Z","shell.execute_reply.started":"2021-07-21T21:17:04.234379Z","shell.execute_reply":"2021-07-21T21:17:04.385548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    epochs = 10,\n    steps_per_epoch = 7213//64,\n    validation_data = val_generator,\n    validation_steps = 3009//64\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-21T21:17:04.431426Z","iopub.execute_input":"2021-07-21T21:17:04.431792Z","iopub.status.idle":"2021-07-21T22:13:23.201017Z","shell.execute_reply.started":"2021-07-21T21:17:04.431756Z","shell.execute_reply":"2021-07-21T22:13:23.200171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc=history.history['accuracy']\nval_acc=history.history['val_accuracy']\nloss=history.history['loss']\nval_loss=history.history['val_loss']\n\nepochs=range(len(acc)) # Get number of epochs\n\nplt.plot(epochs, acc, 'r', \"Training Accuracy\")\nplt.plot(epochs, val_acc, 'b', \"Validation Accuracy\")\nplt.title('Training and validation accuracy')\nplt.figure()\n\nplt.plot(epochs, loss, 'r', \"Training Loss\")\nplt.plot(epochs, val_loss, 'b', \"Validation Loss\")\n\n\nplt.title('Training and validation loss')","metadata":{"execution":{"iopub.status.busy":"2021-07-21T22:13:23.202635Z","iopub.execute_input":"2021-07-21T22:13:23.202989Z","iopub.status.idle":"2021-07-21T22:13:23.416103Z","shell.execute_reply.started":"2021-07-21T22:13:23.202953Z","shell.execute_reply":"2021-07-21T22:13:23.415122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Ensembling 9 Models","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras.applications.inception_v3 import preprocess_input as inceptpp\nfrom tensorflow.keras.applications.vgg19 import VGG19\nfrom tensorflow.keras.applications.vgg19 import preprocess_input as vggpp\nfrom tensorflow.keras.applications.resnet import ResNet101\nfrom tensorflow.keras.applications.resnet import preprocess_input as respp\nfrom tensorflow.keras.applications.xception import Xception\nfrom tensorflow.keras.applications.xception import preprocess_input as xceptpp\nfrom tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2\nfrom tensorflow.keras.applications.inception_resnet_v2 import preprocess_input as inceptrespp\nfrom tensorflow.keras.applications.nasnet import NASNetLarge\nfrom tensorflow.keras.applications.nasnet import preprocess_input as naspp\nfrom tensorflow.keras.applications.densenet import DenseNet201\nfrom tensorflow.keras.applications.densenet import preprocess_input as densepp\nfrom tensorflow.keras.applications.efficientnet import EfficientNetB7\nfrom tensorflow.keras.applications.efficientnet import preprocess_input as effpp\nfrom tensorflow.keras.applications.mobilenet_v2 import MobileNetV2\nfrom tensorflow.keras.applications.mobilenet_v2 import preprocess_input as mobpp","metadata":{"execution":{"iopub.status.busy":"2021-07-22T07:39:31.018823Z","iopub.execute_input":"2021-07-22T07:39:31.019168Z","iopub.status.idle":"2021-07-22T07:39:31.030212Z","shell.execute_reply.started":"2021-07-22T07:39:31.019136Z","shell.execute_reply":"2021-07-22T07:39:31.028453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pretrained_model_prediction(MODEL,Pre,data):\n  dnn_model = MODEL(\n      input_shape=(331,331,3),\n      include_top = False,\n      weights = 'imagenet'\n  )\n  for layer in dnn_model.layers:\n    layer.trainable = False\n  x = data\n  x = tf.keras.layers.Lambda(Pre)(x)\n  x = dnn_model(x)\n  x = tf.keras.layers.GlobalAveragePooling2D()(x)\n  return x","metadata":{"execution":{"iopub.status.busy":"2021-07-22T07:39:40.50827Z","iopub.execute_input":"2021-07-22T07:39:40.508686Z","iopub.status.idle":"2021-07-22T07:39:40.516012Z","shell.execute_reply.started":"2021-07-22T07:39:40.508647Z","shell.execute_reply":"2021-07-22T07:39:40.514291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = tf.keras.layers.Input((331,331,3))\ninception_prediction = pretrained_model_prediction(InceptionV3,inceptpp,inputs)\nvgg_prediction = pretrained_model_prediction(VGG19,vggpp,inputs)\nxception_prediction = pretrained_model_prediction(Xception,xceptpp,inputs)\nresnet_prediction = pretrained_model_prediction(ResNet101,respp,inputs)\ninceptionres_prediction = pretrained_model_prediction(InceptionResNetV2,inceptrespp,inputs)\nnasnet_prediction = pretrained_model_prediction(NASNetLarge,naspp,inputs)\ndensenet_prediction = pretrained_model_prediction(DenseNet201,densepp,inputs)\nefficient_prediction = pretrained_model_prediction(EfficientNetB7,effpp,inputs)\nmobile_prediction = pretrained_model_prediction(MobileNetV2,mobpp,inputs)\nnew_features = tf.keras.layers.Concatenate()([inception_prediction,vgg_prediction,xception_prediction,resnet_prediction,inceptionres_prediction,\n                                              nasnet_prediction,densenet_prediction,efficient_prediction,mobile_prediction])\nconcatenated_pretrained_model = tf.keras.Model(inputs,new_features)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-22T07:39:47.233327Z","iopub.execute_input":"2021-07-22T07:39:47.233703Z","iopub.status.idle":"2021-07-22T07:41:45.267156Z","shell.execute_reply.started":"2021-07-22T07:39:47.233675Z","shell.execute_reply":"2021-07-22T07:41:45.266065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"concatenated_pretrained_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-22T07:41:57.173005Z","iopub.execute_input":"2021-07-22T07:41:57.173397Z","iopub.status.idle":"2021-07-22T07:41:57.456868Z","shell.execute_reply.started":"2021-07-22T07:41:57.173346Z","shell.execute_reply":"2021-07-22T07:41:57.455775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(concatenated_pretrained_model,show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T07:42:11.298385Z","iopub.execute_input":"2021-07-22T07:42:11.298798Z","iopub.status.idle":"2021-07-22T07:42:12.075325Z","shell.execute_reply.started":"2021-07-22T07:42:11.298756Z","shell.execute_reply":"2021-07-22T07:42:12.074141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n                         concatenated_pretrained_model,\n                         tf.keras.layers.Dense(120,activation='softmax')    \n])","metadata":{"execution":{"iopub.status.busy":"2021-07-22T07:42:23.019629Z","iopub.execute_input":"2021-07-22T07:42:23.020045Z","iopub.status.idle":"2021-07-22T07:42:34.841387Z","shell.execute_reply.started":"2021-07-22T07:42:23.02001Z","shell.execute_reply":"2021-07-22T07:42:34.840231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-22T07:42:45.921809Z","iopub.execute_input":"2021-07-22T07:42:45.922404Z","iopub.status.idle":"2021-07-22T07:42:46.642754Z","shell.execute_reply.started":"2021-07-22T07:42:45.922358Z","shell.execute_reply":"2021-07-22T07:42:46.637035Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(model,show_shapes = True)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T07:42:55.743662Z","iopub.execute_input":"2021-07-22T07:42:55.744018Z","iopub.status.idle":"2021-07-22T07:42:55.898495Z","shell.execute_reply.started":"2021-07-22T07:42:55.743987Z","shell.execute_reply":"2021-07-22T07:42:55.897291Z"},"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-22T07:43:04.508402Z","iopub.execute_input":"2021-07-22T07:43:04.508786Z","iopub.status.idle":"2021-07-22T07:43:04.605042Z","shell.execute_reply.started":"2021-07-22T07:43:04.508752Z","shell.execute_reply":"2021-07-22T07:43:04.604037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    epochs = 10,\n    steps_per_epoch = 7213//64,\n    validation_data = val_generator,\n    validation_steps = 3009//64\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T07:44:07.798611Z","iopub.execute_input":"2021-07-22T07:44:07.799051Z","iopub.status.idle":"2021-07-22T09:27:59.80578Z","shell.execute_reply.started":"2021-07-22T07:44:07.799019Z","shell.execute_reply":"2021-07-22T09:27:59.804756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Adding More Dataset for Training","metadata":{}},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        if os.stat(os.path.join(dirname, filename)).st_size == 0:\n            print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:23:08.440684Z","iopub.execute_input":"2021-07-22T12:23:08.4411Z","iopub.status.idle":"2021-07-22T12:23:26.183518Z","shell.execute_reply.started":"2021-07-22T12:23:08.441066Z","shell.execute_reply":"2021-07-22T12:23:26.182645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fldr = os.listdir('../input/stanford-dogs-dataset/images/Images')","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:23:28.97065Z","iopub.execute_input":"2021-07-22T12:23:28.971028Z","iopub.status.idle":"2021-07-22T12:23:28.975982Z","shell.execute_reply.started":"2021-07-22T12:23:28.970996Z","shell.execute_reply":"2021-07-22T12:23:28.974849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fldr[0][10:]","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:23:31.635093Z","iopub.execute_input":"2021-07-22T12:23:31.635418Z","iopub.status.idle":"2021-07-22T12:23:31.641144Z","shell.execute_reply.started":"2021-07-22T12:23:31.635389Z","shell.execute_reply":"2021-07-22T12:23:31.640226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/dog-breed-identification/labels.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:23:45.805439Z","iopub.execute_input":"2021-07-22T12:23:45.805791Z","iopub.status.idle":"2021-07-22T12:23:45.824583Z","shell.execute_reply.started":"2021-07-22T12:23:45.805724Z","shell.execute_reply":"2021-07-22T12:23:45.823824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"breeds = df.groupby('breed')","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:23:48.530156Z","iopub.execute_input":"2021-07-22T12:23:48.530464Z","iopub.status.idle":"2021-07-22T12:23:48.535179Z","shell.execute_reply.started":"2021-07-22T12:23:48.530437Z","shell.execute_reply":"2021-07-22T12:23:48.534187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree('./dogbreeds')","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:24:43.610716Z","iopub.execute_input":"2021-07-22T12:24:43.611075Z","iopub.status.idle":"2021-07-22T12:24:43.922097Z","shell.execute_reply.started":"2021-07-22T12:24:43.611044Z","shell.execute_reply":"2021-07-22T12:24:43.921208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('dogbreeds')\nos.mkdir('dogbreeds/everything')","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:24:52.020237Z","iopub.execute_input":"2021-07-22T12:24:52.020539Z","iopub.status.idle":"2021-07-22T12:24:52.024605Z","shell.execute_reply.started":"2021-07-22T12:24:52.020512Z","shell.execute_reply":"2021-07-22T12:24:52.023822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for breed,group in breeds:\n    files = list(group['id'])\n    ln = len(files)\n    train_p = 'dogbreeds/everything/'+breed\n    os.mkdir(train_p)\n    for ind,fn in enumerate(files):\n        fn_path = '../input/dog-breed-identification/train/' + fn + '.jpg'\n        tar_path = train_p +'/'+ fn + '.jpg'\n        copyfile(fn_path,tar_path)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:24:57.635664Z","iopub.execute_input":"2021-07-22T12:24:57.636013Z","iopub.status.idle":"2021-07-22T12:25:03.41724Z","shell.execute_reply.started":"2021-07-22T12:24:57.635982Z","shell.execute_reply":"2021-07-22T12:25:03.416341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('./dogbreeds/train')","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:25:04.8108Z","iopub.execute_input":"2021-07-22T12:25:04.811293Z","iopub.status.idle":"2021-07-22T12:25:04.819796Z","shell.execute_reply.started":"2021-07-22T12:25:04.811232Z","shell.execute_reply":"2021-07-22T12:25:04.818967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for bre in fldr:\n    newnm = bre[10:].lower()\n    newfldr = os.path.join('./dogbreeds/train',newnm)\n    os.mkdir(newfldr)\n    oldpth = os.path.join('../input/stanford-dogs-dataset/images/Images',bre)\n    for f in os.listdir(oldpth):\n        filepth = os.path.join(oldpth,f)\n        newfilepth = os.path.join(newfldr,f)\n        copyfile(filepth,newfilepth)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:25:07.760307Z","iopub.execute_input":"2021-07-22T12:25:07.760653Z","iopub.status.idle":"2021-07-22T12:26:39.115056Z","shell.execute_reply.started":"2021-07-22T12:25:07.760621Z","shell.execute_reply":"2021-07-22T12:26:39.114175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_breeds = os.listdir('./dogbreeds/train')\nlen_dir = []\nfor dog_dir in all_breeds:\n    tmp = os.path.join('./dogbreeds/train',dog_dir)\n    len_dir.append(len(os.listdir(tmp)))","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:26:39.116527Z","iopub.execute_input":"2021-07-22T12:26:39.116869Z","iopub.status.idle":"2021-07-22T12:26:39.135678Z","shell.execute_reply.started":"2021-07-22T12:26:39.116834Z","shell.execute_reply":"2021-07-22T12:26:39.13497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len_dir = np.array(len_dir)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:26:39.137745Z","iopub.execute_input":"2021-07-22T12:26:39.138238Z","iopub.status.idle":"2021-07-22T12:26:39.143075Z","shell.execute_reply.started":"2021-07-22T12:26:39.1382Z","shell.execute_reply":"2021-07-22T12:26:39.142115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_at = np.argmax(len_dir)\nprint(\"max number of files is for : \",all_breeds[max_at],len_dir[max_at])","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:26:39.144596Z","iopub.execute_input":"2021-07-22T12:26:39.14528Z","iopub.status.idle":"2021-07-22T12:26:39.152549Z","shell.execute_reply.started":"2021-07-22T12:26:39.145224Z","shell.execute_reply":"2021-07-22T12:26:39.151486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"min_at = np.argmin(len_dir)\nprint(\"max number of files is for : \",all_breeds[min_at],len_dir[min_at])","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:28:19.471233Z","iopub.execute_input":"2021-07-22T12:28:19.471568Z","iopub.status.idle":"2021-07-22T12:28:19.476443Z","shell.execute_reply.started":"2021-07-22T12:28:19.471536Z","shell.execute_reply":"2021-07-22T12:28:19.475585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Previous data we had for :\",all_breeds[min_at],\"is\",len(os.listdir(os.path.join('./dogbreeds/everything',all_breeds[min_at]))))","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:28:23.25497Z","iopub.execute_input":"2021-07-22T12:28:23.255294Z","iopub.status.idle":"2021-07-22T12:28:23.264598Z","shell.execute_reply.started":"2021-07-22T12:28:23.255265Z","shell.execute_reply":"2021-07-22T12:28:23.263815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_breeds = os.listdir('./dogbreeds/everything')\nlen_dir = []\nfor dog_dir in all_breeds:\n    tmp = os.path.join('./dogbreeds/everything',dog_dir)\n    len_dir.append(len(os.listdir(tmp)))","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:28:46.63024Z","iopub.execute_input":"2021-07-22T12:28:46.630572Z","iopub.status.idle":"2021-07-22T12:28:46.644489Z","shell.execute_reply.started":"2021-07-22T12:28:46.630541Z","shell.execute_reply":"2021-07-22T12:28:46.643584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len_dir = np.array(len_dir)\nmin_at = np.argmin(len_dir)\nprint(\"max number of files is for : \",all_breeds[min_at],len_dir[min_at])","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:28:51.890225Z","iopub.execute_input":"2021-07-22T12:28:51.890538Z","iopub.status.idle":"2021-07-22T12:28:51.895946Z","shell.execute_reply.started":"2021-07-22T12:28:51.890509Z","shell.execute_reply":"2021-07-22T12:28:51.894986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Previous data we had for :\",all_breeds[min_at],\"is\",len(os.listdir(os.path.join('./dogbreeds/train',all_breeds[min_at]))))","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:28:59.350154Z","iopub.execute_input":"2021-07-22T12:28:59.350465Z","iopub.status.idle":"2021-07-22T12:28:59.356612Z","shell.execute_reply.started":"2021-07-22T12:28:59.350437Z","shell.execute_reply":"2021-07-22T12:28:59.355507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_train_size = 210","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:29:05.391323Z","iopub.execute_input":"2021-07-22T12:29:05.391671Z","iopub.status.idle":"2021-07-22T12:29:05.395574Z","shell.execute_reply.started":"2021-07-22T12:29:05.39164Z","shell.execute_reply":"2021-07-22T12:29:05.394445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('./dogbreeds/validation')","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:29:07.770264Z","iopub.execute_input":"2021-07-22T12:29:07.770606Z","iopub.status.idle":"2021-07-22T12:29:07.775945Z","shell.execute_reply.started":"2021-07-22T12:29:07.770575Z","shell.execute_reply":"2021-07-22T12:29:07.77479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for breed in os.listdir('./dogbreeds/everything'):\n    dog_train = os.path.join('./dogbreeds/train',breed)\n    dog_val = os.path.join('./dogbreeds/validation',breed)\n    os.mkdir(dog_val)\n    dog_every = os.path.join('./dogbreeds/everything',breed)\n    initial_length = len(os.listdir(dog_train))\n    for dogs in os.listdir(dog_every):\n        f_path = os.path.join(dog_every,dogs)\n        if initial_length<target_train_size:\n            dest_path = os.path.join(dog_train,dogs)\n            copyfile(f_path,dest_path)\n        else:\n            dest_path = os.path.join(dog_val,dogs)\n            copyfile(f_path,dest_path)\n        initial_length +=1","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:29:13.730925Z","iopub.execute_input":"2021-07-22T12:29:13.731238Z","iopub.status.idle":"2021-07-22T12:29:14.893371Z","shell.execute_reply.started":"2021-07-22T12:29:13.73121Z","shell.execute_reply":"2021-07-22T12:29:14.89246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_breeds = os.listdir('./dogbreeds/train')\nlen_dir = []\nfor dog_dir in all_breeds:\n    tmp = os.path.join('./dogbreeds/train',dog_dir)\n    len_dir.append(len(os.listdir(tmp)))","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:29:20.715886Z","iopub.execute_input":"2021-07-22T12:29:20.716201Z","iopub.status.idle":"2021-07-22T12:29:20.739877Z","shell.execute_reply.started":"2021-07-22T12:29:20.716173Z","shell.execute_reply":"2021-07-22T12:29:20.739162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(max(len_dir))","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:29:26.99012Z","iopub.execute_input":"2021-07-22T12:29:26.990472Z","iopub.status.idle":"2021-07-22T12:29:26.994923Z","shell.execute_reply.started":"2021-07-22T12:29:26.990437Z","shell.execute_reply":"2021-07-22T12:29:26.994059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(sum(len_dir))","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:29:29.430102Z","iopub.execute_input":"2021-07-22T12:29:29.430424Z","iopub.status.idle":"2021-07-22T12:29:29.434573Z","shell.execute_reply.started":"2021-07-22T12:29:29.430394Z","shell.execute_reply":"2021-07-22T12:29:29.433787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_breed = {}\nfor i in len_dir:\n    train_breed.setdefault(i,0)\n    train_breed[i]+=1\nprint(train_breed)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:29:36.270954Z","iopub.execute_input":"2021-07-22T12:29:36.271267Z","iopub.status.idle":"2021-07-22T12:29:36.276446Z","shell.execute_reply.started":"2021-07-22T12:29:36.271239Z","shell.execute_reply":"2021-07-22T12:29:36.275436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_breeds = os.listdir('./dogbreeds/validation')\nlen_dir = []\nfor dog_dir in all_breeds:\n    tmp = os.path.join('./dogbreeds/validation',dog_dir)\n    len_dir.append(len(os.listdir(tmp)))","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:29:42.481004Z","iopub.execute_input":"2021-07-22T12:29:42.481327Z","iopub.status.idle":"2021-07-22T12:29:42.492544Z","shell.execute_reply.started":"2021-07-22T12:29:42.481298Z","shell.execute_reply":"2021-07-22T12:29:42.491671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(max(len_dir))","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:29:45.190205Z","iopub.execute_input":"2021-07-22T12:29:45.190532Z","iopub.status.idle":"2021-07-22T12:29:45.194982Z","shell.execute_reply.started":"2021-07-22T12:29:45.190503Z","shell.execute_reply":"2021-07-22T12:29:45.19406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(sum(len_dir))","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:29:51.171098Z","iopub.execute_input":"2021-07-22T12:29:51.171431Z","iopub.status.idle":"2021-07-22T12:29:51.176925Z","shell.execute_reply.started":"2021-07-22T12:29:51.171402Z","shell.execute_reply":"2021-07-22T12:29:51.175324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Datagenerator","metadata":{}},{"cell_type":"code","source":"train_dir = './dogbreeds/train'\n\ntrain_datagen = ImageDataGenerator()\n\ntrain_generator = train_datagen.flow_from_directory(\n    train_dir,\n    target_size = (331,331),\n    batch_size = 64,\n    class_mode = 'categorical',\n    shuffle = True\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:31:14.254355Z","iopub.execute_input":"2021-07-22T12:31:14.254684Z","iopub.status.idle":"2021-07-22T12:31:15.638767Z","shell.execute_reply.started":"2021-07-22T12:31:14.254654Z","shell.execute_reply":"2021-07-22T12:31:15.63796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_dir = './dogbreeds/validation'\n\nval_datagen = ImageDataGenerator()\n\nval_generator = val_datagen.flow_from_directory(\n    val_dir,\n    target_size = (331,331),\n    batch_size = 64,\n    class_mode = 'categorical'\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:31:54.577699Z","iopub.execute_input":"2021-07-22T12:31:54.578052Z","iopub.status.idle":"2021-07-22T12:31:55.002877Z","shell.execute_reply.started":"2021-07-22T12:31:54.578023Z","shell.execute_reply":"2021-07-22T12:31:55.002077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Using our 9 Ensembled Model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications.inception_v3 import InceptionV3\nfrom tensorflow.keras.applications.inception_v3 import preprocess_input as inceptpp\nfrom tensorflow.keras.applications.vgg19 import VGG19\nfrom tensorflow.keras.applications.vgg19 import preprocess_input as vggpp\nfrom tensorflow.keras.applications.resnet import ResNet101\nfrom tensorflow.keras.applications.resnet import preprocess_input as respp\nfrom tensorflow.keras.applications.xception import Xception\nfrom tensorflow.keras.applications.xception import preprocess_input as xceptpp\nfrom tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2\nfrom tensorflow.keras.applications.inception_resnet_v2 import preprocess_input as inceptrespp\nfrom tensorflow.keras.applications.nasnet import NASNetLarge\nfrom tensorflow.keras.applications.nasnet import preprocess_input as naspp\nfrom tensorflow.keras.applications.densenet import DenseNet201\nfrom tensorflow.keras.applications.densenet import preprocess_input as densepp\nfrom tensorflow.keras.applications.efficientnet import EfficientNetB7\nfrom tensorflow.keras.applications.efficientnet import preprocess_input as effpp\nfrom tensorflow.keras.applications.mobilenet_v2 import MobileNetV2\nfrom tensorflow.keras.applications.mobilenet_v2 import preprocess_input as mobpp","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:32:49.689039Z","iopub.execute_input":"2021-07-22T12:32:49.689377Z","iopub.status.idle":"2021-07-22T12:32:49.699294Z","shell.execute_reply.started":"2021-07-22T12:32:49.689349Z","shell.execute_reply":"2021-07-22T12:32:49.698529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pretrained_model_prediction(MODEL,Pre,data):\n  dnn_model = MODEL(\n      input_shape=(331,331,3),\n      include_top = False,\n      weights = 'imagenet'\n  )\n  for layer in dnn_model.layers:\n    layer.trainable = False\n  x = data\n  x = tf.keras.layers.Lambda(Pre)(x)\n  x = dnn_model(x)\n  x = tf.keras.layers.GlobalAveragePooling2D()(x)\n  return x","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:33:12.723582Z","iopub.execute_input":"2021-07-22T12:33:12.723966Z","iopub.status.idle":"2021-07-22T12:33:12.731277Z","shell.execute_reply.started":"2021-07-22T12:33:12.723933Z","shell.execute_reply":"2021-07-22T12:33:12.728859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = tf.keras.layers.Input((331,331,3))\ninception_prediction = pretrained_model_prediction(InceptionV3,inceptpp,inputs)\nvgg_prediction = pretrained_model_prediction(VGG19,vggpp,inputs)\nxception_prediction = pretrained_model_prediction(Xception,xceptpp,inputs)\nresnet_prediction = pretrained_model_prediction(ResNet101,respp,inputs)\ninceptionres_prediction = pretrained_model_prediction(InceptionResNetV2,inceptrespp,inputs)\nnasnet_prediction = pretrained_model_prediction(NASNetLarge,naspp,inputs)\ndensenet_prediction = pretrained_model_prediction(DenseNet201,densepp,inputs)\nefficient_prediction = pretrained_model_prediction(EfficientNetB7,effpp,inputs)\nmobile_prediction = pretrained_model_prediction(MobileNetV2,mobpp,inputs)\nnew_features = tf.keras.layers.Concatenate()([inception_prediction,vgg_prediction,xception_prediction,resnet_prediction,inceptionres_prediction,\n                                              nasnet_prediction,densenet_prediction,efficient_prediction,mobile_prediction])\nconcatenated_pretrained_model = tf.keras.Model(inputs,new_features)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:33:38.552633Z","iopub.execute_input":"2021-07-22T12:33:38.55299Z","iopub.status.idle":"2021-07-22T12:34:30.218152Z","shell.execute_reply.started":"2021-07-22T12:33:38.55296Z","shell.execute_reply":"2021-07-22T12:34:30.217326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"concatenated_pretrained_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:34:30.219505Z","iopub.execute_input":"2021-07-22T12:34:30.219856Z","iopub.status.idle":"2021-07-22T12:34:30.437046Z","shell.execute_reply.started":"2021-07-22T12:34:30.219821Z","shell.execute_reply":"2021-07-22T12:34:30.43624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(concatenated_pretrained_model,show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:34:50.67766Z","iopub.execute_input":"2021-07-22T12:34:50.678038Z","iopub.status.idle":"2021-07-22T12:34:51.362862Z","shell.execute_reply.started":"2021-07-22T12:34:50.678006Z","shell.execute_reply":"2021-07-22T12:34:51.361972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n                         concatenated_pretrained_model,\n                         tf.keras.layers.Dense(120,activation='softmax')    \n])","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:36:09.874614Z","iopub.execute_input":"2021-07-22T12:36:09.875018Z","iopub.status.idle":"2021-07-22T12:36:19.808452Z","shell.execute_reply.started":"2021-07-22T12:36:09.874984Z","shell.execute_reply":"2021-07-22T12:36:19.80763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:36:24.042774Z","iopub.execute_input":"2021-07-22T12:36:24.043137Z","iopub.status.idle":"2021-07-22T12:36:24.272763Z","shell.execute_reply.started":"2021-07-22T12:36:24.043104Z","shell.execute_reply":"2021-07-22T12:36:24.27117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(model,show_shapes = True)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:36:54.17431Z","iopub.execute_input":"2021-07-22T12:36:54.174687Z","iopub.status.idle":"2021-07-22T12:36:54.318724Z","shell.execute_reply.started":"2021-07-22T12:36:54.174651Z","shell.execute_reply":"2021-07-22T12:36:54.317692Z"},"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-22T12:36:59.899051Z","iopub.execute_input":"2021-07-22T12:36:59.899401Z","iopub.status.idle":"2021-07-22T12:37:00.035189Z","shell.execute_reply.started":"2021-07-22T12:36:59.899365Z","shell.execute_reply":"2021-07-22T12:37:00.034153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    epochs = 10,\n    steps_per_epoch = 25333//64,\n    validation_data = val_generator,\n    validation_steps = 5469//64\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T12:43:55.349422Z","iopub.execute_input":"2021-07-22T12:43:55.349765Z","iopub.status.idle":"2021-07-22T17:52:18.857243Z","shell.execute_reply.started":"2021-07-22T12:43:55.349721Z","shell.execute_reply":"2021-07-22T17:52:18.856264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator.class_indices","metadata":{"execution":{"iopub.status.busy":"2021-07-22T17:53:44.723062Z","iopub.execute_input":"2021-07-22T17:53:44.723541Z","iopub.status.idle":"2021-07-22T17:53:44.746512Z","shell.execute_reply.started":"2021-07-22T17:53:44.723493Z","shell.execute_reply":"2021-07-22T17:53:44.745637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"It is in sorted manner","metadata":{}},{"cell_type":"code","source":"cols = ['id']\ntmp2 = train_generator.class_indices.keys()","metadata":{"execution":{"iopub.status.busy":"2021-07-22T17:55:39.63746Z","iopub.execute_input":"2021-07-22T17:55:39.637798Z","iopub.status.idle":"2021-07-22T17:55:39.643244Z","shell.execute_reply.started":"2021-07-22T17:55:39.637745Z","shell.execute_reply":"2021-07-22T17:55:39.641625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols.extend(tmp2)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T17:56:01.37179Z","iopub.execute_input":"2021-07-22T17:56:01.372152Z","iopub.status.idle":"2021-07-22T17:56:01.376981Z","shell.execute_reply.started":"2021-07-22T17:56:01.372117Z","shell.execute_reply":"2021-07-22T17:56:01.375976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols","metadata":{"execution":{"iopub.status.busy":"2021-07-22T17:56:09.287899Z","iopub.execute_input":"2021-07-22T17:56:09.288266Z","iopub.status.idle":"2021-07-22T17:56:09.29813Z","shell.execute_reply.started":"2021-07-22T17:56:09.288227Z","shell.execute_reply":"2021-07-22T17:56:09.297052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = '../input/dog-breed-identification/test'","metadata":{"execution":{"iopub.status.busy":"2021-07-22T17:56:38.198298Z","iopub.execute_input":"2021-07-22T17:56:38.198635Z","iopub.status.idle":"2021-07-22T17:56:38.203127Z","shell.execute_reply.started":"2021-07-22T17:56:38.198604Z","shell.execute_reply":"2021-07-22T17:56:38.201719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tqdm as tq","metadata":{"execution":{"iopub.status.busy":"2021-07-22T18:30:47.084799Z","iopub.execute_input":"2021-07-22T18:30:47.085119Z","iopub.status.idle":"2021-07-22T18:30:47.088545Z","shell.execute_reply.started":"2021-07-22T18:30:47.08509Z","shell.execute_reply":"2021-07-22T18:30:47.087721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing import image\nall_labels = []\nfor fn in tq.tqdm(os.listdir(test_path)):\n    file_path = os.path.join(test_path,fn)\n    tmp = []\n    tmp.append(fn)\n    img = image.load_img(file_path, target_size=(300,300))\n    x = image.img_to_array(img)\n    x = np.expand_dims(x, axis=0)\n    classes = model.predict(x)\n    classes = list(classes)\n    tmp.extend(classes)\n    all_labels.append(tmp)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T18:32:02.183183Z","iopub.execute_input":"2021-07-22T18:32:02.183504Z","iopub.status.idle":"2021-07-22T19:11:56.343997Z","shell.execute_reply.started":"2021-07-22T18:32:02.183473Z","shell.execute_reply":"2021-07-22T19:11:56.343091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(all_labels)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T19:12:47.924417Z","iopub.execute_input":"2021-07-22T19:12:47.924762Z","iopub.status.idle":"2021-07-22T19:12:47.93118Z","shell.execute_reply.started":"2021-07-22T19:12:47.924717Z","shell.execute_reply":"2021-07-22T19:12:47.93021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(all_labels[0])","metadata":{"execution":{"iopub.status.busy":"2021-07-22T19:13:08.209496Z","iopub.execute_input":"2021-07-22T19:13:08.209833Z","iopub.status.idle":"2021-07-22T19:13:08.215668Z","shell.execute_reply.started":"2021-07-22T19:13:08.209802Z","shell.execute_reply":"2021-07-22T19:13:08.214714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_labels[0]\n","metadata":{"execution":{"iopub.status.busy":"2021-07-22T19:14:25.484106Z","iopub.execute_input":"2021-07-22T19:14:25.484519Z","iopub.status.idle":"2021-07-22T19:14:25.495078Z","shell.execute_reply.started":"2021-07-22T19:14:25.484481Z","shell.execute_reply":"2021-07-22T19:14:25.493657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-22T19:32:58.197061Z","iopub.execute_input":"2021-07-22T19:32:58.197382Z","iopub.status.idle":"2021-07-22T19:32:58.423578Z","shell.execute_reply.started":"2021-07-22T19:32:58.197353Z","shell.execute_reply":"2021-07-22T19:32:58.422745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    epochs = 5,\n    steps_per_epoch = 25333//64,\n    validation_data = val_generator,\n    validation_steps = 5469//64\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T19:40:51.023990Z","iopub.execute_input":"2021-07-22T19:40:51.024316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_generator,\n    epochs = 1,\n    steps_per_epoch = 25333//64,\n    validation_data = val_generator,\n    validation_steps = 5469//64\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-22T21:30:43.450835Z","iopub.execute_input":"2021-07-22T21:30:43.451189Z","iopub.status.idle":"2021-07-22T21:30:43.508235Z","shell.execute_reply.started":"2021-07-22T21:30:43.451161Z","shell.execute_reply":"2021-07-22T21:30:43.506918Z"},"trusted":true},"execution_count":null,"outputs":[]}]}