def ann(self, iid): ann_path = self.ann_path.format(iid=iid) scale = self.scales.get(iid, None) if not os.path.exists(ann_path): ann_path = None return None, iid else: return Ann(file=ann_path, scale=scale), iid
def fun(timg): img, tgt = timg.ssd() if cuda: img = img.cuda() dets = model.forward(Variable(img)) dim = img.size(2) dets = dets[0].data if cuda: dets = dets.cpu() ann = Ann(tensor=dets, dim=img.size(2)) return ann
def merge(self, fun, d=900, s=800, mimg=None): ret = [] for timg, x, y in self.mtile(mimg, d, s): ann = fun(timg) if ann is None: continue ann.dxy(-x, -y) ret.append(ann.dets) ret = np.concatenate(ret) return Ann(dets=ret)
def posmid(self, d, dist=None): if dist is None: dist = 1.5 * d pos = list([aimg.np() for cl, aimg in self.crop_ann(d)]) dets, _ = self.ann.midd(dist) ann = Ann(dets=dets) print("posmid", len(pos), ann.count) aimg = AnnImg(self.img, ann) mid = list([aimg.np() for cl, aimg in aimg.crop_ann(d)]) Xp, Xn = np.stack(pos), np.stack(mid) Yp, Yn = np.ones(Xp.shape[0]), np.zeros(Xn.shape[0]) return np.concatenate([Xp, Xn]), np.concatenate([Yp, Yn]), Xp, Xn
def neg(self, ratio=1.0, num=None, minn=None): if num is None: num = int(self.count * ratio) if minn is not None: num = max(num, minn) mask = self.neg_mask().inv().np() #print("mask", mask.shape) y, x = mask[0].nonzero() pts = np.stack([x, y], axis=1) np.random.shuffle(pts) xy = pts[:num] cl = -1 * np.ones((xy.shape[0], 1)) ann = Ann(dets=np.concatenate([cl, xy], axis=1).astype('int32')) return self.wann(ann)
def nimg(self, d, ratio=1): numneg = int(self.count * ratio) W, H = self.WH arr = [] steps = 0 while len(arr) < numneg and steps < 10000: steps += 1 x, y = (randint(0, W), randint(0, H)) if self.cropdd(x, y, d).count == 0: arr.append((-1, x, y)) ann = Ann(dets=np.array(arr)) return AnnImg(self.img, ann)
def __init__(self, img=None, ann=None, **kwargs): if not img is None: self.img = img elif "npimg" in kwargs: self.img = Image.fromarray(kwargs["npimg"].astype('uint8')) else: self.img = Image.open(kwargs["img_path"]) if ann is not None: self.ann = ann elif "ann_path" in kwargs: scale = kwargs.get("scale", None) self.sc = scale #print("AnnImg scale", scale) self.ann = Ann(file=kwargs["ann_path"], scale=scale) else: self.ann = None
def hneg(self, ratio=1.0, num=None, minn=None, th=False, conf=10): if num is None: num = int(self.count * ratio) if minn is not None: num = max(num, minn) mask = self.neg_mask().inv().np() #print("mask", mask.shape) y, x = mask[0].nonzero() hm = self.np()[0] #print("hm", hm.shape) idx = np.argsort(hm[y, x]) py, px = y[idx], x[idx] xy = np.stack([px, py], axis=1) cl = -1 * np.ones((xy.shape[0], 1)) h = hm[xy[:, 1], xy[:, 0]] ann = Ann(dets=np.concatenate([cl.reshape(-1, 1), xy], axis=1).astype( 'int32')) ann.conf = ((255 - h) * 100) / 255 ann = ann.fconf(conf) #print(ann) if ann.count < num: return self.wann(ann) if th: ann = ann.pnms(50) return self.wann(ann.take(num))
def open(cls, img_path, ann_path=None, scale=None): ann = None if not ann_path is None: ann = Ann(file=ann_path, scale=scale) return AnnImg(img, ann)