{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":7327,"databundleVersionId":861871,"sourceType":"competition"},{"sourceId":8146290,"sourceType":"datasetVersion","datasetId":4817287},{"sourceId":8146364,"sourceType":"datasetVersion","datasetId":4817348},{"sourceId":8157436,"sourceType":"datasetVersion","datasetId":4825673}],"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nimport gc\n\nfrom sklearn.model_selection import train_test_split\n\nimport tensorflow as tf\nfrom tqdm 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\nfrom tensorflow.keras.models import Model\nfrom keras.preprocessing.image import load_img\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:02:44.432167Z","iopub.execute_input":"2024-04-18T15:02:44.432480Z","iopub.status.idle":"2024-04-18T15:02:59.534647Z","shell.execute_reply.started":"2024-04-18T15:02:44.432453Z","shell.execute_reply":"2024-04-18T15:02:59.533585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"GPU\", \"available (Yes):\" if tf.config.list_physical_devices(\"GPU\") else \"not available: (\")\ntf.config.list_physical_devices(\"GPU\")","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:02:59.536511Z","iopub.execute_input":"2024-04-18T15:02:59.537058Z","iopub.status.idle":"2024-04-18T15:02:59.778843Z","shell.execute_reply.started":"2024-04-18T15:02:59.537032Z","shell.execute_reply":"2024-04-18T15:02:59.777758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#reading labels csv file\n\nlabels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:02:59.780223Z","iopub.execute_input":"2024-04-18T15:02:59.780621Z","iopub.status.idle":"2024-04-18T15:02:59.857904Z","shell.execute_reply.started":"2024-04-18T15:02:59.780586Z","shell.execute_reply":"2024-04-18T15:02:59.856907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.describe()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:02:59.859063Z","iopub.execute_input":"2024-04-18T15:02:59.859342Z","iopub.status.idle":"2024-04-18T15:02:59.890446Z","shell.execute_reply.started":"2024-04-18T15:02:59.859319Z","shell.execute_reply":"2024-04-18T15:02:59.889527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#function to show bar length\ndef barw(ax):\n    for p in ax.patches: \n        val = p.get_width()\n        x = p.get_x() + p.get_width()\n        y = p.get_y() + p.get_height()/2\n        ax.annotate(round(val,2), (x,y))\n        \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":"2024-04-18T15:02:59.893769Z","iopub.execute_input":"2024-04-18T15:02:59.894238Z","iopub.status.idle":"2024-04-18T15:03:01.775344Z","shell.execute_reply.started":"2024-04-18T15:02:59.894213Z","shell.execute_reply":"2024-04-18T15:03:01.774347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['breed'].nunique()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:03:01.776512Z","iopub.execute_input":"2024-04-18T15:03:01.776807Z","iopub.status.idle":"2024-04-18T15:03:01.784189Z","shell.execute_reply.started":"2024-04-18T15:03:01.776781Z","shell.execute_reply":"2024-04-18T15:03:01.783221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import display, Image\nImage(\"/kaggle/input/dog-breed-identification/train/001513dfcb2ffafc82cccf4d8bbaba97.jpg\")","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:03:01.785386Z","iopub.execute_input":"2024-04-18T15:03:01.785828Z","iopub.status.idle":"2024-04-18T15:03:01.803818Z","shell.execute_reply.started":"2024-04-18T15:03:01.785796Z","shell.execute_reply":"2024-04-18T15:03:01.803068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nif len(os.listdir('/kaggle/input/dog-breed-identification/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":"2024-04-18T15:03:01.804867Z","iopub.execute_input":"2024-04-18T15:03:01.805123Z","iopub.status.idle":"2024-04-18T15:03:02.194371Z","shell.execute_reply.started":"2024-04-18T15:03:01.805101Z","shell.execute_reply":"2024-04-18T15:03:02.193334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = sorted(list(set(labels['breed'])))\nn_classes = len(classes)\nprint('Total unique breed {}'.format(n_classes))\n\nclass_to_num = dict(zip(classes, range(n_classes)))\nclass_to_num","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:03:02.195711Z","iopub.execute_input":"2024-04-18T15:03:02.196084Z","iopub.status.idle":"2024-04-18T15:03:02.208541Z","shell.execute_reply.started":"2024-04-18T15:03:02.196051Z","shell.execute_reply":"2024-04-18T15:03:02.207693Z"},"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    image_labels = label_dataframe['breed']\n    images = np.zeros([len(label_dataframe), target_size[0], target_size[1], target_size[2]], dtype=np.uint8)\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        \n        images[ix] = img\n        \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    return images, y\n    ","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:03:02.209622Z","iopub.execute_input":"2024-04-18T15:03:02.209887Z","iopub.status.idle":"2024-04-18T15:03:02.217058Z","shell.execute_reply.started":"2024-04-18T15:03:02.209862Z","shell.execute_reply":"2024-04-18T15:03:02.216299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"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":"2024-04-18T15:03:02.218064Z","iopub.execute_input":"2024-04-18T15:03:02.218358Z","iopub.status.idle":"2024-04-18T15:04:29.330411Z","shell.execute_reply.started":"2024-04-18T15:03:02.218336Z","shell.execute_reply":"2024-04-18T15:04:29.329450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#check dogs and their breeds\nn = 25\n\n#setup the figure\nplt.figure(figsize=(20,20))\n\nfor i in range(n):\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'))\n    ","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:04:29.331682Z","iopub.execute_input":"2024-04-18T15:04:29.331992Z","iopub.status.idle":"2024-04-18T15:04:35.936481Z","shell.execute_reply.started":"2024-04-18T15:04:29.331967Z","shell.execute_reply":"2024-04-18T15:04:35.935421Z"},"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":"2024-04-18T15:04:35.937782Z","iopub.execute_input":"2024-04-18T15:04:35.938112Z","iopub.status.idle":"2024-04-18T15:04:35.943649Z","shell.execute_reply.started":"2024-04-18T15:04:35.938081Z","shell.execute_reply":"2024-04-18T15:04:35.942692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hyperparameters\nbatch_size= 128\nepochs=50\nlearn_rate=.001\nsgd=SGD(learning_rate=learn_rate,momentum=.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":"2024-04-18T15:04:35.948464Z","iopub.execute_input":"2024-04-18T15:04:35.948849Z","iopub.status.idle":"2024-04-18T15:04:36.499326Z","shell.execute_reply.started":"2024-04-18T15:04:35.948822Z","shell.execute_reply":"2024-04-18T15:04:36.498473Z"},"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":"2024-04-18T15:04:36.500560Z","iopub.execute_input":"2024-04-18T15:04:36.500940Z","iopub.status.idle":"2024-04-18T15:04:36.507803Z","shell.execute_reply.started":"2024-04-18T15:04:36.500905Z","shell.execute_reply":"2024-04-18T15:04:36.506959Z"},"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":"2024-04-18T15:04:36.509027Z","iopub.execute_input":"2024-04-18T15:04:36.509372Z","iopub.status.idle":"2024-04-18T15:06:06.689387Z","shell.execute_reply.started":"2024-04-18T15:04:36.509347Z","shell.execute_reply":"2024-04-18T15:06:06.688237Z"},"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":{"execution":{"iopub.status.busy":"2024-04-18T15:06:06.691229Z","iopub.execute_input":"2024-04-18T15:06:06.691594Z","iopub.status.idle":"2024-04-18T15:08:30.244290Z","shell.execute_reply.started":"2024-04-18T15:06:06.691566Z","shell.execute_reply":"2024-04-18T15:08:30.243340Z"},"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":{"iopub.status.busy":"2024-04-18T15:08:30.245685Z","iopub.execute_input":"2024-04-18T15:08:30.245975Z","iopub.status.idle":"2024-04-18T15:11:13.066777Z","shell.execute_reply.started":"2024-04-18T15:08:30.245949Z","shell.execute_reply":"2024-04-18T15:11:13.065742Z"},"trusted":true},"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":{"iopub.status.busy":"2024-04-18T15:11:13.068453Z","iopub.execute_input":"2024-04-18T15:11:13.068829Z","iopub.status.idle":"2024-04-18T15:16:44.088528Z","shell.execute_reply.started":"2024-04-18T15:11:13.068785Z","shell.execute_reply":"2024-04-18T15:16:44.087451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X #to free up some ram memory\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:16:44.090367Z","iopub.execute_input":"2024-04-18T15:16:44.090660Z","iopub.status.idle":"2024-04-18T15:16:44.983129Z","shell.execute_reply.started":"2024-04-18T15:16:44.090634Z","shell.execute_reply":"2024-04-18T15:16:44.982167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating final featuremap by combining all extracted features\n\nfinal_features = np.concatenate([inception_features,\n                                 xception_features,\n                                 nasnet_features,\n                                 inc_resnet_features,], axis=-1) #axis=-1 to concatinate horizontally\n\nprint('Final feature maps shape', final_features.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:16:44.986897Z","iopub.execute_input":"2024-04-18T15:16:44.987158Z","iopub.status.idle":"2024-04-18T15:16:45.148214Z","shell.execute_reply.started":"2024-04-18T15:16:44.987135Z","shell.execute_reply":"2024-04-18T15:16:45.147230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.optimizers import Adam\nfrom keras.callbacks import ReduceLROnPlateau\n\n# Khởi tạo callback giảm learning rate\nlrr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1, mode='min', min_delta=0.0001, cooldown=2, min_lr=0)\n\n# Khởi tạo optimizer Adam\nadam = Adam()\n#Prepare Deep net\nmodel = Sequential()\n# model.add(Dense(1028,input_shape=(final_features.shape[1],)))\nmodel.add(Dropout(0.7,input_shape=(final_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(final_features, y,\n            batch_size=batch_size,\n            epochs=epochs,\n            validation_split=0.2,\n            callbacks=[lrr,EarlyStop])","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:16:45.149408Z","iopub.execute_input":"2024-04-18T15:16:45.149680Z","iopub.status.idle":"2024-04-18T15:16:54.736095Z","shell.execute_reply.started":"2024-04-18T15:16:45.149656Z","shell.execute_reply":"2024-04-18T15:16:54.735317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#deleting to free up ram memory\n\ndel inception_features\ndel xception_features\ndel nasnet_features\ndel inc_resnet_features\ndel final_features\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:16:54.738124Z","iopub.execute_input":"2024-04-18T15:16:54.738534Z","iopub.status.idle":"2024-04-18T15:16:54.974289Z","shell.execute_reply.started":"2024-04-18T15:16:54.738508Z","shell.execute_reply":"2024-04-18T15:16:54.973330Z"},"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\n    data_size = len(test_filenames)\n    images = np.zeros([data_size, img_size[0], img_size[1], 3], dtype=np.uint8)\n    \n    \n    for ix,img_dir in enumerate(tqdm(test_filenames)):\n#         img_dir = os.path.join(directory, image_name + '.jpg')\n        img = load_img(img_dir, target_size = img_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    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":"2024-04-18T15:16:54.975601Z","iopub.execute_input":"2024-04-18T15:16:54.976205Z","iopub.status.idle":"2024-04-18T15:18:31.857778Z","shell.execute_reply.started":"2024-04-18T15:16:54.976169Z","shell.execute_reply":"2024-04-18T15:18:31.856851Z"},"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    xception_features = get_features(Xception, xception_preprocessor, img_size, data)\n    nasnet_features = get_features(NASNetLarge, nasnet_preprocessor, img_size, data)\n    inc_resnet_features = get_features(InceptionResNetV2, inc_resnet_preprocessor, img_size, data)\n\n    final_features = np.concatenate([inception_features,\n                                     xception_features,\n                                     nasnet_features,\n                                     inc_resnet_features],axis=-1)\n    \n    print('Final feature maps shape', final_features.shape)\n    \n    #deleting to free up ram memory\n    del inception_features\n    del xception_features\n    del nasnet_features\n    del inc_resnet_features\n    gc.collect()\n    \n    \n    return final_features\n\ntest_features = extact_features(test_data)","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:18:31.858922Z","iopub.execute_input":"2024-04-18T15:18:31.859193Z","iopub.status.idle":"2024-04-18T15:29:13.124907Z","shell.execute_reply.started":"2024-04-18T15:18:31.859169Z","shell.execute_reply":"2024-04-18T15:29:13.123838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Free up some space.\ndel test_data\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:29:13.135384Z","iopub.execute_input":"2024-04-18T15:29:13.135695Z","iopub.status.idle":"2024-04-18T15:29:13.571438Z","shell.execute_reply.started":"2024-04-18T15:29:13.135670Z","shell.execute_reply":"2024-04-18T15:29:13.570440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Predict test labels given test data features.\n\npred = model.predict(test_features)","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:29:13.572914Z","iopub.execute_input":"2024-04-18T15:29:13.573536Z","iopub.status.idle":"2024-04-18T15:29:15.562358Z","shell.execute_reply.started":"2024-04-18T15:29:13.573502Z","shell.execute_reply":"2024-04-18T15:29:15.561576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(pred[0])\nprint(f\"Max value (probability of prediction): {np.max(pred[0])}\") # the max probability value predicted by the model\nprint(f\"Sum: {np.sum(pred[0])}\") # because we used softmax activation in our model, this will be close to 1\nprint(f\"Max index: {np.argmax(pred[0])}\") # the index of where the max value in predictions[0] occurs\nprint(f\"Predicted label: {classes[np.argmax(pred[0])]}\")","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:29:15.564184Z","iopub.execute_input":"2024-04-18T15:29:15.564491Z","iopub.status.idle":"2024-04-18T15:29:15.571756Z","shell.execute_reply.started":"2024-04-18T15:29:15.564466Z","shell.execute_reply":"2024-04-18T15:29:15.570622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create pandas DataFrame with empty columns\npreds_df = pd.DataFrame(columns=[\"id\"] + list(classes))\npreds_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:29:15.572908Z","iopub.execute_input":"2024-04-18T15:29:15.573220Z","iopub.status.idle":"2024-04-18T15:29:15.601452Z","shell.execute_reply.started":"2024-04-18T15:29:15.573196Z","shell.execute_reply":"2024-04-18T15:29:15.600598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Append test image ID's to predictions DataFrame\ntest_path = \"/kaggle/input/dog-breed-identification/test/\"\npreds_df[\"id\"] = [os.path.splitext(path)[0] for path in os.listdir(test_path)]\npreds_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:29:15.602550Z","iopub.execute_input":"2024-04-18T15:29:15.602898Z","iopub.status.idle":"2024-04-18T15:29:15.673145Z","shell.execute_reply.started":"2024-04-18T15:29:15.602867Z","shell.execute_reply":"2024-04-18T15:29:15.672275Z"},"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)\npreds_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:29:15.674316Z","iopub.execute_input":"2024-04-18T15:29:15.674641Z","iopub.status.idle":"2024-04-18T15:29:18.479314Z","shell.execute_reply.started":"2024-04-18T15:29:15.674614Z","shell.execute_reply":"2024-04-18T15:29:18.478344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Custom input\n\nImage('/kaggle/input/newtest/368054480_1286460275372045_842154678665043350_n.png')","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:33:35.445494Z","iopub.execute_input":"2024-04-18T15:33:35.446154Z","iopub.status.idle":"2024-04-18T15:33:35.486617Z","shell.execute_reply.started":"2024-04-18T15:33:35.446122Z","shell.execute_reply":"2024-04-18T15:33:35.485695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#reading the image and converting it into an np array\n\nimg_g = load_img('/kaggle/input/newtest/368054480_1286460275372045_842154678665043350_n.png',target_size = img_size)\nimg_g = np.expand_dims(img_g, axis=0) # as we trained our model in (row, img_height, img_width, img_rgb) format, np.expand_dims convert the image into this format\n# img_g","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:33:49.841540Z","iopub.execute_input":"2024-04-18T15:33:49.842490Z","iopub.status.idle":"2024-04-18T15:33:49.858717Z","shell.execute_reply.started":"2024-04-18T15:33:49.842454Z","shell.execute_reply":"2024-04-18T15:33:49.857958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_g.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:33:53.504230Z","iopub.execute_input":"2024-04-18T15:33:53.505085Z","iopub.status.idle":"2024-04-18T15:33:53.510978Z","shell.execute_reply.started":"2024-04-18T15:33:53.505053Z","shell.execute_reply":"2024-04-18T15:33:53.509967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #Predict test labels given test data features.\ntest_features = extact_features(img_g)\npredg = model.predict(test_features)\nprint(f\"Predicted label: {classes[np.argmax(predg[0])]}\")\nprint(f\"Probability of prediction: {round(np.max(predg[0])) * 100} %\")","metadata":{"execution":{"iopub.status.busy":"2024-04-18T15:33:55.553483Z","iopub.execute_input":"2024-04-18T15:33:55.554371Z","iopub.status.idle":"2024-04-18T15:34:48.176154Z","shell.execute_reply.started":"2024-04-18T15:33:55.554324Z","shell.execute_reply":"2024-04-18T15:34:48.175134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}