{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install pydicom\n!pip install python-gdcm\n!pip install pylibjpeg pylibjpeg-libjpeg pydicom","metadata":{"execution":{"iopub.status.busy":"2023-09-24T05:58:04.179348Z","iopub.execute_input":"2023-09-24T05:58:04.179823Z","iopub.status.idle":"2023-09-24T05:58:46.71148Z","shell.execute_reply.started":"2023-09-24T05:58:04.17973Z","shell.execute_reply":"2023-09-24T05:58:46.709919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Setup and helper functions","metadata":{"_kg_hide-output":true}},{"cell_type":"code","source":"import os\nimport re\nimport csv\nimport cv2\nimport random\nimport shutil\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom matplotlib.patches import Rectangle\n\nimport pydicom as dicom\n\nimport nibabel as nib","metadata":{"execution":{"iopub.status.busy":"2023-09-24T05:58:46.714078Z","iopub.execute_input":"2023-09-24T05:58:46.714818Z","iopub.status.idle":"2023-09-24T05:58:47.387409Z","shell.execute_reply.started":"2023-09-24T05:58:46.714768Z","shell.execute_reply":"2023-09-24T05:58:47.386244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rsna_root = '/kaggle/input/rsna-2022-cervical-spine-fracture-detection'","metadata":{"execution":{"iopub.status.busy":"2023-09-24T05:58:47.388824Z","iopub.execute_input":"2023-09-24T05:58:47.389502Z","iopub.status.idle":"2023-09-24T05:58:47.395386Z","shell.execute_reply.started":"2023-09-24T05:58:47.389463Z","shell.execute_reply":"2023-09-24T05:58:47.394086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Helper functions\n\ndef load_img_from_dcm(path):\n    img = dicom.dcmread(path)\n    img.PhotometricInterpretation = 'YBR_FULL'\n    data = img.pixel_array\n    data = data - np.min(data)\n    if np.max(data != 0):\n        data = data/np.max(data)\n    data = (data * 255).astype(np.uint8)\n    return cv2.cvtColor(data, cv2.COLOR_GRAY2RGB)\n\ndef CT_path_to_3D_arr(folder_path, l=None): # folder_path is for folder of dcm files constituting one CT scan\n    if l == None:\n        l = os.listdir(folder_path) # list of 2D slices of the CT scan\n        l.sort()\n        l = sorted(l, key=len)\n    \n    CT_arr = [] # the full 3D CT\n    for dcm in l:\n        dcm_path = os.path.join(folder_path, dcm)\n        dcm_arr = load_img_from_dcm(dcm_path)\n        CT_arr.append(dcm_arr)\n    CT_arr = np.asarray(CT_arr)\n    return CT_arr","metadata":{"execution":{"iopub.status.busy":"2023-09-24T05:58:47.398708Z","iopub.execute_input":"2023-09-24T05:58:47.399752Z","iopub.status.idle":"2023-09-24T05:58:47.4171Z","shell.execute_reply.started":"2023-09-24T05:58:47.399658Z","shell.execute_reply":"2023-09-24T05:58:47.415998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Bounding boxes around vertebrae\ncan be obtained from the segmentations data. First I will visualize the segmentation slices side-by-side with their corresponding CT slices.","metadata":{}},{"cell_type":"code","source":"patient_id = \"1.2.826.0.1.3680043.780\"\n\nsegm_path = os.path.join(rsna_root, 'segmentations', patient_id+'.nii')\nsegm_arr = nib.load(segm_path).get_fdata()\nsegm_arr = np.transpose(segm_arr, (2, 0, 1))\n\nCT_path = os.path.join(rsna_root, 'train_images', patient_id)\nCT_arr = CT_path_to_3D_arr(CT_path)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-24T05:58:47.418746Z","iopub.execute_input":"2023-09-24T05:58:47.419108Z","iopub.status.idle":"2023-09-24T05:58:59.042186Z","shell.execute_reply.started":"2023-09-24T05:58:47.419072Z","shell.execute_reply":"2023-09-24T05:58:59.041129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 2, figsize=(12, 6))\n\nslice_num = 80 # from 0 to len(array)-1\nsegm_slice = segm_arr[len(segm_arr)-1 - slice_num]\nsegm_slice = np.rot90(segm_slice)\nCT_slice = CT_arr[slice_num]\nax[0].imshow(CT_slice, cmap=plt.get_cmap('bone'))\nax[1].imshow(segm_slice, cmap=plt.get_cmap('bone'))","metadata":{"execution":{"iopub.status.busy":"2023-09-24T05:58:59.043854Z","iopub.execute_input":"2023-09-24T05:58:59.044263Z","iopub.status.idle":"2023-09-24T05:58:59.568378Z","shell.execute_reply.started":"2023-09-24T05:58:59.044223Z","shell.execute_reply":"2023-09-24T05:58:59.567342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can draw a bounding box (bbox) around the vertebrae by using the segmentation slice like this:","metadata":{}},{"cell_type":"code","source":"rows = np.any(segm_slice, axis=1)\ncols = np.any(segm_slice, axis=0)\nrmin, rmax = np.where(rows)[0][[0, -1]]\ncmin, cmax = np.where(cols)[0][[0, -1]]\nwidth = cmax - cmin\nheight = rmax - rmin\n\nfig, ax = plt.subplots(1, 1, figsize=(6,6))\nax.imshow(segm_slice)\nrect = Rectangle((cmin, rmin), width, height,\n                 linewidth=1, edgecolor='r', facecolor='none')\nax.add_patch(rect)","metadata":{"execution":{"iopub.status.busy":"2023-09-24T05:58:59.569556Z","iopub.execute_input":"2023-09-24T05:58:59.569926Z","iopub.status.idle":"2023-09-24T05:58:59.852952Z","shell.execute_reply.started":"2023-09-24T05:58:59.56989Z","shell.execute_reply":"2023-09-24T05:58:59.851999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Drawing the bbox on the original CT scan:","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(1, 1, figsize=(6,6))\nax.imshow(CT_slice, cmap=plt.get_cmap('bone'))\nrect = Rectangle((cmin, rmin), width, height,\n                 linewidth=1, edgecolor='r', facecolor='none')\nax.add_patch(rect)\n\nprint(cmin, rmin, width, height)","metadata":{"execution":{"iopub.status.busy":"2023-09-24T05:58:59.854641Z","iopub.execute_input":"2023-09-24T05:58:59.855014Z","iopub.status.idle":"2023-09-24T05:59:00.165288Z","shell.execute_reply.started":"2023-09-24T05:58:59.854976Z","shell.execute_reply":"2023-09-24T05:59:00.164297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save labels (bboxes) in YOLOv5 format to use for training\nI used [Ultralytics YOLOv5](https://docs.ultralytics.com/quick-start/), which I found super easy to use. The specifics are well-documented on their website, but here is the gist:\n- one txt file per image\n- one row per object/bbox\n- each row in 'class_number, xcentre, ycentre, width, height' format (here, we only have one class '0: vertebra')\n- bbox coordinates are normalized, ie divided by width/height in length\n- (0,0) is top-left","metadata":{}},{"cell_type":"code","source":"def save_yolo_coord(pt_num, slice_num, slice_, destination_folder):\n    '''\n    Saves the yolov8 coord txt file for a single slice segmentation mask\n    \n    pt_num: number that follows 1.2.826.0.1.3680043.\n    slice_num: which slice\n    arr: 2D array of slice\n    destination_folder: where to save the txt files\n    '''\n    rows = np.any(slice_, axis=1)\n    cols = np.any(slice_, axis=0)\n    rmin, rmax = np.where(rows)[0][[0, -1]]\n    cmin, cmax = np.where(cols)[0][[0, -1]]\n    \n    xcentre = int((cmin+cmax)/2)\n    ycentre = int((rmin+rmax)/2)\n    width = cmax - cmin\n    height = rmax - rmin\n    img_width = slice_.shape[1]\n    img_height = slice_.shape[0]\n    \n    # yolo coordiates: class, xcentre, ycentre, width, height (normalized by width/hegith of image)\n    yolo_coord = [0, xcentre/img_width, ycentre/img_height, width/img_width, height/img_height]\n    \n    filename = os.path.join(destination_folder, str(pt_num)+\"_\"+str(slice_num)+'.txt')\n    with open(filename, 'w') as file:\n        writer = csv.writer(file, delimiter=' ')\n        writer.writerow(yolo_coord)","metadata":{"execution":{"iopub.status.busy":"2023-09-24T05:59:00.167445Z","iopub.execute_input":"2023-09-24T05:59:00.168496Z","iopub.status.idle":"2023-09-24T05:59:00.17948Z","shell.execute_reply.started":"2023-09-24T05:59:00.168455Z","shell.execute_reply":"2023-09-24T05:59:00.178407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_bboxes_from_nii(pt_num, nii_folder, dest_folder_name):\n    '''\n    Saves yolov8 coord txt files for one patient's segmentation masks (ie, one nii file)\n    '''\n    destination_folder = os.path.join(os.getcwd(), dest_folder_name)\n    if not os.path.exists(destination_folder):\n        print(f'Creating destination folder in current directory: {dest_folder_name}')\n        os.mkdir(destination_folder)\n    \n    arr = nib.load(os.path.join(nii_folder, '1.2.826.0.1.3680043.'+str(pt_num)+'.nii')).get_fdata()\n    arr = np.transpose(arr, (2, 0, 1))\n    arr = np.flip(arr, axis=0)\n    \n    for slice_num, slice_ in enumerate(arr):\n        if not slice_.any():\n            continue\n        slice_ = np.rot90(slice_)\n        \n        save_yolo_coord(pt_num, slice_num, slice_, destination_folder)","metadata":{"execution":{"iopub.status.busy":"2023-09-24T05:59:00.184808Z","iopub.execute_input":"2023-09-24T05:59:00.185163Z","iopub.status.idle":"2023-09-24T05:59:00.195585Z","shell.execute_reply.started":"2023-09-24T05:59:00.185133Z","shell.execute_reply":"2023-09-24T05:59:00.194431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# List of patient numbers in segmentations folder\nniis = os.listdir('/kaggle/input/rsna-2022-cervical-spine-fracture-detection/segmentations')\npts = [re.search('(?<=1.2.826.0.1.3680043.)([0-9]*)(?=.nii)', filename).group(0) for filename in niis]\npts = [int(s) for s in pts]\nprint(\"List of pts with segmentations: \", pts)","metadata":{"execution":{"iopub.status.busy":"2023-09-24T05:59:00.196809Z","iopub.execute_input":"2023-09-24T05:59:00.198243Z","iopub.status.idle":"2023-09-24T05:59:00.242222Z","shell.execute_reply.started":"2023-09-24T05:59:00.198202Z","shell.execute_reply":"2023-09-24T05:59:00.241141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dest_folder_name = 'yolo_coords'\nnii_folder = '/kaggle/input/rsna-2022-cervical-spine-fracture-detection/segmentations'\nfor pt_num in pts:\n    save_bboxes_from_nii(pt_num, nii_folder, dest_folder_name)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-09-24T05:59:00.243943Z","iopub.execute_input":"2023-09-24T05:59:00.244311Z","iopub.status.idle":"2023-09-24T06:02:09.535165Z","shell.execute_reply.started":"2023-09-24T05:59:00.244275Z","shell.execute_reply":"2023-09-24T06:02:09.533962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test to see if saved coords are correct\n\nl = os.listdir('/kaggle/working/yolo_coords')\nl = sorted(l)\nl.sort(key=len)\n\nf = np.random.choice(l)\nprint(f)\npt_num = re.search(\"^([0-9]*)(?=_)\", f).group(0)\nslice_num = re.search(\"(?<=_)([0-9]*)(?=.txt)\", f).group(0)\n\nCT_path = os.path.join(rsna_root, 'train_images', \"1.2.826.0.1.3680043.\"+pt_num)\nCT_arr = CT_path_to_3D_arr(CT_path)\nCT_slice = CT_arr[int(slice_num)]\nimg_width = CT_slice.shape[1]\nimg_height = CT_slice.shape[0]\n\nfig, ax = plt.subplots(1,1,figsize=(6,6))\nax.imshow(CT_slice, cmap=plt.get_cmap('bone'))\n\np = '/kaggle/working/yolo_coords/'+f\nwith open(p, 'r') as txt_file:\n    reader = csv.reader(txt_file)\n    row = next(reader)\n\nrow = [float(num) for num in row[0].split()]\nbbox_xcentre = img_width * row[1]\nbbox_ycentre = img_height * row[2]\nbbox_width = img_width * row[3]\nbbox_height = img_height * row[4]\nrect = Rectangle((bbox_xcentre-int(bbox_width/2), bbox_ycentre-int(bbox_height/2)),\n                  bbox_width, bbox_height,\n                  linewidth=1, edgecolor='g', facecolor='none')\nax.add_patch(rect)","metadata":{"execution":{"iopub.status.busy":"2023-09-24T06:02:09.536807Z","iopub.execute_input":"2023-09-24T06:02:09.537505Z","iopub.status.idle":"2023-09-24T06:02:15.437319Z","shell.execute_reply.started":"2023-09-24T06:02:09.537462Z","shell.execute_reply":"2023-09-24T06:02:15.436321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Save Images for Faster Rcnn training\nI found it easier to save the CT slices as jpegs for training Ultralytics YOLO.","metadata":{}},{"cell_type":"code","source":"# yolo coord txt files for all patients\ntxt_files = os.listdir('/kaggle/working/yolo_coords')\n\n# patients with segmentation data\nniis = os.listdir('/kaggle/input/rsna-2022-cervical-spine-fracture-detection/segmentations')\npts = [re.search('(?<=1.2.826.0.1.3680043.)([0-9]*)(?=.nii)', filename).group(0) for filename in niis]\npts = [int(s) for s in pts]\n\n# Save slices corresponding to each yolo_coord txt file\ntrain_images = '/kaggle/input/rsna-2022-cervical-spine-fracture-detection/train_images'\n\nyolo_slices = os.path.join(os.getcwd(), 'yolo_slices')\nif not os.path.exists(yolo_slices):\n    os.mkdir(yolo_slices)\n    \nfor pt in pts:\n    slice_nums = []\n    for txt_file in txt_files:\n        if re.search('^([0-9]*)(?=_)', txt_file).group(0) == str(pt): # txt files for pt\n            slice_num = int(re.search(\"(?<=_)([0-9]*)(?=.txt)\", txt_file).group(0))\n            slice_nums.append(slice_num)\n    \n    pt_CT = os.path.join(train_images, f\"1.2.826.0.1.3680043.{str(pt)}\") \n\n    pt_slices_all = os.listdir(pt_CT) # list of dcms\n    pt_slices_all = sorted(pt_slices_all)\n    pt_slices_all.sort(key=len)\n    \n    pt_slices = [pt_slices_all[slice_num] for slice_num in sorted(slice_nums)]\n    \n    if min(pt_slices_all, key=len) == '2.dcm': # scans that are missing 1.dcm\n        for slice_ in pt_slices:\n            img_path = os.path.join(train_images, \"1.2.826.0.1.3680043.\"+str(pt), slice_)\n            img = load_img_from_dcm(img_path)\n\n            imgs_savepath = os.path.join(yolo_slices, f\"{str(pt)}_{int(slice_[:-4])-2}.jpg\")\n            cv2.imwrite(imgs_savepath, img)        \n    \n    else: # normal scans that start from 1.dcm\n        for slice_ in pt_slices:\n            img_path = os.path.join(train_images, \"1.2.826.0.1.3680043.\"+str(pt), slice_)\n            img = load_img_from_dcm(img_path)\n\n            imgs_savepath = os.path.join(yolo_slices, f\"{str(pt)}_{int(slice_[:-4])-1}.jpg\")\n            cv2.imwrite(imgs_savepath, img)","metadata":{"scrolled":true,"_kg_hide-input":false,"_kg_hide-output":false,"execution":{"iopub.status.busy":"2023-09-24T06:02:15.438932Z","iopub.execute_input":"2023-09-24T06:02:15.439556Z","iopub.status.idle":"2023-09-24T06:12:05.393544Z","shell.execute_reply.started":"2023-09-24T06:02:15.439518Z","shell.execute_reply":"2023-09-24T06:12:05.392439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Organize images and labels into directories","metadata":{}},{"cell_type":"code","source":"# Split into train and valid set\nall_images = os.listdir('/kaggle/working/yolo_slices')\nall_images = [f[:-4] for f in all_images]\nrandom.shuffle(all_images)\nSPLIT_POINT = int(len(all_images) * 0.9)\ntrain_set = all_images[:SPLIT_POINT]\nvalid_set = all_images[SPLIT_POINT:]\n\n# Put each image/label into right directory\nfolders = [\"train/images\", \"train/labels\", \"valid/images\", \"valid/labels\"]\nfor folder in folders:\n    if not os.path.exists(folder):\n        os.makedirs(folder)\n        \nfor idx in train_set:\n    jpg_file = f'/kaggle/working/yolo_slices/{idx}.jpg'\n    txt_file = f'/kaggle/working/yolo_coords/{idx}.txt'\n    \n    shutil.move(jpg_file, '/kaggle/working/train/images')\n    shutil.move(txt_file, '/kaggle/working/train/labels')\n\nfor idx in valid_set:\n    jpg_file = f'/kaggle/working/yolo_slices/{idx}.jpg'\n    txt_file = f'/kaggle/working/yolo_coords/{idx}.txt'\n    \n    shutil.move(jpg_file, '/kaggle/working/valid/images')\n    shutil.move(txt_file, '/kaggle/working/valid/labels')","metadata":{"execution":{"iopub.status.busy":"2023-09-24T06:12:05.394996Z","iopub.execute_input":"2023-09-24T06:12:05.395431Z","iopub.status.idle":"2023-09-24T06:12:07.732389Z","shell.execute_reply.started":"2023-09-24T06:12:05.395389Z","shell.execute_reply":"2023-09-24T06:12:07.731288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/ultralytics/yolov5.git\n!cd yolov5\n!cd /kaggle/working/yolov5","metadata":{"execution":{"iopub.status.busy":"2023-09-24T06:12:07.733858Z","iopub.execute_input":"2023-09-24T06:12:07.734947Z","iopub.status.idle":"2023-09-24T06:12:12.857051Z","shell.execute_reply.started":"2023-09-24T06:12:07.734908Z","shell.execute_reply":"2023-09-24T06:12:12.855609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Navigate to the yolov5 directory if you haven't already\n%cd /kaggle/working/yolov5\n\n# Use a text editor to edit the file, or use a shell command like 'sed'\n!sed -i 's/numpy>=1.22.2/numpy==1.21.2/' requirements.txt\n\n# Then try installing the requirements again\n!pip install -U -r requirements.txt \n","metadata":{"execution":{"iopub.status.busy":"2023-09-24T06:12:12.860056Z","iopub.execute_input":"2023-09-24T06:12:12.860951Z","iopub.status.idle":"2023-09-24T06:20:29.089656Z","shell.execute_reply.started":"2023-09-24T06:12:12.860902Z","shell.execute_reply":"2023-09-24T06:20:29.088334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"yaml_content = '''\ntrain: /kaggle/working/train/images\nval: /kaggle/working/valid/images\nnc: 1\nnames: ['vertebra']\n'''\n\nwith open(\"/kaggle/working/yolov5/data/my_config.yaml\", \"w\") as yaml_file:\n    yaml_file.write(yaml_content)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-24T06:20:29.091716Z","iopub.execute_input":"2023-09-24T06:20:29.092526Z","iopub.status.idle":"2023-09-24T06:20:29.101686Z","shell.execute_reply.started":"2023-09-24T06:20:29.09248Z","shell.execute_reply":"2023-09-24T06:20:29.100603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone --recursive https://github.com/smallcorgi/Faster-RCNN_TF.git","metadata":{"execution":{"iopub.status.busy":"2023-09-24T06:20:29.102804Z","iopub.execute_input":"2023-09-24T06:20:29.10312Z","iopub.status.idle":"2023-09-24T06:20:30.718925Z","shell.execute_reply.started":"2023-09-24T06:20:29.103093Z","shell.execute_reply":"2023-09-24T06:20:30.717697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install transforms","metadata":{"execution":{"iopub.status.busy":"2023-09-24T06:20:30.720938Z","iopub.execute_input":"2023-09-24T06:20:30.721309Z","iopub.status.idle":"2023-09-24T06:20:44.946686Z","shell.execute_reply.started":"2023-09-24T06:20:30.721276Z","shell.execute_reply":"2023-09-24T06:20:44.945382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# references/detection\n!pip install pycocotools --quiet\n!git clone https://github.com/pytorch/vision.git\n!git checkout v0.3.0\n\n!cp vision/references/detection/utils.py ./\n!cp vision/references/detection/transforms.py ./\n!cp vision/references/detection/coco_eval.py ./\n!cp vision/references/detection/engine.py ./\n!cp vision/references/detection/coco_utils.py ./","metadata":{"execution":{"iopub.status.busy":"2023-09-24T06:20:44.948753Z","iopub.execute_input":"2023-09-24T06:20:44.949564Z","iopub.status.idle":"2023-09-24T06:22:29.563862Z","shell.execute_reply.started":"2023-09-24T06:20:44.949515Z","shell.execute_reply":"2023-09-24T06:22:29.562456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"conda install torchvision -c pytorch","metadata":{"execution":{"iopub.status.busy":"2023-09-24T06:22:29.566207Z","iopub.execute_input":"2023-09-24T06:22:29.566657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install comet_ml\n","metadata":{"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python train.py --img 224 --batch 16 --epochs  --data ./data/my_config.yaml --cfg ./models/yolov5m.yaml --weights yolov5m.pt\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tensorboard --logdir runs/\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Dummy data: replace with your own metrics\nepochs = list(range(1, 101))\ntraining_loss = [0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.05]\nmAP50 = [0.1, 0.2, 0.3, 0.4, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75]\n\n# Plotting\nplt.figure(figsize=(12, 5))\n\nplt.subplot(1, 2, 1)\nplt.plot(epochs, training_loss, '-o', label='Training Loss')\nplt.title('Training Loss over Epochs')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs, mAP50, '-o', label='mAP50')\nplt.title('mAP50 over Epochs')\nplt.xlabel('Epochs')\nplt.ylabel('mAP50')\nplt.legend()\n\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Example data\nepochs = list(range(1, 11))\naccuracy = [0.65, 0.68, 0.71, 0.73, 0.74, 0.76, 0.78, 0.79, 0.81, 0.83, 0.84, ]\nloss = [0.7, 0.6, 0.55, 0.51, 0.49, 0.45, 0.43, 0.41, 0.39, 0.37]\n\n# Create the subplots\nfig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(6, 8))\n\n# Plot accuracy results\nax1.plot(epochs, accuracy, '-o', label='Accuracy')\nax1.set_title('Accuracy Results over Epochs')\nax1.set_xlabel('Epochs')\nax1.set_ylabel('Accuracy')\nax1.legend()\nax1.grid(True)\n\n# Plot loss results\nax2.plot(epochs, loss, '-o', label='Loss', color='red')\nax2.set_title('Loss Results over Epochs')\nax2.set_xlabel('Epochs')\nax2.set_ylabel('Loss')\nax2.legend()\nax2.grid(True)\n\n# Show the plot\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Dummy data: replace with your own metrics\nepochs = list(range(1, 11))\ntraining_loss = [0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.05]\nmAP50 = [0.1, 0.2, 0.3, 0.4, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75]\naccuracy = [0.65, 0.68, 0.7, 0.72, 0.74, 0.76, 0.78, 0.79, 0.8, 0.82]  # Example accuracy data\n\n# Plotting\nplt.figure(figsize=(18, 5))\n\n# Plot training loss\nplt.subplot(1, 3, 1)\nplt.plot(epochs, training_loss, '-o', label='Training Loss')\nplt.title('Training Loss over Epochs')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\n\n# Plot mAP50\nplt.subplot(1, 3, 2)\nplt.plot(epochs, mAP50, '-o', label='mAP50')\nplt.title('mAP50 over Epochs')\nplt.xlabel('Epochs')\nplt.ylabel('mAP50')\nplt.legend()\n\n# Plot accuracy\nplt.subplot(1, 3, 3)\nplt.plot(epochs, accuracy, '-o', label='Accuracy')\nplt.title('Accuracy over Epochs')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\n\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n# Load model\nmodel = torch.hub.load('ultralytics/yolov5', 'custom', path='/kaggle/working/yolov5/runs/train/exp/weights/best.pt')\n\n# Perform inference\nresults = model('/kaggle/working/train/images/19333_142.jpg')  # You can also use PIL images, NumPy arrays, etc.\n\n# Display results\nresults.show()\n\n# Save results\nresults.save()  # Save results like 'results.jpg', 'results.mp4', etc.\n\n# Access data\nlabels = results.xyxy[0][:, -1].numpy()  # class labels\ncoordinates = results.xyxy[0][:, :-1].numpy()  # object coordinates\n\n# Perform further analysis based on labels and coordinates\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!tensorboard --logdir runs/\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nprint(os.getcwd())  # This will give the current working directory\nprint(os.listdir('/kaggle/working/train/images'))  # This will list the items in the specified directory\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transforms.RandomCrop(224)\ntransform = transforms.Compose([\n    transforms.RandomHorizontalFlip(),\n    transforms.RandomRotation(10),\n    transforms.RandomCrop(224),\n    transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1),\n    transforms.ToTensor(),\n    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n])\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision.transforms as transforms\nfrom torchvision.datasets import ImageFolder\nfrom torch.utils.data import DataLoader\n\ntransform = transforms.Compose([\n    transforms.Resize((256, 256)),  # resize to the expected size\n    transforms.ToTensor(),\n])\n\ntrain_dataset = ImageFolder(root='/kaggle/working/train/images', transform=transform)\nvalid_dataset = ImageFolder(root='/kaggle/working/valid/images', transform=transform)\n\ntrain_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)\nvalid_loader = DataLoader(valid_dataset, batch_size=32, shuffle=False)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\n\nclass NextVit(nn.Module):\n    def __init__(self):\n        super(NextVit, self).__init__()\n        self.patch_embed = nn.Conv2d(3, 256, kernel_size=16, stride=16)  # Increased depth to 256\n        self.encoder_blocks = nn.ModuleList([TransformerBlock(256, 4) for _ in range(13)])  # 13 blocks & 4 heads\n        self.fc = nn.Linear(256, 2)  # Increased depth to 256\n\n    def forward(self, x):\n        x = self.patch_embed(x)\n        x = x.flatten(2).transpose(1, 2)\n        for block in self.encoder_blocks:\n            x = block(x)\n        x = x.mean(dim=1)\n        x = self.fc(x)\n        return x\n\n# Adjust the TransformerBlock's feed-forward network to be deeper\nclass TransformerBlock(nn.Module):\n    def __init__(self, d_model, num_heads):\n        super(TransformerBlock, self).__init__()\n        self.attention = MultiHeadAttention(d_model, num_heads)\n        self.norm1 = nn.LayerNorm(d_model)\n        self.norm2 = nn.LayerNorm(d_model)\n        self.feed_forward = nn.Sequential(\n            nn.Linear(d_model, 4 * d_model),\n            nn.ReLU(),\n            nn.Linear(4 * d_model, 2 * d_model),  # Increased depth\n            nn.ReLU(),\n            nn.Linear(2 * d_model, d_model)\n        )\n\n    # ... rest of the class remains the same\n\n# Create the model\nnextvit_model = NextVit()\nprint(nextvit_model)\n\n# Test the model \nx = torch.randn(16, 3, 256, 256)\noutput = nextvit_model(x)\nprint(output.shape)  # Should output [16, 2]\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls /kaggle/working/train  # To list files in the 'train' directory\n!ls /kaggle/working        # To list files in the '/kaggle/working' directory\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}