{"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":"# 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\nimport matplotlib.pyplot as plt\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\nimport pandas as pd\nimport ast\nimport shutil\nfrom sklearn import model_selection\nfrom tqdm import tqdm\nimport numpy as np\nimport pydicom\nfrom tqdm import tqdm\nimport cv2\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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-05T18:38:41.774035Z","iopub.execute_input":"2022-01-05T18:38:41.774510Z","iopub.status.idle":"2022-01-05T18:38:41.781768Z","shell.execute_reply.started":"2022-01-05T18:38:41.774470Z","shell.execute_reply":"2022-01-05T18:38:41.780423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Copying jpeg images in new test dir. ","metadata":{}},{"cell_type":"code","source":"!mkdir Test_images\n!cp -r ../input/rsna-jpeg-stage-2/Test_images/Test_images/ \"Test_images\"\n","metadata":{"execution":{"iopub.status.busy":"2022-01-05T18:38:44.625491Z","iopub.execute_input":"2022-01-05T18:38:44.626244Z","iopub.status.idle":"2022-01-05T18:39:00.264930Z","shell.execute_reply.started":"2022-01-05T18:38:44.626183Z","shell.execute_reply":"2022-01-05T18:39:00.263475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cp -r ../input/flexible-yolov5/flexible-yolov5-main .","metadata":{"execution":{"iopub.status.busy":"2022-01-05T18:39:00.267355Z","iopub.execute_input":"2022-01-05T18:39:00.267712Z","iopub.status.idle":"2022-01-05T18:39:01.328607Z","shell.execute_reply.started":"2022-01-05T18:39:00.267654Z","shell.execute_reply":"2022-01-05T18:39:01.327198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Installing requirements","metadata":{}},{"cell_type":"code","source":"!pip install -r flexible-yolov5-main/requirements.txt","metadata":{"execution":{"iopub.status.busy":"2022-01-05T18:39:01.331033Z","iopub.execute_input":"2022-01-05T18:39:01.331358Z","iopub.status.idle":"2022-01-05T18:39:29.591169Z","shell.execute_reply.started":"2022-01-05T18:39:01.331319Z","shell.execute_reply":"2022-01-05T18:39:29.589899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Copying the yaml files","metadata":{}},{"cell_type":"code","source":"!cp \"../input/yolov5-pneumonia-yaml/Pneumonia.yaml\" \"flexible-yolov5-main\"\n!cp \"../input/flexible-yolov5-efficient/Flexible_Yolo_efficientnet.yaml\" \"flexible-yolov5-main\"","metadata":{"execution":{"iopub.status.busy":"2022-01-05T18:39:29.593670Z","iopub.execute_input":"2022-01-05T18:39:29.594024Z","iopub.status.idle":"2022-01-05T18:39:31.116035Z","shell.execute_reply.started":"2022-01-05T18:39:29.593972Z","shell.execute_reply":"2022-01-05T18:39:31.114851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd ./flexible-yolov5-main","metadata":{"execution":{"iopub.status.busy":"2022-01-05T18:39:31.117755Z","iopub.execute_input":"2022-01-05T18:39:31.118280Z","iopub.status.idle":"2022-01-05T18:39:31.125249Z","shell.execute_reply.started":"2022-01-05T18:39:31.118227Z","shell.execute_reply":"2022-01-05T18:39:31.124306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"kaggle/working","metadata":{}},{"cell_type":"markdown","source":"# Testing","metadata":{}},{"cell_type":"markdown","source":"utility functions","metadata":{}},{"cell_type":"code","source":"import sys\n\nsys.path.append('.')\nfrom od.models.modules.experimental import *\nfrom od.data.datasets import letterbox\nfrom utils.general import *\nfrom utils.split_detector import SPLITINFERENCE\nfrom utils.torch_utils import *\n\n\nclass Detector(object):\n    def __init__(self, pt_path,img_size, conf_thres=0.4, iou_thres=0.3, classes=0, agnostic_nms=False,\n                 xcycwh=True, device=\"cuda\"):  #namesfile, #device=0\n        self.pt_path = pt_path\n        self.img_size = img_size\n        self.device = torch.device(device)\n        self.model = self.load_model()\n        self.conf_thres = conf_thres\n        self.iou_thres = iou_thres\n        self.classes = classes\n        self.agnostic_nms = agnostic_nms\n        self.xcycwh = xcycwh\n        #self.class_names = self.load_class_names(namesfile)\n\n    def load_model(self):\n        model = attempt_load(self.pt_path, map_location=self.device)  # load FP32 model\n        return model\n\n    #def load_class_names(self, namesfile):\n    #    with open(namesfile, 'r', encoding='utf8') as fp:\n     #       class_names = [line.strip() for line in fp.readlines()]\n     #   return class_names\n\n    def __call__(self, ori_img, split_width=1, split_height=1):\n        if split_width == 1 and split_height == 1:\n            bboxes, scores, ids = self.detect_image(ori_img)\n        else:\n            bboxes = []\n            scores = []\n            ids = []\n            output = self.detect_img_split(image=ori_img, split_width=split_width, split_height=split_height)['data']\n            for key in output.keys():\n                values = output[key]\n                for value in values:\n                    x_min = value[0]\n                    y_min = value[1]\n                    x_max = value[2]\n                    y_max = value[3]\n                    w = x_max - x_min\n                    h = y_max - y_min\n                    if self.xcycwh:\n                        bboxes.append([x_min + w / 2, y_min + h / 2, w, h])\n                    else:\n                        bboxes.append(value[:4])\n                    scores.append(value[4])\n                    ids.append(key)\n        return np.asarray(bboxes), np.asarray(scores), np.asarray(ids)\n\n    def detect_image(self, image):\n        bboxes = []\n        scores = []\n        ids = []\n        im0s = image\n        img = letterbox(im0s, new_shape=self.img_size)[0]\n        img = img[:, :, ::-1].transpose(2, 0, 1)\n        img = np.ascontiguousarray(img)\n        img = torch.from_numpy(img).to(self.device)\n        img = img.float()\n        img /= 255.0\n        if img.ndimension() == 3:\n            img = img.unsqueeze(0)\n        pred = self.model(img)[0]\n        pred = non_max_suppression(pred, self.conf_thres, self.iou_thres, classes=self.classes,\n                                   agnostic=self.agnostic_nms)\n        for i, det in enumerate(pred):\n            if det is not None and len(det):\n                det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0s.shape).round()\n                for *xyxy, conf, cls in det:\n                    x_min = xyxy[0].cpu()\n                    y_min = xyxy[1].cpu()\n                    x_max = xyxy[2].cpu()\n                    y_max = xyxy[3].cpu()\n                    score = conf.cpu()\n                    clas = cls.cpu()\n                    w = x_max - x_min\n                    h = y_max - y_min\n                    if self.xcycwh:\n                        # center coord, w, h\n                        bboxes.append([x_min + w / 2, y_min + h / 2, w, h])\n                    else:\n                        bboxes.append([x_min, y_min, x_max, y_max])\n                    scores.append(score)\n                    ids.append(clas)\n        return np.asarray(bboxes), np.asarray(scores), np.asarray(ids)\n\n    @SPLITINFERENCE(split_width=2, split_height=1)\n    def detect_img_split(self, image='', **kwargs):\n        outputs_json = {}\n        im0s = image\n        img = letterbox(im0s, new_shape=self.img_size)[0]\n        img = img[:, :, ::-1].transpose(2, 0, 1)\n        img = np.ascontiguousarray(img)\n        img = torch.from_numpy(img).to(self.device)\n        img = img.float()\n        img /= 255.0\n        if img.ndimension() == 3:\n            img = img.unsqueeze(0)\n        pred = self.model(img)[0]\n        pred = non_max_suppression(pred, self.conf_thres, self.iou_thres, classes=self.classes,\n                                   agnostic=self.agnostic_nms)\n        for i, det in enumerate(pred):\n            if det is not None and len(det):\n                det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0s.shape).round()\n                for *xyxy, conf, cls in det:\n                    x_min = xyxy[0].cpu()\n                    y_min = xyxy[1].cpu()\n                    x_max = xyxy[2].cpu()\n                    y_max = xyxy[3].cpu()\n                    score = conf.cpu()\n                    clas = cls.cpu()\n                    if clas in outputs_json:\n                        outputs_json[clas].append([x_min, y_min, x_max, y_max, score])\n                    else:\n                        outputs_json[clas] = [[x_min, y_min, x_max, y_max, score]]\n        return {'data': outputs_json}\n","metadata":{"execution":{"iopub.status.busy":"2022-01-05T18:39:31.127458Z","iopub.execute_input":"2022-01-05T18:39:31.128135Z","iopub.status.idle":"2022-01-05T18:39:33.385160Z","shell.execute_reply.started":"2022-01-05T18:39:31.128087Z","shell.execute_reply":"2022-01-05T18:39:33.383560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Inference of an image folder","metadata":{}},{"cell_type":"code","source":"\n'''def multiple_detection(pt_path = \"\", img_root = \"\", save_dir = \"\", size = 512):\n    \n    model = Detector(pt_path, size, xcycwh=False)\n    imgs = os.listdir(imgs_root)\n    #save_dir =  '/kaggle/working/output2/'\n    label_dir = os.path.join(save_dir,'labels')\n    if not os.path.exists(save_dir):\n        os.mkdir(save_dir)\n\n    print(save_dir)\n    print(label_dir)\n    for img in imgs:\n        im = cv2.imread(os.path.join(imgs_root, img))\n        bboxes, scores, ids = model.detect_image(im)\n        for idx in range(bboxes.shape[0]):\n            bbox = bboxes[idx].astype(int)\n            score = scores[idx]\n            cv2.rectangle(im, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 0, 255), 2, 1)\n\n\n            text = \"Pneumonia: {:.4f}\".format(score)\n            cv2.putText(im, text, (bbox[0], bbox[1] - 5), cv2.FONT_HERSHEY_SIMPLEX,\n                              0.5, (0, 0, 255), 2)\n            cv2.imwrite(os.path.join(save_dir, img), im)\n\n            if not os.path.exists(label_dir):\n                      os.makedirs(label_dir)\n            with open((os.path.join(label_dir, img[:-4] + '.txt')), 'a+') as f:\n                      f.write(\"0\" + ' ' + str(bbox[0]) + ' ' + str(bbox[1]) + ' ' + str(bbox[2]) + ' ' + str(bbox[3]) +' '+ str(score) + '\\n')\n\n\n'''","metadata":{"execution":{"iopub.status.busy":"2022-01-05T18:39:33.386764Z","iopub.execute_input":"2022-01-05T18:39:33.387091Z","iopub.status.idle":"2022-01-05T18:39:33.398729Z","shell.execute_reply.started":"2022-01-05T18:39:33.387057Z","shell.execute_reply":"2022-01-05T18:39:33.397379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Multiple image detection (images are saved in an output folder)","metadata":{}},{"cell_type":"code","source":"'''pt_path = '../../input/yolov5-efficientnetb7/best.pt'\n\nimgs_root = \"/kaggle/working/Test_images/Test_images/\"\nsave_dir =  '/kaggle/working/output_test3/'\nmultiple_detection(pt_path, imgs_root,  save_dir, 512)'''","metadata":{"execution":{"iopub.status.busy":"2022-01-05T18:39:33.401024Z","iopub.execute_input":"2022-01-05T18:39:33.401309Z","iopub.status.idle":"2022-01-05T18:39:33.471398Z","shell.execute_reply.started":"2022-01-05T18:39:33.401283Z","shell.execute_reply":"2022-01-05T18:39:33.470112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def display_single_img_detect(pt_path=\"\", size = 512, img_path=\"\" ):\n    \n   # pt_path = '../../input/yolov5-efficientnetb7/best.pt'\n    model = Detector(pt_path, size, xcycwh=False, device='cpu')\n    \n    im = cv2.imread(img_path)\n    bboxes, scores, ids = model.detect_image(im)\n    for idx in range(bboxes.shape[0]):\n        bbox = bboxes[idx].astype(int)\n        score = scores[idx]\n        cv2.rectangle(im, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0, 0, 255), 2, 1)\n\n\n        text = \"Pneumonia: {:.4f}\".format(score)\n        cv2.putText(im, text, (bbox[0], bbox[1] - 5), cv2.FONT_HERSHEY_SIMPLEX,\n                          0.5, (0, 0, 255), 2)\n        #cv2.imwrite(os.path.join(save_dir, img), im)\n        print(score)\n    # displaying image\n    \n    plt.imshow(im)\n   # cv2.waitKey(0)\n    plt.show()\n      ","metadata":{"execution":{"iopub.status.busy":"2022-01-05T18:39:33.472734Z","iopub.execute_input":"2022-01-05T18:39:33.473192Z","iopub.status.idle":"2022-01-05T18:39:33.481842Z","shell.execute_reply.started":"2022-01-05T18:39:33.473158Z","shell.execute_reply":"2022-01-05T18:39:33.481220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Displaying single image detection (images are not saved in folder)","metadata":{}},{"cell_type":"code","source":"imgs_root = \"/kaggle/working/Test_images/Test_images\"\nimg_path = \"00ad18b7-06ee-4c4d-abca-14bdf814e8b2.jpg\"\nimg_full_path = os.path.join(imgs_root, img_path)\nprint(img_full_path)\npt_path = '../../input/yolov5-efficientnetb7/best.pt'\n\n\n\ndisplay_single_img_detect(pt_path, 512, img_full_path)","metadata":{"execution":{"iopub.status.busy":"2022-01-05T18:39:33.482887Z","iopub.execute_input":"2022-01-05T18:39:33.483312Z","iopub.status.idle":"2022-01-05T18:39:40.736774Z","shell.execute_reply.started":"2022-01-05T18:39:33.483280Z","shell.execute_reply":"2022-01-05T18:39:40.736056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}