{"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 pycocotools\n!pip install thop\n\n!cd /kaggle/input/yolo5-zfturbo-xray-code/;python3 /kaggle/input/yolo5-zfturbo-xray-code/detect.py --weights /kaggle/input/chest-xray-abnormalities-detection-models/yolo_best/best_fold_0_mAP_0.383_0.184.pt --device 0 --img-size 640 --save-txt --save-conf --conf-thres 0.01 --iou-thres 0.25 --source /kaggle/input/chest-xray-test-diff-sizes/test_png_div_4/ --project /kaggle/working/yolov5_fold0/ --name test_iou_0.25_0.01\n\n!cd /kaggle/input/yolo5-zfturbo-xray-code/;python3 /kaggle/input/yolo5-zfturbo-xray-code/detect.py --weights /kaggle/input/chest-xray-abnormalities-detection-models/yolo_best/best_fold_1_mAP_0.395_0.182.pt --device 0 --img-size 640 --save-txt --save-conf --conf-thres 0.01 --iou-thres 0.25 --source /kaggle/input/chest-xray-test-diff-sizes/test_png_div_4/ --project /kaggle/working/yolov5_fold1/ --name test_iou_0.25_0.01\n\n!cd /kaggle/input/yolo5-zfturbo-xray-code/;python3 /kaggle/input/yolo5-zfturbo-xray-code/detect.py --weights /kaggle/input/chest-xray-abnormalities-detection-models/yolo_best/best_fold_2_mAP_0.415_0.196.pt --device 0 --img-size 640 --save-txt --save-conf --conf-thres 0.01 --iou-thres 0.25 --source /kaggle/input/chest-xray-test-diff-sizes/test_png_div_4/ --project /kaggle/working/yolov5_fold2/ --name test_iou_0.25_0.01\n\n!cd /kaggle/input/yolo5-zfturbo-xray-code/;python3 /kaggle/input/yolo5-zfturbo-xray-code/detect.py --weights /kaggle/input/chest-xray-abnormalities-detection-models/yolo_best/best_fold_3_mAP_0.382_0.183.pt --device 0 --img-size 640 --save-txt --save-conf --conf-thres 0.01 --iou-thres 0.25 --source /kaggle/input/chest-xray-test-diff-sizes/test_png_div_4/ --project /kaggle/working/yolov5_fold3/ --name test_iou_0.25_0.01\n\n!cd /kaggle/input/yolo5-zfturbo-xray-code/;python3 /kaggle/input/yolo5-zfturbo-xray-code/detect.py --weights /kaggle/input/chest-xray-abnormalities-detection-models/yolo_best/best_fold_4_mAP_0.409_0.189.pt --device 0 --img-size 640 --save-txt --save-conf --conf-thres 0.01 --iou-thres 0.25 --source /kaggle/input/chest-xray-test-diff-sizes/test_png_div_4/ --project /kaggle/working/yolov5_fold4/ --name test_iou_0.25_0.01\n\n!cd /kaggle/input/yolo5-zfturbo-xray-code/;python3 /kaggle/input/yolo5-zfturbo-xray-code/detect_mirror.py --weights /kaggle/input/chest-xray-abnormalities-detection-models/yolo_best/best_fold_0_mAP_0.383_0.184.pt --device 0 --img-size 640 --save-txt --save-conf --conf-thres 0.01 --iou-thres 0.25 --source /kaggle/input/chest-xray-test-diff-sizes/test_png_div_4/ --project /kaggle/working/yolov5_fold0/ --name test_iou_0.25_0.01_mirror\n\n!cd /kaggle/input/yolo5-zfturbo-xray-code/;python3 /kaggle/input/yolo5-zfturbo-xray-code/detect_mirror.py --weights /kaggle/input/chest-xray-abnormalities-detection-models/yolo_best/best_fold_1_mAP_0.395_0.182.pt --device 0 --img-size 640 --save-txt --save-conf --conf-thres 0.01 --iou-thres 0.25 --source /kaggle/input/chest-xray-test-diff-sizes/test_png_div_4/ --project /kaggle/working/yolov5_fold1/ --name test_iou_0.25_0.01_mirror\n\n!cd /kaggle/input/yolo5-zfturbo-xray-code/;python3 /kaggle/input/yolo5-zfturbo-xray-code/detect_mirror.py --weights /kaggle/input/chest-xray-abnormalities-detection-models/yolo_best/best_fold_2_mAP_0.415_0.196.pt --device 0 --img-size 640 --save-txt --save-conf --conf-thres 0.01 --iou-thres 0.25 --source /kaggle/input/chest-xray-test-diff-sizes/test_png_div_4/ --project /kaggle/working/yolov5_fold2/ --name test_iou_0.25_0.01_mirror\n\n!cd /kaggle/input/yolo5-zfturbo-xray-code/;python3 /kaggle/input/yolo5-zfturbo-xray-code/detect_mirror.py --weights /kaggle/input/chest-xray-abnormalities-detection-models/yolo_best/best_fold_3_mAP_0.382_0.183.pt --device 0 --img-size 640 --save-txt --save-conf --conf-thres 0.01 --iou-thres 0.25 --source /kaggle/input/chest-xray-test-diff-sizes/test_png_div_4/ --project /kaggle/working/yolov5_fold3/ --name test_iou_0.25_0.01_mirror\n\n!cd /kaggle/input/yolo5-zfturbo-xray-code/;python3 /kaggle/input/yolo5-zfturbo-xray-code/detect_mirror.py --weights /kaggle/input/chest-xray-abnormalities-detection-models/yolo_best/best_fold_4_mAP_0.409_0.189.pt --device 0 --img-size 640 --save-txt --save-conf --conf-thres 0.01 --iou-thres 0.25 --source /kaggle/input/chest-xray-test-diff-sizes/test_png_div_4/ --project /kaggle/working/yolov5_fold4/ --name test_iou_0.25_0.01_mirror","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob \nfor i in range(5):\n    files_orig = glob.glob('yolo*_fold{}/test_iou_0.25_0.01/labels/*'.format(i))\n    print(i, len(files_orig))\n    files_mirror = glob.glob('yolo*_fold{}/test_iou_0.25_0.01_mirror/labels/*'.format(i))\n    print(i, len(files_mirror))","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# coding: utf-8\n__author__ = 'ZFTurbo: https://kaggle.com/zfturbo'\n\n\nimport numpy as np\nimport gzip\nimport pickle\nimport os\nimport glob\nimport time\nimport cv2\nimport datetime\nimport pandas as pd\nfrom sklearn.metrics import fbeta_score\nfrom sklearn.model_selection import KFold, train_test_split\nfrom collections import Counter, defaultdict\nfrom sklearn.metrics import accuracy_score, roc_auc_score, log_loss\nimport random\nimport shutil\nimport operator\nfrom PIL import Image\nimport platform\nimport json\nimport base64\nimport typing as t\nimport zlib\nimport pydicom\nimport re\nfrom tqdm import tqdm\n\n\nINPUT_PATH = '../input/vinbigdata-chest-xray-abnormalities-detection/'\nOUTPUT_PATH = './'\nSUBM_PATH = './'\n\n\ndef get_train_test_image_sizes():\n    sizes = dict()\n    sizes_train = pd.read_csv('../input/meta-xray/image_width_height_train.csv')\n    sizes_test = pd.read_csv('../input/meta-xray/image_width_height_test.csv')\n    sizes_df = pd.concat((sizes_train, sizes_test), axis=0)\n    for index, row in sizes_df.iterrows():\n        sizes[row['image_id']] = (row['height'], row['width'])\n    return sizes\n\n\ndef convert_preds_test(input_dir, out_file):\n    sizes = get_train_test_image_sizes()\n    s = pd.read_csv(INPUT_PATH + 'sample_submission.csv')\n    files = glob.glob(input_dir + '*.txt')\n    print(len(files))\n    part = s\n    print(len(part))\n    valid_ids = set(part['image_id'].values)\n    out = open(out_file, 'w')\n    out.write('image_id,PredictionString\\n')\n    for f in files:\n        image_id = os.path.basename(f)[:-4]\n        in1 = open(f, 'r')\n        lines = in1.readlines()\n        in1.close()\n        valid_ids.remove(image_id)\n        out.write('{},'.format(image_id))\n        pred_str = ''\n        for line in lines:\n            arr = line.strip().split(' ')\n            class_id = arr[0]\n            x = float(arr[1])\n            y = float(arr[2])\n            w = float(arr[3])\n            h = float(arr[4])\n            x1 = x - (w / 2)\n            x2 = x + (w / 2)\n            y1 = y - (h / 2)\n            y2 = y + (h / 2)\n            conf = arr[5]\n\n            x1 = int(round(x1 * sizes[image_id][1]))\n            y1 = int(round(y1 * sizes[image_id][0]))\n            x2 = int(round(x2 * sizes[image_id][1]))\n            y2 = int(round(y2 * sizes[image_id][0]))\n\n            pred_str += '{} {} {} {} {} {} '.format(class_id, conf, x1, y1, x2, y2)\n        out.write('{}\\n'.format(pred_str))\n\n    print(len(valid_ids))\n\n    # Output empty IDs\n    for image_id in list(valid_ids):\n        out.write('{},14 1 0 0 1 1\\n'.format(image_id))\n\n    out.close()\n\n\nif __name__ == '__main__':\n    for fold_num in range(5):\n        input_dir = OUTPUT_PATH + 'yolov5_fold{}/test_iou_0.25_0.01/labels/'.format(fold_num)\n        out_file = SUBM_PATH + 'yolov5_fold{}_iou_0.25_thr_0.01_test.csv'.format(fold_num)\n        convert_preds_test(input_dir, out_file)\n\n        input_dir = OUTPUT_PATH + 'yolov5_fold{}/test_iou_0.25_0.01_mirror/labels/'.format(fold_num)\n        out_file = SUBM_PATH + 'yolov5_fold{}_iou_0.25_thr_0.01_mirror_test.csv'.format(fold_num)\n        convert_preds_test(input_dir, out_file)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install ensemble-boxes","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ensemble_boxes import weighted_boxes_fusion\n\n\ndef ensemble(\n    subm_list,\n    iou_same=0.5,\n    out_path=None,\n    skip_box_thr=0.00000001,\n):\n    sizes = get_train_test_image_sizes()\n    preds = []\n    weights = []\n    checker = None\n    for path, weight in subm_list:\n        s = pd.read_csv(path)\n        s.sort_values('image_id', inplace=True)\n        s.reset_index(drop=True, inplace=True)\n        ids = s['image_id']\n        if checker:\n            if tuple(ids) != checker:\n                print(set(checker) - set(ids))\n                print('Different IDS!', len(tuple(ids)), path)\n                exit()\n        else:\n            checker = tuple(ids)\n        preds.append(s['PredictionString'].values)\n        weights.append(weight)\n\n    if out_path is None:\n        out_path = SUBM_PATH + 'ensemble_iou_{}.csv'.format(iou_same)\n    out = open(out_path, 'w')\n    out.write('image_id,PredictionString\\n')\n    for j, id in enumerate(list(checker)):\n        # print(id)\n        boxes_list = []\n        scores_list = []\n        labels_list = []\n        empty = True\n        for i in range(len(preds)):\n            boxes = []\n            scores = []\n            labels = []\n            p1 = preds[i][j]\n            if str(p1) != 'nan':\n                arr = p1.strip().split(' ')\n                for k in range(0, len(arr), 6):\n                    cls = int(arr[k])\n                    prob = float(arr[k + 1])\n                    x1 = float(arr[k + 2]) / sizes[id][1]\n                    y1 = float(arr[k + 3]) / sizes[id][0]\n                    x2 = float(arr[k + 4]) / sizes[id][1]\n                    y2 = float(arr[k + 5]) / sizes[id][0]\n                    boxes.append([x1, y1, x2, y2])\n                    scores.append(prob)\n                    labels.append(cls)\n\n            boxes_list.append(boxes)\n            scores_list.append(scores)\n            labels_list.append(labels)\n\n        boxes, scores, labels = weighted_boxes_fusion(\n            boxes_list,\n            scores_list,\n            labels_list,\n            iou_thr=iou_same,\n            skip_box_thr=skip_box_thr,\n            weights=weights,\n            allows_overflow=True\n        )\n        # print(len(boxes), len(labels), len(scores))\n        if len(boxes) == 0:\n            out.write('{},14 1 0 0 1 1\\n'.format(id, ))\n        else:\n            final_str = ''\n            for i in range(len(boxes)):\n                cls = int(labels[i])\n                prob = scores[i]\n                x1 = int(boxes[i][0] * sizes[id][1])\n                y1 = int(boxes[i][1] * sizes[id][0])\n                x2 = int(boxes[i][2] * sizes[id][1])\n                y2 = int(boxes[i][3] * sizes[id][0])\n                if cls == 14:\n                    final_str += '{} {} {} {} {} {} '.format(cls, prob, 0, 0, 1, 1)\n                else:\n                    final_str += '{} {} {} {} {} {} '.format(cls, prob, x1, y1, x2, y2)\n            out.write('{},{}\\n'.format(id, final_str.strip()))\n\n    out.close()\n    return out_path\n\n\ndef get_test_from_subm_list(subm_list):\n    out = []\n    for s, w in subm_list:\n        s1 = s.replace('_train', '_test')\n        out.append((s1, w))\n    return out\n\n\ndef ensemble_experiment_v4_yolo():\n\n    sp = SUBM_PATH\n    subm_list = [\n        (sp + 'yolov5_fold0_iou_0.25_thr_0.01_train.csv', 1),\n        (sp + 'yolov5_fold1_iou_0.25_thr_0.01_train.csv', 1),\n        (sp + 'yolov5_fold2_iou_0.25_thr_0.01_train.csv', 1),\n        (sp + 'yolov5_fold3_iou_0.25_thr_0.01_train.csv', 1),\n        (sp + 'yolov5_fold4_iou_0.25_thr_0.01_train.csv', 1),\n    ]\n    subm_list_test = get_test_from_subm_list(subm_list)\n\n    best_iou = 0.3\n    out_path = SUBM_PATH + 'ensemble_yolo_standard.csv'.format(len(subm_list_test), best_iou)\n    predictions = ensemble(subm_list_test, best_iou, out_path)\n\n\ndef ensemble_experiment_v12_yolo_mirror():\n    sp = SUBM_PATH\n    subm_list_test = [\n        (sp + 'yolov5_fold0_iou_0.25_thr_0.01_mirror_test.csv', 1),\n        (sp + 'yolov5_fold1_iou_0.25_thr_0.01_mirror_test.csv', 1),\n        (sp + 'yolov5_fold2_iou_0.25_thr_0.01_mirror_test.csv', 1),\n        (sp + 'yolov5_fold3_iou_0.25_thr_0.01_mirror_test.csv', 1),\n        (sp + 'yolov5_fold4_iou_0.25_thr_0.01_mirror_test.csv', 1),\n    ]\n\n    best_iou = 0.3\n    best_map = -1\n    out_path = SUBM_PATH + 'ensemble_yolo_mirror.csv'.format(len(subm_list_test), best_iou, best_map)\n    predictions = ensemble(subm_list_test, best_iou, out_path)\n\n\ndef ensemble_experiment_v13_ensemble_yolo():\n    iou = 0.4\n    mean_ap = -1\n    subm_list = [\n        (SUBM_PATH + 'ensemble_yolo_standard.csv', 1),\n        (SUBM_PATH + 'ensemble_yolo_mirror.csv', 1),\n    ]\n    out_path = SUBM_PATH + 'ensemble_yolo_final.csv'.format(len(subm_list), iou, mean_ap)\n    predictions = ensemble(subm_list, iou, out_path)\n\n    \nif __name__ == '__main__':\n    ensemble_experiment_v4_yolo()\n    ensemble_experiment_v12_yolo_mirror()\n    ensemble_experiment_v13_ensemble_yolo()\n    print('Finished!')","metadata":{},"execution_count":null,"outputs":[]}]}