{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Version\n* `v4`: **2-cls filter**\n* `v5`: **2-cls filter** + [**1x1 bbox trick** 🔥](https://www.kaggle.com/c/vinbigdata-chest-xray-abnormalities-detection/discussion/211971)"},{"metadata":{},"cell_type":"markdown","source":"# 🌟2 Class Filter🌟\nPreviously I have trained `YOLOv5` using `14` class data. As it creates `FP` we can tackle that just simply using a `2 class filter`. Here I'll be using 2 class model (`AUC`:`0.98`) prediction to filter out the `FP` predictions. I used `EfficientNetB6` to generate these predictions.\nIt should increase the score as `FP` would be reduced significantly\n\n**Notebooks**\n* [14 class train](https://www.kaggle.com/awsaf49/vinbigdata-cxr-ad-yolov5-14-class-train)\n* [14 class infer](https://www.kaggle.com/awsaf49/vinbigdata-cxr-ad-yolov5-14-class-infer)\n\n**Dataset:**\n* [YOLOv5 Labels](https://www.kaggle.com/awsaf49/vinbigdata-yolo-labels-dataset)\n* [1024x1024 Dataset](https://www.kaggle.com/awsaf49/vinbigdata-1024-image-dataset)\n* [512x512 Dataset](https://www.kaggle.com/awsaf49/vinbigdata-512-image-dataset)\n* [256x256 Dataset](https://www.kaggle.com/awsaf49/vinbigdata-512-image-dataset)\n* [Original Size '.jpg'](https://www.kaggle.com/awsaf49/vinbigdata-original-image-dataset)"},{"metadata":{},"cell_type":"markdown","source":"# Loading Package"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nfrom glob import glob\nimport shutil","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Threshold For `2 Class Filter`\n**NB**: The threshold was chosen arbitarily"},{"metadata":{"trusted":true},"cell_type":"code","source":"low_thr  = 0.08\nhigh_thr = 0.95","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Loading csv"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"pred_14cls = pd.read_csv('../input/vinbigdata-14-class-submission-lb0154/submission.csv')\npred_2cls = pd.read_csv('../input/vinbigdata-2class-prediction/2-cls test pred.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_14cls.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred_2cls.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred = pd.merge(pred_14cls, pred_2cls, on = 'image_id', how = 'left')\npred.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Before 2 Class Filter Number of `No Finding`"},{"metadata":{"trusted":true},"cell_type":"code","source":"pred['PredictionString'].value_counts().iloc[[0]]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# 2 Class Filter + [**1x1 bbox trick** 🔥](https://www.kaggle.com/c/vinbigdata-chest-xray-abnormalities-detection/discussion/211971)"},{"metadata":{"trusted":true},"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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pred.apply(filter_2cls, axis=1)\nsub.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# After 2 Class Filter Number of `No Finding`"},{"metadata":{"trusted":true},"cell_type":"code","source":"sub['PredictionString'].value_counts().iloc[[0]]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As we can see from above that applying `2 class filter` Number of `'No Finding'`increases significanly. **[549->1912]**. We can also see that `1x1 bbox trick` increases the result"},{"metadata":{"trusted":true},"cell_type":"code","source":"sub[['image_id', 'PredictionString']].to_csv('submission.csv',index = False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Result\nAs we can see applying the `2 class filter` improves the result significantly, from `0.154` to `0.201`. But bear in mind that choosing the `thershold` could be a bit `tricky`."},{"metadata":{},"cell_type":"markdown","source":"# Please Upvote If You Have Found This Notebook Useful 😃"}],"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":4,"nbformat_minor":4}