{"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":"!conda install '/kaggle/input/pydicom-conda-helper/libjpeg-turbo-2.1.0-h7f98852_0.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/libgcc-ng-9.3.0-h2828fa1_19.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/gdcm-2.8.9-py37h500ead1_1.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/conda-4.10.1-py37h89c1867_0.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/certifi-2020.12.5-py37h89c1867_1.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/openssl-1.1.1k-h7f98852_0.tar.bz2' -c conda-forge -y","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-19T10:45:17.042256Z","iopub.execute_input":"2021-08-19T10:45:17.042566Z","iopub.status.idle":"2021-08-19T10:46:25.231646Z","shell.execute_reply.started":"2021-08-19T10:45:17.042499Z","shell.execute_reply":"2021-08-19T10:46:25.230731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nprint(tf.__version__)\nimport torch\nprint(f\"Setup complete. Using torch {torch.__version__} ({torch.cuda.get_device_properties(0).name if torch.cuda.is_available() else 'CPU'})\")\n\nimport os\nimport gc\nimport cv2\nimport glob\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nfrom tqdm import tqdm\nfrom shutil import copyfile\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import train_test_split\n\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut","metadata":{"execution":{"iopub.status.busy":"2021-08-19T10:46:47.375585Z","iopub.execute_input":"2021-08-19T10:46:47.375943Z","iopub.status.idle":"2021-08-19T10:46:54.104854Z","shell.execute_reply.started":"2021-08-19T10:46:47.375907Z","shell.execute_reply":"2021-08-19T10:46:54.104009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gpus = tf.config.list_physical_devices('GPU')\nif gpus:\n  try:\n    # Currently, memory growth needs to be the same across GPUs\n    for gpu in gpus:\n      tf.config.experimental.set_memory_growth(gpu, True)\n    logical_gpus = tf.config.experimental.list_logical_devices('GPU')\n    print(len(gpus), \"Physical GPUs,\", len(logical_gpus), \"Logical GPUs\")\n  except RuntimeError as e:\n    # Memory growth must be set before GPUs have been initialized\n    print(e)","metadata":{"execution":{"iopub.status.busy":"2021-08-19T10:46:54.106297Z","iopub.execute_input":"2021-08-19T10:46:54.106617Z","iopub.status.idle":"2021-08-19T10:47:00.022513Z","shell.execute_reply.started":"2021-08-19T10:46:54.106584Z","shell.execute_reply":"2021-08-19T10:47:00.020048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.read_csv('/kaggle/input/siim-covid19-detection/sample_submission.csv')\nprint(len(sub_df))\nsub_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.100616Z","iopub.status.idle":"2021-08-12T11:01:28.101193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_df = sub_df.loc[sub_df.id.str.contains('_study')]\nlen(study_df)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.102389Z","iopub.status.idle":"2021-08-12T11:01:28.102943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_df = sub_df.loc[sub_df.id.str.contains('_image')]\nlen(image_df)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.104069Z","iopub.status.idle":"2021-08-12T11:01:28.104615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Ref: https://www.kaggle.com/xhlulu/siim-covid-19-convert-to-jpg-256px\ndef read_xray(path, voi_lut = True, fix_monochrome = True):\n    # Original from: https://www.kaggle.com/raddar/convert-dicom-to-np-array-the-correct-way\n    dicom = pydicom.read_file(path)\n    \n    # VOI LUT (if available by DICOM device) is used to transform raw DICOM data to \n    # \"human-friendly\" view\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n               \n    # depending on this value, X-ray may look inverted - fix that:\n    if fix_monochrome and dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n        \n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n        \n    return data\n\ndef resize_xray(array, size, keep_ratio=False, resample=Image.LANCZOS):\n    # Original from: https://www.kaggle.com/xhlulu/vinbigdata-process-and-resize-to-image\n    im = Image.fromarray(array)\n    \n    if keep_ratio:\n        im.thumbnail((size, size), resample)\n    else:\n        im = im.resize((size, size), resample)\n    \n    return im","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.105851Z","iopub.status.idle":"2021-08-12T11:01:28.1064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_PATH = f'/kaggle/tmp/test/'\nIMG_SIZE = 512\n\ndef prepare_test_images():\n    image_id = []\n    dim0 = []\n    dim1 = []\n\n    os.makedirs(TEST_PATH, exist_ok=True)\n\n    for dirname, _, filenames in tqdm(os.walk(f'../input/siim-covid19-detection/test')):\n        for file in filenames:\n            # set keep_ratio=True to have original aspect ratio\n            xray = read_xray(os.path.join(dirname, file))\n            im = resize_xray(xray, size=IMG_SIZE)  \n            im.save(os.path.join(TEST_PATH, file.replace('dcm', 'png')))\n\n            image_id.append(file.replace('.dcm', ''))\n            dim0.append(xray.shape[0])\n            dim1.append(xray.shape[1])\n            \n    return image_id, dim0, dim1","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.107495Z","iopub.status.idle":"2021-08-12T11:01:28.108067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_ids, dim0, dim1 = prepare_test_images()\nprint(f'Number of test images: {len(os.listdir(TEST_PATH))}')","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.109195Z","iopub.status.idle":"2021-08-12T11:01:28.109731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta_df = pd.DataFrame.from_dict({'image_id': image_ids, 'dim0': dim0, 'dim1': dim1})\n\n# Associate image-level id with study-level ids.\n# Note that a study-level might have more than one image-level ids.\nfor study_dir in os.listdir('../input/siim-covid19-detection/test'):\n    for series in os.listdir(f'../input/siim-covid19-detection/test/{study_dir}'):\n        for image in os.listdir(f'../input/siim-covid19-detection/test/{study_dir}/{series}/'):\n            image_id = image[:-4]\n            meta_df.loc[meta_df['image_id'] == image_id, 'study_id'] = study_dir\n        \nmeta_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.110939Z","iopub.status.idle":"2021-08-12T11:01:28.111488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"/kaggle/tmp/yolo5\")","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:10:38.710067Z","iopub.execute_input":"2021-08-12T11:10:38.710396Z","iopub.status.idle":"2021-08-12T11:10:38.71721Z","shell.execute_reply.started":"2021-08-12T11:10:38.710367Z","shell.execute_reply":"2021-08-12T11:10:38.716269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ../\n!mkdir tmp\n%cd tmp\n%cd ../\n\nfrom shutil import copytree\ncopytree(\"/kaggle/input/yolo-v5/yolov5-master\",\"/kaggle/tmp/yolo5\")","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:09:52.969243Z","iopub.execute_input":"2021-08-12T11:09:52.969608Z","iopub.status.idle":"2021-08-12T11:09:53.763035Z","shell.execute_reply.started":"2021-08-12T11:09:52.969544Z","shell.execute_reply":"2021-08-12T11:09:53.759944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"/kaggle/tmp/yolo5\")","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:02:45.196166Z","iopub.execute_input":"2021-08-12T11:02:45.196541Z","iopub.status.idle":"2021-08-12T11:02:45.206964Z","shell.execute_reply.started":"2021-08-12T11:02:45.196504Z","shell.execute_reply":"2021-08-12T11:02:45.205709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Covid_image =Image.open(\"/kaggle/input/covid19-xray-test/x-ray-COVID-19.jpg\").convert(\"L\").resize((256,256))\nCovid_image.size\nCovid_image = np.asarray(Covid_image)/1.0\nCovid_image = np.true_divide(Covid_image,255)\nCovid_image = Image.fromarray(Covid_image).convert(\"L\")\nCovid_image\nCovid_image.save(\"/kaggle/x-ray-COVID-19_resized.jpg\")\n","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:34:35.694449Z","iopub.execute_input":"2021-08-12T11:34:35.694854Z","iopub.status.idle":"2021-08-12T11:34:35.752066Z","shell.execute_reply.started":"2021-08-12T11:34:35.694793Z","shell.execute_reply":"2021-08-12T11:34:35.751086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"/kaggle/tmp/yolo5/\")","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:39:23.297153Z","iopub.execute_input":"2021-08-12T11:39:23.297478Z","iopub.status.idle":"2021-08-12T11:39:23.304108Z","shell.execute_reply.started":"2021-08-12T11:39:23.29745Z","shell.execute_reply":"2021-08-12T11:39:23.303261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# YOLO_MODEL_PATH = '../input/yolo-models/yolov5s-e-100-img-512.pt'\nYOLO_MODEL_PATHS = [\n    \"/kaggle/input/best-ipt/best_i.pt\"\n]\n%cd /kaggle/tmp/yolo5\n!python /kaggle/tmp/yolo5/detect.py --weights yolov5s.pt \\\n                                      --source \"/kaggle/x-ray-COVID-19_resized.jpg\" \\\n                                      --img  256 \\\n                                      --conf 0.2 \\\n                                      --iou-thres 0.5 \\\n                                      --max-det 10 \\\n                                      --save-txt \\\n                                      --save-conf\\\n                                      --nosave","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-08-12T11:40:57.258628Z","iopub.execute_input":"2021-08-12T11:40:57.259026Z","iopub.status.idle":"2021-08-12T11:41:40.452583Z","shell.execute_reply.started":"2021-08-12T11:40:57.258987Z","shell.execute_reply":"2021-08-12T11:41:40.451458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"PRED_PATH = 'runs/detect/exp/labels'\nprediction_files = os.listdir(PRED_PATH)\nprint(f'Number of opacity predicted by YOLOv5: {len(prediction_files)}')","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.118239Z","iopub.status.idle":"2021-08-12T11:01:28.118819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The submisison requires xmin, ymin, xmax, ymax format. \n# YOLOv5 returns x_center, y_center, width, height\ndef correct_bbox_format(bboxes):\n    correct_bboxes = []\n    for b in bboxes:\n        xc, yc = int(np.round(b[0]*IMG_SIZE)), int(np.round(b[1]*IMG_SIZE))\n        w, h = int(np.round(b[2]*IMG_SIZE)), int(np.round(b[3]*IMG_SIZE))\n\n        xmin = xc - int(np.round(w/2))\n        xmax = xc + int(np.round(w/2))\n        ymin = yc - int(np.round(h/2))\n        ymax = yc + int(np.round(h/2))\n        \n        correct_bboxes.append([xmin, xmax, ymin, ymax])\n        \n    return correct_bboxes\n\n# Read the txt file generated by YOLOv5 during inference and extract \n# confidence and bounding box coordinates.\ndef get_conf_bboxes(file_path):\n    confidence = []\n    bboxes = []\n    with open(file_path, 'r') as file:\n        for line in file:\n            preds = line.strip('\\n').split(' ')\n            preds = list(map(float, preds))\n            confidence.append(preds[-1])\n            bboxes.append(preds[1:-1])\n    return confidence, bboxes","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.11994Z","iopub.status.idle":"2021-08-12T11:01:28.120687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read the submisison file\nsub_df = pd.read_csv('/kaggle/input/siim-covid19-detection/sample_submission.csv')\nsub_df","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.121778Z","iopub.status.idle":"2021-08-12T11:01:28.12242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prediction loop for submission\npredictions = []\n\nfor i in tqdm(range(len(sub_df))):\n    row = sub_df.loc[i]\n    id_name = row.id.split('_')[0]\n    id_level = row.id.split('_')[-1]\n    if id_level == 'study':\n        # do study-level classification\n        predictions.append(\"Negative 1 0 0 1 1\") # dummy prediction\n        \n    elif id_level == 'image':\n        # we can do image-level classification here.\n        # also we can rely on the object detector's classification head.\n        # for this example submisison we will use YOLO's classification head. \n        # since we already ran the inference we know which test images belong to opacity.\n        if f'{id_name}.txt' in prediction_files:\n            # opacity label\n            confidence, bboxes = get_conf_bboxes(f'{PRED_PATH}/{id_name}.txt')\n            bboxes = correct_bbox_format(bboxes)\n            pred_string = ''\n            for j, conf in enumerate(confidence):\n                pred_string += f'opacity {conf} ' + ' '.join(map(str, bboxes[j])) + ' '\n            predictions.append(pred_string[:-1]) \n        else:\n            predictions.append(\"None 1 0 0 1 1\")","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.123536Z","iopub.status.idle":"2021-08-12T11:01:28.124184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df['PredictionString'] = predictions\nsub_df.to_csv('submission.csv', index=False)\nsub_df.tail()\nprint(len(sub_df))\nsub_df\nsub_df.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.125262Z","iopub.status.idle":"2021-08-12T11:01:28.125883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.AUTOTUNE\n\nCONFIG = dict (\n    seed = 42,\n    num_labels = 4,\n    num_folds = 5,\n    img_width = 224,\n    img_height = 224,\n    batch_size = 32,\n    _wandb_kernel = 'ayut',\n    architecture = \"CNN\",\n    infra = \"GCP\",\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.12712Z","iopub.status.idle":"2021-08-12T11:01:28.127759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_df['path'] = image_df.apply(lambda row: TEST_PATH+row.id.split('_')[0]+'.png', axis=1)\nimage_df = image_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.128854Z","iopub.status.idle":"2021-08-12T11:01:28.129479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"@tf.function\ndef decode_image(image):\n    # convert the compressed string to a 3D uint8 tensor\n    image = tf.image.decode_png(image, channels=3)\n    # Normalize image\n    image = tf.image.convert_image_dtype(image, dtype=tf.float32)\n    return image\n\n@tf.function\ndef load_image(df_dict):\n    # Load image\n    image = tf.io.read_file(df_dict['path'])\n    image = decode_image(image)\n    \n    # Resize image\n    image = tf.image.resize(image, (CONFIG['img_height'], CONFIG['img_width']))\n    \n    return image\n\ntestloader = tf.data.Dataset.from_tensor_slices(dict(image_df))\n\ntestloader = (\n    testloader\n    .shuffle(1024)\n    .map(load_image, num_parallel_calls=AUTOTUNE)\n    .batch(CONFIG['batch_size'])\n    .prefetch(AUTOTUNE)\n)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.13061Z","iopub.status.idle":"2021-08-12T11:01:28.131229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STUDY_MODEL_PATHS = '/kaggle/input/models/'\nstudy_models = os.listdir(STUDY_MODEL_PATHS)\nstudy_models","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.132418Z","iopub.status.idle":"2021-08-12T11:01:28.133036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = []\nfor model in study_models:\n    # Load model\n    tf.keras.backend.clear_session()\n    model = tf.keras.models.load_model(STUDY_MODEL_PATHS+model)\n    # Prediction\n    tmp = []\n    for img_batch in tqdm(testloader):\n        preds = model.predict(img_batch)\n        tmp.extend(preds)\n        \n    predictions.append(tmp)\n    \n    del model\n    _ = gc.collect()\n    \npredictions = np.mean(predictions, axis=0)","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.134187Z","iopub.status.idle":"2021-08-12T11:01:28.134842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"/kaggle/tmp/test/\")","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.135927Z","iopub.status.idle":"2021-08-12T11:01:28.136545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels = ['0', '1', '2', '3']\nimage_df.loc[:, class_labels] = predictions\nimage_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.137743Z","iopub.status.idle":"2021-08-12T11:01:28.138388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_to_id = { \n    'negative': 0,\n    'typical': 1,\n    'indeterminate': 2,\n    'atypical': 3}\nid_to_class  = {v:k for k, v in class_to_id.items()}\n\ndef get_study_prediction_string(preds, threshold=0):\n    string = ''\n    for idx in range(4):\n        conf =  preds[idx]\n        if conf>threshold:\n            string+=f'{id_to_class[idx]} {conf:0.2f} 0 0 1 1 '\n    string = string.strip()\n    return string","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.139564Z","iopub.status.idle":"2021-08-12T11:01:28.140183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"study_ids = []\npred_strings = []\n\nfor study_id, df in meta_df.groupby('study_id'):\n    # accumulate preds for diff images belonging to same study_id\n    tmp_pred = []\n    \n    df = df.reset_index(drop=True)\n    for image_id in df.image_id.values:\n        preds = image_df.loc[image_df.id == image_id+'_image'].values[0]\n        tmp_pred.append(preds[3:])\n    \n    preds = np.mean(tmp_pred, axis=0)\n    pred_string = get_study_prediction_string(preds)\n    pred_strings.append(pred_string)\n    \n    study_ids.append(f'{study_id}_study')\n    \nstudy_df = pd.DataFrame.from_dict({'id': study_ids, 'PredictionString': pred_strings})\nstudy_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.141321Z","iopub.status.idle":"2021-08-12T11:01:28.14193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def correct_bbox_format(bboxes):\n    correct_bboxes = []\n    for b in bboxes:\n        xc, yc = int(np.round(b[0]*IMG_SIZE)), int(np.round(b[1]*IMG_SIZE))\n        w, h = int(np.round(b[2]*IMG_SIZE)), int(np.round(b[3]*IMG_SIZE))\n\n        xmin = xc - int(np.round(w/2))\n        ymin = yc - int(np.round(h/2))\n        xmax = xc + int(np.round(w/2))\n        ymax = yc + int(np.round(h/2))\n        \n        correct_bboxes.append([xmin, ymin, xmax, ymax])\n        \n    return correct_bboxes\n\ndef scale_bboxes_to_original(row, bboxes):\n    # Get scaling factor\n    scale_x = IMG_SIZE/row.dim1\n    scale_y = IMG_SIZE/row.dim0\n    \n    scaled_bboxes = []\n    for bbox in bboxes:\n        xmin, ymin, xmax, ymax = bbox\n        \n        xmin = int(np.round(xmin/scale_x))\n        ymin = int(np.round(ymin/scale_y))\n        xmax = int(np.round(xmax/scale_x))\n        ymax = int(np.round(ymax/scale_y))\n        \n        scaled_bboxes.append([xmin, ymin, xmax, ymax])\n        \n    return scaled_bboxes\n\n# Read the txt file generated by YOLOv5 during inference and extract \n# confidence and bounding box coordinates.\ndef get_conf_bboxes(file_path):\n    confidence = []\n    bboxes = []\n    with open(file_path, 'r') as file:\n        for line in file:\n            preds = line.strip('\\n').split(' ')\n            preds = list(map(float, preds))\n            confidence.append(preds[-1])\n            bboxes.append(preds[1:-1])\n    return confidence, bboxes","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.143067Z","iopub.status.idle":"2021-08-12T11:01:28.143654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_pred_strings = []\nfor i in tqdm(range(len(image_df))):\n    row = meta_df.loc[i]\n    id_name = row.image_id\n    \n    if f'{id_name}.txt' in prediction_files:\n        # opacity label\n        confidence, bboxes = get_conf_bboxes(f'{PRED_PATH}/{id_name}.txt')\n        bboxes = correct_bbox_format(bboxes)\n        ori_bboxes = scale_bboxes_to_original(row, bboxes)\n        \n        pred_string = ''\n        for j, conf in enumerate(confidence):\n            pred_string += f'opacity {conf} ' + ' '.join(map(str, ori_bboxes[j])) + ' '\n        image_pred_strings.append(pred_string[:-1]) \n    else:\n        image_pred_strings.append(\"none 1 0 0 1 1\")","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.144746Z","iopub.status.idle":"2021-08-12T11:01:28.145374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta_df['PredictionString'] = image_pred_strings\nimage_df = meta_df[['image_id', 'PredictionString']]\nimage_df.insert(0, 'id', image_df.apply(lambda row: row.image_id+'_image', axis=1))\nimage_df = image_df.drop('image_id', axis=1)\nimage_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.14643Z","iopub.status.idle":"2021-08-12T11:01:28.147042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf runs","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.148209Z","iopub.status.idle":"2021-08-12T11:01:28.148792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.concat([study_df, image_df])\nsub_df.to_csv('/kaggle/working/submission.csv', index=False)\nsub_df","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.149898Z","iopub.status.idle":"2021-08-12T11:01:28.150505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport requests\nimport urllib\nurl = input(\"Enter the link of the image here: \")\nim = Image.open(urllib.request.urlopen(url).read())\nim","metadata":{"execution":{"iopub.status.busy":"2021-08-12T11:01:28.151631Z","iopub.status.idle":"2021-08-12T11:01:28.152326Z"},"trusted":true},"execution_count":null,"outputs":[]}]}