{"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":"!git clone https://github.com/WongKinYiu/yolov7 # Downloading YOLOv7 repository and installing requirements","metadata":{"papermill":{"duration":2.582872,"end_time":"2022-10-11T05:31:15.024272","exception":false,"start_time":"2022-10-11T05:31:12.4414","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport numpy as np\nimport shutil\nimport yaml\nimport matplotlib.pyplot as plt\nimport random\nimport cv2\n\nfrom sklearn import model_selection\nfrom tqdm import tqdm\nfrom glob import glob","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":0.968304,"end_time":"2022-10-11T05:31:16.019836","exception":false,"start_time":"2022-10-11T05:31:15.051532","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd yolov7","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size = 512\nTRAIN_LABELS_PATH = './vinbigdata/labels/train'\nVAL_LABELS_PATH = './vinbigdata/labels/val'\nTRAIN_IMAGES_PATH = './vinbigdata/images/train' #12000\nVAL_IMAGES_PATH = './vinbigdata/images/val' #3000\n#External_DIR = f'../input/vinbigdata-{size}-image-dataset/vinbigdata/train' # 15000\nExternal_DIR = '/kaggle/input/vinbigdata-competition-jpg-data-3x-downsampled/train/train'\nos.makedirs(TRAIN_LABELS_PATH, exist_ok = True)\nos.makedirs(VAL_LABELS_PATH, exist_ok = True)\nos.makedirs(TRAIN_IMAGES_PATH, exist_ok = True)\nos.makedirs(VAL_IMAGES_PATH, exist_ok = True)","metadata":{"papermill":{"duration":0.035276,"end_time":"2022-10-11T05:31:16.082148","exception":false,"start_time":"2022-10-11T05:31:16.046872","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#original_df = pd.read_csv(f'/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train.csv')\n#original_df = original_df[original_df['class_name'] != 'No finding']\n#original_df.reset_index(inplace = True)\n#original_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"original_df = pd.read_csv(f'/kaggle/input/vinbigdata-{size}-image-dataset/vinbigdata/train.csv')\nnumber_of_imageids = len(original_df['image_id'].values)\nprint(f'Total number of image_ids (train + validation) {number_of_imageids}')\n\nnumber_of_images = len(os.listdir('/kaggle/input/vinbigdata-competition-jpg-data-3x-downsampled/train/train'))\nprint(f'Total number of images (train + validation) {number_of_images}')\n\nnumber_of_labels = len(os.listdir('/kaggle/input/vinbigdata-yolo-labels-dataset/labels'))\nprint(f'Total number of labels (train + validation) {number_of_labels}')","metadata":{"papermill":{"duration":0.539495,"end_time":"2022-10-11T05:31:16.648549","exception":false,"start_time":"2022-10-11T05:31:16.109054","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(f'/kaggle/input/vinbigdata-{size}-image-dataset/vinbigdata/train.csv')\n#df = df[df['class_name'] != 'No finding']\n#df.reset_index(inplace = True)\ndf","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nnumber_of_images = len(df['image_id'].values)\nprint(f'Total number of image ids (train + validation) {number_of_images}')\n\n#df = df[df.class_id!=14].reset_index(drop = True)\nnumber_of_images = len(df['image_id'].values)\nprint(f'Total number of image ids (train + validation) {number_of_images}')\n\ndf.head()","metadata":{"papermill":{"duration":0.2186,"end_time":"2022-10-11T05:31:16.894418","exception":false,"start_time":"2022-10-11T05:31:16.675818","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop(columns=['class_name', 'rad_id', 'x_min', 'x_max', 'y_min', 'y_max', 'width', 'height', 'class_id']) # we only need image ids, labels are pre-made","metadata":{"papermill":{"duration":0.040306,"end_time":"2022-10-11T05:31:16.962341","exception":false,"start_time":"2022-10-11T05:31:16.922035","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df = df.drop(['index'], axis=1)\ndf.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train, df_valid = model_selection.train_test_split(df, test_size=0.30, random_state=42, shuffle=True)","metadata":{"papermill":{"duration":0.037758,"end_time":"2022-10-11T05:31:17.027455","exception":false,"start_time":"2022-10-11T05:31:16.989697","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_valid.shape","metadata":{"papermill":{"duration":0.036617,"end_time":"2022-10-11T05:31:17.091688","exception":false,"start_time":"2022-10-11T05:31:17.055071","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"number_of_images = len(df_train['image_id'].values)\nprint(f'Total number of training image_ids {number_of_images}')\n\nnumber_of_images = len(df_valid['image_id'].values)\nprint(f'Total number of validation image_ids {number_of_images}')\n","metadata":{"papermill":{"duration":0.03683,"end_time":"2022-10-11T05:31:17.15626","exception":false,"start_time":"2022-10-11T05:31:17.11943","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# need to delete duplicate image ids, len(labels) should be equal len(df.imageids.values), ","metadata":{"papermill":{"duration":0.070985,"end_time":"2022-10-11T05:31:17.270794","exception":false,"start_time":"2022-10-11T05:31:17.199809","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Total number of training images {len(df_train.image_id.unique())}')\nprint(f'Total number of validation images {len(df_valid.image_id.unique())}')","metadata":{"papermill":{"duration":0.075219,"end_time":"2022-10-11T05:31:17.40631","exception":false,"start_time":"2022-10-11T05:31:17.331091","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(df.image_id.unique()))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preproccess_data(df, labels_path, images_path):\n    for img_id in tqdm(df.image_id.unique()):\n        shutil.copy(os.path.join('/kaggle/input/vinbigdata-yolo-labels-dataset/labels/', f\"{img_id}\"+'.txt'), labels_path)\n        shutil.copy(os.path.join(f'/kaggle/input/vinbigdata-competition-jpg-data-3x-downsampled/train/train/', f\"{img_id}.jpg\"), images_path)","metadata":{"papermill":{"duration":0.051409,"end_time":"2022-10-11T05:31:17.500112","exception":false,"start_time":"2022-10-11T05:31:17.448703","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preproccess_data(df_train, TRAIN_LABELS_PATH, TRAIN_IMAGES_PATH)\npreproccess_data(df_valid, VAL_LABELS_PATH, VAL_IMAGES_PATH)","metadata":{"papermill":{"duration":41.656756,"end_time":"2022-10-11T05:31:59.198369","exception":false,"start_time":"2022-10-11T05:31:17.541613","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check that data was preprocessed correctly\nprint(len(os.listdir(TRAIN_LABELS_PATH)))\nprint(len(os.listdir(TRAIN_IMAGES_PATH)))\n\n\nprint(len(os.listdir(VAL_LABELS_PATH)))\nprint(len(os.listdir(VAL_IMAGES_PATH)))","metadata":{"papermill":{"duration":0.141303,"end_time":"2022-10-11T05:31:59.462139","exception":false,"start_time":"2022-10-11T05:31:59.320836","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.copy('/kaggle/input/fyaml6/vinbigdata.yaml','/kaggle/working/yolov7/data/')","metadata":{"papermill":{"duration":0.130515,"end_time":"2022-10-11T05:31:59.713454","exception":false,"start_time":"2022-10-11T05:31:59.582939","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('/kaggle/working/yolov7/weights',exist_ok = True)\nshutil.copy('/kaggle/input/yolov7-280-epoch/best.pt', '/kaggle/working/yolov7/weights')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install -q -U -r requirements.txt\n#!pip install -q pycocotools>=2.0 seaborn>=0.11.0 thop","metadata":{"papermill":{"duration":123.3629,"end_time":"2022-10-11T05:34:03.197244","exception":false,"start_time":"2022-10-11T05:31:59.834344","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/working/yolov7/data')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!WANDB_MODE=\"dryrun\" python train.py --workers 0 --device 0 --batch-size 20 --data data/vinbigdata.yaml --img 256 256 --cfg cfg/training/yolov7.yaml --weights 'weights/best.pt' --name yolov7 --hyp data/hyp.scratch.p5.yaml --epochs 20","metadata":{"papermill":{"duration":11220.973208,"end_time":"2022-10-11T08:41:04.294575","exception":false,"start_time":"2022-10-11T05:34:03.321367","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(f'/kaggle/input/vinbigdata-{size}-image-dataset/vinbigdata/test.csv')","metadata":{"papermill":{"duration":8.717897,"end_time":"2022-10-11T08:41:21.079277","exception":false,"start_time":"2022-10-11T08:41:12.36138","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"papermill":{"duration":8.056492,"end_time":"2022-10-11T08:41:37.077444","exception":false,"start_time":"2022-10-11T08:41:29.020952","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('./runs/train/yolov7/weights')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = f'/kaggle/input/vinbigdata-competition-jpg-data-3x-downsampled/test/test'\nweights_dir = './runs/train/yolov7/weights/best.pt'\n#shutil.copy('/kaggle/input/yolov7-280-epoch/best.pt','./runs/train/yolov7/weights')\nos.listdir('./runs/train/yolov7/weights')","metadata":{"papermill":{"duration":8.109856,"end_time":"2022-10-11T08:41:53.8202","exception":false,"start_time":"2022-10-11T08:41:45.710344","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python detect.py --weights $weights_dir\\\n--img 640\\\n--conf 0.005\\\n--iou 0.45\\\n--source $test_dir\\\n--save-txt --save-conf --exist-ok","metadata":{"papermill":{"duration":273.846798,"end_time":"2022-10-11T08:46:35.572687","exception":false,"start_time":"2022-10-11T08:42:01.725889","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# credit / source https://www.kaggle.com/awsaf49/vinbigdata-cxr-ad-yolov5-14-class-infer\ndef 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":{"papermill":{"duration":8.940757,"end_time":"2022-10-11T08:46:53.229911","exception":false,"start_time":"2022-10-11T08:46:44.289154","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(glob('runs/detect/exp/labels/*txt'))","metadata":{"papermill":{"duration":8.726758,"end_time":"2022-10-11T08:47:11.298101","exception":false,"start_time":"2022-10-11T08:47:02.571343","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# credit / source https://www.kaggle.com/awsaf49/vinbigdata-cxr-ad-yolov5-14-class-infer\nimage_ids = []\nPredictionStrings = []\n\ndef process_submission():\n    for file_path in tqdm(glob('runs/detect/exp/labels/*txt')):\n        image_id = file_path.split('/')[-1].split('.')[0] # extract image id\n        w, h = test_df.loc[test_df.image_id==image_id,['width', 'height']].values[0] #  get the weight & height from  the test df\n        f = open(file_path, 'r')  # open the label text file\n        data = np.array(f.read().replace('\\n', ' ').strip().split(' ')).astype(np.float32).reshape(-1, 6) # move all the labels to the same line..?\n        data = data[:, [0, 5, 1, 2, 3, 4]]\n        bboxes = 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(bboxes)):\n            bboxes[idx] = str(int(float(bboxes[idx]))) if idx%6!=1 else bboxes[idx] # 6 is the length of  the prediction string, so..?\n        image_ids.append(image_id)\n        PredictionStrings.append(' '.join(bboxes))\n\n    # credit / source: https://www.kaggle.com/awsaf49/vinbigdata-cxr-ad-yolov5-14-class-infer\n    pred_df = pd.DataFrame({'image_id':image_ids,\n                            'PredictionString':PredictionStrings})\n    sub_df = pd.merge(test_df, pred_df, on = 'image_id', how = 'left').fillna(\"14 1 0 0 1 1\")\n    sub_df = sub_df[['image_id', 'PredictionString']]\n    sub_df.to_csv('/kaggle/working/submission_3.csv',index = False)\n    sub_df.tail()","metadata":{"papermill":{"duration":8.865591,"end_time":"2022-10-11T08:47:29.006301","exception":false,"start_time":"2022-10-11T08:47:20.14071","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip uninstall pandas\n!pip install --upgrade pip\n!pip install matplotlib==3.1.1","metadata":{"papermill":{"duration":19.164559,"end_time":"2022-10-11T08:47:57.475424","exception":false,"start_time":"2022-10-11T08:47:38.310865","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pandas==1.3.0","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"process_submission()","metadata":{"papermill":{"duration":14.369157,"end_time":"2022-10-11T08:48:20.884033","exception":false,"start_time":"2022-10-11T08:48:06.514876","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install matplotlib==3.3","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('./runs/train/yolov7')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\n#plt.imshow('./runs/detect/exp/13db1898f2d711db4aaf0b2a9a4059cd.png')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread('./runs/train/yolov7/R_curve.png'));","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread('./runs/train/yolov7/R_curve.png'));","metadata":{"papermill":{"duration":9.711617,"end_time":"2022-10-11T08:51:25.433123","exception":false,"start_time":"2022-10-11T08:51:15.721506","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread('./runs/train/yolov7/confusion_matrix.png'));","metadata":{"papermill":{"duration":10.125256,"end_time":"2022-10-11T08:51:44.117308","exception":false,"start_time":"2022-10-11T08:51:33.992052","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread('./runs/train/yolov7/results.png'));","metadata":{"papermill":{"duration":9.87953,"end_time":"2022-10-11T08:52:03.112146","exception":false,"start_time":"2022-10-11T08:51:53.232616","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd","metadata":{"papermill":{"duration":10.354444,"end_time":"2022-10-11T08:52:22.220993","exception":false,"start_time":"2022-10-11T08:52:11.866549","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load yolo submission\nyolo = pd.read_csv('../submission_3.csv')\nyolo = yolo.drop_duplicates()\neffnetb6 = pd.read_csv('/kaggle/input/preds123/Vgg and resnet.csv') # AUC:0.98\npred = pd.merge(yolo, effnetb6, on = 'image_id', how = 'left')\nlow_thr  = 0.08\nhigh_thr = 0.95","metadata":{"papermill":{"duration":8.789437,"end_time":"2022-10-11T08:52:39.872961","exception":false,"start_time":"2022-10-11T08:52:31.083524","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nyolo","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":{"papermill":{"duration":8.802349,"end_time":"2022-10-11T08:52:57.918641","exception":false,"start_time":"2022-10-11T08:52:49.116292","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pred.apply(filter_2cls, axis=1)\nsub[['image_id', 'PredictionString']].to_csv('../submission.csv',index = False)","metadata":{"papermill":{"duration":9.066841,"end_time":"2022-10-11T08:53:15.814609","exception":false,"start_time":"2022-10-11T08:53:06.747768","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":8.713755,"end_time":"2022-10-11T08:53:33.577779","exception":false,"start_time":"2022-10-11T08:53:24.864024","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"papermill":{"duration":9.267807,"end_time":"2022-10-11T08:53:51.734793","exception":false,"start_time":"2022-10-11T08:53:42.466986","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}