{"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\nimport os\nimport gc\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport numpy as np\nimport pandas as pd \nfrom sklearn.model_selection import train_test_split\nfrom tqdm.autonotebook import tqdm\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","metadata":{"execution":{"iopub.status.busy":"2023-07-15T13:29:58.974141Z","iopub.execute_input":"2023-07-15T13:29:58.974528Z","iopub.status.idle":"2023-07-15T13:29:58.983003Z","shell.execute_reply.started":"2023-07-15T13:29:58.974500Z","shell.execute_reply":"2023-07-15T13:29:58.981612Z"},"trusted":true},"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-07-15T13:29:55.079826Z","iopub.execute_input":"2023-07-15T13:29:55.080664Z","iopub.status.idle":"2023-07-15T13:29:55.143174Z","shell.execute_reply.started":"2023-07-15T13:29:55.080633Z","shell.execute_reply":"2023-07-15T13:29:55.142005Z"},"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-07-15T13:30:03.262682Z","iopub.execute_input":"2023-07-15T13:30:03.263061Z","iopub.status.idle":"2023-07-15T13:30:03.271417Z","shell.execute_reply.started":"2023-07-15T13:30:03.263034Z","shell.execute_reply":"2023-07-15T13:30:03.270245Z"},"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-07-15T13:30:06.286370Z","iopub.execute_input":"2023-07-15T13:30:06.286767Z","iopub.status.idle":"2023-07-15T13:30:06.295424Z","shell.execute_reply.started":"2023-07-15T13:30:06.286722Z","shell.execute_reply":"2023-07-15T13:30:06.294088Z"},"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-07-15T13:30:08.774630Z","iopub.execute_input":"2023-07-15T13:30:08.775056Z","iopub.status.idle":"2023-07-15T13:32:34.209628Z","shell.execute_reply.started":"2023-07-15T13:30:08.775023Z","shell.execute_reply":"2023-07-15T13:32:34.208466Z"},"trusted":true},"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-07-15T13:32:39.187367Z","iopub.execute_input":"2023-07-15T13:32:39.187757Z","iopub.status.idle":"2023-07-15T13:32:40.570396Z","shell.execute_reply.started":"2023-07-15T13:32:39.187707Z","shell.execute_reply":"2023-07-15T13:32:40.569116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"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-07-15T13:32:43.984430Z","iopub.execute_input":"2023-07-15T13:32:43.985516Z","iopub.status.idle":"2023-07-15T13:32:43.990492Z","shell.execute_reply.started":"2023-07-15T13:32:43.985475Z","shell.execute_reply":"2023-07-15T13:32:43.989279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hyperparameters\nbatch_size= 128\nepochs=25\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-07-15T13:32:45.425138Z","iopub.execute_input":"2023-07-15T13:32:45.425554Z","iopub.status.idle":"2023-07-15T13:32:45.446831Z","shell.execute_reply.started":"2023-07-15T13:32:45.425526Z","shell.execute_reply":"2023-07-15T13:32:45.445952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\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='/kaggle/input/weight-weight/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5', 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 maps.\n    feature_maps = feature_extractor.predict(data, verbose=1)\n    print('Feature maps shape:', feature_maps.shape)\n\n    # Visualize the feature maps.\n\n\n    return feature_maps\n\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\n","metadata":{"execution":{"iopub.status.busy":"2023-07-15T13:32:48.166085Z","iopub.execute_input":"2023-07-15T13:32:48.166457Z","iopub.status.idle":"2023-07-15T13:47:55.841754Z","shell.execute_reply.started":"2023-07-15T13:32:48.166429Z","shell.execute_reply":"2023-07-15T13:47:55.840835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n","metadata":{"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-07-15T13:48:02.619528Z","iopub.execute_input":"2023-07-15T13:48:02.619950Z","iopub.status.idle":"2023-07-15T13:48:03.384406Z","shell.execute_reply.started":"2023-07-15T13:48:02.619917Z","shell.execute_reply":"2023-07-15T13:48:03.383223Z"},"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])\npredicted_probabilities = model.predict(test_features)[0]\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-15T13:48:04.735021Z","iopub.execute_input":"2023-07-15T13:48:04.735632Z","iopub.status.idle":"2023-07-15T13:48:15.561476Z","shell.execute_reply.started":"2023-07-15T13:48:04.735600Z","shell.execute_reply":"2023-07-15T13:48:15.559196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-07-15T13:48:32.204600Z","iopub.execute_input":"2023-07-15T13:48:32.205030Z","iopub.status.idle":"2023-07-15T13:48:32.221281Z","shell.execute_reply.started":"2023-07-15T13:48:32.204996Z","shell.execute_reply":"2023-07-15T13:48:32.220103Z"},"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()","metadata":{"execution":{"iopub.status.busy":"2023-06-29T11:01:47.372536Z","iopub.execute_input":"2023-06-29T11:01:47.373525Z","iopub.status.idle":"2023-06-29T11:01:48.092263Z","shell.execute_reply.started":"2023-06-29T11:01:47.373483Z","shell.execute_reply":"2023-06-29T11:01:48.091027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del inception_features\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-07-15T13:48:38.151415Z","iopub.execute_input":"2023-07-15T13:48:38.151864Z","iopub.status.idle":"2023-07-15T13:48:38.437574Z","shell.execute_reply.started":"2023-07-15T13:48:38.151830Z","shell.execute_reply":"2023-07-15T13:48:38.436337Z"},"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(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T13:48:43.195640Z","iopub.execute_input":"2023-07-15T13:48:43.196059Z","iopub.status.idle":"2023-07-15T13:55:17.080525Z","shell.execute_reply.started":"2023-07-15T13:48:43.196027Z","shell.execute_reply":"2023-07-15T13:55:17.078978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ny_pred = model.predict(test_features)\n\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-07-15T13:55:21.878894Z","iopub.execute_input":"2023-07-15T13:55:21.879315Z","iopub.status.idle":"2023-07-15T13:55:22.171968Z","shell.execute_reply.started":"2023-07-15T13:55:21.879280Z","shell.execute_reply":"2023-07-15T13:55:22.170702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\n\ny_test_indices = np.argmax(y_test, axis=1)\ny_pred_indices = np.argmax(y_pred, axis=1)\n\naccuracy_score(y_test_indices, y_pred_indices)","metadata":{"execution":{"iopub.status.busy":"2023-07-15T13:55:24.482766Z","iopub.execute_input":"2023-07-15T13:55:24.483178Z","iopub.status.idle":"2023-07-15T13:55:24.495039Z","shell.execute_reply.started":"2023-07-15T13:55:24.483146Z","shell.execute_reply":"2023-07-15T13:55:24.493813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import load_img, img_to_array\nfrom keras.applications.inception_v3 import preprocess_input\n\n# Load and preprocess the test image\ntest_image_path = '/kaggle/input/dog-breed-identification/train/002a283a315af96eaea0e28e7163b21b.jpg'\ntest_image = load_img(test_image_path, target_size=img_size)\ntest_image = img_to_array(test_image)\ntest_image = preprocess_input(test_image)\ntest_image = np.expand_dims(test_image, axis=0)\n\n# Extract features for the test image\ntest_features = get_features(InceptionV3, inception_preprocessor, img_size, test_image)\n\n# Predict the dog breed\npredicted_probabilities = model.predict(test_features)[0]\n\n# Determine the predicted breed and accuracy\npredicted_breed_index = np.argmax(predicted_probabilities)\npredicted_breed = classes[predicted_breed_index]\naccuracy = predicted_probabilities[predicted_breed_index]\n\nprint(\"Predicted Dog Breed:\", predicted_breed)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-06-29T11:14:51.704781Z","iopub.execute_input":"2023-06-29T11:14:51.705204Z","iopub.status.idle":"2023-06-29T11:14:58.368198Z","shell.execute_reply.started":"2023-06-29T11:14:51.705174Z","shell.execute_reply":"2023-06-29T11:14:58.367230Z"},"trusted":true},"execution_count":null,"outputs":[]}]}