{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"papermill":{"default_parameters":{},"duration":13967.250849,"end_time":"2021-03-22T15:40:06.249867","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2021-03-22T11:47:18.999018","version":"2.2.2"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceType":"competition","sourceId":24800,"datasetId":1042002,"databundleVersionId":1831594},{"sourceType":"datasetVersion","sourceId":1799839,"datasetId":1069682,"databundleVersionId":1837296},{"sourceType":"datasetVersion","sourceId":1832961,"datasetId":1089494,"databundleVersionId":1870583},{"sourceType":"datasetVersion","sourceId":1800067,"datasetId":1069810,"databundleVersionId":1837524},{"sourceType":"datasetVersion","sourceId":1800777,"datasetId":1069787,"databundleVersionId":1838236},{"sourceType":"datasetVersion","sourceId":1800778,"datasetId":1070222,"databundleVersionId":1838237}],"dockerImageVersionId":30919,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install ultralytics","metadata":{"execution":{"iopub.status.busy":"2025-04-05T08:00:59.908364Z","iopub.execute_input":"2025-04-05T08:00:59.908639Z","iopub.status.idle":"2025-04-05T08:01:05.537335Z","shell.execute_reply.started":"2025-04-05T08:00:59.908609Z","shell.execute_reply":"2025-04-05T08:01:05.536563Z"},"papermill":{"duration":2.026576,"end_time":"2021-03-22T11:47:26.345268","exception":false,"start_time":"2021-03-22T11:47:24.318692","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport numpy as np\nimport shutil\nimport yaml\nimport matplotlib.pyplot as plt\nimport random\nimport cv2\n\nfrom sklearn import model_selection\nfrom tqdm import tqdm\nfrom glob import glob","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2025-04-05T08:01:05.538261Z","iopub.execute_input":"2025-04-05T08:01:05.538581Z","iopub.status.idle":"2025-04-05T08:01:06.690094Z","shell.execute_reply.started":"2025-04-05T08:01:05.538551Z","shell.execute_reply":"2025-04-05T08:01:06.689201Z"},"papermill":{"duration":1.332949,"end_time":"2021-03-22T11:47:27.699278","exception":false,"start_time":"2021-03-22T11:47:26.366329","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"size = 512\nTRAIN_LABELS_PATH = './vinbigdata/labels/train'\nVAL_LABELS_PATH = './vinbigdata/labels/val'\nTRAIN_IMAGES_PATH = './vinbigdata/images/train' #12000\nVAL_IMAGES_PATH = './vinbigdata/images/val' #3000\nExternal_DIR = f'../input/vinbigdata-{size}-image-dataset/vinbigdata/train' # 15000\nos.makedirs(TRAIN_LABELS_PATH, exist_ok = True)\nos.makedirs(VAL_LABELS_PATH, exist_ok = True)\nos.makedirs(TRAIN_IMAGES_PATH, exist_ok = True)\nos.makedirs(VAL_IMAGES_PATH, exist_ok = True)","metadata":{"execution":{"iopub.status.busy":"2025-04-05T08:01:06.690997Z","iopub.execute_input":"2025-04-05T08:01:06.691392Z","iopub.status.idle":"2025-04-05T08:01:06.696385Z","shell.execute_reply.started":"2025-04-05T08:01:06.691368Z","shell.execute_reply":"2025-04-05T08:01:06.695596Z"},"papermill":{"duration":0.047244,"end_time":"2021-03-22T11:47:27.783548","exception":false,"start_time":"2021-03-22T11:47:27.736304","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"original_df = pd.read_csv('../input/vinbigdata-chest-xray-abnormalities-detection/train.csv')\nnumber_of_imageids = len(original_df['image_id'].values)\nprint(f'Total number of image_ids (train + validation) {number_of_imageids}')\n\nnumber_of_images = len(os.listdir('../input/vinbigdata-chest-xray-abnormalities-detection/train'))\nprint(f'Total number of images (train + validation) {number_of_images}')\n\nnumber_of_labels = len(os.listdir('../input/vinbigdata-yolo-labels-dataset/labels'))\nprint(f'Total number of labels (train + validation) {number_of_labels}')","metadata":{"execution":{"iopub.status.busy":"2025-04-05T08:01:06.697126Z","iopub.execute_input":"2025-04-05T08:01:06.697398Z","iopub.status.idle":"2025-04-05T08:01:10.254075Z","shell.execute_reply.started":"2025-04-05T08:01:06.697371Z","shell.execute_reply":"2025-04-05T08:01:10.253288Z"},"papermill":{"duration":1.218673,"end_time":"2021-03-22T11:47:29.035839","exception":false,"start_time":"2021-03-22T11:47:27.817166","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pydicom\nimport multiprocessing\nfrom tqdm import tqdm\nfrom skimage import exposure\n\ndef dicom2array(path, voi_lut=True, fix_monochrome=True):\n    dicom = pydicom.read_file(path)\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    return data\n\ndef process_image(dicom_path_output_dir):\n    dicom_path, output_dir = dicom_path_output_dir\n    file_name = os.path.splitext(os.path.basename(dicom_path))[0]\n    image_array = dicom2array(dicom_path)\n    equalized_image = exposure.equalize_hist(image_array)\n    equalized_image = (equalized_image * 255).astype(np.uint8)\n    cv2.imwrite(os.path.join(output_dir, f\"{file_name}.jpeg\"), equalized_image)\n\ndef saving_image(output_dir, dicom_path_list):\n    os.makedirs(output_dir, exist_ok=True)\n    dicom_path_output_dir_list = [(path, output_dir) for path in dicom_path_list]\n\n    # Use multiprocessing Pool for parallel processing\n    with multiprocessing.Pool() as pool:\n        list(tqdm(pool.imap(process_image, dicom_path_output_dir_list), total=len(dicom_path_list), desc=\"Processing Images\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T08:01:10.254875Z","iopub.execute_input":"2025-04-05T08:01:10.255139Z","iopub.status.idle":"2025-04-05T08:01:10.792471Z","shell.execute_reply.started":"2025-04-05T08:01:10.255097Z","shell.execute_reply":"2025-04-05T08:01:10.79182Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train.csv')\nnumber_of_images = len(df['image_id'].values)\nprint(f'Total number of image ids (train + validation) {number_of_images}')\n\ndf = df[df.class_id!=14].reset_index(drop = True)\nnumber_of_images = len(df['image_id'].values)\nprint(f'Total number of image ids after dropping normal images (train + validation) {number_of_images}')\n\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2025-04-05T08:01:10.794421Z","iopub.execute_input":"2025-04-05T08:01:10.794663Z","iopub.status.idle":"2025-04-05T08:01:10.907961Z","shell.execute_reply.started":"2025-04-05T08:01:10.794642Z","shell.execute_reply":"2025-04-05T08:01:10.907272Z"},"papermill":{"duration":0.261507,"end_time":"2021-03-22T11:47:29.322761","exception":false,"start_time":"2021-03-22T11:47:29.061254","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.drop(columns=['class_name', 'rad_id', 'x_min', 'x_max', 'y_min', 'y_max',  'class_id']) # we only need image ids, labels are pre-made\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2025-04-05T08:01:10.90949Z","iopub.execute_input":"2025-04-05T08:01:10.909699Z","iopub.status.idle":"2025-04-05T08:01:10.91868Z","shell.execute_reply.started":"2025-04-05T08:01:10.909681Z","shell.execute_reply":"2025-04-05T08:01:10.917822Z"},"papermill":{"duration":0.035035,"end_time":"2021-03-22T11:47:29.380339","exception":false,"start_time":"2021-03-22T11:47:29.345304","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_train, df_valid = model_selection.train_test_split(df, test_size=0.15, random_state=42, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2025-04-05T08:01:10.919644Z","iopub.execute_input":"2025-04-05T08:01:10.919937Z","iopub.status.idle":"2025-04-05T08:01:10.937182Z","shell.execute_reply.started":"2025-04-05T08:01:10.919914Z","shell.execute_reply":"2025-04-05T08:01:10.93644Z"},"papermill":{"duration":0.032099,"end_time":"2021-03-22T11:47:29.434859","exception":false,"start_time":"2021-03-22T11:47:29.40276","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"number_of_images = len(df_train['image_id'].values)\nprint(f'Total number of training image_ids {number_of_images}')\n\nnumber_of_images = len(df_valid['image_id'].values)\nprint(f'Total number of validation image_ids {number_of_images}')\n","metadata":{"execution":{"iopub.status.busy":"2025-04-05T08:01:10.938021Z","iopub.execute_input":"2025-04-05T08:01:10.938277Z","iopub.status.idle":"2025-04-05T08:01:10.950132Z","shell.execute_reply.started":"2025-04-05T08:01:10.938257Z","shell.execute_reply":"2025-04-05T08:01:10.949385Z"},"papermill":{"duration":0.033279,"end_time":"2021-03-22T11:47:29.4905","exception":false,"start_time":"2021-03-22T11:47:29.457221","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# need to delete duplicate image ids, len(labels) should be equal len(df.imageids.values), ","metadata":{"execution":{"iopub.status.busy":"2025-04-05T08:01:10.950989Z","iopub.execute_input":"2025-04-05T08:01:10.951223Z","iopub.status.idle":"2025-04-05T08:01:10.964216Z","shell.execute_reply.started":"2025-04-05T08:01:10.951205Z","shell.execute_reply":"2025-04-05T08:01:10.963471Z"},"papermill":{"duration":0.029053,"end_time":"2021-03-22T11:47:29.542843","exception":false,"start_time":"2021-03-22T11:47:29.51379","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(f'Total number of training images {len(df_train.image_id.unique())}')\nprint(f'Total number of validation images {len(df_valid.image_id.unique())}')","metadata":{"execution":{"iopub.status.busy":"2025-04-05T08:01:10.965047Z","iopub.execute_input":"2025-04-05T08:01:10.965266Z","iopub.status.idle":"2025-04-05T08:01:10.987424Z","shell.execute_reply.started":"2025-04-05T08:01:10.965248Z","shell.execute_reply":"2025-04-05T08:01:10.986725Z"},"papermill":{"duration":0.036667,"end_time":"2021-03-22T11:47:29.602779","exception":false,"start_time":"2021-03-22T11:47:29.566112","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def preproccess_data(df, labels_path, images_path):\n    for img_id in tqdm(df.image_id.unique()):\n        shutil.copy(os.path.join('../input/vinbigdata-yolo-labels-dataset/labels', f\"{img_id}\"+'.txt'), labels_path)\n        shutil.copy(os.path.join(f'/kaggle/input/vinbigdata-{size}-image-dataset/vinbigdata/train', f\"{img_id}.png\"), images_path)","metadata":{"execution":{"iopub.status.busy":"2025-04-05T08:01:10.988276Z","iopub.execute_input":"2025-04-05T08:01:10.988513Z","iopub.status.idle":"2025-04-05T08:01:10.997925Z","shell.execute_reply.started":"2025-04-05T08:01:10.988494Z","shell.execute_reply":"2025-04-05T08:01:10.99712Z"},"papermill":{"duration":0.030833,"end_time":"2021-03-22T11:47:29.65776","exception":false,"start_time":"2021-03-22T11:47:29.626927","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"preproccess_data(df_train, TRAIN_LABELS_PATH, TRAIN_IMAGES_PATH)\npreproccess_data(df_valid, VAL_LABELS_PATH, VAL_IMAGES_PATH)","metadata":{"execution":{"iopub.status.busy":"2025-04-05T08:01:10.998624Z","iopub.execute_input":"2025-04-05T08:01:10.998907Z","iopub.status.idle":"2025-04-05T08:02:53.460581Z","shell.execute_reply.started":"2025-04-05T08:01:10.998882Z","shell.execute_reply":"2025-04-05T08:02:53.459845Z"},"papermill":{"duration":70.190899,"end_time":"2021-03-22T11:48:39.87281","exception":false,"start_time":"2021-03-22T11:47:29.681911","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# check that data was preprocessed correctly\nprint(len(os.listdir(TRAIN_LABELS_PATH)))\nprint(len(os.listdir(TRAIN_IMAGES_PATH)))\n\nprint(len(os.listdir(VAL_LABELS_PATH)))\nprint(len(os.listdir(VAL_IMAGES_PATH)))","metadata":{"execution":{"iopub.status.busy":"2025-04-05T08:02:53.461285Z","iopub.execute_input":"2025-04-05T08:02:53.461536Z","iopub.status.idle":"2025-04-05T08:02:53.475195Z","shell.execute_reply.started":"2025-04-05T08:02:53.461514Z","shell.execute_reply":"2025-04-05T08:02:53.474397Z"},"papermill":{"duration":0.25267,"end_time":"2021-03-22T11:48:40.418588","exception":false,"start_time":"2021-03-22T11:48:40.165918","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# credit / source https://www.kaggle.com/awsaf49/vinbigdata-cxr-ad-yolov5-14-class-train\nclasses = [ 'Aortic enlargement',\n            'Atelectasis',\n            'Calcification',\n            'Cardiomegaly',\n            'Consolidation',\n            'ILD',\n            'Infiltration',\n            'Lung Opacity',\n            'Nodule/Mass',\n            'Other lesion',\n            'Pleural effusion',\n            'Pleural thickening',\n            'Pneumothorax',\n            'Pulmonary fibrosis']\n\ndata = dict(\n    train =  '../vinbigdata/images/train',\n    val   =  '../vinbigdata/images/val',\n    nc    = 14,\n    names = classes\n    )\n\nwith open('/kaggle/working/vinbigdata.yaml', 'w') as outfile:\n    yaml.dump(data, outfile, default_flow_style=False)\n    \nf = open(join( cwd , 'vinbigdata.yaml'), 'r')\nprint('\\nyaml:')\nprint(f.read())","metadata":{"execution":{"iopub.status.busy":"2025-04-05T08:07:57.152967Z","iopub.execute_input":"2025-04-05T08:07:57.153337Z","iopub.status.idle":"2025-04-05T08:07:57.160855Z","shell.execute_reply.started":"2025-04-05T08:07:57.153287Z","shell.execute_reply":"2025-04-05T08:07:57.160072Z"},"papermill":{"duration":0.188854,"end_time":"2021-03-22T11:48:40.78604","exception":false,"start_time":"2021-03-22T11:48:40.597186","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nfrom ultralytics import YOLO  \nos.environ[\"WANDB_MODE\"] = \"dryrun\"\n\nmodel = YOLO('yolov9c.pt')  \n\nmodel.train(\n    data='./vinbigdata.yaml',  \n    imgsz=640,                \n    batch=16,                 \n    epochs=30,               \n    device=0                 \n)\n\n","metadata":{"execution":{"iopub.status.busy":"2025-04-05T08:08:21.322992Z","iopub.execute_input":"2025-04-05T08:08:21.323338Z","iopub.status.idle":"2025-04-05T10:44:20.096096Z","shell.execute_reply.started":"2025-04-05T08:08:21.323288Z","shell.execute_reply":"2025-04-05T10:44:20.095105Z"},"papermill":{"duration":13235.560353,"end_time":"2021-03-22T15:32:04.017468","exception":false,"start_time":"2021-03-22T11:51:28.457115","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df = pd.read_csv(f'/kaggle/input/vinbigdata-{size}-image-dataset/vinbigdata/test.csv')","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:44:29.50996Z","iopub.execute_input":"2025-04-05T10:44:29.510271Z","iopub.status.idle":"2025-04-05T10:44:29.535503Z","shell.execute_reply.started":"2025-04-05T10:44:29.510249Z","shell.execute_reply":"2025-04-05T10:44:29.534735Z"},"papermill":{"duration":3.288561,"end_time":"2021-03-22T15:32:10.867223","exception":false,"start_time":"2021-03-22T15:32:07.578662","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_dir = f'/kaggle/input/vinbigdata-{size}-image-dataset/vinbigdata/test'\nos.listdir('/kaggle/working/runs/detect/train/weights/')","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:44:30.889383Z","iopub.execute_input":"2025-04-05T10:44:30.889693Z","iopub.status.idle":"2025-04-05T10:44:30.895213Z","shell.execute_reply.started":"2025-04-05T10:44:30.88967Z","shell.execute_reply":"2025-04-05T10:44:30.894538Z"},"papermill":{"duration":3.251265,"end_time":"2021-03-22T15:32:17.606242","exception":false,"start_time":"2021-03-22T15:32:14.354977","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nweights_dir = \"/kaggle/working/runs/detect/train/weights/best.pt\"  # Trọng số mô hình đã huấn luyện\n\nmodel = YOLO(weights_dir)\n\nresults = model.predict(\n    source=test_dir,\n    imgsz=640,\n    conf=0.005,\n    iou=0.45,\n    save_txt=True,\n    save_conf=True,\n    project=\"/kaggle/working/runs/detect\",  \n    name=\"exp\",                            \n    exist_ok=True,                         \n    save=True                              # Lưu ảnh với bounding box\n)\n\n","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:44:31.714566Z","iopub.execute_input":"2025-04-05T10:44:31.714878Z","iopub.status.idle":"2025-04-05T10:47:25.524575Z","shell.execute_reply.started":"2025-04-05T10:44:31.714854Z","shell.execute_reply":"2025-04-05T10:47:25.523711Z"},"papermill":{"duration":255.960967,"end_time":"2021-03-22T15:36:36.738226","exception":false,"start_time":"2021-03-22T15:32:20.777259","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# credit / source https://www.kaggle.com/awsaf49/vinbigdata-cxr-ad-yolov5-14-class-infer\ndef yolo2voc(image_height, image_width, bboxes):\n    \"\"\"\n    yolo => [xmid, ymid, w, h] (normalized)\n    voc  => [x1, y1, x2, y1]\n    \n    \"\"\" \n    bboxes = bboxes.copy().astype(float) # otherwise all value will be 0 as voc_pascal dtype is np.int\n    \n    bboxes[..., [0, 2]] = bboxes[..., [0, 2]]* image_width\n    bboxes[..., [1, 3]] = bboxes[..., [1, 3]]* image_height\n    \n    bboxes[..., [0, 1]] = bboxes[..., [0, 1]] - bboxes[..., [2, 3]]/2\n    bboxes[..., [2, 3]] = bboxes[..., [0, 1]] + bboxes[..., [2, 3]]\n    \n    return bboxes","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:49:02.244947Z","iopub.execute_input":"2025-04-05T10:49:02.245254Z","iopub.status.idle":"2025-04-05T10:49:02.250527Z","shell.execute_reply.started":"2025-04-05T10:49:02.24523Z","shell.execute_reply":"2025-04-05T10:49:02.249623Z"},"papermill":{"duration":3.905138,"end_time":"2021-03-22T15:36:45.236215","exception":false,"start_time":"2021-03-22T15:36:41.331077","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(glob('runs/detect/exp/labels/*txt'))","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:49:03.382991Z","iopub.execute_input":"2025-04-05T10:49:03.383354Z","iopub.status.idle":"2025-04-05T10:49:03.396544Z","shell.execute_reply.started":"2025-04-05T10:49:03.383294Z","shell.execute_reply":"2025-04-05T10:49:03.395703Z"},"papermill":{"duration":4.757871,"end_time":"2021-03-22T15:36:54.420338","exception":false,"start_time":"2021-03-22T15:36:49.662467","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# credit / source https://www.kaggle.com/awsaf49/vinbigdata-cxr-ad-yolov5-14-class-infer\nimage_ids = []\nPredictionStrings = []\n\ndef process_submission():\n    for file_path in tqdm(glob('runs/detect/exp/labels/*txt')):\n        image_id = file_path.split('/')[-1].split('.')[0] # extract image id\n        w, h = test_df.loc[test_df.image_id==image_id,['width', 'height']].values[0] #  get the weight & height from  the test df\n        f = open(file_path, 'r')  # open the label text file\n        data = np.array(f.read().replace('\\n', ' ').strip().split(' ')).astype(np.float32).reshape(-1, 6) # move all the labels to the same line..?\n        data = data[:, [0, 5, 1, 2, 3, 4]]\n        bboxes = list(np.round(np.concatenate((data[:, :2], np.round(yolo2voc(h, w, data[:, 2:]))), axis =1).reshape(-1), 1).astype(str))\n        for idx in range(len(bboxes)):\n            bboxes[idx] = str(int(float(bboxes[idx]))) if idx%6!=1 else bboxes[idx] # 6 is the length of  the prediction string, so..?\n        image_ids.append(image_id)\n        PredictionStrings.append(' '.join(bboxes))\n\n    # credit / source: https://www.kaggle.com/awsaf49/vinbigdata-cxr-ad-yolov5-14-class-infer\n    pred_df = pd.DataFrame({'image_id':image_ids,\n                            'PredictionString':PredictionStrings})\n    sub_df = pd.merge(test_df, pred_df, on = 'image_id', how = 'left').fillna(\"14 1 0 0 1 1\")\n    sub_df = sub_df[['image_id', 'PredictionString']]\n    sub_df.to_csv('/kaggle/working/submission.csv',index = False)\n    sub_df.tail()","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:49:04.530893Z","iopub.execute_input":"2025-04-05T10:49:04.531248Z","iopub.status.idle":"2025-04-05T10:49:04.538889Z","shell.execute_reply.started":"2025-04-05T10:49:04.531216Z","shell.execute_reply":"2025-04-05T10:49:04.537858Z"},"papermill":{"duration":4.448343,"end_time":"2021-03-22T15:37:02.811122","exception":false,"start_time":"2021-03-22T15:36:58.362779","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# !pip uninstall pandas\n!pip install -q pandas==1.1.5","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:49:07.941201Z","iopub.execute_input":"2025-04-05T10:49:07.941538Z","iopub.status.idle":"2025-04-05T10:50:24.629539Z","shell.execute_reply.started":"2025-04-05T10:49:07.941512Z","shell.execute_reply":"2025-04-05T10:50:24.628335Z"},"papermill":{"duration":16.799605,"end_time":"2021-03-22T15:37:23.530401","exception":false,"start_time":"2021-03-22T15:37:06.730796","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"process_submission()","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:50:24.631052Z","iopub.execute_input":"2025-04-05T10:50:24.631403Z","iopub.status.idle":"2025-04-05T10:50:28.805809Z","shell.execute_reply.started":"2025-04-05T10:50:24.631375Z","shell.execute_reply":"2025-04-05T10:50:28.804839Z"},"papermill":{"duration":10.05832,"end_time":"2021-03-22T15:37:37.75557","exception":false,"start_time":"2021-03-22T15:37:27.69725","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import random\nfrom glob import glob\nfrom tqdm import tqdm\nimport cv2\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.axes_grid1 import ImageGrid\n\nfiles = glob('/kaggle/working/runs/detect/exp/*.[jp][pn]g')  # Tìm cả .jpg và .png\nprint(f\"Số lượng file ảnh: {len(files)}\")\n\ndef plot_sample_images():\n    if not files:\n        print(\"Không tìm thấy file ảnh nào trong '/kaggle/working/runs/detect/exp/'!\")\n        return\n    \n    # Số lần hiển thị lưới (tùy chỉnh nếu cần)\n    num_grids = min(3, len(files) // 16 + 1)  # Hiển thị tối đa 3 lưới, mỗi lưới 16 ảnh\n    \n    for _ in range(num_grids):\n        row = 4\n        col = 4\n        num_samples = min(row * col, len(files))  # Giới hạn số mẫu\n        grid_files = random.sample(files, num_samples)\n        images = []\n        \n        # Đọc ảnh\n        for image_path in tqdm(grid_files):\n            img = cv2.imread(image_path)\n            if img is not None:\n                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n                images.append(img)\n            else:\n                print(f\"Không thể đọc file: {image_path}\")\n        \n        if not images:\n            print(\"Không có ảnh nào để hiển thị trong lượt này!\")\n            continue\n        \n        # Tạo lưới ảnh\n        fig = plt.figure(figsize=(col * 5, row * 5))\n        grid = ImageGrid(fig, 111,\n                         nrows_ncols=(row, col),\n                         axes_pad=0.05)\n        \n        # Hiển thị ảnh\n        for ax, im in zip(grid, images):\n            ax.imshow(im)\n            ax.set_xticks([])\n            ax.set_yticks([])\n        \n        # Ẩn các ô trống\n        for ax in grid[len(images):]:\n            ax.axis('off')\n        \n        plt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:50:28.80776Z","iopub.execute_input":"2025-04-05T10:50:28.808012Z","iopub.status.idle":"2025-04-05T10:50:28.857683Z","shell.execute_reply.started":"2025-04-05T10:50:28.80799Z","shell.execute_reply":"2025-04-05T10:50:28.856921Z"},"papermill":{"duration":4.411302,"end_time":"2021-03-22T15:37:46.133503","exception":false,"start_time":"2021-03-22T15:37:41.722201","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install -q Pillow==4.0.0\n!pip install -q PIL\n!pip install -q image","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:50:28.858856Z","iopub.execute_input":"2025-04-05T10:50:28.85911Z","iopub.status.idle":"2025-04-05T10:50:55.301061Z","shell.execute_reply.started":"2025-04-05T10:50:28.859089Z","shell.execute_reply":"2025-04-05T10:50:55.300078Z"},"papermill":{"duration":45.113678,"end_time":"2021-03-22T15:38:35.113372","exception":false,"start_time":"2021-03-22T15:37:49.999694","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plot_sample_images()","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:50:55.302081Z","iopub.execute_input":"2025-04-05T10:50:55.30238Z","iopub.status.idle":"2025-04-05T10:51:02.478079Z","shell.execute_reply.started":"2025-04-05T10:50:55.302354Z","shell.execute_reply":"2025-04-05T10:51:02.476792Z"},"papermill":{"duration":10.186261,"end_time":"2021-03-22T15:38:49.589123","exception":false,"start_time":"2021-03-22T15:38:39.402862","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nfig, ax = plt.subplots(3, 2, figsize=(2*5, 3*5), constrained_layout=True)\n\nfor row in range(3):\n    ax[row][0].imshow(plt.imread(f'/kaggle/working/runs/detect/train/val_batch{row}_labels.jpg'))\n    ax[row][0].set_xticks([])\n    ax[row][0].set_yticks([])\n    ax[row][0].set_title(f'/kaggle/working/runs/detect/train/val_batch{row}_labels.jpg', fontsize=12)\n    \n    ax[row][1].imshow(plt.imread(f'/kaggle/working/runs/detect/train/val_batch{row}_pred.jpg'))\n    ax[row][1].set_xticks([])\n    ax[row][1].set_yticks([])\n    ax[row][1].set_title(f'/kaggle/working/runs/detect/train/val_batch{row}_pred.jpg', fontsize=12)\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T10:51:05.426027Z","iopub.execute_input":"2025-04-05T10:51:05.426394Z","iopub.status.idle":"2025-04-05T10:51:08.306814Z","shell.execute_reply.started":"2025-04-05T10:51:05.42636Z","shell.execute_reply":"2025-04-05T10:51:08.305857Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread('/kaggle/working/runs/detect/train/confusion_matrix.png'));","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:51:08.308526Z","iopub.execute_input":"2025-04-05T10:51:08.308796Z","iopub.status.idle":"2025-04-05T10:51:10.198762Z","shell.execute_reply.started":"2025-04-05T10:51:08.308773Z","shell.execute_reply":"2025-04-05T10:51:10.197758Z"},"papermill":{"duration":5.60974,"end_time":"2021-03-22T15:38:59.568753","exception":false,"start_time":"2021-03-22T15:38:53.959013","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread('/kaggle/working/runs/detect/train/results.png'));","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:52:12.042791Z","iopub.execute_input":"2025-04-05T10:52:12.043134Z","iopub.status.idle":"2025-04-05T10:52:13.354372Z","shell.execute_reply.started":"2025-04-05T10:52:12.043105Z","shell.execute_reply":"2025-04-05T10:52:13.353327Z"},"papermill":{"duration":4.906206,"end_time":"2021-03-22T15:39:08.924971","exception":false,"start_time":"2021-03-22T15:39:04.018765","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread('/kaggle/working/runs/detect/train/F1_curve.png'));","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T10:52:13.355524Z","iopub.execute_input":"2025-04-05T10:52:13.355794Z","iopub.status.idle":"2025-04-05T10:52:14.603952Z","shell.execute_reply.started":"2025-04-05T10:52:13.355772Z","shell.execute_reply":"2025-04-05T10:52:14.60296Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread('/kaggle/working/runs/detect/train/labels.jpg'));","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T10:52:14.605281Z","iopub.execute_input":"2025-04-05T10:52:14.605595Z","iopub.status.idle":"2025-04-05T10:52:15.415043Z","shell.execute_reply.started":"2025-04-05T10:52:14.605572Z","shell.execute_reply":"2025-04-05T10:52:15.413912Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread('//kaggle/working/runs/detect/train/confusion_matrix_normalized.png'));","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T10:52:15.4162Z","iopub.execute_input":"2025-04-05T10:52:15.416552Z","iopub.status.idle":"2025-04-05T10:52:17.256896Z","shell.execute_reply.started":"2025-04-05T10:52:15.416521Z","shell.execute_reply":"2025-04-05T10:52:17.255969Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:52:24.996964Z","iopub.execute_input":"2025-04-05T10:52:24.997267Z","iopub.status.idle":"2025-04-05T10:52:25.24348Z","shell.execute_reply.started":"2025-04-05T10:52:24.997242Z","shell.execute_reply":"2025-04-05T10:52:25.242272Z"},"papermill":{"duration":4.676753,"end_time":"2021-03-22T15:39:18.218375","exception":false,"start_time":"2021-03-22T15:39:13.541622","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# load yolo submission\nyolo = pd.read_csv('/kaggle/working/submission.csv')\neffnetb6 = pd.read_csv('/kaggle/input/vinbigdata-2class-prediction/2-cls test pred.csv') # AUC:0.98\npred = pd.merge(yolo, effnetb6, on = 'image_id', how = 'left')\nlow_thr  = 0.08\nhigh_thr = 0.95","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:52:25.247933Z","iopub.execute_input":"2025-04-05T10:52:25.248248Z","iopub.status.idle":"2025-04-05T10:52:25.303934Z","shell.execute_reply.started":"2025-04-05T10:52:25.248219Z","shell.execute_reply":"2025-04-05T10:52:25.303232Z"},"papermill":{"duration":4.560448,"end_time":"2021-03-22T15:39:26.748396","exception":false,"start_time":"2021-03-22T15:39:22.187948","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def filter_2cls(row, low_thr=low_thr, high_thr=high_thr):\n    prob = row['target']\n    if prob<low_thr:\n        ## Less chance of having any disease\n        row['PredictionString'] = '14 1 0 0 1 1'\n    elif low_thr<=prob<high_thr:\n        ## More change of having any diesease\n        row['PredictionString']+=f' 14 {prob} 0 0 1 1'\n    elif high_thr<=prob:\n        ## Good chance of having any disease so believe in object detection model\n        row['PredictionString'] = row['PredictionString']\n    else:\n        raise ValueError('Prediction must be from [0-1]')\n    return row","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:52:26.528796Z","iopub.execute_input":"2025-04-05T10:52:26.529102Z","iopub.status.idle":"2025-04-05T10:52:26.534096Z","shell.execute_reply.started":"2025-04-05T10:52:26.52908Z","shell.execute_reply":"2025-04-05T10:52:26.533022Z"},"papermill":{"duration":4.5035,"end_time":"2021-03-22T15:39:35.686503","exception":false,"start_time":"2021-03-22T15:39:31.183003","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub = pred.apply(filter_2cls, axis=1)\nsub[['image_id', 'PredictionString']].to_csv('../submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2025-04-05T10:52:27.799141Z","iopub.execute_input":"2025-04-05T10:52:27.799503Z","iopub.status.idle":"2025-04-05T10:52:27.981176Z","shell.execute_reply.started":"2025-04-05T10:52:27.799474Z","shell.execute_reply":"2025-04-05T10:52:27.980286Z"},"papermill":{"duration":4.269371,"end_time":"2021-03-22T15:39:44.068693","exception":false,"start_time":"2021-03-22T15:39:39.799322","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}