def computeDPIoU(self, imgId, catId): p = self.params if p.useCats: gt = self._gts[imgId, catId] dt = self._dts[imgId, catId] else: gt = [_ for cId in p.catIds for _ in self._gts[imgId, cId]] dt = [_ for cId in p.catIds for _ in self._dts[imgId, cId]] if len(gt) == 0 and len(dt) == 0: return [] inds = np.argsort([-d["score"] for d in dt], kind="mergesort") dt = [dt[i] for i in inds] if len(dt) > p.maxDets[-1]: dt = dt[0 : p.maxDets[-1]] gtmasks = [] for g in gt: if DensePoseDataRelative.S_KEY in g: # convert DensePose mask to a binary mask mask = np.minimum(self.getDensePoseMask(g[DensePoseDataRelative.S_KEY]), 1.0) _, _, w, h = g["bbox"] scale_x = float(max(w, 1)) / mask.shape[1] scale_y = float(max(h, 1)) / mask.shape[0] mask = spzoom(mask, (scale_y, scale_x), order=1, prefilter=False) mask = np.array(mask > 0.5, dtype=np.uint8) rle_mask = self._generate_rlemask_on_image(mask, imgId, g) elif "segmentation" in g: segmentation = g["segmentation"] if isinstance(segmentation, list) and segmentation: # polygons im_h, im_w = self.size_mapping[imgId] rles = maskUtils.frPyObjects(segmentation, im_h, im_w) rle_mask = maskUtils.merge(rles) elif isinstance(segmentation, dict): if isinstance(segmentation["counts"], list): # uncompressed RLE im_h, im_w = self.size_mapping[imgId] rle_mask = maskUtils.frPyObjects(segmentation, im_h, im_w) else: # compressed RLE rle_mask = segmentation else: rle_mask = self._generate_rlemask_on_image(None, imgId, g) else: rle_mask = self._generate_rlemask_on_image(None, imgId, g) gtmasks.append(rle_mask) dtmasks = [] for d in dt: mask = self._extract_mask(d) mask = np.require(np.asarray(mask > 0), dtype=np.uint8, requirements=["F"]) rle_mask = self._generate_rlemask_on_image(mask, imgId, d) dtmasks.append(rle_mask) # compute iou between each dt and gt region iscrowd = [int(o["iscrowd"]) for o in gt] iousDP = maskUtils.iou(dtmasks, gtmasks, iscrowd) return iousDP
ct = nibabel.load(c1) seg = nibabel.load(c2) T = ct.get_sform() ct = ct.get_data() seg = seg.get_data() res = [1.5, 1.5, 1.5] zoom_factor = [] for k in range(3): zoom_factor.append(abs(T[k, k]) / res[k]) zoom_factor = tuple(zoom_factor) ct = np.round( spzoom( ct.astype(float), zoom_factor, order=1 ) ).astype('int16') seg = np.round( spzoom( seg.astype(float), zoom_factor, order=0 ) ).astype('uint8') print ('Size after zoom: ', ct.shape) if ct.shape[0] < W or ct.shape[1] < W or ct.shape[2] < W: print ("Patch size too big. Skipping ...") small_files.append(f) continue ''' if ct.shape[0] < W: p = W-ct.shape[0] ct = np.pad(ct,( (p,0), (0,0), (0,0) ), mode='minimum') seg = np.pad(seg,( (p,0), (0,0), (0,0) ), mode='minimum')
T = ct.affine ct = ct.get_data() seg = seg.get_data() res = [1., 1., 1.] W=96 zoom_factor = [] for k in range(3): zoom_factor.append(abs(T[k, k]) / res[k]) zoom_factor = tuple(zoom_factor) ct = np.round( spzoom( ct.astype(float), zoom_factor, order=1 ) ).astype('int16') seg = np.round( spzoom( seg.astype(float), zoom_factor, order=0 ) ).astype('uint8') xmax = ct.shape[0] - W ymax = ct.shape[1] - W zmax = ct.shape[2] - W stride = W // 4 # Calculating indices at which to extract patches z_list = list(range(0, zmax, stride)) z_list = [z for z in z_list if (z + W) < (ct.shape[2] - 1)] if z_list: z_list[-1] = zmax