{"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":"#Importing required libraries\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nimport os\nimport gc\n\nfrom sklearn.model_selection import train_test_split\n\n\nimport tensorflow as tf\nfrom tqdm.autonotebook import tqdm\n\nimport numpy as np #\nimport pandas as pd \n\nfrom keras import Sequential\nfrom keras.callbacks import EarlyStopping\n\nfrom keras.optimizers import Adam, SGD\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras.layers import Flatten,Dense,BatchNormalization,Activation,Dropout\nfrom keras.layers import Lambda, Input, GlobalAveragePooling2D,BatchNormalization\nfrom keras.utils import to_categorical\n# from keras import regularizers\nfrom tensorflow.keras.models import Model\n\n\n# from keras.preprocessing.image import img_to_array\n# from keras.applications.imagenet_utils import decode_predictions\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\n# import 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 5GB 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":"2023-10-23T13:21:01.308852Z","iopub.execute_input":"2023-10-23T13:21:01.309282Z","iopub.status.idle":"2023-10-23T13:21:01.319771Z","shell.execute_reply.started":"2023-10-23T13:21:01.309252Z","shell.execute_reply":"2023-10-23T13:21:01.31832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#reading labels csv file\n\nlabels = pd.read_csv('/kaggle/input/dog-breed-identification-data/labels.csv')\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-23T13:21:48.949343Z","iopub.execute_input":"2023-10-23T13:21:48.950018Z","iopub.status.idle":"2023-10-23T13:21:48.983598Z","shell.execute_reply.started":"2023-10-23T13:21:48.949931Z","shell.execute_reply":"2023-10-23T13:21:48.982726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.describe()","metadata":{"execution":{"iopub.status.busy":"2023-10-23T13:22:20.551131Z","iopub.execute_input":"2023-10-23T13:22:20.551747Z","iopub.status.idle":"2023-10-23T13:22:20.598731Z","shell.execute_reply.started":"2023-10-23T13:22:20.551704Z","shell.execute_reply":"2023-10-23T13:22:20.59696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#function to show bar length\ndef barw(ax): \n    \n    for p in ax.patches:\n        val = p.get_width() #height of the bar\n        x = p.get_x()+ p.get_width() # x- position \n        y = p.get_y() + p.get_height()/2 #y-position\n        ax.annotate(round(val,2),(x,y))\n        \n#finding top dog brands\n\nplt.figure(figsize = (15,30))\nax0 =sns.countplot(y=labels['breed'],order=labels['breed'].value_counts().index)\nbarw(ax0)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-23T13:22:57.226394Z","iopub.execute_input":"2023-10-23T13:22:57.226934Z","iopub.status.idle":"2023-10-23T13:22:59.671478Z","shell.execute_reply.started":"2023-10-23T13:22:57.226893Z","shell.execute_reply":"2023-10-23T13:22:59.670118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Lets check one image\nfrom IPython.display import display, Image\nImage(\"../input/dog-breed-identification-data/train/43572ba7edf772a95f539e57afd9eb43.jpg\")","metadata":{"execution":{"iopub.status.busy":"2023-10-23T13:23:24.495095Z","iopub.execute_input":"2023-10-23T13:23:24.495608Z","iopub.status.idle":"2023-10-23T13:23:24.511919Z","shell.execute_reply.started":"2023-10-23T13:23:24.495562Z","shell.execute_reply":"2023-10-23T13:23:24.510512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nif len(os.listdir('/kaggle/input/dog-breed-identification-data/train/')) == len(labels['id']):\n    print('Number of file matches number of actual images!')\nelse:\n    print('Number of file doesnot matches number of actual images!!')","metadata":{"execution":{"iopub.status.busy":"2023-10-23T13:23:47.712636Z","iopub.execute_input":"2023-10-23T13:23:47.713151Z","iopub.status.idle":"2023-10-23T13:23:47.728534Z","shell.execute_reply.started":"2023-10-23T13:23:47.713112Z","shell.execute_reply":"2023-10-23T13:23:47.727094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Create list of alphabetically sorted labels.\nclasses = sorted(list(set(labels['breed'])))\nn_classes = len(classes)\nprint('Total unique breed {}'.format(n_classes))\n\n\n\n#Map each label string to an integer label.\nclass_to_num = dict(zip(classes, range(n_classes)))\nclass_to_num","metadata":{"execution":{"iopub.status.busy":"2023-10-23T13:24:30.199096Z","iopub.execute_input":"2023-10-23T13:24:30.199557Z","iopub.status.idle":"2023-10-23T13:24:30.217804Z","shell.execute_reply.started":"2023-10-23T13:24:30.199525Z","shell.execute_reply":"2023-10-23T13:24:30.216303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = (331,331,3)\n\n\ndef images_to_array(directory, label_dataframe, target_size = input_shape):\n    \n    image_labels = label_dataframe['breed']\n    images = np.zeros([len(label_dataframe), target_size[0], target_size[1], target_size[2]],dtype=np.uint8) #as we have huge data and limited ram memory. uint8 takes less memory\n    y = np.zeros([len(label_dataframe),1],dtype = np.uint8)\n    \n    for ix, image_name in enumerate(tqdm(label_dataframe['id'].values)):\n        img_dir = os.path.join(directory, image_name + '.jpg')\n        img = load_img(img_dir, target_size = target_size)\n#         img = np.expand_dims(img, axis=0)\n#         img = processed_image_resnet(img)\n#         img = img/255\n        images[ix]=img\n#         images[ix] = img_to_array(img)\n        del img\n        \n        dog_breed = image_labels[ix]\n        y[ix] = class_to_num[dog_breed]\n    \n    y = to_categorical(y)\n    \n    return images,y","metadata":{"execution":{"iopub.status.busy":"2023-10-23T13:24:55.183152Z","iopub.execute_input":"2023-10-23T13:24:55.183661Z","iopub.status.idle":"2023-10-23T13:24:55.195671Z","shell.execute_reply.started":"2023-10-23T13:24:55.183616Z","shell.execute_reply":"2023-10-23T13:24:55.194058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import time \nt = time.time()\n\nX,y = images_to_array('/kaggle/input/dog-breed-identification-data/train', labels[:])\n\nprint('runtime in seconds: {}'.format(time.time() - t))","metadata":{"execution":{"iopub.status.busy":"2023-10-23T13:25:45.114767Z","iopub.execute_input":"2023-10-23T13:25:45.115201Z","iopub.status.idle":"2023-10-23T13:27:23.44854Z","shell.execute_reply.started":"2023-10-23T13:25:45.115158Z","shell.execute_reply":"2023-10-23T13:27:23.447242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n=25\n\n# setup the figure \nplt.figure(figsize=(20,20))\n\nfor i in range(n):\n#     print(i)\n    ax = plt.subplot(5, 5, i+1)\n    plt.title(classes[np.where(y[i] ==1)[0][0]])\n    plt.imshow(X[i].astype('int32')) # .astype('int32') ---> as imshow() needs integer data to read the image","metadata":{"execution":{"iopub.status.busy":"2023-10-23T13:27:23.450573Z","iopub.execute_input":"2023-10-23T13:27:23.450955Z","iopub.status.idle":"2023-10-23T13:27:30.935005Z","shell.execute_reply.started":"2023-10-23T13:27:23.450922Z","shell.execute_reply":"2023-10-23T13:27:30.933304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Learning Rate Annealer\nlrr= ReduceLROnPlateau(monitor='val_acc', factor=.01, patience=3, min_lr=1e-5,verbose = 1)\n\n#Prepare call backs\nEarlyStop = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-23T13:28:01.73776Z","iopub.execute_input":"2023-10-23T13:28:01.739356Z","iopub.status.idle":"2023-10-23T13:28:01.746316Z","shell.execute_reply.started":"2023-10-23T13:28:01.739301Z","shell.execute_reply":"2023-10-23T13:28:01.744798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hyperparameters\nbatch_size= 128\nepochs=50\nlearn_rate=.001\nsgd=SGD(lr=learn_rate,momentum=.9,nesterov=False)\nadam=Adam(lr=learn_rate, beta_1=0.9, beta_2=0.999, epsilon=None,  amsgrad=False)","metadata":{"execution":{"iopub.status.busy":"2023-10-23T13:28:15.304537Z","iopub.execute_input":"2023-10-23T13:28:15.305105Z","iopub.status.idle":"2023-10-23T13:28:15.327905Z","shell.execute_reply.started":"2023-10-23T13:28:15.305063Z","shell.execute_reply":"2023-10-23T13:28:15.326724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#function to extract features from the dataset by a given pretrained model\nimg_size = (331,331,3)\n\ndef get_features(model_name, model_preprocessor, input_size, data):\n\n    input_layer = Input(input_size)\n    preprocessor = Lambda(model_preprocessor)(input_layer)\n    base_model = model_name(weights='imagenet', include_top=False,\n                            input_shape=input_size)(preprocessor)\n    avg = GlobalAveragePooling2D()(base_model)\n    feature_extractor = Model(inputs = input_layer, outputs = avg)\n    \n    #Extract feature.\n    feature_maps = feature_extractor.predict(data, verbose=1)\n    print('Feature maps shape: ', feature_maps.shape)\n    return feature_maps","metadata":{"execution":{"iopub.status.busy":"2023-10-23T13:28:46.577802Z","iopub.execute_input":"2023-10-23T13:28:46.578332Z","iopub.status.idle":"2023-10-23T13:28:46.591681Z","shell.execute_reply.started":"2023-10-23T13:28:46.578293Z","shell.execute_reply":"2023-10-23T13:28:46.59002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract features using InceptionV3 \nfrom keras.applications.inception_v3 import InceptionV3, preprocess_input\ninception_preprocessor = preprocess_input\ninception_features = get_features(InceptionV3,\n                                  inception_preprocessor,\n                                  img_size, X)","metadata":{"execution":{"iopub.status.busy":"2023-10-23T13:28:56.265273Z","iopub.execute_input":"2023-10-23T13:28:56.266028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract features using Xception \nfrom keras.applications.xception import Xception, preprocess_input\nxception_preprocessor = preprocess_input\nxception_features = get_features(Xception,\n                                 xception_preprocessor,\n                                 img_size, X)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract features using InceptionResNetV2 \nfrom keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input\ninc_resnet_preprocessor = preprocess_input\ninc_resnet_features = get_features(InceptionResNetV2,\n                                   inc_resnet_preprocessor,\n                                   img_size, X)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract features using NASNetLarge \nfrom keras.applications.nasnet import NASNetLarge, preprocess_input\nnasnet_preprocessor = preprocess_input\nnasnet_features = get_features(NASNetLarge,\n                               nasnet_preprocessor,\n                               img_size, X)","metadata":{},"execution_count":null,"outputs":[]}]}