{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":29653,"databundleVersionId":2420395,"sourceType":"competition"},{"sourceId":9155373,"sourceType":"datasetVersion","datasetId":5530724}],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","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\nfor 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-12T16:54:06.105328Z","iopub.execute_input":"2024-08-12T16:54:06.105974Z","iopub.status.idle":"2024-08-12T16:54:06.894780Z","shell.execute_reply.started":"2024-08-12T16:54:06.105941Z","shell.execute_reply":"2024-08-12T16:54:06.893587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ultralytics","metadata":{"execution":{"iopub.status.busy":"2024-08-15T07:45:00.827624Z","iopub.execute_input":"2024-08-15T07:45:00.828823Z","iopub.status.idle":"2024-08-15T07:45:19.589910Z","shell.execute_reply.started":"2024-08-15T07:45:00.828769Z","shell.execute_reply":"2024-08-15T07:45:19.588549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pillow","metadata":{"execution":{"iopub.status.busy":"2024-08-15T16:26:58.053976Z","iopub.execute_input":"2024-08-15T16:26:58.054580Z","iopub.status.idle":"2024-08-15T16:27:13.378659Z","shell.execute_reply.started":"2024-08-15T16:26:58.054532Z","shell.execute_reply":"2024-08-15T16:27:13.377014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pydicom","metadata":{"execution":{"iopub.status.busy":"2024-08-15T16:26:40.590060Z","iopub.execute_input":"2024-08-15T16:26:40.590648Z","iopub.status.idle":"2024-08-15T16:26:55.962584Z","shell.execute_reply.started":"2024-08-15T16:26:40.590599Z","shell.execute_reply":"2024-08-15T16:26:55.961147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install natsort","metadata":{"execution":{"iopub.status.busy":"2024-08-15T16:26:20.085159Z","iopub.execute_input":"2024-08-15T16:26:20.085611Z","iopub.status.idle":"2024-08-15T16:26:37.326334Z","shell.execute_reply.started":"2024-08-15T16:26:20.085566Z","shell.execute_reply":"2024-08-15T16:26:37.324779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import ultralytics\n# from ultralytics import YOLO\nimport pandas as pd\nimport numpy as np\nimport math\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nfrom matplotlib import image\nfrom PIL import Image\nimport seaborn as sns\nimport random\nimport shutil\nimport pydicom as dicom\nfrom natsort import natsorted","metadata":{"execution":{"iopub.status.busy":"2024-08-15T16:27:15.800060Z","iopub.execute_input":"2024-08-15T16:27:15.800679Z","iopub.status.idle":"2024-08-15T16:27:15.852190Z","shell.execute_reply.started":"2024-08-15T16:27:15.800634Z","shell.execute_reply":"2024-08-15T16:27:15.850875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if os.path.exists('axial_t1wce_2_class'):\n    shutil.rmtree('axial_t1wce_2_class')\nshutil.copytree('/kaggle/input/brain-tumor/axial_t1wce_2_class/train', 'axial_t1wce_2_class/train')","metadata":{"execution":{"iopub.status.busy":"2024-08-12T16:47:22.888853Z","iopub.execute_input":"2024-08-12T16:47:22.889522Z","iopub.status.idle":"2024-08-12T16:47:26.167689Z","shell.execute_reply.started":"2024-08-12T16:47:22.889468Z","shell.execute_reply":"2024-08-12T16:47:26.166509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if os.path.exists('coronal_t1wce_2_class'):\n    shutil.rmtree('coronal_t1wce_2_class')\nshutil.copytree('/kaggle/input/brain-tumor/coronal_t1wce_2_class/train', 'coronal_t1wce_2_class/train')","metadata":{"execution":{"iopub.status.busy":"2024-08-12T16:48:19.265948Z","iopub.execute_input":"2024-08-12T16:48:19.266395Z","iopub.status.idle":"2024-08-12T16:48:22.768825Z","shell.execute_reply.started":"2024-08-12T16:48:19.266363Z","shell.execute_reply":"2024-08-12T16:48:22.767452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if os.path.exists('sagittal_t1wce_2_class'):\n    shutil.rmtree('sagittal_t1wce_2_class')\nshutil.copytree('/kaggle/input/brain-tumor/sagittal_t1wce_2_class/train', 'sagittal_t1wce_2_class/train')","metadata":{"execution":{"iopub.status.busy":"2024-08-12T16:48:55.370192Z","iopub.execute_input":"2024-08-12T16:48:55.370659Z","iopub.status.idle":"2024-08-12T16:48:58.132147Z","shell.execute_reply.started":"2024-08-12T16:48:55.370618Z","shell.execute_reply":"2024-08-12T16:48:58.130560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Evaluate(num_train_images, train_images):\n    val_split = int(num_train_images * 0.2)\n    val_images = random.sample(train_images, val_split)\n    train_images = [img for img in train_images if img not in val_images]  # Remaining 80% for training\n    return train_images, val_images","metadata":{"execution":{"iopub.status.busy":"2024-08-12T16:49:06.056524Z","iopub.execute_input":"2024-08-12T16:49:06.057142Z","iopub.status.idle":"2024-08-12T16:49:06.064033Z","shell.execute_reply.started":"2024-08-12T16:49:06.057099Z","shell.execute_reply":"2024-08-12T16:49:06.062787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir_axial_img_train = os.listdir('/kaggle/working/axial_t1wce_2_class/train/images')\ndir_coronal_img_train = os.listdir('/kaggle/working/coronal_t1wce_2_class/train/images')\ndir_sagittal_img_train = os.listdir('/kaggle/working/sagittal_t1wce_2_class/train/images')\naxial_img_train = len(dir_axial_img_train)\ncoronal_img_train = len(dir_coronal_img_train)\nsagittal_img_train = len(dir_sagittal_img_train)\naxial_train_array, axial_img_val = Evaluate(axial_img_train, os.listdir('/kaggle/working/axial_t1wce_2_class/train/images'))\ncoronal_train_array, coronal_img_val = Evaluate(coronal_img_train, os.listdir('/kaggle/working/coronal_t1wce_2_class/train/images'))\nsagittal_train_array, sagittal_img_val = Evaluate(sagittal_img_train, os.listdir('/kaggle/working/sagittal_t1wce_2_class/train/images'))\nprint(axial_img_val)\nprint(coronal_img_val)\nprint(sagittal_img_val)","metadata":{"execution":{"iopub.status.busy":"2024-08-12T16:49:18.706812Z","iopub.execute_input":"2024-08-12T16:49:18.707257Z","iopub.status.idle":"2024-08-12T16:49:18.720255Z","shell.execute_reply.started":"2024-08-12T16:49:18.707221Z","shell.execute_reply":"2024-08-12T16:49:18.718968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(axial_train_array), len(axial_img_val))\nprint(len(coronal_train_array), len(coronal_img_val))\nprint(len(sagittal_train_array), len(sagittal_img_val))\n","metadata":{"execution":{"iopub.status.busy":"2024-08-12T16:49:30.651509Z","iopub.execute_input":"2024-08-12T16:49:30.652820Z","iopub.status.idle":"2024-08-12T16:49:30.659069Z","shell.execute_reply.started":"2024-08-12T16:49:30.652776Z","shell.execute_reply":"2024-08-12T16:49:30.657683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/working/axial_t1wce_2_class/val'\n\n# Tạo thư mục và các thư mục con nếu chưa tồn tại\nos.makedirs(path, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2024-08-12T16:50:58.404640Z","iopub.execute_input":"2024-08-12T16:50:58.405723Z","iopub.status.idle":"2024-08-12T16:50:58.411358Z","shell.execute_reply.started":"2024-08-12T16:50:58.405679Z","shell.execute_reply":"2024-08-12T16:50:58.409820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def move_val_data(dataset, val_images):\n  if os.path.exists(f'/kaggle/working/{dataset}/val/images'):\n      shutil.rmtree(f'/kaggle/working/{dataset}/val/images')\n  if os.path.exists(f'/kaggle/working/{dataset}/val/labels'):\n      shutil.rmtree(f'/kaggle/working/{dataset}/val/labels')\n\n  os.mkdir(f'/kaggle/working/{dataset}/val/images')\n  os.mkdir(f'/kaggle/working/{dataset}/val/labels')\n\n  for image in val_images:\n      shutil.move(os.path.join(f'/kaggle/working/{dataset}/train/images', image), f'/kaggle/working/{dataset}/val/images')\n\n  for image in val_images:\n      label = os.path.splitext(image)[0] + '.txt'\n      shutil.move(os.path.join(f'/kaggle/working/{dataset}/train/labels', label), f'/kaggle/working/{dataset}/val/labels')\n\nmove_val_data('axial_t1wce_2_class', axial_img_val)\nmove_val_data('coronal_t1wce_2_class', coronal_img_val)\nmove_val_data('sagittal_t1wce_2_class', sagittal_img_val)","metadata":{"execution":{"iopub.status.busy":"2024-08-12T16:51:33.200736Z","iopub.execute_input":"2024-08-12T16:51:33.201237Z","iopub.status.idle":"2024-08-12T16:51:33.231737Z","shell.execute_reply.started":"2024-08-12T16:51:33.201201Z","shell.execute_reply":"2024-08-12T16:51:33.230531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dir_axial_img_train = os.listdir('/kaggle/working/axial_t1wce_2_class/train/images')\ndir_coronal_img_train = os.listdir('/kaggle/working/coronal_t1wce_2_class/train/images')\ndir_sagittal_img_train = os.listdir('/kaggle/working/sagittal_t1wce_2_class/train/images')\naxial_img_train = len(dir_axial_img_train)\ncoronal_img_train = len(dir_coronal_img_train)\nsagittal_img_train = len(dir_sagittal_img_train)\nprint(axial_img_train)\nprint(coronal_img_train)\nprint(sagittal_img_train)","metadata":{"execution":{"iopub.status.busy":"2024-08-12T16:55:01.213347Z","iopub.execute_input":"2024-08-12T16:55:01.214135Z","iopub.status.idle":"2024-08-12T16:55:01.220590Z","shell.execute_reply.started":"2024-08-12T16:55:01.214104Z","shell.execute_reply":"2024-08-12T16:55:01.219587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text = \"\"\"\npath: /kaggle/working/\ntrain:\n  - axial_t1wce_2_class/train/images\n  - coronal_t1wce_2_class/train/images\n  - sagittal_t1wce_2_class/train/images\nval:\n  - axial_t1wce_2_class/val/images\n  - coronal_t1wce_2_class/val/images\n  - sagittal_t1wce_2_class/val/images\n# Classes\nnc: 6\nnames:\n  0: 'axial_negative'\n  1: 'axial_positive'\n  2: 'coronal_negative'\n  3: 'coronal_positive'\n  4: 'sagittal_negative'\n  5: 'sagittal_positive'\n\"\"\"\n\n\nwith open(\"/kaggle/working/data.yaml\", 'w') as file:\n      file.write(text)","metadata":{"execution":{"iopub.status.busy":"2024-08-12T16:55:07.187190Z","iopub.execute_input":"2024-08-12T16:55:07.187559Z","iopub.status.idle":"2024-08-12T16:55:07.194036Z","shell.execute_reply.started":"2024-08-12T16:55:07.187530Z","shell.execute_reply":"2024-08-12T16:55:07.192929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = YOLO('yolov10s.pt')","metadata":{"execution":{"iopub.status.busy":"2024-08-12T16:56:02.012598Z","iopub.execute_input":"2024-08-12T16:56:02.012937Z","iopub.status.idle":"2024-08-12T16:56:02.831992Z","shell.execute_reply.started":"2024-08-12T16:56:02.012909Z","shell.execute_reply":"2024-08-12T16:56:02.830996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.info()","metadata":{"execution":{"iopub.status.busy":"2024-08-12T16:56:05.377334Z","iopub.execute_input":"2024-08-12T16:56:05.378250Z","iopub.status.idle":"2024-08-12T16:56:05.879044Z","shell.execute_reply.started":"2024-08-12T16:56:05.378210Z","shell.execute_reply":"2024-08-12T16:56:05.878089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"wandb login","metadata":{"execution":{"iopub.status.busy":"2024-08-12T16:57:22.129462Z","iopub.execute_input":"2024-08-12T16:57:22.130392Z","iopub.status.idle":"2024-08-12T16:57:45.398003Z","shell.execute_reply.started":"2024-08-12T16:57:22.130357Z","shell.execute_reply":"2024-08-12T16:57:45.396930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import wandb\nwandb.login(key='c74855400c44bcf22055bb3395f3fb76e8f9066e')","metadata":{"execution":{"iopub.status.busy":"2024-08-12T16:58:18.397807Z","iopub.execute_input":"2024-08-12T16:58:18.398916Z","iopub.status.idle":"2024-08-12T16:58:18.463662Z","shell.execute_reply.started":"2024-08-12T16:58:18.398880Z","shell.execute_reply":"2024-08-12T16:58:18.462750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = model.train(data = \"/kaggle/working/data.yaml\", epochs=250, imgsz=640, device=[0,1])","metadata":{"execution":{"iopub.status.busy":"2024-08-15T07:53:02.235495Z","iopub.execute_input":"2024-08-15T07:53:02.236521Z","iopub.status.idle":"2024-08-15T07:53:02.287667Z","shell.execute_reply.started":"2024-08-15T07:53:02.236472Z","shell.execute_reply":"2024-08-15T07:53:02.285910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metrics=image.imread('/kaggle/working/runs/detect/train2/results.png')\nplt.figure(figsize=(12,7))\nplt.imshow(metrics)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-12T17:58:48.929091Z","iopub.execute_input":"2024-08-12T17:58:48.929747Z","iopub.status.idle":"2024-08-12T17:58:49.680658Z","shell.execute_reply.started":"2024-08-12T17:58:48.929713Z","shell.execute_reply":"2024-08-12T17:58:49.679788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conf_matrix=image.imread('/kaggle/working/runs/detect/train2/confusion_matrix.png')\nplt.figure(figsize=(10,10))\nplt.imshow(conf_matrix)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-12T17:59:03.786380Z","iopub.execute_input":"2024-08-12T17:59:03.786822Z","iopub.status.idle":"2024-08-12T17:59:05.221954Z","shell.execute_reply.started":"2024-08-12T17:59:03.786785Z","shell.execute_reply":"2024-08-12T17:59:05.221075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# All the important curves\n\ncurves=['R_curve','P_curve','PR_curve','F1_curve']\nfor curve in curves:\n    img=image.imread('/kaggle/working/runs/detect/train2/'+curve+'.png')\n    plt.imshow(img)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-12T17:59:51.674268Z","iopub.execute_input":"2024-08-12T17:59:51.675025Z","iopub.status.idle":"2024-08-12T17:59:54.744887Z","shell.execute_reply.started":"2024-08-12T17:59:51.674991Z","shell.execute_reply":"2024-08-12T17:59:54.743636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read images\nlabel1 = plt.imread('/kaggle/working/runs/detect/train2/val_batch0_labels.jpg')\nlabel2 = plt.imread('/kaggle/working/runs/detect/train2/val_batch1_labels.jpg')\npred1 = plt.imread('/kaggle/working/runs/detect/train2/val_batch0_pred.jpg')\npred2 = plt.imread('/kaggle/working/runs/detect/train2/val_batch1_pred.jpg')\n\n# Create subplots with increased figure size\nfig, axarr = plt.subplots(2, 2, figsize=(12, 12))\n\n# Display images with titles and remove axis numbers\naxarr[0, 0].imshow(label1)\naxarr[0, 0].set_title('Label 1')\naxarr[0, 0].axis('off')\n\naxarr[0, 1].imshow(pred1)\naxarr[0, 1].set_title('Prediction 1')\naxarr[0, 1].axis('off')\n\naxarr[1, 0].imshow(label2)\naxarr[1, 0].set_title('Label 2')\naxarr[1, 0].axis('off')\n\naxarr[1, 1].imshow(pred2)\naxarr[1, 1].set_title('Prediction 2')\naxarr[1, 1].axis('off')\n\n# Adjust layout with reduced padding\nplt.tight_layout(pad=1)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-12T18:02:38.331683Z","iopub.execute_input":"2024-08-12T18:02:38.332362Z","iopub.status.idle":"2024-08-12T18:02:40.835635Z","shell.execute_reply.started":"2024-08-12T18:02:38.332334Z","shell.execute_reply":"2024-08-12T18:02:40.834587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_model = YOLO('/kaggle/working/runs/detect/train2/weights/best.pt')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T07:54:54.645256Z","iopub.execute_input":"2024-08-15T07:54:54.645842Z","iopub.status.idle":"2024-08-15T07:54:54.831860Z","shell.execute_reply.started":"2024-08-15T07:54:54.645800Z","shell.execute_reply":"2024-08-15T07:54:54.830231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = best_model.predict('/kaggle/input/brain-tumor/axial_t1wce_2_class/test/images', conf=0.4, save_txt=True, save_conf=True)","metadata":{"execution":{"iopub.status.busy":"2024-08-12T19:42:17.336799Z","iopub.execute_input":"2024-08-12T19:42:17.337196Z","iopub.status.idle":"2024-08-12T19:42:48.496070Z","shell.execute_reply.started":"2024-08-12T19:42:17.337166Z","shell.execute_reply":"2024-08-12T19:42:48.495239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_box(file_path, file_name, img):\n    txt_path = os.path.join(file_path, f'{file_name}.txt')\n    color = (0,0,0)\n    label = 'unknown'\n    x_pt1, y_pt1, x_pt2, y_pt2 = 0, 0, 0, 0 \n    if os.path.exists(txt_path):\n        with open(os.path.join(file_path, f'{file_name}.txt'), 'r') as f:\n            labels = f.readlines()\n            labels = labels[0].split(' ')\n            f.close()\n          \n        tumor_class, x, y, w, h = int(labels[0]), float(labels[1]), float(labels[2]), float(labels[3]), float(labels[4])\n        x_pt1 = int((x - w/2) * img.shape[1])\n        y_pt1 = int((y - h/2) * img.shape[0])\n        x_pt2 = int((x + w/2) * img.shape[1])\n        y_pt2 = int((y + h/2) * img.shape[0])\n\n        if tumor_class == 0:\n            color = (255, 165, 0)\n            label = 'axial_negative'\n        elif tumor_class == 1: \n            color = (225, 192, 203)\n            label = 'axial_positive'\n        elif tumor_class == 2:\n            color = (0, 225, 0)\n            label = 'coronal_negative'\n        elif tumor_class == 3:\n            color = (255, 255, 0)\n            label = 'coronal_positive'\n        elif tumor_class == 4:\n            color = (204, 153, 255)\n            label = 'sagittal_negative'\n        else:\n            color = (173, 216, 230)\n            label = 'sagittal_positive'\n        \n        if (len(labels) > 5):\n            prob = float(labels[5])\n            prob = round(prob, 1)\n            prob = str(prob)\n            label = label + ' ' + prob\n            \n    cv2.rectangle(img, (x_pt1, y_pt1), (x_pt2, y_pt2), color, 2)\n    cv2.putText(img, label, (x_pt1 - 50, y_pt1 - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.45, color, 1)\n\n    ","metadata":{"execution":{"iopub.status.busy":"2024-08-15T17:29:46.401662Z","iopub.execute_input":"2024-08-15T17:29:46.402210Z","iopub.status.idle":"2024-08-15T17:29:46.422053Z","shell.execute_reply.started":"2024-08-15T17:29:46.402170Z","shell.execute_reply":"2024-08-15T17:29:46.420530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_img(file_dir, file_path_pred, file_name):\n    files = os.listdir(os.path.join(file_dir, f'{file_name}/test/images'))\n    random_file = random.sample(files, 16)\n    fig, axes = plt.subplots(4, 4, figsize=(16,16))\n    for i, file in enumerate(random_file):\n        row = i//4\n        col = i%4\n       \n        filename = os.path.splitext(file)[0]\n        \n        img_pred = os.path.join(file_dir, f'{file_name}/test/images', f'{filename}.jpg')\n        img_pred = cv2.imread(img_pred, 1)\n        img_pred = cv2.cvtColor(img_pred, cv2.COLOR_BGR2RGB)\n        draw_box(file_path_pred, filename, img_pred)\n        \n        axes[row, col].imshow(img_pred)\n        axes[row, col].set_title(file)\n        axes[row, col].axis('off')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-15T07:48:26.556188Z","iopub.execute_input":"2024-08-15T07:48:26.556624Z","iopub.status.idle":"2024-08-15T07:48:26.567061Z","shell.execute_reply.started":"2024-08-15T07:48:26.556578Z","shell.execute_reply":"2024-08-15T07:48:26.565678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_img('/kaggle/input/brain-tumor','/kaggle/working/runs/detect/predict/labels', 'axial_t1wce_2_class')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T06:31:23.256670Z","iopub.execute_input":"2024-08-15T06:31:23.257670Z","iopub.status.idle":"2024-08-15T06:31:25.044506Z","shell.execute_reply.started":"2024-08-15T06:31:23.257630Z","shell.execute_reply":"2024-08-15T06:31:25.043064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict1 = best_model.predict('/kaggle/input/brain-tumor/coronal_t1wce_2_class/test/images', conf=0.4, save_txt=True, save_conf=True)","metadata":{"execution":{"iopub.status.busy":"2024-08-12T20:01:55.547931Z","iopub.execute_input":"2024-08-12T20:01:55.548494Z","iopub.status.idle":"2024-08-12T20:02:28.180010Z","shell.execute_reply.started":"2024-08-12T20:01:55.548447Z","shell.execute_reply":"2024-08-12T20:02:28.179155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_img('/kaggle/input/brain-tumor','/kaggle/working/runs/detect/predict/labels', 'coronal_t1wce_2_class')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T06:30:56.894369Z","iopub.execute_input":"2024-08-15T06:30:56.894789Z","iopub.status.idle":"2024-08-15T06:30:59.342785Z","shell.execute_reply.started":"2024-08-15T06:30:56.894758Z","shell.execute_reply":"2024-08-15T06:30:59.341262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = best_model.predict('/kaggle/input/brain-tumor/sagittal_t1wce_2_class/test/images', conf=0.4, save_txt=True, save_conf=True)","metadata":{"execution":{"iopub.status.busy":"2024-08-12T20:06:10.757589Z","iopub.execute_input":"2024-08-12T20:06:10.758058Z","iopub.status.idle":"2024-08-12T20:06:42.656497Z","shell.execute_reply.started":"2024-08-12T20:06:10.758023Z","shell.execute_reply":"2024-08-12T20:06:42.655449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_img('/kaggle/input/brain-tumor','/kaggle/working/runs/detect/predict/labels', 'sagittal_t1wce_2_class')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T06:31:33.281823Z","iopub.execute_input":"2024-08-15T06:31:33.282917Z","iopub.status.idle":"2024-08-15T06:31:35.663039Z","shell.execute_reply.started":"2024-08-15T06:31:33.282859Z","shell.execute_reply":"2024-08-15T06:31:35.661601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Chuyển đổi file dicom sang jpg","metadata":{}},{"cell_type":"code","source":"def convert_dcm_to_jpg(dcm_folder, jpg_folder):\n    if not os.path.exists(jpg_folder):\n        os.makedirs(jpg_folder)\n    \n    for filename in os.listdir(dcm_folder):\n        if filename.endswith('.dcm'):\n            dcm_path = os.path.join(dcm_folder, filename)\n            jpg_path = os.path.join(jpg_folder, filename.replace('.dcm', '.jpg'))\n            \n            # Đọc file DICOM\n            dcm = dicom.dcmread(dcm_path)\n            \n            # Chuyển đổi thành ảnh JPG\n            img_array = dcm.pixel_array\n            \n            #Chuyển hóa giá trị pixel thành dạng uint8 (0-255)\n            img_array = (img_array / np.max(img_array)) * 255.0\n            img_array = img_array.astype(np.uint8)\n\n            # Chuyển đổi array của numpy thành ảnh\n            img = Image.fromarray(img_array)\n            # Lưu ảnh JPG\n            img.save(jpg_path)\n            print(f'Converted {dcm_path} to {jpg_path}')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T06:34:21.885312Z","iopub.execute_input":"2024-08-15T06:34:21.885739Z","iopub.status.idle":"2024-08-15T06:34:21.894668Z","shell.execute_reply.started":"2024-08-15T06:34:21.885699Z","shell.execute_reply":"2024-08-15T06:34:21.893385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Chọn 5 người từ file dicom để chuyển sang jpg","metadata":{}},{"cell_type":"code","source":"os.makedirs('/kaggle/working/rsna-miccai-brain-tumor-radiogenomic-classification')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T06:47:29.275686Z","iopub.execute_input":"2024-08-15T06:47:29.276100Z","iopub.status.idle":"2024-08-15T06:47:29.282824Z","shell.execute_reply.started":"2024-08-15T06:47:29.276070Z","shell.execute_reply":"2024-08-15T06:47:29.280927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = os.listdir('/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train')\nrandom_file = random.sample(files, 5)\nfor car in random_file:\n    file_path = os.path.join('/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train', car)\n    for file in os.listdir(file_path):\n        convert_dcm_to_jpg(os.path.join(file_path, file), f'/kaggle/working/rsna-miccai-brain-tumor-radiogenomic-classification/{car}/{file}')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T06:52:08.039840Z","iopub.execute_input":"2024-08-15T06:52:08.040267Z","iopub.status.idle":"2024-08-15T06:52:51.449817Z","shell.execute_reply.started":"2024-08-15T06:52:08.040237Z","shell.execute_reply":"2024-08-15T06:52:51.448645Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in os.listdir('/kaggle/working/rsna-miccai-brain-tumor-radiogenomic-classification'):\n    print(i)","metadata":{"execution":{"iopub.status.busy":"2024-08-15T07:01:36.943838Z","iopub.execute_input":"2024-08-15T07:01:36.944268Z","iopub.status.idle":"2024-08-15T07:01:36.951290Z","shell.execute_reply.started":"2024-08-15T07:01:36.944237Z","shell.execute_reply":"2024-08-15T07:01:36.949793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw_img_dicom(file_dir, file_path_pred, file_name):\n    # Lấy danh sách các tệp trong thư mục con\n    files = os.listdir(file_path_pred)\n    files = natsorted(files)\n    n = math.ceil((len(files)/4))\n    fig, axes = plt.subplots(n, 4, figsize=(16,n*4))\n    \n    if n == 1:\n        axes = axes.reshape(1, 4)\n    \n    for i, file in enumerate(files):\n        row = i//4\n        col = i%4\n    \n        filename = os.path.splitext(file)[0]\n        \n        # Đọc hình ảnh dự đoán\n        img_pred_path = os.path.join(file_dir, file_name, f'{filename}.jpg')\n        img_pred = cv2.imread(img_pred_path, 1)\n        img_pred = cv2.cvtColor(img_pred, cv2.COLOR_BGR2RGB)\n        \n        # Vẽ hộp nếu cần\n        draw_box(file_path_pred, filename, img_pred)\n        \n        # Hiển thị hình ảnh\n        axes[row, col].imshow(img_pred)\n        axes[row, col].set_title(file)\n        axes[row, col].axis('off')\n        \n        for j in range(len(files), n * 4):\n            row = j // 4\n            col = j % 4\n            axes[row, col].axis('off')\n        \n    plt.tight_layout()\n    plt.show() ","metadata":{"execution":{"iopub.status.busy":"2024-08-15T17:29:34.027433Z","iopub.execute_input":"2024-08-15T17:29:34.029253Z","iopub.status.idle":"2024-08-15T17:29:34.042357Z","shell.execute_reply.started":"2024-08-15T17:29:34.029202Z","shell.execute_reply":"2024-08-15T17:29:34.040991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Dự đoán các mặt của một người","metadata":{}},{"cell_type":"code","source":"def train_cat(cat):\n    input_dir = f'/kaggle/working/rsna-miccai-brain-tumor-radiogenomic-classification/{cat}'\n\n    output_base_dir = os.path.join('/kaggle/working/runs/detect/predict3', os.path.basename(input_dir))\n\n    for item in os.listdir(input_dir):\n        item_path = os.path.join(input_dir, item)\n\n        output_dir = os.path.join(output_base_dir, item)\n        os.makedirs(output_dir, exist_ok=True)\n\n        predict_dicom1 = best_model.predict(item_path,conf=0.4, save_txt=True, save_conf=True)\n        shutil.move(os.path.join('/kaggle/working/runs/detect/predict3', 'labels'), output_dir)\n\n    if os.path.exists('/kaggle/working/runs/detect/predict3/labels'):\n        shutil.rmtree('/kaggle/working/runs/detect/predict3/labels')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T07:59:27.503403Z","iopub.execute_input":"2024-08-15T07:59:27.503899Z","iopub.status.idle":"2024-08-15T07:59:27.513381Z","shell.execute_reply.started":"2024-08-15T07:59:27.503864Z","shell.execute_reply":"2024-08-15T07:59:27.511699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cat('00060')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T07:59:38.204892Z","iopub.execute_input":"2024-08-15T07:59:38.205319Z","iopub.status.idle":"2024-08-15T08:05:45.539166Z","shell.execute_reply.started":"2024-08-15T07:59:38.205287Z","shell.execute_reply":"2024-08-15T08:05:45.537888Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_img_dicom('/kaggle/working/rsna-miccai-brain-tumor-radiogenomic-classification/00060', f'/kaggle/working/runs/detect/predict3/00060/T1wCE/labels', 'T1wCE')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T17:29:52.173933Z","iopub.execute_input":"2024-08-15T17:29:52.174440Z","iopub.status.idle":"2024-08-15T17:29:56.124591Z","shell.execute_reply.started":"2024-08-15T17:29:52.174402Z","shell.execute_reply":"2024-08-15T17:29:56.123242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_img_dicom('/kaggle/working/rsna-miccai-brain-tumor-radiogenomic-classification/00060', f'/kaggle/working/runs/detect/predict3/00060/FLAIR/labels', 'FLAIR')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T17:30:15.381969Z","iopub.execute_input":"2024-08-15T17:30:15.382527Z","iopub.status.idle":"2024-08-15T17:30:21.400061Z","shell.execute_reply.started":"2024-08-15T17:30:15.382480Z","shell.execute_reply":"2024-08-15T17:30:21.398815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_img_dicom('/kaggle/working/rsna-miccai-brain-tumor-radiogenomic-classification/00060', '/kaggle/working/runs/detect/predict3/00060/T1w/labels', 'T1w')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T08:28:03.318836Z","iopub.execute_input":"2024-08-15T08:28:03.319316Z","iopub.status.idle":"2024-08-15T08:28:03.714162Z","shell.execute_reply.started":"2024-08-15T08:28:03.319282Z","shell.execute_reply":"2024-08-15T08:28:03.712978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_img_dicom('/kaggle/working/rsna-miccai-brain-tumor-radiogenomic-classification/00060', f'/kaggle/working/runs/detect/predict3/00060/T2w/labels', 'T2w')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T08:24:07.199182Z","iopub.execute_input":"2024-08-15T08:24:07.199624Z","iopub.status.idle":"2024-08-15T08:24:21.452660Z","shell.execute_reply.started":"2024-08-15T08:24:07.199577Z","shell.execute_reply":"2024-08-15T08:24:21.450313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Predict tập ảnh người không có ung thư 00242","metadata":{}},{"cell_type":"code","source":"train_cat('00242')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T08:30:39.844908Z","iopub.execute_input":"2024-08-15T08:30:39.845399Z","iopub.status.idle":"2024-08-15T08:33:25.638344Z","shell.execute_reply.started":"2024-08-15T08:30:39.845358Z","shell.execute_reply":"2024-08-15T08:33:25.637446Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_img_dicom('/kaggle/working/rsna-miccai-brain-tumor-radiogenomic-classification/00242', '/kaggle/working/runs/detect/predict3/00242/T1wCE/labels', 'T1wCE')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T08:39:41.685867Z","iopub.execute_input":"2024-08-15T08:39:41.687190Z","iopub.status.idle":"2024-08-15T08:39:48.156021Z","shell.execute_reply.started":"2024-08-15T08:39:41.687147Z","shell.execute_reply":"2024-08-15T08:39:48.154741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_img_dicom('/kaggle/working/rsna-miccai-brain-tumor-radiogenomic-classification/00242', '/kaggle/working/runs/detect/predict3/00242/FLAIR/labels', 'FLAIR')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T08:40:40.284944Z","iopub.execute_input":"2024-08-15T08:40:40.286217Z","iopub.status.idle":"2024-08-15T08:40:43.431106Z","shell.execute_reply.started":"2024-08-15T08:40:40.286150Z","shell.execute_reply":"2024-08-15T08:40:43.429738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_img_dicom('/kaggle/working/rsna-miccai-brain-tumor-radiogenomic-classification/00242', '/kaggle/working/runs/detect/predict3/00242/T1w/labels', 'T1w')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T08:41:17.436749Z","iopub.execute_input":"2024-08-15T08:41:17.438266Z","iopub.status.idle":"2024-08-15T08:41:21.299488Z","shell.execute_reply.started":"2024-08-15T08:41:17.438207Z","shell.execute_reply":"2024-08-15T08:41:21.297996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw_img_dicom('/kaggle/working/rsna-miccai-brain-tumor-radiogenomic-classification/00242', '/kaggle/working/runs/detect/predict3/00242/T2w/labels', 'T2w')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T08:42:57.565931Z","iopub.execute_input":"2024-08-15T08:42:57.566429Z","iopub.status.idle":"2024-08-15T08:43:01.072427Z","shell.execute_reply.started":"2024-08-15T08:42:57.566396Z","shell.execute_reply":"2024-08-15T08:43:01.071015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def many_cat(dir_cat):\n    folder = []\n    face_tumor = []\n    SL = []\n    for cat in os.listdir(dir_cat):\n        axial = 0\n        coronal = 0\n        sagittal = 0\n        \n        dir_file = os.path.join(dir_cat,cat,\"labels\")\n        for file in os.listdir(dir_file):\n            with open(os.path.join(dir_file, f'{file}'), 'r') as f:\n                labels = f.readlines()\n                labels = labels[0].split(' ')\n                f.close()\n                \n            tumor_class = int(labels[0])\n            if (tumor_class == 0) or (tumor_class == 1):\n                axial += 1\n            elif (tumor_class == 2) or (tumor_class == 3):\n                coronal += 1\n            else:\n                sagittal += 1\n        if (axial > coronal) and (axial > sagittal):\n            folder.append(cat)\n            face_tumor.append('axial')\n            SL.append(axial)\n            print(f\"{cat} có axial nhiều là: {axial},{coronal},{sagittal}\")\n        elif (coronal > axial) and (coronal > sagittal):\n            folder.append(cat)\n            face_tumor.append('coronal')\n            SL.append(coronal)\n            print(f\"{cat} có coronal nhiều là:  {axial},{coronal},{sagittal}\")\n        else:\n            folder.append(cat)\n            face_tumor.append('sagittal')\n            SL.append(sagittal)\n            print(f\"{cat} có sagittal nhiều là:  {axial},{coronal},{sagittal}\")\n            \n    return folder ,face_tumor, SL\n    ","metadata":{"execution":{"iopub.status.busy":"2024-08-15T16:27:45.591640Z","iopub.execute_input":"2024-08-15T16:27:45.592348Z","iopub.status.idle":"2024-08-15T16:27:45.606367Z","shell.execute_reply.started":"2024-08-15T16:27:45.592310Z","shell.execute_reply":"2024-08-15T16:27:45.604968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder, face_tumor, sl = many_cat('/kaggle/working/runs/detect/predict3/00060')","metadata":{"execution":{"iopub.status.busy":"2024-08-15T16:27:49.125027Z","iopub.execute_input":"2024-08-15T16:27:49.126374Z","iopub.status.idle":"2024-08-15T16:27:49.144002Z","shell.execute_reply.started":"2024-08-15T16:27:49.126324Z","shell.execute_reply":"2024-08-15T16:27:49.142204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(folder, face_tumor, sl)","metadata":{"execution":{"iopub.status.busy":"2024-08-15T16:27:52.894715Z","iopub.execute_input":"2024-08-15T16:27:52.895286Z","iopub.status.idle":"2024-08-15T16:27:52.902198Z","shell.execute_reply.started":"2024-08-15T16:27:52.895245Z","shell.execute_reply":"2024-08-15T16:27:52.900773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_cm(x):\n    y = x*512\n    mm = y*0.5\n    return mm/10","metadata":{"execution":{"iopub.status.busy":"2024-08-15T18:15:06.130680Z","iopub.execute_input":"2024-08-15T18:15:06.131188Z","iopub.status.idle":"2024-08-15T18:15:06.137683Z","shell.execute_reply.started":"2024-08-15T18:15:06.131152Z","shell.execute_reply":"2024-08-15T18:15:06.136090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder = ['T1w', 'T2w', 'FLAIR']\nface_tumor = ['axial', 'sagittal', 'coronal']\nsl = [1, 83, 27]\nweight1 = []\nw_h = 0\n# Lấy chiều rộng mặt sagittal và chiều rộng, cao coronal\ndir = '/kaggle/working/runs/detect/predict3/00060/T2w/labels'\nfor i in os.listdir(dir):\n    with open(os.path.join(dir, i), 'r') as f:\n        file = f.readlines()\n        file = file[0].split(' ')\n        f.close()\n    \n    weight1.append(float(file[3]))\nmax_weight1 = max(weight1)\nprint(convert_cm(max_weight1))\n\nweight2 = []\nheight = []\ndir2 = '/kaggle/working/runs/detect/predict3/00060/FLAIR/labels'\nfor j in os.listdir(dir2):\n    with open(os.path.join(dir2, j), 'r') as f:\n        file_coro = f.readlines()\n        file_coro = file_coro[0].split(' ')\n        f.close()\n        \n    weight2.append(float(file_coro[3]))\n    height.append(float(file_coro[4]))\n\nmax_weight2 = max(weight2)\nmax_height = max(height)\nprint(convert_cm(max_weight2))\nprint(convert_cm(max_height))\nthe_tich = max_weight1*max_weight2*max_height\nprint(f'Thể tích bounding box: {convert_cm(max_weight1)*convert_cm(max_weight2)*convert_cm(max_height)}')\n        \n","metadata":{"execution":{"iopub.status.busy":"2024-08-15T18:15:08.580691Z","iopub.execute_input":"2024-08-15T18:15:08.581248Z","iopub.status.idle":"2024-08-15T18:15:08.606612Z","shell.execute_reply.started":"2024-08-15T18:15:08.581204Z","shell.execute_reply":"2024-08-15T18:15:08.604537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Diện tích ảnh chiếm bao nhiu pixel: chìu dài * chìu rộng","metadata":{}},{"cell_type":"markdown","source":"Số DPI = 300 điểm ảnh của mỗi inch, hay gọi là số lượng điểm ảnh trên mỗi inch, 1 inch = 2,54 cm","metadata":{}},{"cell_type":"code","source":"dir_img = '/kaggle/working/rsna-miccai-brain-tumor-radiogenomic-classification/00060/T2w/Image-309.jpg'\nwith Image.open(dir_img) as img:\n    width, height = img.size\n\npixel_img = width\ninch = pixel_img / 300\nprint(inch * 2.54)","metadata":{"execution":{"iopub.status.busy":"2024-08-15T17:26:48.624568Z","iopub.execute_input":"2024-08-15T17:26:48.626127Z","iopub.status.idle":"2024-08-15T17:26:48.635430Z","shell.execute_reply.started":"2024-08-15T17:26:48.626043Z","shell.execute_reply":"2024-08-15T17:26:48.634176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"many_cat('/kaggle/working/runs/detect/predict3/00242') # Trường hợp các mặt toàn là axial","metadata":{"execution":{"iopub.status.busy":"2024-08-15T09:54:54.935909Z","iopub.execute_input":"2024-08-15T09:54:54.936345Z","iopub.status.idle":"2024-08-15T09:54:54.949765Z","shell.execute_reply.started":"2024-08-15T09:54:54.936313Z","shell.execute_reply":"2024-08-15T09:54:54.948121Z"}}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom PIL import Image\n\ndef check_image_sizes(folder_path):\n    \"\"\"\n    Kiểm tra kích thước của tất cả các hình ảnh trong một thư mục.\n\n    :param folder_path: Đường dẫn đến thư mục chứa các hình ảnh\n    :return: Một danh sách các tuple (tên tập tin, kích thước) của các hình ảnh\n    \"\"\"\n    image_sizes = []\n\n    # Lấy danh sách các tệp trong thư mục\n    files = os.listdir(folder_path)\n\n    # Duyệt qua từng tệp trong thư mục\n    for file_name in files:\n        file_path = os.path.join(folder_path, file_name)\n\n        # Kiểm tra nếu tệp là hình ảnh\n        if os.path.isfile(file_path):\n            try:\n                with Image.open(file_path) as img:\n                    width, height = img.size\n                    image_sizes.append((file_name, width, height))\n            except IOError:\n                print(f\"Không thể mở hình ảnh: {file_name}\")\n\n    return image_sizes\n\n# Sử dụng hàm để kiểm tra kích thước hình ảnh trong thư mục\nfolder_path = '/kaggle/working/rsna-miccai-brain-tumor-radiogenomic-classification/00060/FLAIR'\nsizes = check_image_sizes(folder_path)\nfor file_name, width, height in sizes:\n    print(f\"Tệp: {file_name}, Kích thước: {width}x{height}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-08-15T17:23:42.581857Z","iopub.execute_input":"2024-08-15T17:23:42.582516Z","iopub.status.idle":"2024-08-15T17:23:42.632908Z","shell.execute_reply.started":"2024-08-15T17:23:42.582465Z","shell.execute_reply":"2024-08-15T17:23:42.631175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nimport os\n\ndef delete_directory(directory):\n    \"\"\"\n    Xóa thư mục cùng với tất cả các tệp và thư mục con bên trong.\n\n    :param directory: Đường dẫn đến thư mục cần xóa.\n    \"\"\"\n    # Đảm bảo thư mục tồn tại\n    if not os.path.isdir(directory):\n        print(f\"Thư mục {directory} không tồn tại.\")\n        return\n\n    try:\n        # Xóa thư mục và tất cả các tệp và thư mục con\n        shutil.rmtree(directory)\n        print(f\"Đã xóa thư mục: {directory}\")\n    except Exception as e:\n        print(f\"Không thể xóa thư mục {directory}. Lỗi: {e}\")\n\n# Ví dụ sử dụng\ndelete_directory('/kaggle/working/runs/detect/predict2')\n","metadata":{"execution":{"iopub.status.busy":"2024-08-15T07:57:13.212867Z","iopub.execute_input":"2024-08-15T07:57:13.213404Z","iopub.status.idle":"2024-08-15T07:57:13.224342Z","shell.execute_reply.started":"2024-08-15T07:57:13.213365Z","shell.execute_reply":"2024-08-15T07:57:13.222964Z"},"trusted":true},"execution_count":null,"outputs":[]}]}