{"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":"import numpy as np \nimport pandas as pd \nimport os\nimport glob\nimport pydicom\nfrom tqdm import tqdm\nimport cv2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-24T14:28:38.18865Z","iopub.execute_input":"2023-04-24T14:28:38.189105Z","iopub.status.idle":"2023-04-24T14:28:38.562654Z","shell.execute_reply.started":"2023-04-24T14:28:38.189002Z","shell.execute_reply":"2023-04-24T14:28:38.561641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* 모든 train 데이터에 대해 bounding_box 값이 주어지지 않는다.\n* 우선 존재하는 StudyInstanceID에 대해서만 YOLO를 진행해보기로 한다.","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train.csv')\ntrain_bbox = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train_bounding_boxes.csv')","metadata":{"execution":{"iopub.status.busy":"2023-04-24T14:31:41.958247Z","iopub.execute_input":"2023-04-24T14:31:41.958811Z","iopub.status.idle":"2023-04-24T14:31:42.009661Z","shell.execute_reply.started":"2023-04-24T14:31:41.958765Z","shell.execute_reply":"2023-04-24T14:31:42.008453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(3) \n# 해당 환자의 골절 여부만 기록되어 있다.","metadata":{"execution":{"iopub.status.busy":"2023-04-24T14:31:50.832034Z","iopub.execute_input":"2023-04-24T14:31:50.832515Z","iopub.status.idle":"2023-04-24T14:31:50.86093Z","shell.execute_reply.started":"2023-04-24T14:31:50.83248Z","shell.execute_reply":"2023-04-24T14:31:50.86015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[train['StudyInstanceUID']=='1.2.826.0.1.3680043.6200']['patient_overall'].values[0]","metadata":{"execution":{"iopub.status.busy":"2023-04-24T14:37:05.293484Z","iopub.execute_input":"2023-04-24T14:37:05.293908Z","iopub.status.idle":"2023-04-24T14:37:05.303032Z","shell.execute_reply.started":"2023-04-24T14:37:05.293875Z","shell.execute_reply":"2023-04-24T14:37:05.302206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_bbox.head(3) \n# 한 환자의 여러 사진 중 어디가 골절인지 나타낸다.","metadata":{"execution":{"iopub.status.busy":"2023-04-24T14:31:53.970211Z","iopub.execute_input":"2023-04-24T14:31:53.970777Z","iopub.status.idle":"2023-04-24T14:31:53.989732Z","shell.execute_reply.started":"2023-04-24T14:31:53.970728Z","shell.execute_reply":"2023-04-24T14:31:53.98826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train and train_bbox의 StudyInstanceUID 갯수 차이\nprint(\"train ID갯수 : \", len(train['StudyInstanceUID']),\", train_bbox ID갯수 : \",len(train_bbox['StudyInstanceUID'].unique()))","metadata":{"execution":{"iopub.status.busy":"2023-04-24T14:32:48.297037Z","iopub.execute_input":"2023-04-24T14:32:48.297462Z","iopub.status.idle":"2023-04-24T14:32:48.304771Z","shell.execute_reply.started":"2023-04-24T14:32:48.297422Z","shell.execute_reply":"2023-04-24T14:32:48.303635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"train data는 **1행에 1개**의 ID값이 부여되고, \\\ntrain_bbox data는 **n행에 1개**의 ID값이 부여될 수 있다. \\\n위 분석을 통해 bbox가 존재하는 데이터만 사용한다면 235개의 ID에 대한 정보를 사용할 수 있음을 알 수 있고, 데이터가 더 필요하다는 사실을 느꼈다.\\\n추후 train_images를 모두 사용할 예정이다.    \n\ntrain_bbox의 값을 가진 환자는 모두 양성인 사람들이다.","metadata":{}},{"cell_type":"code","source":"for t in train_bbox['StudyInstanceUID'].unique():\n    if train[train['StudyInstanceUID']==t]['patient_overall'].values[0]==0:\n        print(train[train['StudyInstanceUID']==t])","metadata":{"execution":{"iopub.status.busy":"2023-04-24T14:37:11.693277Z","iopub.execute_input":"2023-04-24T14:37:11.693671Z","iopub.status.idle":"2023-04-24T14:37:11.858733Z","shell.execute_reply.started":"2023-04-24T14:37:11.693639Z","shell.execute_reply":"2023-04-24T14:37:11.857814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '../input/rsna-2022-cervical-spine-fracture-detection/train_images/'\ntrain_dir_list = []\ntrain_dir_list = os.listdir(train_dir)\ntrain_bbox_list = train_bbox['StudyInstanceUID'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-10-06T15:23:46.728422Z","iopub.execute_input":"2022-10-06T15:23:46.728826Z","iopub.status.idle":"2022-10-06T15:23:46.738366Z","shell.execute_reply.started":"2022-10-06T15:23:46.728792Z","shell.execute_reply":"2022-10-06T15:23:46.737097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"혹시나 train_bbox안에 있는 데이터들이 train_images에 없을까봐 확인해보았다. \\\n다행히도 모든 데이터가 이미지로 존재했다.","metadata":{}},{"cell_type":"code","source":"cnt=0\nfor tb in train_bbox_list:\n    if tb in train_dir_list:\n        cnt+=1\n    else :\n        pass\nprint(\"train.csv파일과 train_bounding_boxes.csv파일의 ID종류 개수는 같은가?\",len(train_bbox_list)==cnt)","metadata":{"execution":{"iopub.status.busy":"2022-10-06T15:25:41.03412Z","iopub.execute_input":"2022-10-06T15:25:41.034498Z","iopub.status.idle":"2022-10-06T15:25:41.04398Z","shell.execute_reply.started":"2022-10-06T15:25:41.034466Z","shell.execute_reply":"2022-10-06T15:25:41.04334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_length=[]\nfor dir in train_dir_list:\n    train_length.append(len(f'{train_dir}/dir'))\nprint(\"각 아이디별로 가지고 있는 dcm파일 갯수\",set(train_length))","metadata":{"execution":{"iopub.status.busy":"2022-10-06T15:27:00.511752Z","iopub.execute_input":"2022-10-06T15:27:00.512148Z","iopub.status.idle":"2022-10-06T15:27:00.520092Z","shell.execute_reply.started":"2022-10-06T15:27:00.512114Z","shell.execute_reply":"2022-10-06T15:27:00.518538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-10-06T15:27:21.277795Z","iopub.execute_input":"2022-10-06T15:27:21.278301Z","iopub.status.idle":"2022-10-06T15:27:21.295076Z","shell.execute_reply.started":"2022-10-06T15:27:21.27826Z","shell.execute_reply":"2022-10-06T15:27:21.293476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_bbox.info()","metadata":{"execution":{"iopub.status.busy":"2022-10-06T15:27:10.367604Z","iopub.execute_input":"2022-10-06T15:27:10.367979Z","iopub.status.idle":"2022-10-06T15:27:10.388824Z","shell.execute_reply.started":"2022-10-06T15:27:10.367946Z","shell.execute_reply":"2022-10-06T15:27:10.38804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- train_bounding_boxes 는 x, y, width, heigh 값이 들어있어서 yolo training 을 시도할 수 있다.\n- dcm 파일로 저장된 이미지들을 jpg파일 형태로 변환하고, train_bounding_boxes.csv는 txt파일 형태로 변환하여 YOLO 학습을 시도해볼 수 있겠다.","metadata":{}}]}