{"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":"gpu","dataSources":[{"sourceId":71549,"databundleVersionId":8561470,"sourceType":"competition"},{"sourceId":9195731,"sourceType":"datasetVersion","datasetId":5559249},{"sourceId":193161758,"sourceType":"kernelVersion"},{"sourceId":193292727,"sourceType":"kernelVersion"}],"dockerImageVersionId":30747,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport pydicom\nfrom tqdm.auto import tqdm\nimport matplotlib.pyplot as plt\nimport cv2\nimport glob","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-20T10:08:31.257284Z","iopub.execute_input":"2024-08-20T10:08:31.257892Z","iopub.status.idle":"2024-08-20T10:08:32.639630Z","shell.execute_reply.started":"2024-08-20T10:08:31.257859Z","shell.execute_reply":"2024-08-20T10:08:32.638593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q /kaggle/input/lsdc-gen-yolo-data-scs/data_fold0.zip","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:11:10.885878Z","iopub.execute_input":"2024-08-20T10:11:10.886723Z","iopub.status.idle":"2024-08-20T10:11:14.185237Z","shell.execute_reply.started":"2024-08-20T10:11:10.886688Z","shell.execute_reply":"2024-08-20T10:11:14.184116Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:11:26.137546Z","iopub.execute_input":"2024-08-20T10:11:26.138451Z","iopub.status.idle":"2024-08-20T10:11:27.127439Z","shell.execute_reply.started":"2024-08-20T10:11:26.138416Z","shell.execute_reply":"2024-08-20T10:11:27.126333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_DIR = \"/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_images\"","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:32.641246Z","iopub.execute_input":"2024-08-20T10:08:32.641822Z","iopub.status.idle":"2024-08-20T10:08:32.645957Z","shell.execute_reply.started":"2024-08-20T10:08:32.641794Z","shell.execute_reply":"2024-08-20T10:08:32.644937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FOLD = 0\nOD_INPUT_SIZE = 384\nSTD_BOX_SIZE = 20\nBATCH_SIZE = 64\nEPOCHS = 40\n\nSAMPLE = None\nCONDITIONS = ['Spinal Canal Stenosis']\nSEVERITIES = ['Normal/Mild', 'Moderate', 'Severe']\nLEVELS = ['l1_l2', 'l2_l3', 'l3_l4', 'l4_l5', 'l5_s1']\n\nDATA_DIR = f'data_fold{FOLD}'\n\n","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:11:41.072523Z","iopub.execute_input":"2024-08-20T10:11:41.072922Z","iopub.status.idle":"2024-08-20T10:11:41.078703Z","shell.execute_reply.started":"2024-08-20T10:11:41.072890Z","shell.execute_reply":"2024-08-20T10:11:41.077692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rm -rf val_fold0","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:32.659076Z","iopub.execute_input":"2024-08-20T10:08:32.659814Z","iopub.status.idle":"2024-08-20T10:08:32.665371Z","shell.execute_reply.started":"2024-08-20T10:08:32.659789Z","shell.execute_reply":"2024-08-20T10:08:32.664515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_val_df = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train.csv')\ntrain_xy = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_label_coordinates.csv')\ntrain_des = pd.read_csv('/kaggle/input/rsna-2024-lumbar-spine-degenerative-classification/train_series_descriptions.csv')","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:32.666541Z","iopub.execute_input":"2024-08-20T10:08:32.666901Z","iopub.status.idle":"2024-08-20T10:08:32.830644Z","shell.execute_reply.started":"2024-08-20T10:08:32.666862Z","shell.execute_reply":"2024-08-20T10:08:32.829732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if SAMPLE:\n    train_val_df = train_val_df.sample(SAMPLE, random_state=2698)","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:32.831980Z","iopub.execute_input":"2024-08-20T10:08:32.832696Z","iopub.status.idle":"2024-08-20T10:08:32.837781Z","shell.execute_reply.started":"2024-08-20T10:08:32.832654Z","shell.execute_reply":"2024-08-20T10:08:32.836571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fold_df = pd.read_csv('/kaggle/input/lsdc-fold-split/5folds.csv')","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:32.839176Z","iopub.execute_input":"2024-08-20T10:08:32.840041Z","iopub.status.idle":"2024-08-20T10:08:32.855497Z","shell.execute_reply.started":"2024-08-20T10:08:32.840009Z","shell.execute_reply":"2024-08-20T10:08:32.854733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_xy.head(3)","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:32.856832Z","iopub.execute_input":"2024-08-20T10:08:32.857148Z","iopub.status.idle":"2024-08-20T10:08:32.877579Z","shell.execute_reply.started":"2024-08-20T10:08:32.857118Z","shell.execute_reply":"2024-08-20T10:08:32.876751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_level(text):\n    for lev in ['l1_l2', 'l2_l3', 'l3_l4', 'l4_l5', 'l5_s1']:\n        if lev in text:\n            split = lev.split('_')\n            split[0] = split[0].capitalize()\n            split[1] = split[1].capitalize()\n            return '/'.join(split)\n    raise ValueError('Level not found '+ lev)\n    \ndef get_condition(text):\n    split = text.split('_')\n    for i in range(len(split)):\n        split[i] = split[i].capitalize()\n    split = split[:-2]\n    return ' '.join(split)\n#     raise ValueError('Condition not found '+ lev)","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:32.878478Z","iopub.execute_input":"2024-08-20T10:08:32.878882Z","iopub.status.idle":"2024-08-20T10:08:32.887538Z","shell.execute_reply.started":"2024-08-20T10:08:32.878851Z","shell.execute_reply":"2024-08-20T10:08:32.886296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_xy['condition'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:32.888929Z","iopub.execute_input":"2024-08-20T10:08:32.889511Z","iopub.status.idle":"2024-08-20T10:08:32.907524Z","shell.execute_reply.started":"2024-08-20T10:08:32.889479Z","shell.execute_reply":"2024-08-20T10:08:32.906739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df = train_df.dropna()","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:32.917004Z","iopub.execute_input":"2024-08-20T10:08:32.918398Z","iopub.status.idle":"2024-08-20T10:08:32.923473Z","shell.execute_reply.started":"2024-08-20T10:08:32.918363Z","shell.execute_reply":"2024-08-20T10:08:32.922532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_df = {'study_id':[], 'condition': [], 'level':[], 'label':[]}\n\nfor i, row in train_val_df.iterrows():\n    study_id = row['study_id']\n    for k, label in row.iloc[1:].to_dict().items():\n        level = get_level(k)\n        condition = get_condition(k)\n        label_df['study_id'].append(study_id)\n        label_df['condition'].append(condition)\n        label_df['level'].append(level)\n        label_df['label'].append(label)\n#         break\n#     break\n\nlabel_df = pd.DataFrame(label_df)\nlabel_df = label_df.merge(fold_df, on='study_id')","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:32.925069Z","iopub.execute_input":"2024-08-20T10:08:32.926209Z","iopub.status.idle":"2024-08-20T10:08:33.605578Z","shell.execute_reply.started":"2024-08-20T10:08:32.926176Z","shell.execute_reply":"2024-08-20T10:08:33.604675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_xy = train_xy.merge(train_des, how='inner', on=['study_id', 'series_id'])\nlabel_df = label_df.merge(train_xy, how='inner', on=['study_id', 'condition', 'level'])","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:33.606776Z","iopub.execute_input":"2024-08-20T10:08:33.607102Z","iopub.status.idle":"2024-08-20T10:08:33.693949Z","shell.execute_reply.started":"2024-08-20T10:08:33.607067Z","shell.execute_reply":"2024-08-20T10:08:33.692946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def query_train_xy_row(study_id, series_id=None, instance_num=None):\n    if series_id is not None and instance_num is not None:\n        return label_df[(label_df.study_id==study_id) & (label_df.series_id==series_id) &\n            (label_df.instance_number==instance_num)]\n    elif series_id is None and instance_num is None:\n        return label_df[(label_df.study_id==study_id)]\n    else:\n        return label_df[(train_xy.study_id==study_id) & (label_df.series_id==series_id)]","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:33.695055Z","iopub.execute_input":"2024-08-20T10:08:33.695331Z","iopub.status.idle":"2024-08-20T10:08:33.704045Z","shell.execute_reply.started":"2024-08-20T10:08:33.695305Z","shell.execute_reply":"2024-08-20T10:08:33.703077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_dcm(src_path):\n    dicom_data = pydicom.dcmread(src_path)\n    image = dicom_data.pixel_array\n    image = (image - image.min()) / (image.max() - image.min() +1e-6) * 255\n    image = np.stack([image]*3, axis=-1).astype('uint8')\n    return image\n\ndef get_accronym(text):\n    split = text.split(' ')\n    return ''.join([x[0] for x in split])","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:33.705262Z","iopub.execute_input":"2024-08-20T10:08:33.706531Z","iopub.status.idle":"2024-08-20T10:08:33.717669Z","shell.execute_reply.started":"2024-08-20T10:08:33.706461Z","shell.execute_reply":"2024-08-20T10:08:33.716673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# study_id = 4003253 \n# series_id = 2448190387\n# instance_num = 28\n\nex = label_df.sample(1).iloc[0]\nstudy_id = ex.study_id\nseries_id = ex.series_id\ninstance_num = ex.instance_number\n\nWIDTH = 10\n\npath = os.path.join(IMG_DIR, str(study_id), str(series_id), f'{instance_num}.dcm')","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:33.718830Z","iopub.execute_input":"2024-08-20T10:08:33.719137Z","iopub.status.idle":"2024-08-20T10:08:33.735409Z","shell.execute_reply.started":"2024-08-20T10:08:33.719112Z","shell.execute_reply":"2024-08-20T10:08:33.734526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = read_dcm(path)\n\ntmp_df = query_train_xy_row(study_id, series_id, instance_num)\nfor i, row in tmp_df.iterrows():\n    lbl = f\"{get_accronym(row['condition'])}_{row['level']}\"\n    x, y = row['x'], row['y']\n    x1 = int(x - WIDTH)\n    x2 = int(x + WIDTH)\n    y1 = int(y - WIDTH)\n    y2 = int(y + WIDTH)\n    color = None\n    if row['label'] == 'Normal/Mild':\n        color =  (0, 255, 0)\n    elif row['label'] == 'Moderate':\n        color = (255,255,0) \n    elif row['label'] == 'Severe':\n        color = (255,0,0)\n        \n    fontFace = cv2.FONT_HERSHEY_SIMPLEX\n    fontScale = 0.5\n    thickness = 1\n    cv2.rectangle(img, (x1,y1), (x2,y2), color, 2)\n    cv2.putText(img, lbl, (x1,y1), fontFace, fontScale, color, thickness, cv2.LINE_AA)\n\ntmp_df","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:33.736552Z","iopub.execute_input":"2024-08-20T10:08:33.736874Z","iopub.status.idle":"2024-08-20T10:08:33.796662Z","shell.execute_reply.started":"2024-08-20T10:08:33.736843Z","shell.execute_reply":"2024-08-20T10:08:33.795677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:33.797906Z","iopub.execute_input":"2024-08-20T10:08:33.798253Z","iopub.status.idle":"2024-08-20T10:08:34.121805Z","shell.execute_reply.started":"2024-08-20T10:08:33.798220Z","shell.execute_reply":"2024-08-20T10:08:34.120682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# label_df[['study_id', 'series_id']].drop_duplicates()","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:34.123080Z","iopub.execute_input":"2024-08-20T10:08:34.123422Z","iopub.status.idle":"2024-08-20T10:08:34.128287Z","shell.execute_reply.started":"2024-08-20T10:08:34.123391Z","shell.execute_reply":"2024-08-20T10:08:34.126998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_dcm(src_path):\n    dicom_data = pydicom.dcmread(src_path)\n    image = dicom_data.pixel_array\n    image = (image - image.min()) / (image.max() - image.min() +1e-6) * 255\n    image = np.stack([image]*3, axis=-1).astype('uint8')\n    return image","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:34.129963Z","iopub.execute_input":"2024-08-20T10:08:34.130299Z","iopub.status.idle":"2024-08-20T10:08:34.139486Z","shell.execute_reply.started":"2024-08-20T10:08:34.130266Z","shell.execute_reply":"2024-08-20T10:08:34.138661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filtered_df = label_df[label_df.condition.map(lambda x: x in CONDITIONS)]","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:34.140876Z","iopub.execute_input":"2024-08-20T10:08:34.141181Z","iopub.status.idle":"2024-08-20T10:08:34.174559Z","shell.execute_reply.started":"2024-08-20T10:08:34.141153Z","shell.execute_reply":"2024-08-20T10:08:34.173615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label2id = {}\nid2label = {}\ni = 0\nfor cond in CONDITIONS:\n    for level in LEVELS:\n        for severity in SEVERITIES:\n            cls_ = f\"{cond.lower().replace(' ', '_')}_{level}_{severity.lower()}\"\n            label2id[cls_] = i\n            id2label[i] = cls_\n            i+=1","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:34.175669Z","iopub.execute_input":"2024-08-20T10:08:34.175937Z","iopub.status.idle":"2024-08-20T10:08:34.181262Z","shell.execute_reply.started":"2024-08-20T10:08:34.175913Z","shell.execute_reply":"2024-08-20T10:08:34.180360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"id2label","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:34.182634Z","iopub.execute_input":"2024-08-20T10:08:34.183519Z","iopub.status.idle":"2024-08-20T10:08:34.191510Z","shell.execute_reply.started":"2024-08-20T10:08:34.183493Z","shell.execute_reply":"2024-08-20T10:08:34.190645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = filtered_df[filtered_df.fold != FOLD]\nval_df = filtered_df[filtered_df.fold == FOLD]","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:08:34.192707Z","iopub.execute_input":"2024-08-20T10:08:34.193146Z","iopub.status.idle":"2024-08-20T10:08:34.202753Z","shell.execute_reply.started":"2024-08-20T10:08:34.193115Z","shell.execute_reply":"2024-08-20T10:08:34.201746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # test generated annotations\n\n_IM_DIR = f'{DATA_DIR}/images/train'\n_ANN_DIR = f'{DATA_DIR}/labels/train'\nname = np.random.choice(os.listdir(_IM_DIR))[:-4]\n\nim = plt.imread(os.path.join(_IM_DIR, name+'.jpg')).copy()\nH,W = im.shape[:2]\nanns = np.loadtxt(os.path.join(_ANN_DIR, name+'.txt')).reshape(-1, 5)\n\nfor _cls, x,y,w,h in anns.tolist():\n    x *= W\n    y *= H\n    w *= W\n    h *= H\n    x1 = int(x-w/2)\n    x2 = int(x+w/2)\n    y1 = int(y-h/2)\n    y2 = int(y+h/2)\n    label = id2label[_cls]\n    \n#     if _cls == 0:\n#         c = (255,0,0)\n#     elif _cls == 1:\n#         c = (0,255,0)\n#     else:\n#         c = (255,255,0)\n    c = (0,255,255)\n\n    im = cv2.rectangle(im, (x1,y1), (x2,y2), c, 2)\n    cv2.putText(im, label, (x1,y1), fontFace, 0.3, c, 1, cv2.LINE_AA)\n\n\nplt.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:11:51.762662Z","iopub.execute_input":"2024-08-20T10:11:51.763461Z","iopub.status.idle":"2024-08-20T10:11:52.090827Z","shell.execute_reply.started":"2024-08-20T10:11:51.763432Z","shell.execute_reply":"2024-08-20T10:11:52.089931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ls data_fold0/labels/val","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:11:54.594524Z","iopub.execute_input":"2024-08-20T10:11:54.595344Z","iopub.status.idle":"2024-08-20T10:11:54.599116Z","shell.execute_reply.started":"2024-08-20T10:11:54.595313Z","shell.execute_reply":"2024-08-20T10:11:54.598233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.path.join(_ANN_DIR, name+'.txt')","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:11:54.732335Z","iopub.execute_input":"2024-08-20T10:11:54.732798Z","iopub.status.idle":"2024-08-20T10:11:54.736496Z","shell.execute_reply.started":"2024-08-20T10:11:54.732766Z","shell.execute_reply":"2024-08-20T10:11:54.735659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# cat 'train_fold0/labels/404602713_1230697721_12.txt'","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:11:55.640704Z","iopub.execute_input":"2024-08-20T10:11:55.641514Z","iopub.status.idle":"2024-08-20T10:11:55.645152Z","shell.execute_reply.started":"2024-08-20T10:11:55.641483Z","shell.execute_reply":"2024-08-20T10:11:55.644273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Install the ultralytics package from GitHub\n!pip install git+https://github.com/ultralytics/ultralytics.git@main","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:11:55.783911Z","iopub.execute_input":"2024-08-20T10:11:55.784209Z","iopub.status.idle":"2024-08-20T10:12:25.659146Z","shell.execute_reply.started":"2024-08-20T10:11:55.784185Z","shell.execute_reply":"2024-08-20T10:12:25.658032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for k, v in id2label.items():\n    print(f'{k}: {v}')","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:12:25.661511Z","iopub.execute_input":"2024-08-20T10:12:25.661914Z","iopub.status.idle":"2024-08-20T10:12:25.667646Z","shell.execute_reply.started":"2024-08-20T10:12:25.661874Z","shell.execute_reply":"2024-08-20T10:12:25.666601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:12:25.668938Z","iopub.execute_input":"2024-08-20T10:12:25.669265Z","iopub.status.idle":"2024-08-20T10:12:26.665156Z","shell.execute_reply.started":"2024-08-20T10:12:25.669234Z","shell.execute_reply":"2024-08-20T10:12:26.664158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile yolo_scs.yaml\npath: /kaggle/working/data_fold0 # dataset root dir\ntrain: images/train  \nval: images/val \ntest: images/val \n\n# Classes\nnames:\n    0: spinal_canal_stenosis_l1_l2_normal/mild\n    1: spinal_canal_stenosis_l1_l2_moderate\n    2: spinal_canal_stenosis_l1_l2_severe\n    3: spinal_canal_stenosis_l2_l3_normal/mild\n    4: spinal_canal_stenosis_l2_l3_moderate\n    5: spinal_canal_stenosis_l2_l3_severe\n    6: spinal_canal_stenosis_l3_l4_normal/mild\n    7: spinal_canal_stenosis_l3_l4_moderate\n    8: spinal_canal_stenosis_l3_l4_severe\n    9: spinal_canal_stenosis_l4_l5_normal/mild\n    10: spinal_canal_stenosis_l4_l5_moderate\n    11: spinal_canal_stenosis_l4_l5_severe\n    12: spinal_canal_stenosis_l5_s1_normal/mild\n    13: spinal_canal_stenosis_l5_s1_moderate\n    14: spinal_canal_stenosis_l5_s1_severe\n  ","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:12:26.673726Z","iopub.execute_input":"2024-08-20T10:12:26.674147Z","iopub.status.idle":"2024-08-20T10:12:26.684103Z","shell.execute_reply.started":"2024-08-20T10:12:26.674114Z","shell.execute_reply":"2024-08-20T10:12:26.683279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import wandb\nfrom wandb.integration.ultralytics import add_wandb_callback\n\nfrom kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\nsecret_value_0 = user_secrets.get_secret(\"wandbkey\")\nwandb.login(key=secret_value_0)","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:12:26.685232Z","iopub.execute_input":"2024-08-20T10:12:26.685970Z","iopub.status.idle":"2024-08-20T10:12:34.082907Z","shell.execute_reply.started":"2024-08-20T10:12:26.685911Z","shell.execute_reply":"2024-08-20T10:12:34.081957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initialize W&B run\nwandb.init(\n    project=\"lsdc_yolov8\",\n#     name=f\"Demo_fold0\",\n#     tags=[\"baseline\", \"search-lr\", ],\n    group=\";\".join(CONDITIONS),\n#     config={\n#         \"lr\": LR,\n#         \"model-name\":\"xtremedistill-trim\",\n#         \"dataset\": [\n#             \"raw_compettion\",\n#             \"MPWare\",\n#             \"Nicholas\"\n#         ]\n#     }\n)","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:12:34.084231Z","iopub.execute_input":"2024-08-20T10:12:34.084858Z","iopub.status.idle":"2024-08-20T10:12:51.804131Z","shell.execute_reply.started":"2024-08-20T10:12:34.084823Z","shell.execute_reply":"2024-08-20T10:12:51.803134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ultralytics import YOLO\n\n\n# Initialize YOLO Model\nmodel = YOLO(\"yolov8s.pt\")\n\n# Add W&B callback for Ultralytics\nadd_wandb_callback(model, enable_model_checkpointing=True)\n\n# Train/fine-tune your model\n# At the end of each epoch, predictions on validation batches are logged\n# to a W&B table with insightful and interactive overlays for\n# computer vision tasks\nmodel.train(project=\"lsdc_yolov8\", data=\"yolo_scs.yaml\", \n            epochs=EPOCHS, imgsz=OD_INPUT_SIZE, batch=BATCH_SIZE)\n\n# Finish the W&B run\nwandb.finish()","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:12:55.139096Z","iopub.execute_input":"2024-08-20T10:12:55.140057Z","iopub.status.idle":"2024-08-20T10:41:48.026154Z","shell.execute_reply.started":"2024-08-20T10:12:55.140023Z","shell.execute_reply":"2024-08-20T10:41:48.024282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # test generated annotations\n\n_IM_DIR = f'{DATA_DIR}/images/val'\n_ANN_DIR = f'{DATA_DIR}/labels/val'\nname = np.random.choice(os.listdir(_IM_DIR))[:-4]\n\npath = os.path.join(_IM_DIR, name+'.jpg')\n\nim = plt.imread(path).copy()\nH,W = im.shape[:2]\nanns = np.loadtxt(os.path.join(_ANN_DIR, name+'.txt')).reshape(-1, 5)\n\nfor _cls, x,y,w,h in anns.tolist():\n    x *= W\n    y *= H\n    w *= W\n    h *= H\n    x1 = int(x-w/2)\n    x2 = int(x+w/2)\n    y1 = int(y-h/2)\n    y2 = int(y+h/2)\n    label = id2label[_cls]\n    print(label)\n    \n#     if _cls == 0:\n#         c = (255,0,0)\n#     elif _cls == 1:\n#         c = (0,255,0)\n#     else:\n#         c = (255,255,0)\n    c = (0,255,255)\n\n    im = cv2.rectangle(im, (x1,y1), (x2,y2), c, 2)\n    cv2.putText(im, label, (x1,y1), fontFace, 0.3, c, 1, cv2.LINE_AA)\n\n\nplt.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:46:01.726807Z","iopub.execute_input":"2024-08-20T10:46:01.727443Z","iopub.status.idle":"2024-08-20T10:46:01.994816Z","shell.execute_reply.started":"2024-08-20T10:46:01.727411Z","shell.execute_reply":"2024-08-20T10:46:01.993869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initialize YOLO Model\nmodel = YOLO(glob.glob(\"lsdc_yolov8/*/weights/best.pt\")[0])\n\n# Add W&B callback for Ultralytics\n# add_wandb_callback(model, enable_model_checkpointing=True)\n\n# Perform prediction which automatically logs to a W&B Table\n# with interactive overlays for bounding boxes, segmentation masks\nout = model.predict([path], save=True, conf=0.2)\n\n# Finish the W&B run\nwandb.finish()","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:46:13.177373Z","iopub.execute_input":"2024-08-20T10:46:13.177929Z","iopub.status.idle":"2024-08-20T10:46:13.499779Z","shell.execute_reply.started":"2024-08-20T10:46:13.177887Z","shell.execute_reply":"2024-08-20T10:46:13.498873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = plt.imread(glob.glob(f'{out[0].save_dir}/*.jpg')[0])\nplt.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2024-08-20T10:46:13.501623Z","iopub.execute_input":"2024-08-20T10:46:13.502059Z","iopub.status.idle":"2024-08-20T10:46:13.775356Z","shell.execute_reply.started":"2024-08-20T10:46:13.502020Z","shell.execute_reply":"2024-08-20T10:46:13.774358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}