{"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":5048,"databundleVersionId":868335,"sourceType":"competition"},{"sourceId":8446990,"sourceType":"datasetVersion","datasetId":5033354},{"sourceId":8548515,"sourceType":"datasetVersion","datasetId":5107875}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import libraries","metadata":{}},{"cell_type":"code","source":"import argparse\nimport random\nimport glob\n\nfrom keras.models import Sequential, Model\nfrom keras.layers import Dense, Dropout, Activation, Flatten, Add, Concatenate, Input, SeparableConv2D\nfrom keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D, AveragePooling2D, LSTM, Reshape, GlobalAveragePooling2D\nfrom keras.layers import BatchNormalization, Flatten, Layer\nfrom tensorflow.keras.regularizers import L2\n\nfrom keras import optimizers\nimport tensorflow as tf\nimport gc\nimport keras\n\nimport numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.utils import shuffle\n\nfrom tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2_as_graph\nfrom sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\n","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:45:05.738770Z","iopub.execute_input":"2024-06-23T15:45:05.739066Z","iopub.status.idle":"2024-06-23T15:45:20.277519Z","shell.execute_reply.started":"2024-06-23T15:45:05.739042Z","shell.execute_reply":"2024-06-23T15:45:20.276717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nimport os\nfrom tqdm import tqdm\npd.options.display.max_colwidth = 1000\ntqdm.pandas()\n\nimport cv2\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom IPython.display import clear_output\n","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:45:20.279371Z","iopub.execute_input":"2024-06-23T15:45:20.280331Z","iopub.status.idle":"2024-06-23T15:45:20.549663Z","shell.execute_reply.started":"2024-06-23T15:45:20.280295Z","shell.execute_reply":"2024-06-23T15:45:20.548676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# SF3D Dataset","metadata":{}},{"cell_type":"markdown","source":"## Create Train and Test files","metadata":{}},{"cell_type":"code","source":"activity_map_SF3D = {'c0': 'Safe driving',\n                'c1': 'Texting - right',\n                'c2': 'Talking on the phone - right',\n                'c3': 'Texting - left',\n                'c4': 'Talking on the phone - left',\n                'c5': 'Operating the radio',\n                'c6': 'Drinking',\n                'c7': 'Reaching behind',\n                'c8': 'Hair and makeup',\n                'c9': 'Talking to passenger'}\n\nIMG_WIDTH = 224\nIMG_HEIGH = 224\nCHANNEL_SIZE = 3","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:45:20.551028Z","iopub.execute_input":"2024-06-23T15:45:20.551648Z","iopub.status.idle":"2024-06-23T15:45:20.557231Z","shell.execute_reply.started":"2024-06-23T15:45:20.551615Z","shell.execute_reply":"2024-06-23T15:45:20.556310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"driver_imgs_list_SF3D = pd.read_csv('/kaggle/input/state-farm-distracted-driver-detection/driver_imgs_list.csv')\ndriver_imgs_list_SF3D['class'] = driver_imgs_list_SF3D['classname'].replace(activity_map_SF3D)\ndriver_imgs_list_SF3D.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:45:20.559992Z","iopub.execute_input":"2024-06-23T15:45:20.560579Z","iopub.status.idle":"2024-06-23T15:45:20.641375Z","shell.execute_reply.started":"2024-06-23T15:45:20.560544Z","shell.execute_reply":"2024-06-23T15:45:20.640414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_SF3D = pd.read_csv('/kaggle/input/state-farm-distracted-driver-detection/sample_submission.csv')\nsample_submission_SF3D.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:45:20.642415Z","iopub.execute_input":"2024-06-23T15:45:20.642714Z","iopub.status.idle":"2024-06-23T15:45:20.819900Z","shell.execute_reply.started":"2024-06-23T15:45:20.642688Z","shell.execute_reply":"2024-06-23T15:45:20.818896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nlist_train_img_SF3D = glob.glob(os.path.join('/kaggle/input/state-farm-distracted-driver-detection/imgs', 'train', '*', \"*.jpg\"))\nprint('Total number of Train Images is: ', len(list_train_img_SF3D))\nlist_train_img_SF3D[:5]","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:45:20.820974Z","iopub.execute_input":"2024-06-23T15:45:20.821248Z","iopub.status.idle":"2024-06-23T15:45:23.053017Z","shell.execute_reply.started":"2024-06-23T15:45:20.821224Z","shell.execute_reply":"2024-06-23T15:45:23.052108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_test_img_SF3D = glob.glob(os.path.join('/kaggle/input/state-farm-distracted-driver-detection/imgs', 'test', \"*.jpg\"))\nprint('Total number of Test Images is: ', len(list_test_img_SF3D))\nlist_test_img_SF3D[:5]","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:45:23.054230Z","iopub.execute_input":"2024-06-23T15:45:23.054595Z","iopub.status.idle":"2024-06-23T15:45:24.219428Z","shell.execute_reply.started":"2024-06-23T15:45:23.054562Z","shell.execute_reply":"2024-06-23T15:45:24.218536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"driver_imgs_list_SF3D['ImgPath'] = driver_imgs_list_SF3D['img'].progress_apply(lambda x: [i for i in list_train_img_SF3D if x in i][0])\ndataset_SF3D = driver_imgs_list_SF3D.copy()\ndel driver_imgs_list_SF3D, list_train_img_SF3D","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:45:24.220714Z","iopub.execute_input":"2024-06-23T15:45:24.221137Z","iopub.status.idle":"2024-06-23T15:45:59.580130Z","shell.execute_reply.started":"2024-06-23T15:45:24.221102Z","shell.execute_reply":"2024-06-23T15:45:59.579229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_SF3D['ImgPath'] = sample_submission_SF3D['img'].progress_apply(lambda x: [i for i in list_test_img_SF3D if x in i][0])\nunlabeled_data_SF3D = sample_submission_SF3D.copy()\ndel sample_submission_SF3D, list_test_img_SF3D","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:45:59.581410Z","iopub.execute_input":"2024-06-23T15:45:59.581783Z","iopub.status.idle":"2024-06-23T15:53:33.466636Z","shell.execute_reply.started":"2024-06-23T15:45:59.581752Z","shell.execute_reply":"2024-06-23T15:53:33.465729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_SF3D.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:53:33.470790Z","iopub.execute_input":"2024-06-23T15:53:33.471062Z","iopub.status.idle":"2024-06-23T15:53:33.481378Z","shell.execute_reply.started":"2024-06-23T15:53:33.471039Z","shell.execute_reply":"2024-06-23T15:53:33.480565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_SF3D.tail(5)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:53:33.482496Z","iopub.execute_input":"2024-06-23T15:53:33.482842Z","iopub.status.idle":"2024-06-23T15:53:33.498896Z","shell.execute_reply.started":"2024-06-23T15:53:33.482808Z","shell.execute_reply":"2024-06-23T15:53:33.497929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_SF3D['classname'].value_counts().to_frame()","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:53:33.499865Z","iopub.execute_input":"2024-06-23T15:53:33.500105Z","iopub.status.idle":"2024-06-23T15:53:33.521800Z","shell.execute_reply.started":"2024-06-23T15:53:33.500084Z","shell.execute_reply":"2024-06-23T15:53:33.520962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unlabeled_data_SF3D.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:53:33.522930Z","iopub.execute_input":"2024-06-23T15:53:33.523218Z","iopub.status.idle":"2024-06-23T15:53:33.545065Z","shell.execute_reply.started":"2024-06-23T15:53:33.523193Z","shell.execute_reply":"2024-06-23T15:53:33.544172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unlabeled_data_SF3D.tail(5)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:53:33.546200Z","iopub.execute_input":"2024-06-23T15:53:33.546679Z","iopub.status.idle":"2024-06-23T15:53:33.567538Z","shell.execute_reply.started":"2024-06-23T15:53:33.546649Z","shell.execute_reply":"2024-06-23T15:53:33.566657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_from_dataframe(class_, ImgPath):\n  img_arr = cv2.imread(ImgPath)\n  plt.imshow(img_arr),\n  plt.title(class_)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:53:33.568657Z","iopub.execute_input":"2024-06-23T15:53:33.568982Z","iopub.status.idle":"2024-06-23T15:53:33.576127Z","shell.execute_reply.started":"2024-06-23T15:53:33.568958Z","shell.execute_reply":"2024-06-23T15:53:33.575229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_from_dataframe(class_ = 'Talking to passenger', ImgPath = '/kaggle/input/state-farm-distracted-driver-detection/imgs/train/c9/img_9877.jpg')","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:53:33.577304Z","iopub.execute_input":"2024-06-23T15:53:33.577627Z","iopub.status.idle":"2024-06-23T15:53:34.089364Z","shell.execute_reply.started":"2024-06-23T15:53:33.577599Z","shell.execute_reply":"2024-06-23T15:53:34.088539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train and Validation Split","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_dataset_SF3D, val_test_dataset_SF3D = train_test_split(dataset_SF3D, test_size = 0.2,\n                                                random_state = 42,\n                                                shuffle = True,\n                                                stratify = dataset_SF3D['class'])\n\nvalid_dataset_SF3D, test_dataset_SF3D = train_test_split(val_test_dataset_SF3D, test_size = 0.5,\n                                                random_state = 42,\n                                                shuffle = True,\n                                                stratify = val_test_dataset_SF3D['class'])","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:53:34.090804Z","iopub.execute_input":"2024-06-23T15:53:34.091109Z","iopub.status.idle":"2024-06-23T15:53:34.140101Z","shell.execute_reply.started":"2024-06-23T15:53:34.091082Z","shell.execute_reply":"2024-06-23T15:53:34.139434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation","metadata":{}},{"cell_type":"code","source":"generator = ImageDataGenerator(rescale = 1./255)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:53:34.140985Z","iopub.execute_input":"2024-06-23T15:53:34.141222Z","iopub.status.idle":"2024-06-23T15:53:34.145256Z","shell.execute_reply.started":"2024-06-23T15:53:34.141200Z","shell.execute_reply":"2024-06-23T15:53:34.144367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_set_SF3D = generator.flow_from_dataframe(dataframe = train_dataset_SF3D,\n                                              x_col = 'ImgPath',\n                                              y_col = 'class',\n                                              target_size=(IMG_HEIGH, IMG_WIDTH),\n                                              class_mode = 'categorical',\n                                              batch_size= 64)\n\nvalidation_set_SF3D = generator.flow_from_dataframe(dataframe = valid_dataset_SF3D,\n                                              x_col = 'ImgPath',\n                                              y_col = 'class',\n                                              target_size = (IMG_HEIGH, IMG_WIDTH),\n                                              class_mode = 'categorical',\n                                              batch_size = 64)\n\ntest_set_SF3D = generator.flow_from_dataframe(dataframe = test_dataset_SF3D,\n                                              x_col = 'ImgPath',\n                                              y_col = 'class',\n                                              target_size=(IMG_HEIGH, IMG_WIDTH),\n                                              class_mode = 'categorical',\n                                              batch_size= 64)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:53:34.146463Z","iopub.execute_input":"2024-06-23T15:53:34.147020Z","iopub.status.idle":"2024-06-23T15:54:12.605664Z","shell.execute_reply.started":"2024-06-23T15:53:34.146989Z","shell.execute_reply":"2024-06-23T15:54:12.604777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# unlabeled_data = generator.flow_from_dataframe(dataframe=test_SF3D,\n#                                                x_col='ImgPath',\n#                                                target_size=(IMG_HEIGH, IMG_WIDTH),\n#                                                batch_size=64,\n#                                                shuffle=False,\n#                                                class_mode=None)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:54:12.606863Z","iopub.execute_input":"2024-06-23T15:54:12.607147Z","iopub.status.idle":"2024-06-23T15:54:12.613150Z","shell.execute_reply.started":"2024-06-23T15:54:12.607122Z","shell.execute_reply":"2024-06-23T15:54:12.612406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# AUC Dataset","metadata":{}},{"cell_type":"markdown","source":"## Create Train and Test files","metadata":{}},{"cell_type":"code","source":"activity_map_AUC = {'c0': 'Drive Safe',\n                'c1': 'Text Right',\n                'c2': 'Talk Right',\n                'c3': 'Text Left',\n                'c4': 'Talk Left',\n                'c5': 'Adjust Radio',\n                'c6': 'Drink',\n                'c7': 'Reach Behind',\n                'c8': 'Hair and Makeup',\n                'c9': 'Talk Passenger'}\n\nIMG_WIDTH = 224\nIMG_HEIGH = 224\nCHANNEL_SIZE = 3","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:54:12.614214Z","iopub.execute_input":"2024-06-23T15:54:12.614519Z","iopub.status.idle":"2024-06-23T15:54:12.627709Z","shell.execute_reply.started":"2024-06-23T15:54:12.614486Z","shell.execute_reply":"2024-06-23T15:54:12.627022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"driver_imgs_list_AUC = pd.read_csv('/kaggle/input/auc-v2-pc/auc.distracted.driver.dataset_v2/v2_cam1_cam2_split_by_driver/Camera_1/cam_1_train.csv')\ndriver_imgs_list_AUC['class'] = driver_imgs_list_AUC['classname'].replace(activity_map_AUC)\ndriver_imgs_list_AUC.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:54:12.628767Z","iopub.execute_input":"2024-06-23T15:54:12.629071Z","iopub.status.idle":"2024-06-23T15:54:12.683644Z","shell.execute_reply.started":"2024-06-23T15:54:12.629041Z","shell.execute_reply":"2024-06-23T15:54:12.682760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"driver_test_list_AUC= pd.read_csv('/kaggle/input/auc-v2-pc/auc.distracted.driver.dataset_v2/v2_cam1_cam2_split_by_driver/Camera_1/cam_1_test.csv')\ndriver_test_list_AUC['class'] = driver_test_list_AUC['classname'].replace(activity_map_AUC)\ndriver_test_list_AUC.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:54:12.684871Z","iopub.execute_input":"2024-06-23T15:54:12.685237Z","iopub.status.idle":"2024-06-23T15:54:12.706071Z","shell.execute_reply.started":"2024-06-23T15:54:12.685203Z","shell.execute_reply":"2024-06-23T15:54:12.705266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n# Tạo mảng list_train_img\nlist_train_img_AUC = []\nfor index, row in driver_imgs_list_AUC.iterrows():\n    img_path = '/kaggle/input/auc-v2-pc/auc.distracted.driver.dataset_v2/v2_cam1_cam2_split_by_driver/Camera_1/train/' + row['classname'] + '/' + row['img']\n    img_path = img_path.replace(' (1)', '')\n    list_train_img_AUC.append(img_path)\n\nlist_train_img_AUC = sorted(list_train_img_AUC, key=lambda x: x.split('/')[8])\ndriver_imgs_list_AUC['ImgPath'] = list_train_img_AUC\n\nprint('Total number of Train Images is: ', len(list_train_img_AUC))\n# In ra mảng list_train_img_AUC\nlist_train_img_AUC[:5]","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:54:12.707005Z","iopub.execute_input":"2024-06-23T15:54:12.707248Z","iopub.status.idle":"2024-06-23T15:54:13.328207Z","shell.execute_reply.started":"2024-06-23T15:54:12.707227Z","shell.execute_reply":"2024-06-23T15:54:13.327326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_AUC = driver_imgs_list_AUC.copy()\ndel driver_imgs_list_AUC, list_train_img_AUC","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:54:13.329383Z","iopub.execute_input":"2024-06-23T15:54:13.329659Z","iopub.status.idle":"2024-06-23T15:54:13.335296Z","shell.execute_reply.started":"2024-06-23T15:54:13.329635Z","shell.execute_reply":"2024-06-23T15:54:13.334474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_AUC.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:54:13.336392Z","iopub.execute_input":"2024-06-23T15:54:13.337000Z","iopub.status.idle":"2024-06-23T15:54:13.355659Z","shell.execute_reply.started":"2024-06-23T15:54:13.336968Z","shell.execute_reply":"2024-06-23T15:54:13.354641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_AUC.tail()","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:54:13.356650Z","iopub.execute_input":"2024-06-23T15:54:13.356917Z","iopub.status.idle":"2024-06-23T15:54:13.371382Z","shell.execute_reply.started":"2024-06-23T15:54:13.356894Z","shell.execute_reply":"2024-06-23T15:54:13.370590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_AUC['classname'].value_counts().to_frame()","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:54:13.379542Z","iopub.execute_input":"2024-06-23T15:54:13.379826Z","iopub.status.idle":"2024-06-23T15:54:13.389700Z","shell.execute_reply.started":"2024-06-23T15:54:13.379804Z","shell.execute_reply":"2024-06-23T15:54:13.388795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train and Validation Split","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_dataset_AUC, val_test_dataset_AUC = train_test_split(dataset_AUC, test_size = 0.2,\n                                                random_state = 42,\n                                                shuffle = True,\n                                                stratify = dataset_AUC['class'])\n\nvalid_dataset_AUC, test_dataset_AUC = train_test_split(val_test_dataset_AUC, test_size = 0.5,\n                                                random_state = 42,\n                                                shuffle = True,\n                                                stratify = val_test_dataset_AUC['class'])","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:54:13.390908Z","iopub.execute_input":"2024-06-23T15:54:13.391641Z","iopub.status.idle":"2024-06-23T15:54:13.418186Z","shell.execute_reply.started":"2024-06-23T15:54:13.391608Z","shell.execute_reply":"2024-06-23T15:54:13.417343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Augmentation","metadata":{}},{"cell_type":"code","source":"generator = ImageDataGenerator(rescale = 1./255)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:54:13.419298Z","iopub.execute_input":"2024-06-23T15:54:13.419897Z","iopub.status.idle":"2024-06-23T15:54:13.424129Z","shell.execute_reply.started":"2024-06-23T15:54:13.419867Z","shell.execute_reply":"2024-06-23T15:54:13.422985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_set_AUC = generator.flow_from_dataframe(dataframe = train_dataset_AUC,\n                                              x_col = 'ImgPath',\n                                              y_col = 'class',\n                                              target_size=(IMG_HEIGH, IMG_WIDTH),\n                                              class_mode = 'categorical',\n                                              batch_size= 64)\n\nvalidation_set_AUC = generator.flow_from_dataframe(dataframe = valid_dataset_AUC,\n                                              x_col = 'ImgPath',\n                                              y_col = 'class',\n                                              target_size = (IMG_HEIGH, IMG_WIDTH),\n                                              class_mode = 'categorical',\n                                              batch_size = 64)\n\ntest_set_AUC = generator.flow_from_dataframe(dataframe = test_dataset_AUC,\n                                              x_col = 'ImgPath',\n                                              y_col = 'class',\n                                              target_size=(IMG_HEIGH, IMG_WIDTH),\n                                              class_mode = 'categorical',\n                                              batch_size= 64)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:54:13.425284Z","iopub.execute_input":"2024-06-23T15:54:13.425649Z","iopub.status.idle":"2024-06-23T15:54:44.112889Z","shell.execute_reply.started":"2024-06-23T15:54:13.425560Z","shell.execute_reply":"2024-06-23T15:54:44.111932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MDAD Dataset","metadata":{}},{"cell_type":"code","source":"activity_map_MDAD = {'AC1': 'Safe driving',\n                'AC2': 'Doing hair and makeup',\n                'AC3': 'Adjusting radio',\n                'AC4': 'GPS operating',\n                'AC5': 'Writing message using right hand',\n                'AC6': 'Writing message using left hand',\n                'AC7': 'Talking phone using right hand',\n                'AC8': 'Talking phone using left hand',\n                'AC9': 'Having picture',\n                'AC10': 'Talking to passenger',\n                'AC11': 'Singing or dancing',\n                'AC12': 'Fatigue and somnolence',\n                'AC13': 'Drinking using right hand',\n                'AC14': 'Drinking using left hand',\n                'AC15': 'Reaching behind',\n                'AC16': 'Smoking'}\n\nIMG_WIDTH = 224\nIMG_HEIGH = 224\nCHANNEL_SIZE = 3","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:54:44.113909Z","iopub.execute_input":"2024-06-23T15:54:44.114197Z","iopub.status.idle":"2024-06-23T15:54:44.120055Z","shell.execute_reply.started":"2024-06-23T15:54:44.114167Z","shell.execute_reply":"2024-06-23T15:54:44.118998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"driver_imgs_list_MDAD = pd.read_csv('/kaggle/input/mdad-pc/MDAD/Day/RGB1/MDAD_Day_RGB1.csv')\ndriver_imgs_list_MDAD['class'] = driver_imgs_list_MDAD['classname'].replace(activity_map_MDAD)\ndriver_imgs_list_MDAD.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:54:44.121368Z","iopub.execute_input":"2024-06-23T15:54:44.122166Z","iopub.status.idle":"2024-06-23T15:54:44.336643Z","shell.execute_reply.started":"2024-06-23T15:54:44.122126Z","shell.execute_reply":"2024-06-23T15:54:44.335688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nlist_train_img_MDAD = glob.glob(os.path.join('/kaggle/input/mdad-pc/MDAD/Day/RGB1', '*', '*', \"*.jpg\"))\nprint('Total number of Train Images is: ', len(list_train_img_MDAD))\nlist_train_img_MDAD[:5]","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:54:44.337571Z","iopub.execute_input":"2024-06-23T15:54:44.337849Z","iopub.status.idle":"2024-06-23T15:55:12.075878Z","shell.execute_reply.started":"2024-06-23T15:54:44.337826Z","shell.execute_reply":"2024-06-23T15:55:12.074986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"driver_imgs_list_MDAD['ImgPath'] = driver_imgs_list_MDAD['img'].progress_apply(lambda x: [i for i in list_train_img_MDAD if x in i][0])\ndataset_MDAD = driver_imgs_list_MDAD.copy()\ndel driver_imgs_list_MDAD, list_train_img_MDAD","metadata":{"execution":{"iopub.status.busy":"2024-06-23T15:55:12.077079Z","iopub.execute_input":"2024-06-23T15:55:12.077369Z","iopub.status.idle":"2024-06-23T16:04:36.414154Z","shell.execute_reply.started":"2024-06-23T15:55:12.077344Z","shell.execute_reply":"2024-06-23T16:04:36.413232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_MDAD.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:04:36.415241Z","iopub.execute_input":"2024-06-23T16:04:36.415515Z","iopub.status.idle":"2024-06-23T16:04:36.425263Z","shell.execute_reply.started":"2024-06-23T16:04:36.415491Z","shell.execute_reply":"2024-06-23T16:04:36.424350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_MDAD.tail(5)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:04:36.426472Z","iopub.execute_input":"2024-06-23T16:04:36.426857Z","iopub.status.idle":"2024-06-23T16:04:36.439120Z","shell.execute_reply.started":"2024-06-23T16:04:36.426812Z","shell.execute_reply":"2024-06-23T16:04:36.438311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_MDAD['classname'].value_counts().to_frame()","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:04:36.440126Z","iopub.execute_input":"2024-06-23T16:04:36.440443Z","iopub.status.idle":"2024-06-23T16:04:36.463480Z","shell.execute_reply.started":"2024-06-23T16:04:36.440412Z","shell.execute_reply":"2024-06-23T16:04:36.462636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_from_dataframe(class_, ImgPath):\n  img_arr = cv2.imread(ImgPath)\n  plt.imshow(img_arr),\n  plt.title(class_)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:04:36.464480Z","iopub.execute_input":"2024-06-23T16:04:36.464776Z","iopub.status.idle":"2024-06-23T16:04:36.471564Z","shell.execute_reply.started":"2024-06-23T16:04:36.464751Z","shell.execute_reply":"2024-06-23T16:04:36.470628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_from_dataframe(class_ = 'Safe driving', ImgPath = '/kaggle/input/mdad-pc/MDAD/Day/RGB1/S1/AC1/RGB1_SUB1ACT1F1.jpg')","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:04:36.472830Z","iopub.execute_input":"2024-06-23T16:04:36.473952Z","iopub.status.idle":"2024-06-23T16:04:36.872694Z","shell.execute_reply.started":"2024-06-23T16:04:36.473928Z","shell.execute_reply":"2024-06-23T16:04:36.871864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train and Validation Split","metadata":{"execution":{"iopub.status.busy":"2024-05-29T04:19:34.664034Z","iopub.execute_input":"2024-05-29T04:19:34.664476Z","iopub.status.idle":"2024-05-29T04:19:34.668961Z","shell.execute_reply.started":"2024-05-29T04:19:34.664442Z","shell.execute_reply":"2024-05-29T04:19:34.667919Z"}}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_dataset_MDAD, val_test_dataset_MDAD = train_test_split(dataset_MDAD, test_size = 0.2,\n                                                random_state = 42,\n                                                shuffle = True,\n                                                stratify = dataset_MDAD['class'])\n\nvalid_dataset_MDAD, test_dataset_MDAD = train_test_split(val_test_dataset_MDAD, test_size = 0.5,\n                                                random_state = 42,\n                                                shuffle = True,\n                                                stratify = val_test_dataset_MDAD['class'])","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:04:36.873827Z","iopub.execute_input":"2024-06-23T16:04:36.874102Z","iopub.status.idle":"2024-06-23T16:04:37.029355Z","shell.execute_reply.started":"2024-06-23T16:04:36.874077Z","shell.execute_reply":"2024-06-23T16:04:37.028562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Augmentation","metadata":{}},{"cell_type":"code","source":"generator = ImageDataGenerator(rescale = 1/255.0)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:04:37.030545Z","iopub.execute_input":"2024-06-23T16:04:37.030875Z","iopub.status.idle":"2024-06-23T16:04:37.035694Z","shell.execute_reply.started":"2024-06-23T16:04:37.030849Z","shell.execute_reply":"2024-06-23T16:04:37.034699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_set_MDAD = generator.flow_from_dataframe(dataframe = train_dataset_MDAD,\n                                              x_col = 'ImgPath',\n                                              y_col = 'class',\n                                              target_size=(IMG_HEIGH, IMG_WIDTH),\n                                              class_mode = 'categorical',\n                                              batch_size= 64)\n\nvalidation_set_MDAD = generator.flow_from_dataframe(dataframe = valid_dataset_MDAD,\n                                              x_col = 'ImgPath',\n                                              y_col = 'class',\n                                              target_size = (IMG_HEIGH, IMG_WIDTH),\n                                              class_mode = 'categorical',\n                                              batch_size = 64)\n\ntest_set_MDAD = generator.flow_from_dataframe(dataframe = test_dataset_MDAD,\n                                              x_col = 'ImgPath',\n                                              y_col = 'class',\n                                              target_size=(IMG_HEIGH, IMG_WIDTH),\n                                              class_mode = 'categorical',\n                                              batch_size= 64)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:04:37.036912Z","iopub.execute_input":"2024-06-23T16:04:37.037257Z","iopub.status.idle":"2024-06-23T16:05:15.165016Z","shell.execute_reply.started":"2024-06-23T16:04:37.037225Z","shell.execute_reply":"2024-06-23T16:05:15.164316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plotlosses","metadata":{}},{"cell_type":"code","source":"class TrainingPlot(keras.callbacks.Callback):\n\n  # This function is called when the training begins\n  def on_train_begin(self, logs={}):\n    # Initialize the lists for holding the logs, losses and and accuracies\n    self.losses = []\n    self.acc = []\n    self.logs = []\n    self.val_losses = []\n    self.val_acc = []\n\n  # This function is called at the end of each epoch\n  def on_epoch_end(self, epoch, logs={}):\n    # Append the logs, losses, and accuracies to the lists\n    self.logs.append(logs)\n    self.losses.append(logs.get('loss'))\n    self.acc.append(logs.get('acc'))\n    self.val_losses.append(logs.get('val_loss'))\n    self.val_acc.append(logs.get('val_acc'))\n\n    # Before plotting ensure at least 2 epochs have passed\n    if len(self.losses) > 1:\n\n      # Clear the previous plot\n      clear_output(wait=True)\n      N = np.arange(0, len(self.losses))\n\n      # You can chose the style of your preference\n      # print(plt.style.available) to see the available options\n      plt.style.use(\"seaborn\")\n\n      # Plot train loss, train acc, val loss and val acc against epochs passed\n      plt.figure()\n      plt.plot(N, self.losses, label = \"Training Loss\")\n      plt.plot(N, self.val_losses, label = \"Val loss\")\n      plt.title(\"Training Loss and Accuracy \")\n      plt.xlabel(\"Epoch #\")\n      plt.ylabel(\"Loss\")\n      plt.legend()\n      plt.show()\n\n      plt.plot(N, self.acc, label = \"Training Acc\")\n      plt.plot(N, self.val_acc, label = \"Val Acc\")\n      plt.title(\"Training Loss and Accuracy \")\n      plt.xlabel(\"Epoch #\")\n      plt.ylabel(\"Accuracy\")\n      plt.legend()\n      plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:05:15.166139Z","iopub.execute_input":"2024-06-23T16:05:15.166490Z","iopub.status.idle":"2024-06-23T16:05:15.181347Z","shell.execute_reply.started":"2024-06-23T16:05:15.166450Z","shell.execute_reply":"2024-06-23T16:05:15.180568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_losses = TrainingPlot()","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:05:15.182513Z","iopub.execute_input":"2024-06-23T16:05:15.183094Z","iopub.status.idle":"2024-06-23T16:05:15.199903Z","shell.execute_reply.started":"2024-06-23T16:05:15.183067Z","shell.execute_reply":"2024-06-23T16:05:15.198966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Resnet50 model","metadata":{}},{"cell_type":"markdown","source":"## Define model","metadata":{}},{"cell_type":"code","source":"def convolutional_block(input_tensor, kernel_size, filters, stage, block, s):\n    filters1, filters2, filters3 = filters\n\n    conv_name_base = 'conv' + str(stage) + block + '_branch'\n    seperable_name_base = 'seperable_conv' + str(stage) + block + '_branch'\n    bn_name_base = 'bn' + str(stage) + block + '_branch'\n    \n    x = Conv2D(filters1, (1, 1), strides=(s, s), name=conv_name_base + '2a')(input_tensor)\n    x = BatchNormalization(axis=3, name=bn_name_base + '2a')(x)\n    x = Activation('relu')(x)\n\n#     x = Conv2D(filters2, kernel_size, padding='same', name=conv_name_base + '2b')(x)\n    x = SeparableConv2D(filters2, kernel_size=kernel_size, padding=\"same\", name=seperable_name_base + '2b')(x)\n    x = BatchNormalization(axis=3, name=bn_name_base + '2b')(x)\n    x = Activation('relu')(x)\n\n    x = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')(x)\n    x = BatchNormalization(axis=3, name=bn_name_base + '2c')(x)\n\n    shortcut = Conv2D(filters3, (1, 1), strides=(s, s), name=conv_name_base + '1')(input_tensor)\n    shortcut = BatchNormalization(axis=3, name=bn_name_base + '1')(shortcut)\n\n    x = Add()([x, shortcut])\n    x = Activation('relu')(x)\n    return x","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:05:15.201325Z","iopub.execute_input":"2024-06-23T16:05:15.201607Z","iopub.status.idle":"2024-06-23T16:05:15.212890Z","shell.execute_reply.started":"2024-06-23T16:05:15.201584Z","shell.execute_reply":"2024-06-23T16:05:15.212010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def identity_block(input_tensor, kernel_size, filters, stage, block):\n    filters1, filters2, filters3 = filters\n\n    conv_name_base = 'conv' + str(stage) + block + '_branch'\n    seperable_name_base = 'seperable_conv' + str(stage) + block + '_branch'\n    bn_name_base = 'bn' + str(stage) + block + '_branch'\n\n    x = Conv2D(filters1, (1, 1), name=conv_name_base + '2a')(input_tensor)\n    x = BatchNormalization(axis=3, name=bn_name_base + '2a')(x)\n    x = Activation('relu')(x)\n\n#     x = Conv2D(filters2, kernel_size, padding='same', name=conv_name_base + '2b')(x)\n    x = SeparableConv2D(filters2, kernel_size=kernel_size, padding=\"same\", name=seperable_name_base + '2b')(x)\n    x = BatchNormalization(axis=3, name=bn_name_base + '2b')(x)\n    x = Activation('relu')(x)\n\n    x = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')(x)\n    x = BatchNormalization(axis=3, name=bn_name_base + '2c')(x)\n\n    x = Add()([x, input_tensor])\n    x = Activation('relu')(x)\n    return x","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:05:15.214090Z","iopub.execute_input":"2024-06-23T16:05:15.214889Z","iopub.status.idle":"2024-06-23T16:05:15.231809Z","shell.execute_reply.started":"2024-06-23T16:05:15.214858Z","shell.execute_reply":"2024-06-23T16:05:15.231045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def ResNet50():\n    # Define the input as a tensor with shape input_shape\n    inputs = Input((IMG_HEIGH, IMG_WIDTH, CHANNEL_SIZE))\n\n    # Zero-Padding\n    x = ZeroPadding2D((3, 3))(inputs)\n    \n    # Stage 1\n#     x = Conv2D(64, (7, 7), strides = (2, 2), name = 'conv1', kernel_initializer = 'glorot_uniform')(x)\n    x = SeparableConv2D(64, kernel_size=(7, 7), strides = (2, 2), name = 'separable_conv1')(x)\n    x = BatchNormalization(axis = 3, name = 'bn_conv1')(x)\n    x = Activation('relu')(x)\n    x = MaxPooling2D((3, 3), strides=(2, 2))(x)\n\n    # Stage 2\n    x = convolutional_block(x, kernel_size = (3,3), filters = [64, 64, 256], stage = 2, block='a', s = 1)\n    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')\n    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')\n\n    # Stage 3 \n    x = convolutional_block(x, kernel_size = (3,3), filters = [128, 128, 512], stage = 3, block='a', s = 2)\n    x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')\n    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')\n    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')\n\n    # Stage 4 \n    x = convolutional_block(x, kernel_size = (3,3), filters = [256, 256, 1024], stage = 4, block='a', s = 2)\n    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')\n    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')\n    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')\n    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')\n    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')\n\n    # Stage 5 \n    x = convolutional_block(x, kernel_size = (3,3), filters = [512, 512, 2048], stage = 5, block='a', s = 2)\n    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')\n    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')\n\n    x = AveragePooling2D(pool_size=(2,2), strides=(2,2), padding='same')(x)\n\n    # output layer\n    x = Flatten()(x)\n    x = Dense(10, activation='softmax', kernel_initializer = 'glorot_uniform')(x)\n    \n    model = Model(inputs = inputs, outputs = x, name='ResNet50')\n\n    return model","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:05:15.233077Z","iopub.execute_input":"2024-06-23T16:05:15.233401Z","iopub.status.idle":"2024-06-23T16:05:15.250185Z","shell.execute_reply.started":"2024-06-23T16:05:15.233373Z","shell.execute_reply":"2024-06-23T16:05:15.249377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## FLOP functions","metadata":{}},{"cell_type":"code","source":"def get_flops(model, batch_size=None):\n    if batch_size is None:\n        batch_size = 1\n\n    real_model = tf.function(model).get_concrete_function(tf.TensorSpec([batch_size] + list(model.inputs[0].shape[1:]), model.inputs[0].dtype))\n    frozen_func, graph_def = convert_variables_to_constants_v2_as_graph(real_model)\n\n    run_meta = tf.compat.v1.RunMetadata()\n    opts = tf.compat.v1.profiler.ProfileOptionBuilder.float_operation()\n    flops = tf.compat.v1.profiler.profile(graph=frozen_func.graph,\n                                            run_meta=run_meta, cmd='op', options=opts)\n    return flops.total_float_ops","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:05:15.251224Z","iopub.execute_input":"2024-06-23T16:05:15.251488Z","iopub.status.idle":"2024-06-23T16:05:15.263424Z","shell.execute_reply.started":"2024-06-23T16:05:15.251468Z","shell.execute_reply":"2024-06-23T16:05:15.262558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Calllbacks","metadata":{}},{"cell_type":"code","source":"# callbacks                                       # đảm bảo hiệu quả của quá trình training\nsave_path_base = '/kaggle/working/Resnet50.h5'\n\ncallbacks = [\n        tf.keras.callbacks.EarlyStopping(patience=30, monitor=\"val_loss\"),                                    # dừng quá trình huấn luyện sớm nếu không có sự cải thiện đáng kể trong độ đo val_loss sau 10 epochs\n        #tf.keras.callbacks.ModelCheckpoint(save_path1, verbose=2, save_best_only=True),                              # verbose=2 là hiển thị thông báo chi tiết khi lưu trạng thái tốt nhất của mô hình\n        tf.keras.callbacks.ReduceLROnPlateau(monitor='val_accuracy', factor=0.1, patience=3, min_lr=0.00001),          # monitor='train_accuracy': theo dõi val_loss trên tập kiểm tra; Khi hiệu suất không cải thiện sau 10 epoch,\n                                                                                                                    # tỷ lệ học sẽ được nhân với factor để giảm; tỷ lệ học giảm đến min_lr, nó sẽ không được giảm nữa\n        plot_losses\n            ]","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:05:15.264554Z","iopub.execute_input":"2024-06-23T16:05:15.264830Z","iopub.status.idle":"2024-06-23T16:05:15.274444Z","shell.execute_reply.started":"2024-06-23T16:05:15.264808Z","shell.execute_reply":"2024-06-23T16:05:15.273531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Self_Attention","metadata":{}},{"cell_type":"markdown","source":"## Compile and Train","metadata":{}},{"cell_type":"code","source":"model = ResNet50()\n\nBATCH_SIZE = 64\nmodel.summary()\n\nflops = get_flops(model, BATCH_SIZE)\nprint(f\"FLOPS: {flops/(10**12):.03f} G\")","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:05:15.275454Z","iopub.execute_input":"2024-06-23T16:05:15.275689Z","iopub.status.idle":"2024-06-23T16:05:22.291883Z","shell.execute_reply.started":"2024-06-23T16:05:15.275668Z","shell.execute_reply":"2024-06-23T16:05:22.290879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create model\noptimizer = tf.keras.optimizers.Adam(learning_rate = 0.00001, beta_1=0.9, beta_2 = 0.999)\nmodel.compile(loss = 'categorical_crossentropy',\n              optimizer = optimizer,\n              metrics = ['acc'])","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:05:22.293202Z","iopub.execute_input":"2024-06-23T16:05:22.293598Z","iopub.status.idle":"2024-06-23T16:05:22.309879Z","shell.execute_reply.started":"2024-06-23T16:05:22.293563Z","shell.execute_reply":"2024-06-23T16:05:22.308980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train\nHistory = model.fit(training_set_SF3D,\n                    batch_size = 64,\n                    validation_data = validation_set_SF3D,\n                    epochs = 30,\n                    callbacks = callbacks,\n                    verbose = 1,\n                    shuffle = True\n)\n\nmodel.save('/kaggle/working/ResNet50.h5')","metadata":{"execution":{"iopub.status.busy":"2024-06-23T16:05:22.310834Z","iopub.execute_input":"2024-06-23T16:05:22.311065Z","iopub.status.idle":"2024-06-23T17:23:46.613012Z","shell.execute_reply.started":"2024-06-23T16:05:22.311045Z","shell.execute_reply":"2024-06-23T17:23:46.612152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating a Model Prediction Function","metadata":{}},{"cell_type":"code","source":"from PIL import Image\nfrom IPython.display import display\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg","metadata":{"execution":{"iopub.status.busy":"2024-06-23T17:23:46.614683Z","iopub.execute_input":"2024-06-23T17:23:46.614980Z","iopub.status.idle":"2024-06-23T17:23:46.619172Z","shell.execute_reply.started":"2024-06-23T17:23:46.614955Z","shell.execute_reply":"2024-06-23T17:23:46.618356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Class Indeces Are :-->\", test_set_SF3D.class_indices)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T17:23:46.620332Z","iopub.execute_input":"2024-06-23T17:23:46.621243Z","iopub.status.idle":"2024-06-23T17:23:46.632258Z","shell.execute_reply.started":"2024-06-23T17:23:46.621215Z","shell.execute_reply":"2024-06-23T17:23:46.631417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def model_prediction(img_path, model, target_size=(IMG_HEIGH,IMG_WIDTH)):\n    # load and preprocess the image\n    img = Image.open(img_path)\n    img = img.resize(target_size)\n    img_array = np.array(img) / 255\n\n    # Expand the dimension of img array to match input shape\n    expand_dim = np.expand_dims(img_array, axis=0)\n\n    #predict\n    predictions = model.predict(expand_dim, verbose=False)\n    predicted_class_index = np.argmax(predictions)\n\n    #Map the label\n    predicted_label = next((k for k, v in test_set_SF3D.class_indices.items() if v == predicted_class_index), None)\n\n    return predicted_label","metadata":{"execution":{"iopub.status.busy":"2024-06-23T17:23:46.633326Z","iopub.execute_input":"2024-06-23T17:23:46.633577Z","iopub.status.idle":"2024-06-23T17:23:46.646713Z","shell.execute_reply.started":"2024-06-23T17:23:46.633554Z","shell.execute_reply":"2024-06-23T17:23:46.646023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset_SF3D = test_dataset_SF3D['ImgPath class'.split()].copy().reset_index(drop=True)\nprint(\"shape_of_the_valid_dataset\", test_dataset_SF3D.shape)\ntest_dataset_SF3D.sample(1)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T17:23:46.647716Z","iopub.execute_input":"2024-06-23T17:23:46.647963Z","iopub.status.idle":"2024-06-23T17:23:46.662034Z","shell.execute_reply.started":"2024-06-23T17:23:46.647943Z","shell.execute_reply":"2024-06-23T17:23:46.661208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prediction on the Valid Dataset","metadata":{}},{"cell_type":"code","source":"test_dataset_SF3D['prediction'] = test_dataset_SF3D['ImgPath'].progress_apply(lambda x:model_prediction(x, model))","metadata":{"execution":{"iopub.status.busy":"2024-06-23T17:23:46.663167Z","iopub.execute_input":"2024-06-23T17:23:46.663521Z","iopub.status.idle":"2024-06-23T17:26:47.000984Z","shell.execute_reply.started":"2024-06-23T17:23:46.663489Z","shell.execute_reply":"2024-06-23T17:26:47.000009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset_SF3D.sample(5)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T17:26:47.002190Z","iopub.execute_input":"2024-06-23T17:26:47.002471Z","iopub.status.idle":"2024-06-23T17:26:47.012641Z","shell.execute_reply.started":"2024-06-23T17:26:47.002445Z","shell.execute_reply":"2024-06-23T17:26:47.011721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Confusion matrix","metadata":{}},{"cell_type":"code","source":"y_test = list(test_dataset_SF3D['class'])\ny_test_pred = list(test_dataset_SF3D['prediction'])\ny_test[:5], y_test_pred[:5]","metadata":{"execution":{"iopub.status.busy":"2024-06-23T17:26:47.013972Z","iopub.execute_input":"2024-06-23T17:26:47.014387Z","iopub.status.idle":"2024-06-23T17:26:47.027032Z","shell.execute_reply.started":"2024-06-23T17:26:47.014351Z","shell.execute_reply":"2024-06-23T17:26:47.026124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install pandas tabulate\n\nimport pandas as pd\nfrom tabulate import tabulate\nfrom sklearn.metrics import classification_report\n\nreport = classification_report(y_test, y_test_pred, output_dict=True)\n\n# Chuyển từ điển vào DataFrame\ndf = pd.DataFrame(report).transpose()\ndf_filtered = df.drop('accuracy')\n\n# In kết quả dưới dạng bảng sử dụng tabulate\ntable = tabulate(df_filtered, headers='keys', tablefmt='fancy_grid')\nprint(table)","metadata":{"execution":{"iopub.status.busy":"2024-06-23T17:26:47.028101Z","iopub.execute_input":"2024-06-23T17:26:47.028465Z","iopub.status.idle":"2024-06-23T17:26:47.123027Z","shell.execute_reply.started":"2024-06-23T17:26:47.028434Z","shell.execute_reply":"2024-06-23T17:26:47.122186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c_m = confusion_matrix(y_test, y_test_pred)\n\n# Magic function that renders the figure in a jupyter notebook\n# instead of displaying a figure object\n%matplotlib inline\n\n\n# Setting default size of the plot\n# Setting default fontsize used in the plot\nplt.rcParams['figure.figsize'] = (10.0, 9.0)\nplt.rcParams['font.size'] = 20\n\n\n# Implementing visualization of Confusion Matrix\ndisplay_c_m = ConfusionMatrixDisplay(c_m, display_labels = activity_map_SF3D.values())\n\n\n# Plotting Confusion Matrix\n# Setting colour map to be used\ndisplay_c_m.plot(cmap='OrRd', xticks_rotation=25)\n# Other possible options for colour map are:\n# 'autumn_r', 'Blues', 'cool', 'Greens', 'Greys', 'PuRd', 'copper_r'\n\n\n# Setting fontsize for xticks and yticks\nplt.xticks(fontsize=10)\nplt.yticks(fontsize=10)\n\n\n# Giving name to the plot\nplt.title('Confusion Matrix', fontsize=24)\n\n\n\n# Showing the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-23T17:26:47.124066Z","iopub.execute_input":"2024-06-23T17:26:47.124328Z","iopub.status.idle":"2024-06-23T17:26:47.730540Z","shell.execute_reply.started":"2024-06-23T17:26:47.124305Z","shell.execute_reply":"2024-06-23T17:26:47.729632Z"},"trusted":true},"execution_count":null,"outputs":[]}]}