{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n        break\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":"2023-06-20T00:01:29.950414Z","iopub.execute_input":"2023-06-20T00:01:29.950927Z","iopub.status.idle":"2023-06-20T00:01:37.688074Z","shell.execute_reply.started":"2023-06-20T00:01:29.950898Z","shell.execute_reply":"2023-06-20T00:01:37.687058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Importing required libraries\nimport os\nimport gc\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport numpy as np\nimport pandas as pd \n\nfrom sklearn.model_selection import train_test_split\nfrom tqdm.autonotebook import tqdm\n\nimport tensorflow as tf\nfrom keras import Sequential\nfrom keras.callbacks import EarlyStopping\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\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.preprocessing.image import load_img\n","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:01:37.690741Z","iopub.execute_input":"2023-06-20T00:01:37.691729Z","iopub.status.idle":"2023-06-20T00:01:44.174905Z","shell.execute_reply.started":"2023-06-20T00:01:37.691694Z","shell.execute_reply":"2023-06-20T00:01:44.173913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:01:44.176298Z","iopub.execute_input":"2023-06-20T00:01:44.177039Z","iopub.status.idle":"2023-06-20T00:01:44.215994Z","shell.execute_reply.started":"2023-06-20T00:01:44.177011Z","shell.execute_reply":"2023-06-20T00:01:44.214941Z"},"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#Map each label string to an integer label.\nclass_to_num = dict(zip(classes, range(n_classes)))","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:01:44.218794Z","iopub.execute_input":"2023-06-20T00:01:44.219250Z","iopub.status.idle":"2023-06-20T00:01:44.226498Z","shell.execute_reply.started":"2023-06-20T00:01:44.219205Z","shell.execute_reply":"2023-06-20T00:01:44.225516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = (331,331,3)\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        images[ix] = 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-06-20T00:01:44.227981Z","iopub.execute_input":"2023-06-20T00:01:44.228649Z","iopub.status.idle":"2023-06-20T00:01:44.240310Z","shell.execute_reply.started":"2023-06-20T00:01:44.228611Z","shell.execute_reply":"2023-06-20T00:01:44.239316Z"},"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/train', labels[:])\n\nprint('runtime in seconds: {}'.format(time.time() - t))","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:01:44.241889Z","iopub.execute_input":"2023-06-20T00:01:44.242267Z","iopub.status.idle":"2023-06-20T00:02:32.508375Z","shell.execute_reply.started":"2023-06-20T00:01:44.242235Z","shell.execute_reply":"2023-06-20T00:02:32.507093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(\n    X, y, test_size=0.3, random_state=42)\n\nprint(X_train.shape, y_train.shape)\nprint(X_test.shape, y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:02:32.510196Z","iopub.execute_input":"2023-06-20T00:02:32.510566Z","iopub.status.idle":"2023-06-20T00:02:33.505232Z","shell.execute_reply.started":"2023-06-20T00:02:32.510533Z","shell.execute_reply":"2023-06-20T00:02:33.504267Z"},"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-06-20T00:02:33.506658Z","iopub.execute_input":"2023-06-20T00:02:33.507291Z","iopub.status.idle":"2023-06-20T00:02:33.514265Z","shell.execute_reply.started":"2023-06-20T00:02:33.507258Z","shell.execute_reply":"2023-06-20T00:02:33.513304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hyperparameters\nbatch_size= 128\nepochs=20\nlearn_rate=.001\nsgd=SGD(learning_rate=learn_rate, momentum=0.9, nesterov=False)\nadam=Adam(learning_rate=learn_rate, beta_1=0.9, beta_2=0.999, epsilon=None,  amsgrad=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:02:33.515558Z","iopub.execute_input":"2023-06-20T00:02:33.516006Z","iopub.status.idle":"2023-06-20T00:02:33.529961Z","shell.execute_reply.started":"2023-06-20T00:02:33.515974Z","shell.execute_reply":"2023-06-20T00:02:33.528994Z"},"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-06-20T00:02:33.533951Z","iopub.execute_input":"2023-06-20T00:02:33.534198Z","iopub.status.idle":"2023-06-20T00:02:33.541050Z","shell.execute_reply.started":"2023-06-20T00:02:33.534176Z","shell.execute_reply":"2023-06-20T00:02:33.540116Z"},"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_train)\n\nprint('Inception feature maps shape', inception_features.shape)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:02:33.543615Z","iopub.execute_input":"2023-06-20T00:02:33.544801Z","iopub.status.idle":"2023-06-20T00:03:29.274744Z","shell.execute_reply.started":"2023-06-20T00:02:33.544536Z","shell.execute_reply":"2023-06-20T00:03:29.273667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X, X_train #to free up some ram memory\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:03:29.276264Z","iopub.execute_input":"2023-06-20T00:03:29.276618Z","iopub.status.idle":"2023-06-20T00:03:29.882509Z","shell.execute_reply.started":"2023-06-20T00:03:29.276586Z","shell.execute_reply":"2023-06-20T00:03:29.881441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Inception feature maps shape', inception_features.shape)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:03:29.884972Z","iopub.execute_input":"2023-06-20T00:03:29.885330Z","iopub.status.idle":"2023-06-20T00:03:29.891176Z","shell.execute_reply.started":"2023-06-20T00:03:29.885298Z","shell.execute_reply":"2023-06-20T00:03:29.890088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Prepare Deep net\n\nmodel = Sequential()\n# model.add(Dense(1028,input_shape=(final_features.shape[1],)))\nmodel.add(Dropout(0.7,input_shape=(inception_features.shape[1],)))\nmodel.add(Dense(n_classes,activation= 'softmax'))\n\nmodel.compile(optimizer=adam,\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\n#Training the model. \nhistory = model.fit(inception_features, y_train,\n            batch_size=batch_size,\n            epochs=epochs,\n            validation_split=0.2,\n            callbacks=[lrr, EarlyStop])","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:03:29.893413Z","iopub.execute_input":"2023-06-20T00:03:29.893719Z","iopub.status.idle":"2023-06-20T00:03:35.725342Z","shell.execute_reply.started":"2023-06-20T00:03:29.893697Z","shell.execute_reply":"2023-06-20T00:03:35.724332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot result\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(1, len(acc) + 1)\n\n# Accuracy\nplt.plot(epochs, acc, label='Train accuracy')\nplt.plot(epochs, val_acc, label='Val accuracy')\nplt.title('Training & validation accuracy')\nplt.legend()\n\n# Loss\nplt.figure()\nplt.plot(epochs, loss, label='Train loss')\nplt.plot(epochs, val_loss, label='Val loss')\nplt.title('Training & validation loss')\nplt.legend()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:03:35.727396Z","iopub.execute_input":"2023-06-20T00:03:35.727786Z","iopub.status.idle":"2023-06-20T00:03:36.258170Z","shell.execute_reply.started":"2023-06-20T00:03:35.727753Z","shell.execute_reply":"2023-06-20T00:03:36.257297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del inception_features\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:03:36.259545Z","iopub.execute_input":"2023-06-20T00:03:36.260543Z","iopub.status.idle":"2023-06-20T00:03:36.485331Z","shell.execute_reply.started":"2023-06-20T00:03:36.260508Z","shell.execute_reply":"2023-06-20T00:03:36.484023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Function to read images from test directory\n\ndef images_to_array_test(test_path, img_size = (331,331,3)):\n    test_filenames = [test_path + fname for fname in os.listdir(test_path)]\n    data_size = len(test_filenames)\n    images = np.zeros([data_size, img_size[0], img_size[1], 3], dtype=np.uint8)\n        \n    for ix,img_dir in enumerate(tqdm(test_filenames)):\n        img = load_img(img_dir, target_size = img_size)\n        images[ix]=img\n        del img\n    print('Ouptut Data Size: ', images.shape)\n    return images\n\ntest_data = images_to_array_test('/kaggle/input/dog-breed-identification/test/', img_size)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:03:36.486583Z","iopub.execute_input":"2023-06-20T00:03:36.487000Z","iopub.status.idle":"2023-06-20T00:04:26.013623Z","shell.execute_reply.started":"2023-06-20T00:03:36.486967Z","shell.execute_reply":"2023-06-20T00:04:26.012533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Extract test data features.\ndef extact_features(data):\n    inception_features = get_features(InceptionV3, inception_preprocessor, img_size, data)\n    print('Inception feature maps shape', inception_features.shape)\n    return inception_features\n\ntest_features = extact_features(test_data)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:04:26.015597Z","iopub.execute_input":"2023-06-20T00:04:26.016024Z","iopub.status.idle":"2023-06-20T00:05:17.780352Z","shell.execute_reply.started":"2023-06-20T00:04:26.015984Z","shell.execute_reply":"2023-06-20T00:05:17.779228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Free up some space.\ndel test_data\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:05:17.786416Z","iopub.execute_input":"2023-06-20T00:05:17.786764Z","iopub.status.idle":"2023-06-20T00:05:18.480135Z","shell.execute_reply.started":"2023-06-20T00:05:17.786734Z","shell.execute_reply":"2023-06-20T00:05:18.479175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test_features)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:05:18.482159Z","iopub.execute_input":"2023-06-20T00:05:18.482535Z","iopub.status.idle":"2023-06-20T00:05:19.422182Z","shell.execute_reply.started":"2023-06-20T00:05:18.482501Z","shell.execute_reply":"2023-06-20T00:05:19.421223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create pandas DataFrame with empty columns\npreds_df = pd.DataFrame(columns=[\"id\"] + list(classes))\n\ntest_path = \"/kaggle/input/dog-breed-identification/test/\"\npreds_df[\"id\"] = [os.path.splitext(path)[0] for path in os.listdir(test_path)]","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:05:19.423673Z","iopub.execute_input":"2023-06-20T00:05:19.424017Z","iopub.status.idle":"2023-06-20T00:05:19.482768Z","shell.execute_reply.started":"2023-06-20T00:05:19.423985Z","shell.execute_reply":"2023-06-20T00:05:19.481802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_df.loc[:,list(classes)] = pred\n\npreds_df.to_csv('submission.csv',index=None)","metadata":{"execution":{"iopub.status.busy":"2023-06-20T00:05:19.484254Z","iopub.execute_input":"2023-06-20T00:05:19.484626Z","iopub.status.idle":"2023-06-20T00:05:23.173530Z","shell.execute_reply.started":"2023-06-20T00:05:19.484593Z","shell.execute_reply":"2023-06-20T00:05:23.172452Z"},"trusted":true},"execution_count":null,"outputs":[]}]}