{"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\n\nimport os,shutil\nimport yaml\nfrom tqdm.notebook import tqdm\n\nimport matplotlib.pyplot as plt\nimport cv2\n\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\nfrom PIL import Image\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.copytree('../input/yolov50326', '/kaggle/working/yolov5') \n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -r ./yolov5/requirements.txt","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.copy('../input/vinyolov5xgroupkfold/general.py', '/kaggle/working/yolov5/utils') ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir('./yolov5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python ../yolov5/detect.py --weights ../../input/vinyolov5x5foldsweights/fold0-30-best.pt ../../input/vinyolov5x5foldsweights/fold1-30-best.pt ../../input/vinyolov5x5foldsweights/fold4-25-best.pt ../../input/vinyolov5x5foldsweights/fold3-25-best2.pt\\\n--img 1024\\\n--conf 0.001\\\n--iou 0.4\\\n--source ../../input/vinbigdata-chest-xray-resized-png-1024x1024/test\\\n--save-txt --save-conf --exist-ok\\\n--augment","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install ensemble_boxes","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def yolo2voc(image_height, image_width, bboxes):\n    \"\"\"\n    yolo => [xmid, ymid, w, h] (normalized)\n    voc  => [x1, y1, x2, y1]\n    \n    \"\"\" \n    bboxes = bboxes.copy().astype(float) # otherwise all value will be 0 as voc_pascal dtype is np.int\n    \n    bboxes[..., [0, 2]] = bboxes[..., [0, 2]]* image_width\n    bboxes[..., [1, 3]] = bboxes[..., [1, 3]]* image_height\n    \n    bboxes[..., [0, 1]] = bboxes[..., [0, 1]] - bboxes[..., [2, 3]]/2\n    bboxes[..., [2, 3]] = bboxes[..., [0, 1]] + bboxes[..., [2, 3]]\n    \n    return bboxes","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from glob import glob\n\ntest_df = pd.read_csv('../../input/vinbigdatayolov5sbestweight/test.csv')\n\nimage_ids = []\nPredictionStrings = []\ntotalTarget = 0\n\nfor file_path in tqdm(glob('runs/detect/exp/labels/*txt')):\n    image_id = file_path.split('/')[-1].split('.')[0]\n    #print(image_id)\n    w, h = test_df.loc[test_df.image_id==image_id,['width', 'height']].values[0]\n    f = open(file_path, 'r')\n    lines=f.readlines()\n    bboxes=''\n    for line in lines:\n        totalTarget=totalTarget+1\n        data = np.array(line.replace('\\n', ' ').strip().split(' ')).astype(np.float32).reshape(-1, 6)\n        data = data[:, [0, 5, 1, 2, 3, 4]]\n        bbox = list(np.round(np.concatenate((data[:, :2], np.round(yolo2voc(h, w, data[:, 2:]))), axis =1).reshape(-1), 1).astype(str))\n        for idx in range(len(bbox)):\n            bbox[idx] = str(int(float(bbox[idx]))) if idx%6!=1 else bbox[idx]\n            bboxes=bboxes+str(bbox[idx])+' '\n        #print (bboxes,'\\n')\n    PredictionStrings.append(bboxes)\n    image_ids.append(image_id)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"totalTarget=totalTarget/3000\nprint (totalTarget)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <a href=\"yolov5\"> Download File </a>","metadata":{}},{"cell_type":"code","source":"PredictionStrings","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_df = pd.DataFrame({'image_id':image_ids,\n                        'PredictionString':PredictionStrings})\nsub_df = pd.merge(test_df, pred_df, on = 'image_id', how = 'left').fillna(\"14 1 0 0 1 1\")\nsub_df = sub_df[['image_id', 'PredictionString']]\nsub_df.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df['PredictionString'].value_counts().iloc[[0]]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_2cls = pd.read_csv('../../input/vinbigdata-2class-prediction/2-cls test pred.csv')\npred = pd.merge(sub_df, pred_2cls, on = 'image_id', how = 'left')\npred.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"low_thr  = 0.08\nhigh_thr = 0.95","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def filter_2cls(row, low_thr=low_thr, high_thr=high_thr):\n    prob = row['target']\n    if prob<low_thr:\n        ## Less chance of having any disease\n        row['PredictionString'] = '14 1 0 0 1 1'\n    elif low_thr<=prob<high_thr:\n        ## More change of having any diesease\n        row['PredictionString']+=f' 14 {prob} 0 0 1 1'\n    elif high_thr<=prob:\n        ## Good chance of having any disease so believe in object detection model\n        row['PredictionString'] = row['PredictionString']\n    else:\n        raise ValueError('Prediction must be from [0-1]')\n    return row","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pred.apply(filter_2cls, axis=1)\nsub.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.info()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub['PredictionString'].value_counts().iloc[[0]]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub[['image_id', 'PredictionString']].to_csv('submission.csv',index = False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}