{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"collapsed":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install bbox-visualizer","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom glob import glob\nimport shutil, os\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport bbox_visualizer as bbv\n\nfrom sklearn.model_selection import GroupKFold\nfrom tqdm.notebook import tqdm\n\nimport cv2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"size = 512\nBASE_DIR = '../input/vinbigdata-chest-xray-abnormalities-detection/'\nif size == 512:\n    External_DIR = '../input/vinbigdata'\nif size == 1024:\n    External_DIR = '../input/vinbigdata-chest-xray-resized-png-1024x1024'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(os.path.join(BASE_DIR, \"train.csv\"))\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = train_df[train_df.class_name!='No finding'].reset_index(drop=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dim = pd.read_csv(os.path.join(External_DIR, \"train_meta.csv\"))\ntrain_dim.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.merge(train_df, train_dim, on='image_id')\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# reshaping the bounding-box w.r.t. the resized image\ntrain['x_min'] = train.apply(lambda row: (row.x_min)/row.dim1, axis = 1)*float(size)\ntrain['y_min'] = train.apply(lambda row: (row.y_min)/row.dim0, axis = 1)*float(size)\n\ntrain['x_max'] = train.apply(lambda row: (row.x_max)/row.dim1, axis =1)*float(size)\ntrain['y_max'] = train.apply(lambda row: (row.y_max)/row.dim0, axis =1)*float(size)\n\n# calculation x-mid, y-mid, width and hight of the bounding box for yolo\ntrain['x_mid'] = train.apply(lambda row: (row.x_max+row.x_min)/2, axis =1)\ntrain['y_mid'] = train.apply(lambda row: (row.y_max+row.y_min)/2, axis =1)\n\ntrain['w'] = train.apply(lambda row: (row.x_max-row.x_min), axis =1)\ntrain['h'] = train.apply(lambda row: (row.y_max-row.y_min), axis =1)\n\ntrain['x_mid'] /= float(size)\ntrain['y_mid'] /= float(size)\n\ntrain['w'] /= float(size)\ntrain['h'] /= float(size)\n\ntrain['area'] = train['w']*train['h']\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Kfold  = GroupKFold(n_splits = 3)\ntrain['fold'] = -1\nfor fold, (train_idx, val_idx) in enumerate(Kfold.split(train, groups = train.image_id.values)):\n    train.loc[val_idx, 'fold'] = fold\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fold_0 = 1\nfold_1 = 2\ntrain_files = []\nval_files   = []\n\ntrain_files += list(train[train.fold==fold_0].image_id.unique())\nval_files += list(train[train.fold==fold_1].image_id.unique())\nlen(train_files), len(val_files)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def visualize_plot(idx):\n    image = train_files[idx]\n    records = train[train['image_id'] == image]\n    boxes = np.array(records[['x_min','y_min','x_max','y_max']])\n    \n    labels = records.class_name\n    sample = cv2.imread(os.path.join('/content','train',f'{image}.png'))\n    print(image)\n    img = sample.copy()\n    plt.figure(figsize=(16, 16))\n    for box,label in zip(boxes,labels):\n        bbv.add_label(img, \n                      label, \n                      [int(round(box[0])), int(round(box[1])),int(round(box[2])), int(round(box[3]))], \n                      draw_bg=True,\n                      text_bg_color=(255,0,0),\n                      text_color=(0,0,0),\n                      )\n        cv2.rectangle(img ,\n                      (int(round(box[0])), int(round(box[1]))),\n                      (int(round(box[2])), int(round(box[3]))),\n                      (255,0,0),\n                      2)\n\n\n    plt.imshow(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#visualize_plot(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.makedirs('./vinbigdata/labels/train', exist_ok = True)\nos.makedirs('./vinbigdata/labels/val', exist_ok = True)\nos.makedirs('./vinbigdata/images/train', exist_ok = True)\nos.makedirs('./vinbigdata/images/val', exist_ok = True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TRAIN_LABELS_PATH = './vinbigdata/labels/train'\nVAL_LABELS_PATH = './vinbigdata/labels/val'\nTRAIN_IMAGES_PATH = './vinbigdata/images/train'\nVAL_IMAGES_PATH = './vinbigdata/images/val'\n\nfor file in tqdm(train_files):\n    records = train[train['image_id'] == file]\n    attributes = records[['class_id','x_mid','y_mid','w','h']].values\n    attributes = np.array(attributes)\n    np.savetxt(\n        os.path.join(\n            TRAIN_LABELS_PATH,\n            f\"{file}.txt\"\n        ),\n        attributes,\n        fmt = [\"%d\",\"%f\",\"%f\",\"%f\",\"%f\"]\n    )\n    shutil.copy(\n        os.path.join(\n            External_DIR,\n            'train',\n            f\"{file}.png\" \n        ),          \n        TRAIN_IMAGES_PATH\n    )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for file in tqdm(val_files):\n    records = train[train['image_id'] == file]\n    attributes = records[['class_id','x_mid','y_mid','w','h']]\n    attributes = np.array(attributes)\n    np.savetxt(\n        os.path.join(\n            VAL_LABELS_PATH,\n            f\"{file}.txt\"\n        ),\n        attributes,\n        fmt = [\"%d\",\"%f\",\"%f\",\"%f\",\"%f\"]\n    )\n    shutil.copy(\n        os.path.join(\n            External_DIR,\n            'train',\n            f\"{file}.png\" \n        ),          \n        VAL_IMAGES_PATH\n    )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_ids, class_names = list(zip(*set(zip(train.class_id, train.class_name))))\nclasses = list(np.array(class_names)[np.argsort(class_ids)])\nclasses = list(map(lambda x: str(x), classes))\nclasses","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from os import listdir\nfrom os.path import isfile, join\nimport yaml\n\ncwd = './'\n\nwith open(join( cwd , 'train.txt'), 'w') as f:\n    for path in glob('./vinbigdata/images/train/*'):\n        f.write(path+'\\n')\n            \nwith open(join( cwd , 'val.txt'), 'w') as f:\n    for path in glob('./vinbigdata/images/val/*'):\n        f.write(path+'\\n')\n\ndata = dict(\n    train =  '../train.txt',\n    val   =  '../val.txt',\n    nc    = 14,\n    names = classes\n    )\n\nwith open(join( cwd , 'vinbigdata.yaml'), 'w') as outfile:\n    yaml.dump(data, outfile, default_flow_style=False)\n    \nf = open(join( cwd, 'vinbigdata.yaml'), 'r')\nprint('\\nyaml:')\nprint(f.read())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"!git clone https://github.com/ultralytics/yolov3.git\n%cd ./yolov3/\n!pip install -r requirements.txt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"!wget https://github.com/ultralytics/yolov3/releases/download/v9.1/yolov3.pt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!WANDB_MODE=\"dryrun\" python train.py --img {size} --batch-size 40 --epochs 60 --data ../vinbigdata.yaml --weights yolov3.pt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install matplotlib==3.1.3\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread('runs/train/exp/results.png'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread('runs/train/exp/precision_recall_curve.png'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(30,15))\nplt.axis('off')\nplt.imshow(plt.imread('./runs/train/exp/labels.jpg'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"_, ax = plt.subplots(1, 2, figsize=(20, 20))\n\nax[0].imshow(plt.imread('runs/train/exp/test_batch0_labels.jpg'))\nax[1].imshow(plt.imread('runs/train/exp/test_batch0_pred.jpg'))\nax[0].title.set_text('Ground Truth')\nax[1].title.set_text('YOLO predictions')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"_, ax = plt.subplots(1, 2, figsize=(20, 20))\n\nax[0].imshow(plt.imread('runs/train/exp/test_batch1_labels.jpg'))\nax[1].imshow(plt.imread('runs/train/exp/test_batch1_pred.jpg'))\nax[0].title.set_text('Ground Truth')\nax[1].title.set_text('YOLO predictions')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"_, ax = plt.subplots(1, 2, figsize=(20, 20))\n\nax[0].imshow(plt.imread('runs/train/exp/test_batch2_labels.jpg'))\nax[1].imshow(plt.imread('runs/train/exp/test_batch2_pred.jpg'))\nax[0].title.set_text('Ground Truth')\nax[1].title.set_text('YOLO predictions')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for file in (glob('runs/train/exp/**/*.png', recursive = True)+glob('runs/train/exp/**/*.jpg', recursive = True)):\n    os.remove(file)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%cd ..\nshutil.rmtree('vinbigdata')","execution_count":null,"outputs":[]}],"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}