def __init__(self, opt): UnalignedDataset.__init__(self, opt) self.npy = npy = np.load(pathjoin(opt.dataroot, 'fgs.npy')) # npy[...,:3][npy[..., -1]<128] = 128 transform = T.Compose([T.Resize((opt.crop_size, opt.crop_size))] + self.transform_A.transforms[-2:]) pils = map2(Image.fromarray, npy) self.fgs = map2(transform, pils) self.fg_size = len(self.fgs)
def evaluateByJson(resJs, gtJs, log=False): gtJs = deepcopy(gtJs) K = len(gtJs['categories']) gt_counts, imgKv = getGtCounts(gtJs) resJs = [d for d in resJs if d['image_id'] in imgKv] def evaluateByThrehold(threhold): [d['countRes'].clear() for d in imgKv.values() if 'countRes' in d] for resd in resJs: if resd['score'] < threhold: continue imgd = imgKv[resd['image_id']] imgd['countRes'] = imgd.get('countRes', []) + [resd['category_id'] - 1] pred_counts = { imgd['file_name']: imgd.get('countRes', []) for imgd in imgKv.values() } scores = evaluate(pred_counts, gt_counts, log=False, K=K) scores['thre'] = threhold return scores thres = (.01, .99) for i in range(debug or 4): xs = np.linspace(min(thres), max(thres), [10, 4][debug]) df = pd.DataFrame(map2( evaluateByThrehold, xs, )) thres = df.loc[df.cAcc.nlargest(3).index].thre row = df.loc[df.cAcc.argmax()] p - "row.thre = %s" % row.thre # p-row row[printOrder] = [round(row[k], 4) for k in printOrder] row['cAcc'] = '%s%%' % round((row['cAcc'] * 100), 2) row['mCIoU'] = '%s%%' % round((row['mCIoU'] * 100), 2) row = { k: round(v, 2) if isinstance(v, float) else v for k, v in row.items() } row = dict(row) if getMmap: from .evaluateMap import evalMap mapd = evalMap(gtJs, resJs, toStr=True) row.update(mapd) return row
# cls_dets = torch.cat((cls_boxes, cls_scores), 1) cls_dets = cls_dets[order] keep = nms(cls_dets, cfg.TEST.NMS) cls_dets = cls_dets[keep.view(-1).long()] all_boxes[j][i] = cls_dets.cpu().numpy() for xyxys in cls_dets.cpu().numpy(): xyxy, score = xyxys[:4], xyxys[4] x,y,width,height = xyxy[0], xyxy[1], xyxy[2] - xyxy[0], xyxy[3]- xyxy[1] fname = os.path.basename(data[-1][0]) coco.append({ "file_name": fname, "image_id" : i, "category_id" : j, "bbox" : map2(float, [x,y,width,height]), "score" : float(score), }) if jsp: coco[-1]["image_id"] = fname2id[fname] else: all_boxes[j][i] = empty_array # Limit to max_per_image detections *over all classes* if max_per_image > 0: image_scores = np.hstack([all_boxes[j][i][:, -1] for j in xrange(1, imdb.num_classes)]) if len(image_scores) > max_per_image: image_thresh = np.sort(image_scores)[-max_per_image] for j in xrange(1, imdb.num_classes):
def forward(self, feats, xyens): logName = 'cyc_r: %s, out_cyc_r: %s' % (self.cyc_r, self.out_cyc_r or 'N') logTag = timegap(self.log_freq, logName) if logTag: logDic = dicto() if self.layerNorma: feats = layerNormaFun(feats) tensorType = feats.type() shape = self.shape = feats.shape feats = feats.view(-1, *shape[-2:]) xyens = xyens.view(-1, 3) pgts = xyens[..., [1, 0]].cpu().numpy() existMask = xyens[..., -1].type(tensorType) # with timeit(logName): masks = map2(getMaskOfPgt, [ dict(pgt=pgt, cycle_tmpl=self.cycle_tmpl, shape=shape, out_cyc_r=self.out_cyc_r) for pgt in pgts ]) # masks = mapmp(self.getMask, pgts, pool=4) masks = np.array(masks) masks = th.from_numpy(np.uint8(masks)).type(tensorType).cuda() loss = 0 #(lambda a,b,t=1:e**(t*a)/(e**(t*a)+e**(t*b)))(2,1,5) def softmaxFgBgOld(fg, bg, t=1): fge = th.exp(fg * t) bge = th.exp(bg * t) prob = fge / (fge + bge + eps) return prob def softmaxFgBgSubMax(fg, bg, t=1): fg = fg * t bg = bg * t maxx = max(float(fg.max()), float(bg.max())) fge = th.exp(fg - maxx) bge = th.exp(bg - maxx) prob = fge / (fge + bge + eps) return prob def softmaxFgBgSub(fg, bg, t=1): diff = bg - fg toExp = t * diff if (toExp > 80).sum() and timegap(1, 'toExp'): from boxx import prettyFrameLocation, pred print(prettyFrameLocation()) pred - "toExp.max() is %.2f > 80, diff.max() is %.2f" % ( toExp.max(), diff.max()) prob = 1 / (1 + th.exp(toExp)) return prob softmaxFgBg = softmaxFgBgSubMax def CE(fg, bg): prob = softmaxFgBg(fg, bg) avgLosses = -th.log(prob + eps) return avgLosses if 'avg' in self.poolings: bgAvgPool = (feats * masks[..., 0, :, :]).sum(-1).sum(-1) / ( masks[..., 0, :, :].sum(-1).sum(-1) + eps) fgAvgPool = (feats * masks[..., 1, :, :]).sum(-1).sum(-1) / ( masks[..., 1, :, :].sum(-1).sum(-1) + eps) avgProbs = softmaxFgBg(fgAvgPool, bgAvgPool, self.temper) avgLosses = -th.log(avgProbs + eps) indexMask = existMask * (avgProbs < self.probMargin).type( tensorType) if self.probMargin else existMask avgLoss = (avgLosses * indexMask).sum() / (indexMask.sum() + eps) loss += avgLoss if logTag: logDic.avgLoss = float(avgLoss) logDic.avgProb = float(avgProbs.mean()) if 'max' in self.poolings: bgMaxPool = (feats * masks[..., 0, :, :]).max(-1)[0].max(-1)[0] fgMaxPool = (feats * masks[..., 1, :, :]).max(-1)[0].max(-1)[0] maxProbs = softmaxFgBg(fgMaxPool, bgMaxPool, self.temper) maxLosses = -th.log(maxProbs + eps) indexMask = existMask * (maxProbs < self.probMargin).type( tensorType) if self.probMargin else existMask maxLoss = (maxLosses * indexMask).sum() / (indexMask.sum() + eps) loss += maxLoss if logTag: logDic.maxLoss = float(maxLoss) logDic.maxProb = float(maxProbs.mean()) if logTag: print("%s | %s" % (logName, ', '.join( map(lambda kv: "%s: %.3f" % kv, logDic.items())))) # print(Markdown([{k:strnum(v) for k,v in logDic.items()}])) # g() return loss