{"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":"!conda install gdcm -c conda-forge -y","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-08-20T09:25:39.334448Z","iopub.execute_input":"2021-08-20T09:25:39.334813Z","iopub.status.idle":"2021-08-20T09:26:40.369445Z","shell.execute_reply.started":"2021-08-20T09:25:39.334736Z","shell.execute_reply":"2021-08-20T09:26:40.368464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nfrom PIL import Image\nimport pandas as pd\nfrom tqdm.auto import tqdm\n\nimport numpy as np\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\nimport matplotlib.pyplot as plt\n\nimport cv2\nfrom IPython.display import  clear_output\n\nclear_output()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-20T10:14:13.850968Z","iopub.execute_input":"2021-08-20T10:14:13.851322Z","iopub.status.idle":"2021-08-20T10:14:13.860119Z","shell.execute_reply.started":"2021-08-20T10:14:13.851292Z","shell.execute_reply":"2021-08-20T10:14:13.859257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n    this function allows you to read an image in dcm format\n'''\ndef readXray(path, voi_lut = True, fix_monochrome = True):\n    # Original from: https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way\n    dicom = pydicom.read_file(path)\n    \n    # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to \n    # \"human-friendly\" view\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n               \n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n        \n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n        \n    return data","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:27:15.853122Z","iopub.execute_input":"2021-08-20T09:27:15.853445Z","iopub.status.idle":"2021-08-20T09:27:15.859929Z","shell.execute_reply.started":"2021-08-20T09:27:15.853417Z","shell.execute_reply":"2021-08-20T09:27:15.859143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/siim-covid19-detection/train_image_level.csv')","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:27:21.583028Z","iopub.execute_input":"2021-08-20T09:27:21.583352Z","iopub.status.idle":"2021-08-20T09:27:21.638712Z","shell.execute_reply.started":"2021-08-20T09:27:21.583324Z","shell.execute_reply":"2021-08-20T09:27:21.637740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_study = pd.read_csv('../input/siim-covid19-detection/train_study_level.csv') ","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:27:23.949025Z","iopub.execute_input":"2021-08-20T09:27:23.949342Z","iopub.status.idle":"2021-08-20T09:27:23.968114Z","shell.execute_reply.started":"2021-08-20T09:27:23.949312Z","shell.execute_reply":"2021-08-20T09:27:23.967359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:27:25.316538Z","iopub.execute_input":"2021-08-20T09:27:25.316860Z","iopub.status.idle":"2021-08-20T09:27:25.340586Z","shell.execute_reply.started":"2021-08-20T09:27:25.316832Z","shell.execute_reply":"2021-08-20T09:27:25.339660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom = readXray(\"../input/siim-covid19-detection/train/0051d9b12e72/152f6ec68d86/bb4b1da810f3.dcm\")\n\nplt.figure(figsize=(15,10)) \nplt.imshow(dicom, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:27:27.549419Z","iopub.execute_input":"2021-08-20T09:27:27.549743Z","iopub.status.idle":"2021-08-20T09:27:29.235653Z","shell.execute_reply.started":"2021-08-20T09:27:27.549713Z","shell.execute_reply":"2021-08-20T09:27:29.234289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bbox_txt = train[train.StudyInstanceUID =='0051d9b12e72']\nprint(bbox_txt.index)\nprint(train.boxes[4605])\nprint(train.label[4605])\n","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:27:32.248597Z","iopub.execute_input":"2021-08-20T09:27:32.248921Z","iopub.status.idle":"2021-08-20T09:27:32.257887Z","shell.execute_reply.started":"2021-08-20T09:27:32.248891Z","shell.execute_reply":"2021-08-20T09:27:32.257036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bbox_txt.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:27:34.702135Z","iopub.execute_input":"2021-08-20T09:27:34.702472Z","iopub.status.idle":"2021-08-20T09:27:34.712513Z","shell.execute_reply.started":"2021-08-20T09:27:34.702442Z","shell.execute_reply":"2021-08-20T09:27:34.711583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import ast\nbbox = ast.literal_eval(train.boxes[4605])\n\nbboxs = []\nfor b in bbox:\n    bboxs.append([b['x'] , b['y'], b['x']+b['width'],  b['y']+b['height']])\nprint(bboxs)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:27:37.072390Z","iopub.execute_input":"2021-08-20T09:27:37.072723Z","iopub.status.idle":"2021-08-20T09:27:37.081100Z","shell.execute_reply.started":"2021-08-20T09:27:37.072694Z","shell.execute_reply":"2021-08-20T09:27:37.079906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nthis function allows to extract the bounding box from text format\n'''\ndef getBboxs(txt_bboxs):\n    try:\n        bbox = ast.literal_eval(txt_bboxs)\n        bboxs = []\n        for b in bbox:\n            bboxs.append([b['x'] , b['y'], b['x']+b['width'],  b['y']+b['height']])\n        return bboxs\n    except:\n        return []","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:27:39.234331Z","iopub.execute_input":"2021-08-20T09:27:39.234677Z","iopub.status.idle":"2021-08-20T09:27:39.240078Z","shell.execute_reply.started":"2021-08-20T09:27:39.234646Z","shell.execute_reply":"2021-08-20T09:27:39.239070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-07-30T07:26:36.305247Z","iopub.execute_input":"2021-07-30T07:26:36.305614Z","iopub.status.idle":"2021-07-30T07:26:36.312286Z","shell.execute_reply.started":"2021-07-30T07:26:36.305576Z","shell.execute_reply":"2021-07-30T07:26:36.311467Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nthis function allows you to draw the bounding box\n'''\ndef drawBoxes(boxes):\n\n    for i in boxes:     \n        # Top\n        x = [i[0] ,  i[2] ]       # [ x1 , x2 ]\n        y = [i[1], i[1] ]               # [ y1 , y2 ]\n        plt.plot(x,y, color='#ff8838', linewidth=2)\n        \n        # Bottom\n        y = [ i[3], i[3] ]\n        plt.plot(x,y, color='#ff8838', linewidth=2)\n        \n        # Left\n        x = [i[0] , i[0]]\n        y = [i[1], i[3] ]\n        plt.plot(x,y, color='#ff8838', linewidth=2)\n\n        # Right         \n        x = [ i[2] ,  i[2] ]\n        plt.plot(x,y, color='#ff8838', linewidth=2)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:27:41.920810Z","iopub.execute_input":"2021-08-20T09:27:41.921245Z","iopub.status.idle":"2021-08-20T09:27:41.928357Z","shell.execute_reply.started":"2021-08-20T09:27:41.921208Z","shell.execute_reply":"2021-08-20T09:27:41.927141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bboxs_to_darw = getBboxs(train.boxes[4605])\n\nplt.figure(figsize=(15,10)) \ndrawBoxes(bboxs_to_darw)\nplt.imshow(dicom, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:27:44.455544Z","iopub.execute_input":"2021-08-20T09:27:44.455868Z","iopub.status.idle":"2021-08-20T09:27:45.541018Z","shell.execute_reply.started":"2021-08-20T09:27:44.455837Z","shell.execute_reply":"2021-08-20T09:27:45.540122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nthis function allows you to resize the bounding box  \n'''\ndef newBbox(boxes, img_o_x, img_o_y, img_x=512, img_y=512):\n    x_scale = ( img_x / img_o_x )\n    y_scale = ( img_y / img_o_y)\n \n   # boxes = ast.literal_eval(boxes)\n\n    new_bbox = []\n    for i in boxes:   \n        n_x1 = i[0] * x_scale\n        n_y1 = i[1] * y_scale       \n        n_x2 = i[2] * x_scale\n        n_y2 = i[3] * y_scale\n        new_bbox.append([n_x1, n_y1, n_x2,n_y2 ])\n    return new_bbox","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:27:48.089206Z","iopub.execute_input":"2021-08-20T09:27:48.089677Z","iopub.status.idle":"2021-08-20T09:27:48.096029Z","shell.execute_reply.started":"2021-08-20T09:27:48.089632Z","shell.execute_reply":"2021-08-20T09:27:48.095066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.resize(dicom, (512,512), interpolation = cv2.INTER_AREA)\ndim0 = dicom.shape[0]\ndim1 = dicom.shape[1]\nplt.figure(figsize=(15,10)) \ndrawBoxes(newBbox(bboxs_to_darw, dim1, dim0))\nplt.imshow(img, cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:27:50.479335Z","iopub.execute_input":"2021-08-20T09:27:50.479755Z","iopub.status.idle":"2021-08-20T09:27:50.798078Z","shell.execute_reply.started":"2021-08-20T09:27:50.479716Z","shell.execute_reply":"2021-08-20T09:27:50.797144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-07-30T10:14:51.669836Z","iopub.execute_input":"2021-07-30T10:14:51.670202Z","iopub.status.idle":"2021-07-30T10:14:51.677868Z","shell.execute_reply.started":"2021-07-30T10:14:51.670172Z","shell.execute_reply":"2021-07-30T10:14:51.676296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nthis function converts the bounding box to yolov5 format\n'''\ndef convert(boxs, class_val=0, size=(512,512)):\n    label = \"\"\n    for box in boxs:\n\n    \n        dw = 1./size[0]\n        dh = 1./size[1]\n        x = ((box[0] + box[2])/2.0)*dw\n        y = ((box[1] + box[3])/2.0)*dh\n        w = (box[2] - box[0])*dw\n        h = (box[3] - box[1])*dh\n        label = label + str(class_val)+ \" \" + str(x) + \" \" + str(y) + \" \" + str(w) + \" \" + str(h) + \"\\n\"\n    return label\nprint(convert(newBbox(bboxs_to_darw, dim1, dim0)))","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:28:15.577904Z","iopub.execute_input":"2021-08-20T09:28:15.578285Z","iopub.status.idle":"2021-08-20T09:28:15.588328Z","shell.execute_reply.started":"2021-08-20T09:28:15.578253Z","shell.execute_reply":"2021-08-20T09:28:15.587284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newBbox(bboxs_to_darw, dim1, dim0)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:28:18.558023Z","iopub.execute_input":"2021-08-20T09:28:18.558368Z","iopub.status.idle":"2021-08-20T09:28:18.564299Z","shell.execute_reply.started":"2021-08-20T09:28:18.558339Z","shell.execute_reply":"2021-08-20T09:28:18.563493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nthis function converts the bounding box from yolov5 format to normal format\n'''\ndef convertRev(boxs, size=(512,512)):\n    bboxs = []\n    for box in boxs:\n        dw = 1./size[0]\n        dh = 1./size[1]\n        x = ((box[0]/dw)*2 - box[2]/dw)/2\n        y = ((box[1]/dh)*2 - box[3]/dh)/2\n        w = box[2]/dw + x   \n        h = box[3]/dw + y \n\n        bboxs.append([x,y,w,h])\n      \n    return bboxs\n\nconvertRev([[ 0.7753488311001411, 0.6818237628424657, 0.1590697566995768, 0.19198785958904108]])","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:28:21.131380Z","iopub.execute_input":"2021-08-20T09:28:21.131728Z","iopub.status.idle":"2021-08-20T09:28:21.143274Z","shell.execute_reply.started":"2021-08-20T09:28:21.131697Z","shell.execute_reply":"2021-08-20T09:28:21.142099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-07-30T10:19:29.756266Z","iopub.execute_input":"2021-07-30T10:19:29.756602Z","iopub.status.idle":"2021-07-30T10:19:29.761956Z","shell.execute_reply.started":"2021-07-30T10:19:29.756566Z","shell.execute_reply":"2021-07-30T10:19:29.760961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-07-30T10:14:22.186904Z","iopub.execute_input":"2021-07-30T10:14:22.187375Z","iopub.status.idle":"2021-07-30T10:14:22.206096Z","shell.execute_reply.started":"2021-07-30T10:14:22.18734Z","shell.execute_reply":"2021-07-30T10:14:22.20478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nhere we will convert all the images to .png format with a resolution of 512x512, by saving the original size of each one of them\n'''           \nimage_id = []\ndim0 = []\ndim1 = []\nsplits = []\n\nfor split in ['test', 'train']:\n    save_dir = f'images/{split}/'\n    os.makedirs(save_dir, exist_ok=True)\n    \n    for dirname, _, filenames in tqdm(os.walk(f'../input/siim-covid19-detection/{split}')):\n        for file in filenames:\n            \n            xray = readXray(os.path.join(dirname, file))\n            \n            im = cv2.resize(xray, (512,512), interpolation = cv2.INTER_AREA)\n            cv2.imwrite(os.path.join(save_dir, file.replace('dcm', 'png')), im)\n            \n            image_id.append(file.replace('.dcm', ''))\n            dim0.append(xray.shape[0])\n            dim1.append(xray.shape[1])\n            splits.append(split)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T09:28:25.105434Z","iopub.execute_input":"2021-08-20T09:28:25.105765Z","iopub.status.idle":"2021-08-20T10:11:02.616846Z","shell.execute_reply.started":"2021-08-20T09:28:25.105736Z","shell.execute_reply":"2021-08-20T10:11:02.616025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r images.zip ./images\nclear_output()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:14:39.885174Z","iopub.execute_input":"2021-08-20T10:14:39.885522Z","iopub.status.idle":"2021-08-20T10:15:12.479973Z","shell.execute_reply.started":"2021-08-20T10:14:39.885491Z","shell.execute_reply":"2021-08-20T10:15:12.478997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta = pd.DataFrame.from_dict({'image_id': image_id, 'dim0': dim0, 'dim1': dim1, 'split': splits})\nmeta.to_csv('meta.csv', index=False)\nmeta.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:15:28.486462Z","iopub.execute_input":"2021-08-20T10:15:28.486793Z","iopub.status.idle":"2021-08-20T10:15:28.535271Z","shell.execute_reply.started":"2021-08-20T10:15:28.486758Z","shell.execute_reply":"2021-08-20T10:15:28.534332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:38:01.075678Z","iopub.execute_input":"2021-07-30T12:38:01.076115Z","iopub.status.idle":"2021-07-30T12:38:01.089605Z","shell.execute_reply.started":"2021-07-30T12:38:01.07607Z","shell.execute_reply":"2021-07-30T12:38:01.0887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-07-30T12:38:19.411812Z","iopub.execute_input":"2021-07-30T12:38:19.412167Z","iopub.status.idle":"2021-07-30T12:38:19.423993Z","shell.execute_reply.started":"2021-07-30T12:38:19.412134Z","shell.execute_reply":"2021-07-30T12:38:19.422901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta_test = meta[meta.split == 'test']\nmeta_train = meta[meta.split == 'train']","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:15:47.382693Z","iopub.execute_input":"2021-08-20T10:15:47.383024Z","iopub.status.idle":"2021-08-20T10:15:47.393040Z","shell.execute_reply.started":"2021-08-20T10:15:47.382974Z","shell.execute_reply":"2021-08-20T10:15:47.392052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:15:49.213504Z","iopub.execute_input":"2021-08-20T10:15:49.213823Z","iopub.status.idle":"2021-08-20T10:15:49.221641Z","shell.execute_reply.started":"2021-08-20T10:15:49.213794Z","shell.execute_reply":"2021-08-20T10:15:49.220566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:15:51.694287Z","iopub.execute_input":"2021-08-20T10:15:51.694612Z","iopub.status.idle":"2021-08-20T10:15:51.708374Z","shell.execute_reply.started":"2021-08-20T10:15:51.694578Z","shell.execute_reply":"2021-08-20T10:15:51.707362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:15:53.454527Z","iopub.execute_input":"2021-08-20T10:15:53.454876Z","iopub.status.idle":"2021-08-20T10:15:53.461255Z","shell.execute_reply.started":"2021-08-20T10:15:53.454838Z","shell.execute_reply":"2021-08-20T10:15:53.460323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['image_id'] = train.id.apply(lambda x: x.split('_')[0])\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:15:55.884521Z","iopub.execute_input":"2021-08-20T10:15:55.884926Z","iopub.status.idle":"2021-08-20T10:15:55.901456Z","shell.execute_reply.started":"2021-08-20T10:15:55.884884Z","shell.execute_reply":"2021-08-20T10:15:55.900473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = pd.merge(meta_train, train, on='image_id', how='outer')\nresult.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:15:57.779282Z","iopub.execute_input":"2021-08-20T10:15:57.779599Z","iopub.status.idle":"2021-08-20T10:15:57.815105Z","shell.execute_reply.started":"2021-08-20T10:15:57.779570Z","shell.execute_reply":"2021-08-20T10:15:57.814143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result['bboxs_512'] = result.apply(lambda x: newBbox(getBboxs(x.boxes), x.dim1, x.dim0), axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:16:01.220554Z","iopub.execute_input":"2021-08-20T10:16:01.220866Z","iopub.status.idle":"2021-08-20T10:16:01.610908Z","shell.execute_reply.started":"2021-08-20T10:16:01.220837Z","shell.execute_reply":"2021-08-20T10:16:01.610054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:16:03.758715Z","iopub.execute_input":"2021-08-20T10:16:03.759052Z","iopub.status.idle":"2021-08-20T10:16:03.775370Z","shell.execute_reply.started":"2021-08-20T10:16:03.759022Z","shell.execute_reply":"2021-08-20T10:16:03.774560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_study.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:16:05.945137Z","iopub.execute_input":"2021-08-20T10:16:05.945502Z","iopub.status.idle":"2021-08-20T10:16:05.959720Z","shell.execute_reply.started":"2021-08-20T10:16:05.945469Z","shell.execute_reply":"2021-08-20T10:16:05.958469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_study['Negative for Pneumonia'] = train_study['Negative for Pneumonia'].replace(1, 'negative')\ntrain_study['Typical Appearance'] = train_study['Typical Appearance'].replace(1, 'typical')\ntrain_study['Indeterminate Appearance'] = train_study['Indeterminate Appearance'].replace(1, 'indeterminate')\ntrain_study['Atypical Appearance'] = train_study['Atypical Appearance'].replace(1, 'atypical')\n\ntrain_study['Negative for Pneumonia'] = train_study['Negative for Pneumonia'].replace(0, '')\ntrain_study['Typical Appearance'] = train_study['Typical Appearance'].replace(0, '')\ntrain_study['Indeterminate Appearance'] = train_study['Indeterminate Appearance'].replace(0, '')\ntrain_study['Atypical Appearance'] = train_study['Atypical Appearance'].replace(0, '')\ntrain_study['class_name'] = train_study['Negative for Pneumonia'] + train_study['Typical Appearance'] + train_study['Indeterminate Appearance'] +  train_study['Atypical Appearance']","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:16:08.238521Z","iopub.execute_input":"2021-08-20T10:16:08.238845Z","iopub.status.idle":"2021-08-20T10:16:08.313925Z","shell.execute_reply.started":"2021-08-20T10:16:08.238815Z","shell.execute_reply":"2021-08-20T10:16:08.313140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_study['class_val'] = train_study['class_name']\n\ntrain_study['class_val'] = train_study['class_val'].replace('negative', 0)\ntrain_study['class_val'] = train_study['class_val'].replace('typical', 1)\ntrain_study['class_val'] = train_study['class_val'].replace('indeterminate', 2)\ntrain_study['class_val'] = train_study['class_val'].replace('atypical', 3)\ntrain_study.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:16:10.962514Z","iopub.execute_input":"2021-08-20T10:16:10.962930Z","iopub.status.idle":"2021-08-20T10:16:10.991784Z","shell.execute_reply.started":"2021-08-20T10:16:10.962893Z","shell.execute_reply":"2021-08-20T10:16:10.991042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-07-30T09:54:18.935015Z","iopub.execute_input":"2021-07-30T09:54:18.93538Z","iopub.status.idle":"2021-07-30T09:54:18.944606Z","shell.execute_reply.started":"2021-07-30T09:54:18.935344Z","shell.execute_reply":"2021-07-30T09:54:18.94367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" train_study['StudyInstanceUID'] = train_study.id.apply(lambda x: x.split('_')[0])","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:16:13.959291Z","iopub.execute_input":"2021-08-20T10:16:13.959644Z","iopub.status.idle":"2021-08-20T10:16:13.968203Z","shell.execute_reply.started":"2021-08-20T10:16:13.959614Z","shell.execute_reply":"2021-08-20T10:16:13.967224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_study.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:16:16.512015Z","iopub.execute_input":"2021-08-20T10:16:16.512338Z","iopub.status.idle":"2021-08-20T10:16:16.525572Z","shell.execute_reply.started":"2021-08-20T10:16:16.512309Z","shell.execute_reply":"2021-08-20T10:16:16.524671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_study_df = train_study[['class_name', 'class_val', 'StudyInstanceUID'] ]\ntrain_study_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:16:18.561496Z","iopub.execute_input":"2021-08-20T10:16:18.561841Z","iopub.status.idle":"2021-08-20T10:16:18.574320Z","shell.execute_reply.started":"2021-08-20T10:16:18.561810Z","shell.execute_reply":"2021-08-20T10:16:18.573318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_df = pd.merge(result, train_study_df, on='StudyInstanceUID', how='outer')\nresult_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:16:20.986654Z","iopub.execute_input":"2021-08-20T10:16:20.986976Z","iopub.status.idle":"2021-08-20T10:16:21.017048Z","shell.execute_reply.started":"2021-08-20T10:16:20.986946Z","shell.execute_reply":"2021-08-20T10:16:21.016074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_df['yolo_label'] = result_df.apply(lambda x: convert(x.bboxs_512, x.class_val), axis=1)\nresult_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:16:24.129130Z","iopub.execute_input":"2021-08-20T10:16:24.129512Z","iopub.status.idle":"2021-08-20T10:16:24.340399Z","shell.execute_reply.started":"2021-08-20T10:16:24.129480Z","shell.execute_reply":"2021-08-20T10:16:24.339384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dir = f'labels/'\nos.makedirs(labels_dir, exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:16:26.978344Z","iopub.execute_input":"2021-08-20T10:16:26.978660Z","iopub.status.idle":"2021-08-20T10:16:26.983042Z","shell.execute_reply.started":"2021-08-20T10:16:26.978631Z","shell.execute_reply":"2021-08-20T10:16:26.982001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def setYoloLables(name, labels):\n    with open(labels_dir+name+'.txt', 'w',) as f:\n        f.write(labels)\n        f.close()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:16:29.815514Z","iopub.execute_input":"2021-08-20T10:16:29.815836Z","iopub.status.idle":"2021-08-20T10:16:29.820425Z","shell.execute_reply.started":"2021-08-20T10:16:29.815806Z","shell.execute_reply":"2021-08-20T10:16:29.819429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_df.apply(lambda x: setYoloLables(x.image_id, x.yolo_label), axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:16:32.632007Z","iopub.execute_input":"2021-08-20T10:16:32.632334Z","iopub.status.idle":"2021-08-20T10:16:33.273697Z","shell.execute_reply.started":"2021-08-20T10:16:32.632305Z","shell.execute_reply":"2021-08-20T10:16:33.272958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nbboxes = []\nwith open(labels_dir+'29b23a11d1e4.txt', 'r') as file:\n     for line in file:\n            print(line)","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:16:53.000969Z","iopub.execute_input":"2021-08-20T10:16:53.001325Z","iopub.status.idle":"2021-08-20T10:16:53.006951Z","shell.execute_reply.started":"2021-08-20T10:16:53.001295Z","shell.execute_reply":"2021-08-20T10:16:53.006009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r labels.zip ./labels\nclear_output()","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:17:43.813138Z","iopub.execute_input":"2021-08-20T10:17:43.813470Z","iopub.status.idle":"2021-08-20T10:17:44.826171Z","shell.execute_reply.started":"2021-08-20T10:17:43.813441Z","shell.execute_reply":"2021-08-20T10:17:44.825322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf ./images\n!rm -rf ./labels","metadata":{"execution":{"iopub.status.busy":"2021-08-20T10:18:34.608665Z","iopub.execute_input":"2021-08-20T10:18:34.609006Z","iopub.status.idle":"2021-08-20T10:18:36.242615Z","shell.execute_reply.started":"2021-08-20T10:18:34.608954Z","shell.execute_reply":"2021-08-20T10:18:36.241531Z"},"trusted":true},"execution_count":null,"outputs":[]}]}