{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":24800,"databundleVersionId":1831594,"sourceType":"competition"},{"sourceId":1799839,"sourceType":"datasetVersion","datasetId":1069682},{"sourceId":1810938,"sourceType":"datasetVersion","datasetId":1075803}],"dockerImageVersionId":30762,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"raw","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\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 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":"2024-08-28T18:06:43.856067Z","iopub.execute_input":"2024-08-28T18:06:43.856959Z","iopub.status.idle":"2024-08-28T18:06:44.226491Z","shell.execute_reply.started":"2024-08-28T18:06:43.85692Z","shell.execute_reply":"2024-08-28T18:06:44.22553Z"}}},{"cell_type":"code","source":"# Display generic output messages\n!pip install colorama\n\n# Library for visualizing bounding boxes\n!pip install bbox-visualizer\n\n# Install ONNX library, will be used to convert from pytorch model to a tf model\n!pip install onnx onnxruntime onnxsim onnx-tf","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:09:43.146311Z","iopub.execute_input":"2024-09-04T16:09:43.146896Z","iopub.status.idle":"2024-09-04T16:10:31.474089Z","shell.execute_reply.started":"2024-09-04T16:09:43.14686Z","shell.execute_reply":"2024-09-04T16:10:31.473049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import bbox_visualizer as bbv\nimport cv2\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport shutil, os\nimport tensorflow as tf\nimport yaml\n\nfrom colorama import Fore, Back, Style\nfrom IPython.display import Image, display, clear_output\nfrom sklearn.model_selection import GroupShuffleSplit \nfrom tqdm.notebook import tqdm\nfrom typing import List","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:10:31.476144Z","iopub.execute_input":"2024-09-04T16:10:31.476516Z","iopub.status.idle":"2024-09-04T16:10:53.507168Z","shell.execute_reply.started":"2024-09-04T16:10:31.47648Z","shell.execute_reply":"2024-09-04T16:10:53.506066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !rm -rf /kaggle/working/data\n!mkdir -p /kaggle/working/data","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:10:53.508534Z","iopub.execute_input":"2024-09-04T16:10:53.509107Z","iopub.status.idle":"2024-09-04T16:10:54.546901Z","shell.execute_reply.started":"2024-09-04T16:10:53.509071Z","shell.execute_reply":"2024-09-04T16:10:54.545621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/input/vinbigdata-chest-xray-resized-png-1024x1024/* /kaggle/working/data/","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:10:54.550512Z","iopub.execute_input":"2024-09-04T16:10:54.550956Z","iopub.status.idle":"2024-09-04T16:16:51.602283Z","shell.execute_reply.started":"2024-09-04T16:10:54.550909Z","shell.execute_reply":"2024-09-04T16:16:51.601077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp /kaggle/input/vinbigdata-1024-image-dataset/vinbigdata/train.csv /kaggle/working/data/","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:16:51.604162Z","iopub.execute_input":"2024-09-04T16:16:51.604611Z","iopub.status.idle":"2024-09-04T16:16:52.764461Z","shell.execute_reply.started":"2024-09-04T16:16:51.604561Z","shell.execute_reply":"2024-09-04T16:16:52.762873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/working/data/train.csv')\ntrain_df #image_id,class_name,class_id,rad_id,x_min,y_min,x_max,y_max,width,height","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:16:52.766159Z","iopub.execute_input":"2024-09-04T16:16:52.766552Z","iopub.status.idle":"2024-09-04T16:16:52.952939Z","shell.execute_reply.started":"2024-09-04T16:16:52.766512Z","shell.execute_reply":"2024-09-04T16:16:52.951861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_sample = len(train_df[train_df.class_id != 14].image_id.unique())\nprint(f'patient sample: {patient_sample}')","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:16:52.954705Z","iopub.execute_input":"2024-09-04T16:16:52.955473Z","iopub.status.idle":"2024-09-04T16:16:52.975217Z","shell.execute_reply.started":"2024-09-04T16:16:52.955408Z","shell.execute_reply":"2024-09-04T16:16:52.974274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df[train_df.class_id!=14].reset_index(drop = True) \n#reset_index: index를 reset시키는데 사용. drop을통해 index로 세팅한 열을 dataframe내에서 삭제할지 여부 결정\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:16:52.9765Z","iopub.execute_input":"2024-09-04T16:16:52.97691Z","iopub.status.idle":"2024-09-04T16:16:53.004137Z","shell.execute_reply.started":"2024-09-04T16:16:52.976867Z","shell.execute_reply":"2024-09-04T16:16:53.00307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# temp_df = train_df[['image_id', 'class_name', 'class_id']]\n# temp_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:16:53.005183Z","iopub.execute_input":"2024-09-04T16:16:53.005475Z","iopub.status.idle":"2024-09-04T16:16:53.009438Z","shell.execute_reply.started":"2024-09-04T16:16:53.005439Z","shell.execute_reply":"2024-09-04T16:16:53.008382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# temp_df.class_id.value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:16:53.010732Z","iopub.execute_input":"2024-09-04T16:16:53.011121Z","iopub.status.idle":"2024-09-04T16:16:53.018895Z","shell.execute_reply.started":"2024-09-04T16:16:53.011084Z","shell.execute_reply":"2024-09-04T16:16:53.018078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/working/data/train/'\nimg_path = []\nfor i in train_df['image_id']:\n  img_path.append(path+i+'.png')\n\ntrain_df['img_path'] = img_path\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:16:53.023764Z","iopub.execute_input":"2024-09-04T16:16:53.024783Z","iopub.status.idle":"2024-09-04T16:16:53.07458Z","shell.execute_reply.started":"2024-09-04T16:16:53.024742Z","shell.execute_reply":"2024-09-04T16:16:53.07369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_df = train_df[['image_id', 'img_path']]\nimg_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:16:53.075986Z","iopub.execute_input":"2024-09-04T16:16:53.076751Z","iopub.status.idle":"2024-09-04T16:16:53.09291Z","shell.execute_reply.started":"2024-09-04T16:16:53.0767Z","shell.execute_reply":"2024-09-04T16:16:53.091846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#x_min, y_min, x_max, y_max normalization값으로 update\ntrain_df['x_min'] = train_df.apply(lambda row: (row.x_min) /row.width, axis =1)\ntrain_df['y_min'] = train_df.apply(lambda row: (row.y_min) /row.height, axis =1)\n\ntrain_df['x_max'] = train_df.apply(lambda row: (row.x_max) /row.width, axis =1)\ntrain_df['y_max'] = train_df.apply(lambda row: (row.y_max) /row.height, axis =1)\n#x_mid, y_mid가 추가\ntrain_df['x_mid'] = train_df.apply(lambda row: (row.x_min + row.x_max)/2,axis =1)\ntrain_df['y_mid'] = train_df.apply(lambda row: (row.y_min + row.y_max)/2, axis =1)\n#normalization된 width & height 추가\ntrain_df['w'] = train_df.apply(lambda row: (row.x_max - row.x_min), axis = 1)\ntrain_df['h'] = train_df.apply(lambda row: (row.y_max - row.y_min), axis = 1)\n#area 추가\ntrain_df['area'] = train_df['w']*train_df['h']\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:16:53.09448Z","iopub.execute_input":"2024-09-04T16:16:53.094808Z","iopub.status.idle":"2024-09-04T16:17:00.982577Z","shell.execute_reply.started":"2024-09-04T16:16:53.094772Z","shell.execute_reply":"2024-09-04T16:17:00.981456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['ori_x_min'] = (train_df['x_min']*1024).astype('int')\ntrain_df['ori_y_min'] = (train_df['y_min']*1024).astype('int')\ntrain_df['ori_x_max'] = (train_df['x_max']*1024).astype('int')\ntrain_df['ori_y_max'] = (train_df['y_max']*1024).astype('int')\n\ntrain_df['ori_x_mid'] = (train_df['x_mid']*1024).astype('int')\ntrain_df['ori_y_mid'] = (train_df['y_mid']*1024).astype('int')\ntrain_df['ori_w'] = (train_df['w']*1024).astype('int')\ntrain_df['ori_h'] = (train_df['h']*1024).astype('int')","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:00.983905Z","iopub.execute_input":"2024-09-04T16:17:00.984377Z","iopub.status.idle":"2024-09-04T16:17:01.003475Z","shell.execute_reply.started":"2024-09-04T16:17:00.984332Z","shell.execute_reply":"2024-09-04T16:17:01.002263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df = train_df.copy()\nfinal_df = final_df[['image_id', 'class_name', 'class_id', 'ori_x_min', 'ori_y_min', 'ori_x_max', 'ori_y_max', 'ori_x_mid', 'ori_y_mid', 'ori_w', 'ori_h','img_path']]\nfinal_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:01.004804Z","iopub.execute_input":"2024-09-04T16:17:01.005196Z","iopub.status.idle":"2024-09-04T16:17:01.048583Z","shell.execute_reply.started":"2024-09-04T16:17:01.005153Z","shell.execute_reply":"2024-09-04T16:17:01.047494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:01.050281Z","iopub.execute_input":"2024-09-04T16:17:01.050684Z","iopub.status.idle":"2024-09-04T16:17:01.054995Z","shell.execute_reply.started":"2024-09-04T16:17:01.050641Z","shell.execute_reply":"2024-09-04T16:17:01.054016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # sample 하나에 있는 bbox 정","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:01.05637Z","iopub.execute_input":"2024-09-04T16:17:01.056682Z","iopub.status.idle":"2024-09-04T16:17:01.068818Z","shell.execute_reply.started":"2024-09-04T16:17:01.056648Z","shell.execute_reply":"2024-09-04T16:17:01.067778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:01.070156Z","iopub.execute_input":"2024-09-04T16:17:01.070476Z","iopub.status.idle":"2024-09-04T16:17:01.078502Z","shell.execute_reply.started":"2024-09-04T16:17:01.070423Z","shell.execute_reply":"2024-09-04T16:17:01.077623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pandas as pd\n\n# # Giả sử bạn đã chuẩn bị DataFrame train_df với các thông số cần thiết\n\n# # Lưu DataFrame vào file CSV\n# csv_file_path = 'train_df_data.csv'  # Đặt tên file CSV mới\n# train_df.to_csv(csv_file_path, index=False)  # Lưu dữ liệu vào file CSV mà không lưu chỉ số\n\n# # Giải phóng bộ nhớ\n# # del train_df\n\n# # Xác nhận rằng DataFrame đã được giải phóng\n# import gc\n# gc.collect()  # Dọn dẹp bộ nhớ không còn sử dụng\n# # ","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:01.079857Z","iopub.execute_input":"2024-09-04T16:17:01.080624Z","iopub.status.idle":"2024-09-04T16:17:01.093051Z","shell.execute_reply.started":"2024-09-04T16:17:01.080579Z","shell.execute_reply":"2024-09-04T16:17:01.091951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pandas as pd\n\n# # Đọc file CSV vào DataFrame\n# df = pd.read_csv('/kaggle/working/train_df_data.csv')\n\n# # Truy xuất dữ liệu từ cột \"Tên\"\n# ten_column = df['Tên']\n\n# # In dữ liệu cột \"Tên\"\n# print(ten_column)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:01.094435Z","iopub.execute_input":"2024-09-04T16:17:01.094803Z","iopub.status.idle":"2024-09-04T16:17:01.109307Z","shell.execute_reply.started":"2024-09-04T16:17:01.094769Z","shell.execute_reply":"2024-09-04T16:17:01.108427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# final_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:01.110439Z","iopub.execute_input":"2024-09-04T16:17:01.11075Z","iopub.status.idle":"2024-09-04T16:17:01.122013Z","shell.execute_reply.started":"2024-09-04T16:17:01.110716Z","shell.execute_reply":"2024-09-04T16:17:01.121058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:01.123363Z","iopub.execute_input":"2024-09-04T16:17:01.123928Z","iopub.status.idle":"2024-09-04T16:17:01.135218Z","shell.execute_reply.started":"2024-09-04T16:17:01.123878Z","shell.execute_reply":"2024-09-04T16:17:01.134172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pandas as pd\n\n# # Giả sử bạn đã chuẩn bị DataFrame train_df với các thông số cần thiết\n\n# # Lưu DataFrame vào file CSV\n# csv_file_path = '/kaggle/working/data/new_train.csv'  # Đặt tên file CSV mới\n# train_df.to_csv(csv_file_path, index=False)  # Lưu dữ liệu vào file CSV mà không lưu chỉ số\n\n# # Giải phóng bộ nhớ\n# del train_df\n\n# # Xác nhận rằng DataFrame đã được giải phóng\n# import gc\n# gc.collect()  # Dọn dẹp bộ nhớ không còn sử dụng\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:01.136621Z","iopub.execute_input":"2024-09-04T16:17:01.138633Z","iopub.status.idle":"2024-09-04T16:17:01.146944Z","shell.execute_reply.started":"2024-09-04T16:17:01.138576Z","shell.execute_reply":"2024-09-04T16:17:01.14607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# uniq_id = pd.read_csv('/kaggle/working/train_df_data.csv')['img_path'].tolist()","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:01.148256Z","iopub.execute_input":"2024-09-04T16:17:01.148655Z","iopub.status.idle":"2024-09-04T16:17:01.160783Z","shell.execute_reply.started":"2024-09-04T16:17:01.14861Z","shell.execute_reply":"2024-09-04T16:17:01.159318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import cv2\n# import numpy as np\n# from tqdm import tqdm\n\n# # Assuming uniq_id is a list of file paths to your images\n# # uniq_id = train_df['img_path'].tolist()\n\n# image_batch = []\n# image_batch_labels = []\n\n# n_images = len(uniq_id)\n# for i in tqdm(range(n_images)):\n#     # Read the image\n#     image = cv2.imread(uniq_id[i])\n    \n#     # Check if the image was loaded successfully\n#     if image is None:\n#         print(f\"Warning: Unable to load image {uniq_id[i]}. Skipping this file.\")\n#         continue\n    \n#     # Convert the image from BGR to RGB\n#     image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n#     image_batch.append(image)\n    \n#     # Generate a random label\n#     label_temp = list(np.floor(np.random.rand(1) * 2.99).astype(int))[0]\n#     if label_temp == 0:\n#         label = [1, 0, 0]\n#     elif label_temp == 1:\n#         label = [0, 1, 0]\n#     else:  # label_temp == 2\n#         label = [0, 0, 1]\n    \n#     image_batch_labels.append(label)\n\n# # Convert image_batch to numpy array\n# image_batch = np.array(image_batch)\n# # Convert image_batch_labels to numpy array\n# image_batch_labels = np.array(image_batch_labels)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:01.162359Z","iopub.execute_input":"2024-09-04T16:17:01.163595Z","iopub.status.idle":"2024-09-04T16:17:01.172433Z","shell.execute_reply.started":"2024-09-04T16:17:01.163492Z","shell.execute_reply":"2024-09-04T16:17:01.171532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def rand_bbox(size, lamb):\n#     W = size[0]\n#     H = size[1]\n#     cut_rat = np.sqrt(1. - lamb)\n#     cut_w = np.int(W * cut_rat)\n#     cut_h = np.int(H * cut_rat)\n\n#     # uniform\n#     cx = np.random.randint(W)\n#     cy = np.random.randint(H)\n\n#     bbx1 = np.clip(cx - cut_w // 2, 0, W)\n#     bby1 = np.clip(cy - cut_h // 2, 0, H)\n#     bbx2 = np.clip(cx + cut_w // 2, 0, W)\n#     bby2 = np.clip(cy + cut_h // 2, 0, H)\n\n#     return bbx1, bby1, bbx2, bby2","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:01.173765Z","iopub.execute_input":"2024-09-04T16:17:01.174299Z","iopub.status.idle":"2024-09-04T16:17:01.187965Z","shell.execute_reply.started":"2024-09-04T16:17:01.174252Z","shell.execute_reply":"2024-09-04T16:17:01.187283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def generate_cutmix_image(image_batch, image_batch_labels, beta):\n#     \"\"\" Generate a CutMix augmented image from a batch \n#     Args:\n#         - image_batch: a batch of input images\n#         - image_batch_labels: labels corresponding to the image batch\n#         - beta: a parameter of Beta distribution.\n#     Returns:\n#         - CutMix image batch, updated labels\n#     \"\"\"\n#     # generate mixed sample\n#     lam = np.random.beta(beta, beta)\n#     rand_index = np.random.permutation(len(image_batch))\n#     target_a = image_batch_labels\n#     target_b = image_batch_labels[rand_index]\n#     bbx1, bby1, bbx2, bby2 = rand_bbox(image_batch[0].shape, lam)\n#     image_batch_updated = image_batch.copy()\n#     image_batch_updated[:, bbx1:bbx2, bby1:bby2, :] = image_batch[rand_index, bbx1:bbx2, bby1:bby2, :]\n    \n#     # adjust lambda to exactly match pixel ratio\n#     lam = 1 - ((bbx2 - bbx1) * (bby2 - bby1) / (image_batch.shape[1] * image_batch.shape[2]))\n#     label = target_a * lam + target_b * (1. - lam)\n    \n#     return image_batch_updated, label","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:01.189068Z","iopub.execute_input":"2024-09-04T16:17:01.189385Z","iopub.status.idle":"2024-09-04T16:17:01.20077Z","shell.execute_reply.started":"2024-09-04T16:17:01.189347Z","shell.execute_reply":"2024-09-04T16:17:01.200078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import numpy as np\n\n# # Kiểm tra kích thước của image_batch\n# print(f\"Kích thước của image_batch: {image_batch.shape}\")\n# print(f\"Kích thước của image_batch_labels: {image_batch_labels.shape}\")\n\n# # Nếu mảng không rỗng, tiếp tục hiển thị hình ảnh\n# if image_batch.size > 0 and image_batch_labels.size > 0:\n#     num_images_to_show = min(5, len(image_batch))  # Đảm bảo không vượt quá số lượng hình ảnh trong mảng\n#     for i in range(num_images_to_show):\n#         plt.figure(figsize=(2, 2))\n#         plt.imshow(image_batch[i])\n#         plt.title(f\"Label: {image_batch_labels[i]}\")\n#         plt.axis('off')\n#         plt.show()\n# else:\n#     print(\"Mảng image_batch hoặc image_batch_labels rỗng!\")\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:01.202031Z","iopub.execute_input":"2024-09-04T16:17:01.202728Z","iopub.status.idle":"2024-09-04T16:17:01.217636Z","shell.execute_reply.started":"2024-09-04T16:17:01.202683Z","shell.execute_reply":"2024-09-04T16:17:01.216756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:01.218584Z","iopub.execute_input":"2024-09-04T16:17:01.218836Z","iopub.status.idle":"2024-09-04T16:17:01.24079Z","shell.execute_reply.started":"2024-09-04T16:17:01.218808Z","shell.execute_reply":"2024-09-04T16:17:01.23975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport cv2\nimport numpy as np\nfrom tqdm import tqdm\nimport os\n\n# Đọc dữ liệu từ final_df (giả sử final_df đã được định nghĩa trước đó)\nfiltered_df = final_df[final_df['class_id'].isin([1, 12])].copy()\n\n# Hàm để cập nhật bounding box sau khi tăng cường\ndef update_bounding_box(bbox, transform_matrix):\n    \"\"\"\n    Cập nhật bounding box sau khi áp dụng ma trận biến đổi.\n    \n    Args:\n        bbox (list): Danh sách chứa các tọa độ bounding box [x_min, y_min, x_max, y_max].\n        transform_matrix (np.array): Ma trận biến đổi affine 2x3.\n    \n    Returns:\n        tuple: Tọa độ bounding box mới [x_min, y_min, x_max, y_max].\n    \"\"\"\n    ori_x_min, ori_y_min, ori_x_max, ori_y_max = bbox\n    \n    # Tạo mảng các điểm của bounding box\n    points = np.array([\n        [ori_x_min, ori_y_min],  # Góc trên bên trái\n        [ori_x_max, ori_y_min],  # Góc trên bên phải\n        [ori_x_max, ori_y_max],  # Góc dưới bên phải\n        [ori_x_min, ori_y_max]   # Góc dưới bên trái\n    ])\n    \n    # Chuyển đổi điểm bằng ma trận biến đổi\n    # Thay đổi transform_matrix thành dạng 3x3 để dễ tính toán\n    # Thêm hàng thứ ba [0, 0, 1] vào ma trận biến đổi để phù hợp với tọa độ đồng nhất\n    transform_matrix_3x3 = np.vstack([transform_matrix, [0, 0, 1]])\n    \n    # Thêm hàng thứ ba [1] vào các điểm để phù hợp với tọa độ đồng nhất\n    points_homogeneous = np.hstack([points, np.ones((points.shape[0], 1))])\n    \n    # Áp dụng ma trận biến đổi\n    transformed_points = np.dot(points_homogeneous, transform_matrix_3x3.T)\n    \n    # Chuyển đổi trở lại tọa độ không đồng nhất\n    transformed_points[:, 0] /= transformed_points[:, 2]\n    transformed_points[:, 1] /= transformed_points[:, 2]\n    \n    # Tìm giá trị min và max cho các trục x và y\n    ori_x_min = transformed_points[:, 0].min()\n    ori_y_min = transformed_points[:, 1].min()\n    ori_x_max = transformed_points[:, 0].max()\n    ori_y_max = transformed_points[:, 1].max()\n    \n    return ori_x_min, ori_y_min, ori_x_max, ori_y_max\n\n# Thực hiện tăng cường dữ liệu và lưu thông số mới vào CSV\nnew_rows = []\n\nfor index, row in tqdm(filtered_df.iterrows(), total=filtered_df.shape[0]):\n    image_path = row['img_path']\n    bbox = [row['ori_x_min'], row['ori_y_min'], row['ori_x_max'], row['ori_y_max']]\n    image_id = row['image_id']\n    class_name = row['class_name']\n    class_id = row['class_id']\n    ori_x_mid = row['ori_x_mid']\n    ori_y_mid = row['ori_y_mid']\n    ori_w = row['ori_w']\n    ori_h = row['ori_h']\n    \n    # Đọc ảnh\n    image = cv2.imread(image_path)\n    if image is None:\n        continue\n    \n    # Ví dụ về một biến đổi: xoay ảnh 90 độ\n    height, width = image.shape[:2]\n    M = cv2.getRotationMatrix2D((width / 2, height / 2), 90, 1)\n    rotated_image = cv2.warpAffine(image, M, (width, height))\n    \n    # Cập nhật bounding box\n    new_bbox = update_bounding_box(bbox, M)\n    \n    # Lưu ảnh mới\n    new_image_path = os.path.splitext(image_path)[0] + '_rotated.png'\n    cv2.imwrite(new_image_path, rotated_image)\n    \n    # Tạo một dòng mới với thông số cập nhật\n    new_row = row.copy()\n    new_row['img_path'] = new_image_path\n    new_row['ori_x_min'], new_row['ori_y_min'], new_row['ori_x_max'], new_row['ori_y_max'] = new_bbox\n    new_row['image_id'] = image_id + '_rotated'\n    new_row['class_name'] = class_name\n    new_row['class_id'] = class_id\n    new_row['ori_x_mid'] = ori_x_mid\n    new_row['ori_y_mid'] = ori_y_mid\n    new_row['ori_w'] = ori_w\n    new_row['ori_h'] = ori_h\n    new_rows.append(new_row)\n\n# Tạo DataFrame từ các dòng mới\nnew_df = pd.DataFrame(new_rows)\n\n# # Gộp dữ liệu mới vào DataFrame gốc\n# augmented_df = pd.concat([final_df, new_df], ignore_index=True)\n\n# # Lưu DataFrame đã cập nhật vào file CSV mới\n# augmented_df.to_csv('/kaggle/working/data/train_augmented.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:01.242551Z","iopub.execute_input":"2024-09-04T16:17:01.242975Z","iopub.status.idle":"2024-09-04T16:17:40.478466Z","shell.execute_reply.started":"2024-09-04T16:17:01.242931Z","shell.execute_reply":"2024-09-04T16:17:40.477341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Gộp dữ liệu mới vào DataFrame gốc\naugmented_df = pd.concat([final_df, new_df], ignore_index=True)\naugmented_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:40.480063Z","iopub.execute_input":"2024-09-04T16:17:40.480849Z","iopub.status.idle":"2024-09-04T16:17:40.515162Z","shell.execute_reply.started":"2024-09-04T16:17:40.480798Z","shell.execute_reply":"2024-09-04T16:17:40.513727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"augmented_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:40.528677Z","iopub.execute_input":"2024-09-04T16:17:40.529532Z","iopub.status.idle":"2024-09-04T16:17:40.561482Z","shell.execute_reply.started":"2024-09-04T16:17:40.52948Z","shell.execute_reply":"2024-09-04T16:17:40.560434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:40.562669Z","iopub.execute_input":"2024-09-04T16:17:40.563061Z","iopub.status.idle":"2024-09-04T16:17:40.582064Z","shell.execute_reply.started":"2024-09-04T16:17:40.562992Z","shell.execute_reply":"2024-09-04T16:17:40.581068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def flip_image(image_path, bbox):\n    \"\"\"\n    Lật ảnh theo chiều ngang và cập nhật bounding box.\n\n    Args:\n        image_path (str): Đường dẫn đến ảnh gốc.\n        bbox (list): Danh sách chứa các tọa độ bounding box [x_min, y_min, x_max, y_max].\n\n    Returns:\n        tuple: Đường dẫn ảnh mới và bounding box đã được cập nhật.\n    \"\"\"\n    # Đọc ảnh gốc\n    image = cv2.imread(image_path)\n    if image is None:\n        return None, None\n\n    # Lật ảnh theo chiều ngang\n    flipped_image = cv2.flip(image, 1)\n\n    # Cập nhật bounding box\n    width = image.shape[1]\n    flipped_bbox = [\n        width - bbox[2], bbox[1],  # x_max -> new_x_min, y_min\n        width - bbox[0], bbox[3]   # x_min -> new_x_max, y_max\n    ]\n\n    # Lưu ảnh mới\n    new_image_path = os.path.splitext(image_path)[0] + '_flip.png'\n    cv2.imwrite(new_image_path, flipped_image)\n    \n    return new_image_path, flipped_bbox\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:40.583528Z","iopub.execute_input":"2024-09-04T16:17:40.584416Z","iopub.status.idle":"2024-09-04T16:17:40.592442Z","shell.execute_reply.started":"2024-09-04T16:17:40.584364Z","shell.execute_reply":"2024-09-04T16:17:40.591431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def flip_images_in_df(df):\n    \"\"\"\n    Lật ảnh cho các lớp cụ thể và cập nhật thông tin bounding box trong DataFrame.\n\n    Args:\n        df (pd.DataFrame): DataFrame chứa thông tin về ảnh và bounding box.\n\n    Returns:\n        pd.DataFrame: DataFrame đã được cập nhật với ảnh lật và bounding box mới.\n    \"\"\"\n    filtered_df = final_df[final_df['class_id'].isin([1, 12])].copy()\n    new_rows = []\n\n    for index, row in tqdm(df.iterrows(), total=df.shape[0]):\n        image_path = row['img_path']\n        bbox = [row['ori_x_min'], row['ori_y_min'], row['ori_x_max'], row['ori_y_max']]\n        image_id = row['image_id']\n        class_name = row['class_name']\n        class_id = row['class_id']\n        ori_x_mid = row['ori_x_mid']\n        ori_y_mid = row['ori_y_mid']\n        ori_w = row['ori_w']\n        ori_h = row['ori_h']\n        \n        # Lật ảnh và cập nhật bounding box\n        new_image_path, new_bbox = flip_image(image_path, bbox)\n        \n        if new_image_path is None:\n            continue\n        \n        # Tạo một dòng mới với thông số cập nhật\n        new_row = row.copy()\n        new_row['img_path'] = new_image_path\n        new_row['ori_x_min'], new_row['ori_y_min'], new_row['ori_x_max'], new_row['ori_y_max'] = new_bbox\n        new_row['image_id'] = image_id + '_flip'\n        new_row['class_name'] = class_name\n        new_row['class_id'] = class_id\n        new_row['ori_x_mid'] = ori_x_mid\n        new_row['ori_y_mid'] = ori_y_mid\n        new_row['ori_w'] = ori_w\n        new_row['ori_h'] = ori_h\n        new_rows.append(new_row)\n\n    # Tạo DataFrame từ các dòng mới\n    new_df = pd.DataFrame(new_rows)\n\n    return new_df\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:40.593728Z","iopub.execute_input":"2024-09-04T16:17:40.594165Z","iopub.status.idle":"2024-09-04T16:17:40.605842Z","shell.execute_reply.started":"2024-09-04T16:17:40.594115Z","shell.execute_reply":"2024-09-04T16:17:40.604864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_df = final_df[final_df['class_id'].isin([1, 12])].copy()","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:40.607107Z","iopub.execute_input":"2024-09-04T16:17:40.607844Z","iopub.status.idle":"2024-09-04T16:17:40.623351Z","shell.execute_reply.started":"2024-09-04T16:17:40.607798Z","shell.execute_reply":"2024-09-04T16:17:40.622574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new1_df = flip_images_in_df(filtered_df)","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:17:40.624967Z","iopub.execute_input":"2024-09-04T16:17:40.62538Z","iopub.status.idle":"2024-09-04T16:18:14.825771Z","shell.execute_reply.started":"2024-09-04T16:17:40.625313Z","shell.execute_reply":"2024-09-04T16:18:14.824717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"augmented_df = pd.concat([augmented_df, new1_df], ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:18:14.827143Z","iopub.execute_input":"2024-09-04T16:18:14.827457Z","iopub.status.idle":"2024-09-04T16:18:14.834119Z","shell.execute_reply.started":"2024-09-04T16:18:14.827424Z","shell.execute_reply":"2024-09-04T16:18:14.833177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport cv2\nimport numpy as np\nimport imgaug.augmenters as iaa\nfrom tqdm import tqdm\nimport os\n\ndef zoom_image(image_path, bbox, zoom_factor):\n    \"\"\"\n    Zoom ảnh và cập nhật bounding box.\n\n    Args:\n        image_path (str): Đường dẫn đến ảnh gốc.\n        bbox (list): Danh sách chứa các tọa độ bounding box [x_min, y_min, x_max, y_max].\n        zoom_factor (float): Hệ số zoom.\n\n    Returns:\n        tuple: Đường dẫn ảnh mới và bounding box đã được cập nhật.\n    \"\"\"\n    # Đọc ảnh gốc\n    image = cv2.imread(image_path)\n    if image is None:\n        return None, None\n\n    # Tăng cường zoom\n    augment_img_zoom = iaa.Affine(scale=(zoom_factor))\n    zoomed_image = augment_img_zoom.augment_image(image)\n\n    # Cập nhật bounding box\n    height, width = image.shape[:2]\n    new_width, new_height = int(width * zoom_factor), int(height * zoom_factor)\n\n    # Tính toán offset để điều chỉnh bounding box\n    offset_x = (new_width - width) / 2\n    offset_y = (new_height - height) / 2\n\n    new_bbox = [\n        bbox[0] * zoom_factor - offset_x,  # x_min\n        bbox[1] * zoom_factor - offset_y,  # y_min\n        bbox[2] * zoom_factor - offset_x,  # x_max\n        bbox[3] * zoom_factor - offset_y   # y_max\n    ]\n\n    # Lưu ảnh mới\n    new_image_path = os.path.splitext(image_path)[0] + '_zoom.png'\n    cv2.imwrite(new_image_path, zoomed_image)\n    \n    return new_image_path, new_bbox\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:18:14.835287Z","iopub.execute_input":"2024-09-04T16:18:14.83564Z","iopub.status.idle":"2024-09-04T16:18:15.483178Z","shell.execute_reply.started":"2024-09-04T16:18:14.835593Z","shell.execute_reply":"2024-09-04T16:18:15.482136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport cv2\nimport numpy as np\nimport imgaug.augmenters as iaa\nfrom tqdm import tqdm\nimport os\n\ndef zoom_image(image_path, bbox, zoom_factor):\n    \"\"\"\n    Zoom ảnh và cập nhật bounding box.\n\n    Args:\n        image_path (str): Đường dẫn đến ảnh gốc.\n        bbox (list): Danh sách chứa các tọa độ bounding box [x_min, y_min, x_max, y_max].\n        zoom_factor (float): Hệ số zoom.\n\n    Returns:\n        tuple: Đường dẫn ảnh mới và bounding box đã được cập nhật.\n    \"\"\"\n    # Đọc ảnh gốc\n    image = cv2.imread(image_path)\n    if image is None:\n        return None, None\n\n    # Tăng cường zoom\n    augment_img_zoom = iaa.Affine(scale=(zoom_factor))\n    zoomed_image = augment_img_zoom.augment_image(image)\n\n    # Cập nhật bounding box\n    height, width = image.shape[:2]\n    new_width, new_height = int(width * zoom_factor), int(height * zoom_factor)\n\n    # Tính toán offset để điều chỉnh bounding box\n    offset_x = (new_width - width) / 2\n    offset_y = (new_height - height) / 2\n\n    new_bbox = [\n        bbox[0] * zoom_factor - offset_x,  # x_min\n        bbox[1] * zoom_factor - offset_y,  # y_min\n        bbox[2] * zoom_factor - offset_x,  # x_max\n        bbox[3] * zoom_factor - offset_y   # y_max\n    ]\n\n    # Lưu ảnh mới\n    new_image_path = os.path.splitext(image_path)[0] + '_zoom.png'\n    cv2.imwrite(new_image_path, zoomed_image)\n    \n    return new_image_path, new_bbox\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:18:15.48451Z","iopub.execute_input":"2024-09-04T16:18:15.485372Z","iopub.status.idle":"2024-09-04T16:18:15.495323Z","shell.execute_reply.started":"2024-09-04T16:18:15.485321Z","shell.execute_reply":"2024-09-04T16:18:15.494272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def zoom_images_in_df(df, zoom_factor):\n    \"\"\"\n    Thực hiện zoom cho các ảnh thuộc lớp cụ thể và cập nhật thông tin bounding box trong DataFrame.\n\n    Args:\n        df (pd.DataFrame): DataFrame chứa thông tin về ảnh và bounding box.\n        zoom_factor (float): Hệ số zoom.\n\n    Returns:\n        pd.DataFrame: DataFrame đã được cập nhật với ảnh zoom và bounding box mới.\n    \"\"\"\n    new_rows = []\n\n    for index, row in tqdm(df.iterrows(), total=df.shape[0]):\n        image_path = row['img_path']\n        bbox = [row['ori_x_min'], row['ori_y_min'], row['ori_x_max'], row['ori_y_max']]\n        image_id = row['image_id']\n        class_name = row['class_name']\n        class_id = row['class_id']\n        ori_x_mid = row['ori_x_mid']\n        ori_y_mid = row['ori_y_mid']\n        ori_w = row['ori_w']\n        ori_h = row['ori_h']\n        \n        # Zoom ảnh và cập nhật bounding box\n        new_image_path, new_bbox = zoom_image(image_path, bbox, zoom_factor)\n        \n        if new_image_path is None:\n            continue\n        \n        # Tạo một dòng mới với thông số cập nhật\n        new_row = row.copy()\n        new_row['img_path'] = new_image_path\n        new_row['ori_x_min'], new_row['ori_y_min'], new_row['ori_x_max'], new_row['ori_y_max'] = new_bbox\n        new_row['image_id'] = image_id + '_zoom'\n        new_row['class_name'] = class_name\n        new_row['class_id'] = class_id\n        new_row['ori_x_mid'] = ori_x_mid\n        new_row['ori_y_mid'] = ori_y_mid\n        new_row['ori_w'] = ori_w\n        new_row['ori_h'] = ori_h\n        new_rows.append(new_row)\n\n    # Tạo DataFrame từ các dòng mới\n    new_df = pd.DataFrame(new_rows)\n\n    return new_df\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:18:15.49694Z","iopub.execute_input":"2024-09-04T16:18:15.497654Z","iopub.status.idle":"2024-09-04T16:18:15.51516Z","shell.execute_reply.started":"2024-09-04T16:18:15.497564Z","shell.execute_reply":"2024-09-04T16:18:15.514229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_df = final_df[final_df['class_id'].isin([1, 12])].copy()\n\n# Áp dụng hàm zoom ảnh cho DataFrame đã lọc với hệ số zoom là 10% (1.1)\nnew3_df = zoom_images_in_df(filtered_df, zoom_factor=1.1)","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:18:15.516322Z","iopub.execute_input":"2024-09-04T16:18:15.516611Z","iopub.status.idle":"2024-09-04T16:18:52.55896Z","shell.execute_reply.started":"2024-09-04T16:18:15.51658Z","shell.execute_reply":"2024-09-04T16:18:52.557866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport os\n\ndef clahe_image(image_list):\n    \"\"\"\n    Áp dụng CLAHE cho ảnh và lưu ảnh đã được tăng cường.\n\n    Args:\n        image_list (list): Danh sách các đường dẫn đến các ảnh cần áp dụng CLAHE.\n    \"\"\"\n    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))\n    \n    for path in image_list:\n        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n        if img is None:\n            continue\n        clahe_img = clahe.apply(img)\n        img_name = os.path.basename(path).split('.')[0]\n        new_image_path = f'{os.path.splitext(path)[0]}_clahe.png'\n        cv2.imwrite(new_image_path, clahe_img)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:18:52.560507Z","iopub.execute_input":"2024-09-04T16:18:52.561279Z","iopub.status.idle":"2024-09-04T16:18:52.568101Z","shell.execute_reply.started":"2024-09-04T16:18:52.561225Z","shell.execute_reply":"2024-09-04T16:18:52.567088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom tqdm import tqdm\n\n# Lọc DataFrame chỉ chứa các lớp có class_id là 1 và 12\nfiltered_df = final_df[final_df['class_id'].isin([1, 12])].copy()\n\n# Tạo danh sách các đường dẫn ảnh\nimage_list = filtered_df['img_path'].tolist()\n\n# Thực hiện CLAHE cho các ảnh\nclahe_image(image_list)\n\n# Cập nhật DataFrame với ảnh mới đã áp dụng CLAHE\ndef update_df_with_clahe(df):\n    new_rows = []\n\n    for index, row in tqdm(df.iterrows(), total=df.shape[0]):\n        image_path = row['img_path']\n        img_name = os.path.basename(image_path).split('.')[0]\n        new_image_path = f'{os.path.splitext(image_path)[0]}_clahe.png'\n\n        # Tạo một dòng mới với đường dẫn ảnh CLAHE\n        new_row = row.copy()\n        new_row['img_path'] = new_image_path\n        new_row['image_id'] = row['image_id'] + '_clahe'\n        new_rows.append(new_row)\n\n    # Tạo DataFrame từ các dòng mới\n    new_df = pd.DataFrame(new_rows)\n    \n    return new_df\n\n# Áp dụng hàm cập nhật DataFrame\nnew4_df = update_df_with_clahe(filtered_df)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:18:52.569429Z","iopub.execute_input":"2024-09-04T16:18:52.569865Z","iopub.status.idle":"2024-09-04T16:19:11.187204Z","shell.execute_reply.started":"2024-09-04T16:18:52.569815Z","shell.execute_reply":"2024-09-04T16:19:11.186213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new4_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:19:11.188429Z","iopub.execute_input":"2024-09-04T16:19:11.188798Z","iopub.status.idle":"2024-09-04T16:19:11.205503Z","shell.execute_reply.started":"2024-09-04T16:19:11.188764Z","shell.execute_reply":"2024-09-04T16:19:11.204471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport os\n\ndef equ_image(image_list):\n    \"\"\"\n    Áp dụng EqualizeHist cho ảnh và lưu ảnh đã được tăng cường.\n\n    Args:\n        image_list (list): Danh sách các đường dẫn đến các ảnh cần áp dụng EqualizeHist.\n    \"\"\"\n    for path in image_list:\n        img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n        if img is None:\n            continue\n        equ_img = cv2.equalizeHist(img)\n        img_name = os.path.basename(path).split('.')[0]\n        new_image_path = f'{os.path.splitext(path)[0]}_equ.png'\n        cv2.imwrite(new_image_path, equ_img)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:19:11.206923Z","iopub.execute_input":"2024-09-04T16:19:11.207273Z","iopub.status.idle":"2024-09-04T16:19:11.218959Z","shell.execute_reply.started":"2024-09-04T16:19:11.207238Z","shell.execute_reply":"2024-09-04T16:19:11.218157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nfrom tqdm import tqdm\n\n# Lọc DataFrame chỉ chứa các lớp có class_id là 1 và 12\nfiltered_df = final_df[final_df['class_id'].isin([1, 12,5,6,2,4])].copy()\n\n# Tạo danh sách các đường dẫn ảnh\nimage_list = filtered_df['img_path'].tolist()\n\n# Thực hiện EqualizeHist cho các ảnh\nequ_image(image_list)\n\n# Cập nhật DataFrame với ảnh mới đã áp dụng EqualizeHist\ndef update_df_with_equ(df):\n    new_rows = []\n\n    for index, row in tqdm(df.iterrows(), total=df.shape[0]):\n        image_path = row['img_path']\n        img_name = os.path.basename(image_path).split('.')[0]\n        new_image_path = f'{os.path.splitext(image_path)[0]}_equ.png'\n\n        # Tạo một dòng mới với đường dẫn ảnh EqualizeHist\n        new_row = row.copy()\n        new_row['img_path'] = new_image_path\n        new_row['image_id'] = row['image_id'] + '_equ'\n        new_rows.append(new_row)\n\n    # Tạo DataFrame từ các dòng mới\n    new_df = pd.DataFrame(new_rows)\n    \n    return new_df\n\n# Áp dụng hàm cập nhật DataFrame\n\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:19:11.220358Z","iopub.execute_input":"2024-09-04T16:19:11.220984Z","iopub.status.idle":"2024-09-04T16:21:40.329601Z","shell.execute_reply.started":"2024-09-04T16:19:11.220939Z","shell.execute_reply":"2024-09-04T16:21:40.328706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new2_df=update_df_with_equ(filtered_df)","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:21:40.331002Z","iopub.execute_input":"2024-09-04T16:21:40.331745Z","iopub.status.idle":"2024-09-04T16:21:41.396423Z","shell.execute_reply.started":"2024-09-04T16:21:40.33169Z","shell.execute_reply":"2024-09-04T16:21:41.395353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new2_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:21:41.39769Z","iopub.execute_input":"2024-09-04T16:21:41.397998Z","iopub.status.idle":"2024-09-04T16:21:41.415608Z","shell.execute_reply.started":"2024-09-04T16:21:41.397966Z","shell.execute_reply":"2024-09-04T16:21:41.414552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install albumentations\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:21:41.417164Z","iopub.execute_input":"2024-09-04T16:21:41.417629Z","iopub.status.idle":"2024-09-04T16:21:54.678478Z","shell.execute_reply.started":"2024-09-04T16:21:41.417579Z","shell.execute_reply":"2024-09-04T16:21:54.677331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport os\nimport pandas as pd\nfrom tqdm import tqdm\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\n\ndef color_jitter_image(image_list):\n    \"\"\"\n    Áp dụng Color Jitter cho ảnh và lưu ảnh đã được tăng cường.\n\n    Args:\n        image_list (list): Danh sách các đường dẫn đến các ảnh cần áp dụng Color Jitter.\n    \"\"\"\n    # Khởi tạo phương pháp Color Jitter\n    color_jitter = A.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1)\n\n    for path in image_list:\n        img = cv2.imread(path)\n        if img is None:\n            continue\n        \n        # Chuyển đổi ảnh từ BGR (OpenCV) sang RGB (Albumentations)\n        img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\n        # Áp dụng Color Jitter\n        augmented = color_jitter(image=img_rgb)\n        jittered_img = augmented['image']\n\n        # Chuyển đổi ảnh trở lại BGR (OpenCV)\n        jittered_img_bgr = cv2.cvtColor(jittered_img, cv2.COLOR_RGB2BGR)\n\n        # Lưu ảnh đã tăng cường\n        img_name = os.path.basename(path).split('.')[0]\n        new_image_path = f'{os.path.splitext(path)[0]}_color_jitter.png'\n        cv2.imwrite(new_image_path, jittered_img_bgr)\n\ndef update_df_with_color_jitter(df):\n    \"\"\"\n    Cập nhật DataFrame với các đường dẫn ảnh mới đã áp dụng Color Jitter.\n\n    Args:\n        df (pd.DataFrame): DataFrame chứa thông tin ảnh gốc.\n\n    Returns:\n        pd.DataFrame: DataFrame đã được cập nhật với thông tin về ảnh đã áp dụng Color Jitter.\n    \"\"\"\n    new_rows = []\n\n    for index, row in tqdm(df.iterrows(), total=df.shape[0]):\n        image_path = row['img_path']\n        img_name = os.path.basename(image_path).split('.')[0]\n        new_image_path = f'{os.path.splitext(image_path)[0]}_color_jitter.png'\n\n        # Tạo một dòng mới với đường dẫn ảnh Color Jitter\n        new_row = row.copy()\n        new_row['img_path'] = new_image_path\n        new_row['image_id'] = row['image_id'] + '_color_jitter'\n        new_rows.append(new_row)\n\n    # Tạo DataFrame từ các dòng mới\n    new_df = pd.DataFrame(new_rows)\n    \n    return new_df\n\n# Lọc DataFrame chỉ chứa các lớp có class_id là 1 và 12\nfiltered_df = final_df[final_df['class_id'].isin([1, 12])].copy()\n\n# Tạo danh sách các đường dẫn ảnh\nimage_list = filtered_df['img_path'].tolist()\n\n# Thực hiện Color Jitter cho các ảnh\ncolor_jitter_image(image_list)\n\n# Cập nhật DataFrame với ảnh mới đã áp dụng Color Jitter\nnew5_df = update_df_with_color_jitter(filtered_df)\n\n# Bạn có thể lưu DataFrame đã cập nhật vào file CSV hoặc xử lý tiếp\n# augmented_df.to_csv('path_to_save_updated_csv.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:21:54.679958Z","iopub.execute_input":"2024-09-04T16:21:54.680307Z","iopub.status.idle":"2024-09-04T16:22:39.519695Z","shell.execute_reply.started":"2024-09-04T16:21:54.680273Z","shell.execute_reply":"2024-09-04T16:22:39.518537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Lưu DataFrame đã cập nhật vào file CSV mới\n# augmented_df.to_csv('/kaggle/working/data/train_augmented.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:22:39.521483Z","iopub.execute_input":"2024-09-04T16:22:39.522324Z","iopub.status.idle":"2024-09-04T16:22:39.526369Z","shell.execute_reply.started":"2024-09-04T16:22:39.522254Z","shell.execute_reply":"2024-09-04T16:22:39.525526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:22:39.52743Z","iopub.execute_input":"2024-09-04T16:22:39.527728Z","iopub.status.idle":"2024-09-04T16:22:39.551934Z","shell.execute_reply.started":"2024-09-04T16:22:39.527697Z","shell.execute_reply":"2024-09-04T16:22:39.551043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\n# Giới hạn số lượng mẫu cho mỗi lớp\nmax_samples_per_class = 3600\n\n# Lọc DataFrame chỉ chứa các lớp cần cân bằng\nclass_ids_to_balance = [0, 3, 11, 13]\nfiltered_df = final_df[final_df['class_id'].isin(class_ids_to_balance)].copy()\n\n# Khởi tạo DataFrame rỗng để lưu kết quả\nbalanced_df = pd.DataFrame()\n\n# Lặp qua từng lớp để cân bằng dữ liệu\nfor class_id in class_ids_to_balance:\n    # Lọc dữ liệu theo class_id\n    class_df = filtered_df[filtered_df['class_id'] == class_id]\n    \n    # Nếu số lượng mẫu nhiều hơn max_samples_per_class, chọn ngẫu nhiên\n    if len(class_df) > max_samples_per_class:\n        class_df = class_df.sample(n=max_samples_per_class, random_state=42)  # random_state để tái tạo kết quả\n     \n    # Thêm vào DataFrame đã cân bằng\n    balanced_df = pd.concat([balanced_df, class_df], ignore_index=True)\n\n# Lọc DataFrame gốc để chỉ giữ lại các lớp không bị ảnh hưởng\nnon_balanced_df = final_df[~final_df['class_id'].isin(class_ids_to_balance)]\n\n# Gộp DataFrame đã cân bằng với các mẫu không bị ảnh hưởng\nnew6_df = pd.concat([non_balanced_df, balanced_df], ignore_index=True)\n\n# # Hiển thị thông tin về DataFrame đã cân bằng\n# print(f\"Số lượng mẫu sau khi cân bằng: {final_df_filtered['class_id'].value_counts()}\")\n\n# Lưu DataFrame đã cân bằng vào file CSV nếu cần\n# final_df_filtered.to_csv('balanced_final_data.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:22:39.552951Z","iopub.execute_input":"2024-09-04T16:22:39.553528Z","iopub.status.idle":"2024-09-04T16:22:39.597748Z","shell.execute_reply.started":"2024-09-04T16:22:39.553484Z","shell.execute_reply":"2024-09-04T16:22:39.596946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new6_df['class_id'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:22:39.598823Z","iopub.execute_input":"2024-09-04T16:22:39.599175Z","iopub.status.idle":"2024-09-04T16:22:39.623217Z","shell.execute_reply.started":"2024-09-04T16:22:39.599138Z","shell.execute_reply":"2024-09-04T16:22:39.622245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Hiển thị thông tin về DataFrame đã cân bằng\nprint(f\"Số lượng mẫu sau khi cân bằng: {final_df['class_id'].value_counts()}\")","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:22:39.624595Z","iopub.execute_input":"2024-09-04T16:22:39.625273Z","iopub.status.idle":"2024-09-04T16:22:39.634449Z","shell.execute_reply.started":"2024-09-04T16:22:39.625219Z","shell.execute_reply":"2024-09-04T16:22:39.63351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_df = pd.concat([new_df, new1_df, new2_df, new3_df,new4_df, new5_df, new6_df], ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:22:39.635589Z","iopub.execute_input":"2024-09-04T16:22:39.635893Z","iopub.status.idle":"2024-09-04T16:22:39.651688Z","shell.execute_reply.started":"2024-09-04T16:22:39.635859Z","shell.execute_reply":"2024-09-04T16:22:39.650881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_df['class_id'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:22:39.652764Z","iopub.execute_input":"2024-09-04T16:22:39.653142Z","iopub.status.idle":"2024-09-04T16:22:39.661082Z","shell.execute_reply.started":"2024-09-04T16:22:39.653096Z","shell.execute_reply":"2024-09-04T16:22:39.66018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_train_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:22:39.662442Z","iopub.execute_input":"2024-09-04T16:22:39.663393Z","iopub.status.idle":"2024-09-04T16:22:39.688853Z","shell.execute_reply.started":"2024-09-04T16:22:39.663343Z","shell.execute_reply":"2024-09-04T16:22:39.68789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_SIZE = 1024\nTRAIN_IMAGES_DIRECTORY = '/kaggle/working/data/train/'\nTEST_IMAGES_DIRECTORY = '/kaggle/working/data/test/'\n\nTRAIN_IMAGES_PATH_REFACTORED = '/kaggle/working/refactored_data/images/train/'\nTRAIN_LABELS_PATH_REFACTORED = '/kaggle/working/refactored_data/labels/train/'\nVALID_IMAGES_PATH_REFACTORED = '/kaggle/working/refactored_data/images/val/'\nVALID_LABELS_PATH_REFACTORED = '/kaggle/working/refactored_data/labels/val/'","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:22:39.689964Z","iopub.execute_input":"2024-09-04T16:22:39.690314Z","iopub.status.idle":"2024-09-04T16:22:39.698201Z","shell.execute_reply.started":"2024-09-04T16:22:39.690278Z","shell.execute_reply":"2024-09-04T16:22:39.697386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\n# Giả sử new_train_df đã tồn tại\n# Ví dụ:\n# new_train_df = pd.DataFrame(...)\n\n# Tạo một bản sao của new_train_df để xử lý\ntrain_df = new_train_df.copy()\n\n# Đổi tên các cột\ntrain_df.rename(columns={\n    'ori_x_min': 'x_min',\n    'ori_y_min': 'y_min',\n    'ori_x_max': 'x_max',\n    'ori_y_max': 'y_max',\n    'ori_x_mid': 'x_center',\n    'ori_y_mid': 'y_center',\n    'ori_w': 'w',\n    'ori_h': 'h'\n}, inplace=True)\n\n# Chuyển dữ liệu của các cột 'x_min', 'y_min', 'x_max', 'y_max' thành kiểu int\ntrain_df[['x_min', 'y_min', 'x_max', 'y_max']] = train_df[['x_min', 'y_min', 'x_max', 'y_max']].astype(int)\n\n# Bỏ cột 'img_path'\ntrain_df.drop(columns=['img_path'], inplace=True)\n\n# Hiển thị kết quả\nprint(train_df.head())","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:22:39.699381Z","iopub.execute_input":"2024-09-04T16:22:39.699691Z","iopub.status.idle":"2024-09-04T16:22:39.731465Z","shell.execute_reply.started":"2024-09-04T16:22:39.699659Z","shell.execute_reply":"2024-09-04T16:22:39.730598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Giả sử train_df đã được khởi tạo và chứa dữ liệu của bạn\n# Bạn có thể ghi DataFrame này thành file CSV như sau:\n\ntrain_df.to_csv('/kaggle/working/data/train.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:22:39.732526Z","iopub.execute_input":"2024-09-04T16:22:39.73281Z","iopub.status.idle":"2024-09-04T16:22:39.968948Z","shell.execute_reply.started":"2024-09-04T16:22:39.732778Z","shell.execute_reply":"2024-09-04T16:22:39.967924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:22:39.970349Z","iopub.execute_input":"2024-09-04T16:22:39.971064Z","iopub.status.idle":"2024-09-04T16:22:39.986792Z","shell.execute_reply.started":"2024-09-04T16:22:39.970995Z","shell.execute_reply":"2024-09-04T16:22:39.985881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rm -r /kaggle/working/refactored_data\n","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:22:39.98793Z","iopub.execute_input":"2024-09-04T16:22:39.988508Z","iopub.status.idle":"2024-09-04T16:22:41.095091Z","shell.execute_reply.started":"2024-09-04T16:22:39.988473Z","shell.execute_reply":"2024-09-04T16:22:41.093988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prime_dataset() -> List[pd.DataFrame]:\n    \"\"\"\n    Ready the dataset and partition it into training and validation folders.\n\n    :return: Two datasets, one raw and another preprocessed\n    \"\"\"\n    train_df = pd.read_csv('/kaggle/working/data/train.csv')\n    \n    train_df['x_min'] = train_df['x_min'] / 1024\n    train_df['y_min'] = train_df['y_min'] / 1024\n\n    train_df['x_max'] = train_df['x_max'] / 1024\n    train_df['y_max'] = train_df['y_max'] / 1024\n\n    train_df['x_center'] = (train_df['x_max'] + train_df['x_min']) / 2\n    train_df['y_center'] = (train_df['y_max'] + train_df['y_min']) / 2\n\n    train_df['w'] = (train_df['x_max'] - train_df['x_min'])\n    train_df['h'] = (train_df['y_max'] - train_df['y_min'])\n    # Training/validation splitting\n    splitter = GroupShuffleSplit(test_size=0.1)\n    split = splitter.split(train_df, groups=train_df['image_id'])\n    train_inds, valid_inds = next(split)\n\n    valid_df = train_df.iloc[valid_inds]\n    train_df = train_df.iloc[train_inds]\n\n    # Create images and labels directories for both training and validation\n    for folder in [\n        TRAIN_IMAGES_PATH_REFACTORED,\n        TRAIN_LABELS_PATH_REFACTORED,\n        VALID_IMAGES_PATH_REFACTORED,\n        VALID_LABELS_PATH_REFACTORED,\n    ]:\n        os.makedirs(folder, exist_ok=True)\n\n    # Copy training images to its designated directory, and create a txt file for each image denoting its classes and their positions\n    for image in tqdm(train_df['image_id'].unique()):\n        records = train_df[train_df['image_id'] == image]\n        attributes = records[['class_id', 'x_center', 'y_center', 'w', 'h']].values\n        attributes = np.array(attributes)\n        np.savetxt(\n            os.path.join(\n                TRAIN_LABELS_PATH_REFACTORED,\n                f'{image}.txt'\n            ),\n            attributes,\n            fmt=['%d', '%f', '%f', '%f', '%f']\n        )\n        shutil.copy(\n            os.path.join(\n                TRAIN_IMAGES_DIRECTORY,\n                f'{image}.png'\n            ),\n            TRAIN_IMAGES_PATH_REFACTORED\n        )\n\n    # Copy validation images to its designated directory, and create a txt file for each image denoting its classes and their positions\n    for image in tqdm(valid_df['image_id'].unique()):\n        records = valid_df[valid_df['image_id'] == image]\n        attributes = records[['class_id', 'x_center', 'y_center', 'w', 'h']].values\n        attributes = np.array(attributes)\n        np.savetxt(\n            os.path.join(\n                VALID_LABELS_PATH_REFACTORED,\n                f'{image}.txt'\n            ),\n            attributes,\n            fmt=['%d', '%f', '%f', '%f', '%f']\n        )\n        shutil.copy(\n            os.path.join(\n                TRAIN_IMAGES_DIRECTORY,\n                f'{image}.png'\n            ),\n            VALID_IMAGES_PATH_REFACTORED\n        )\n\n    # Order the classes based on the class ID numerical value (in an ascending order)\n    class_ids, class_names = list(zip(*set(zip(train_df['class_id'], train_df['class_name']))))\n    classes = list(np.array(class_names)[np.argsort(class_ids)])\n    classes = list(map(lambda x: str(x), classes))\n\n    # Store a list containing the path of each training image in a TXT file\n    with open('train.txt', 'w') as f:\n        for path in os.listdir(TRAIN_IMAGES_PATH_REFACTORED):\n            f.write(f'{TRAIN_IMAGES_PATH_REFACTORED}{path}\\n')\n\n    # Store a list containing the path of each validation image in a TXT file\n    with open('valid.txt', 'w') as f:\n        for path in os.listdir(VALID_IMAGES_PATH_REFACTORED):\n            f.write(f'{VALID_IMAGES_PATH_REFACTORED}{path}\\n')\n\n    # Create a dictionary containing the necessary configurations to run YOLO\n    data = dict(\n        train='train.txt',\n        val='valid.txt',\n        nc=14,\n        names=classes\n    )\n\n    # Store the configurations in a YAML file, to be absorbed later by YOLO\n    with open('yolo.yaml', 'w') as outfile:\n        yaml.dump(data, outfile, default_flow_style=False)\n\n    return [pd.read_csv('/kaggle/working/data/train.csv'), train_df]","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:22:41.096776Z","iopub.execute_input":"2024-09-04T16:22:41.097152Z","iopub.status.idle":"2024-09-04T16:22:41.117847Z","shell.execute_reply.started":"2024-09-04T16:22:41.097114Z","shell.execute_reply":"2024-09-04T16:22:41.116828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"raw_df, preprocessed_df = prime_dataset()","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:22:41.119058Z","iopub.execute_input":"2024-09-04T16:22:41.119447Z","iopub.status.idle":"2024-09-04T16:23:40.239329Z","shell.execute_reply.started":"2024-09-04T16:22:41.119411Z","shell.execute_reply":"2024-09-04T16:23:40.238185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !git clone https://github.com/WongKinYiu/yolov9.git\n# !wget https://github.com/WongKinYiu/yolov9/releases/download/v0.1/yolov9-e.pt","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:23:40.240906Z","iopub.execute_input":"2024-09-04T16:23:40.241345Z","iopub.status.idle":"2024-09-04T16:23:40.247215Z","shell.execute_reply.started":"2024-09-04T16:23:40.241287Z","shell.execute_reply":"2024-09-04T16:23:40.245846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !wget https://github.com/WongKinYiu/yolov9/releases/download/v0.1/yolov9-c.pt","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:23:40.248456Z","iopub.execute_input":"2024-09-04T16:23:40.248759Z","iopub.status.idle":"2024-09-04T16:23:40.261701Z","shell.execute_reply.started":"2024-09-04T16:23:40.248727Z","shell.execute_reply":"2024-09-04T16:23:40.260935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !wget https://github.com/WongKinYiu/yolov9/releases/download/v0.1/gelan-c.pt","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:23:40.262735Z","iopub.execute_input":"2024-09-04T16:23:40.263023Z","iopub.status.idle":"2024-09-04T16:23:40.274252Z","shell.execute_reply.started":"2024-09-04T16:23:40.262975Z","shell.execute_reply":"2024-09-04T16:23:40.27326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install -r /kaggle/working/yolov9/requirements.txt","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:23:40.275427Z","iopub.execute_input":"2024-09-04T16:23:40.275771Z","iopub.status.idle":"2024-09-04T16:23:40.286767Z","shell.execute_reply.started":"2024-09-04T16:23:40.27573Z","shell.execute_reply":"2024-09-04T16:23:40.285946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_model(\n        visualize: bool = False,\n        export_as_onnx: bool = False,\n        export_as_tf: bool = False,\n        export_as_tflite: bool = False,\n):\n    \"\"\"\n    Builds the model, and export a PyTorch model.\n\n    :param bool visualize: Specify whether to carry out the evaluation metrics on the created model or not (show plots containing multiple evaluation metrics, including the confusion matrix and the precision-recall curve)\n    :param bool export_as_onnx: Specify whether to export the model in a notation that is interpretable by ONNX or not\n    :param bool export_as_tf: Specify whether to export the model in a notation that is interpretable by TensorFlow or not, ignores export_as_onnx value when set to True\n    :param bool export_as_tflite: Specify whether to export the model in a notation that is interpretable by TensorFlow and optimized on edge devices or not, ignores export_as_onnx and export_as_tf values when set to True\n    \"\"\"\n\n    !torchrun --nproc_per_node=2 --master_port 9527 /kaggle/working/yolov9/train_dual.py --device 0,1 --sync-bn --img {IMAGE_SIZE} --cfg /kaggle/working/yolov9/models/detect/yolov9-c.yaml --batch-size 8 --epochs 20 --data yolo.yaml --weights /kaggle/working/yolov9-c.pt --hyp /kaggle/working/yolov9/data/hyps/hyp.scratch-high.yaml --min-items 0 --close-mosaic 15\n    # !torchrun --nproc_per_node=2 --master_port 9527 /kaggle/working/yolov9/train_dual.py --device 0,1 --sync-bn --img {IMAGE_SIZE} --cfg /kaggle/working/yolov9/models/detect/yolov9-c.yaml --batch-size 8 --epochs 50 --data yolo.yaml --resume /kaggle/working/yolov9/runs/train/exp6/weights/last.pt\n\n    if visualize:\n        # Modify matplotlib figure size, and remove axis lines and ticks\n        plt.rcParams.update({\n            'figure.figsize': (15, 8),\n            'axes.spines.left': False,\n            'axes.spines.right': False,\n            'axes.spines.bottom': False,\n            'axes.spines.top': False,\n            'xtick.bottom': False,\n            'xtick.labelbottom': False,\n            'ytick.labelleft': False,\n            'ytick.left': False,\n        })\n\n        plt.imshow(plt.imread('runs/train/exp/results.png'))\n        plt.imshow(plt.imread('runs/train/exp/PR_curve.png'))\n        plt.imshow(plt.imread('runs/train/exp/confusion_matrix.png'))\n\n        plt.rcParams.update(plt.rcParamsDefault)\n        plt.rcParams.update({'figure.figsize': (15, 8)})\n\n    if export_as_onnx or export_as_tf or export_as_tflite:\n        !python yolov9/export.py --weights yolov9-c.pt --img-size {IMAGE_SIZE} {IMAGE_SIZE} --max-wh {IMAGE_SIZE} --grid --end2end --simplify\n\n        if export_as_tf or export_as_tflite:\n            !onnx-tf convert -i yolov9.onnx -o ./\n\n            if export_as_tflite:\n                converter = tf.lite.TFLiteConverter.from_saved_model('./')\n                tflite_model = converter.convert()\n                with open('yolov9.tflite', 'wb') as f:\n                    f.write(tflite_model)","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:23:40.287962Z","iopub.execute_input":"2024-09-04T16:23:40.288274Z","iopub.status.idle":"2024-09-04T16:23:40.299906Z","shell.execute_reply.started":"2024-09-04T16:23:40.288242Z","shell.execute_reply":"2024-09-04T16:23:40.299058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:23:40.301057Z","iopub.execute_input":"2024-09-04T16:23:40.301315Z","iopub.status.idle":"2024-09-04T16:23:40.329944Z","shell.execute_reply.started":"2024-09-04T16:23:40.301286Z","shell.execute_reply":"2024-09-04T16:23:40.329095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip uninstall -y wandb","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:23:40.331387Z","iopub.execute_input":"2024-09-04T16:23:40.331743Z","iopub.status.idle":"2024-09-04T16:23:40.342615Z","shell.execute_reply.started":"2024-09-04T16:23:40.331702Z","shell.execute_reply":"2024-09-04T16:23:40.341546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !cp /kaggle/working/valid.txt /kaggle/working/yolov9/valid.txt\n# !cp /kaggle/working/train.txt /kaggle/working/yolov9/train.txt","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:23:40.34368Z","iopub.execute_input":"2024-09-04T16:23:40.344023Z","iopub.status.idle":"2024-09-04T16:23:40.353705Z","shell.execute_reply.started":"2024-09-04T16:23:40.343975Z","shell.execute_reply":"2024-09-04T16:23:40.352835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rm -rf /kaggle/working/yolov9/runs","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:23:40.354989Z","iopub.execute_input":"2024-09-04T16:23:40.355757Z","iopub.status.idle":"2024-09-04T16:23:40.366342Z","shell.execute_reply.started":"2024-09-04T16:23:40.355724Z","shell.execute_reply":"2024-09-04T16:23:40.3654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rm -rf /kaggle/working/data","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:23:40.367554Z","iopub.execute_input":"2024-09-04T16:23:40.367929Z","iopub.status.idle":"2024-09-04T16:23:40.379242Z","shell.execute_reply.started":"2024-09-04T16:23:40.367887Z","shell.execute_reply":"2024-09-04T16:23:40.37835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_model()","metadata":{"execution":{"iopub.status.busy":"2024-09-04T16:23:40.380443Z","iopub.execute_input":"2024-09-04T16:23:40.380807Z","iopub.status.idle":"2024-09-04T16:23:40.390399Z","shell.execute_reply.started":"2024-09-04T16:23:40.380767Z","shell.execute_reply":"2024-09-04T16:23:40.389542Z"},"trusted":true},"execution_count":null,"outputs":[]}]}