def __call__(self, loc, score, anchor, img_size, scale=1.): if self.mode == "training": n_pre_nms = self.n_train_pre_nms n_post_nms = self.n_train_post_nms else: n_pre_nms = self.n_test_pre_nms n_post_nms = self.n_test_post_nms # 将RPN网络预测结果转化成建议框 roi = loc2bbox(anchor, loc) # 利用slice进行分割,防止建议框超出图像边缘 roi[:, slice(0, 4, 2)] = np.clip(roi[:, slice(0, 4, 2)], 0, img_size[1]) roi[:, slice(1, 4, 2)] = np.clip(roi[:, slice(1, 4, 2)], 0, img_size[0]) # 宽高的最小值不可以小于16 min_size = self.min_size * scale # 计算高宽 ws = roi[:, 2] - roi[:, 0] hs = roi[:, 3] - roi[:, 1] # 防止建议框过小 keep = np.where((hs >= min_size) & (ws >= min_size))[0] roi = roi[keep, :] score = score[keep] # 取出成绩最好的一些建议框 order = score.ravel().argsort()[::-1] if n_pre_nms > 0: order = order[:n_pre_nms] roi = roi[order, :] roi = nms(roi, self.nms_thresh) roi = torch.Tensor(roi) roi = roi[:n_post_nms] return roi
def __call__(self, loc, score, anchor, img_size, scale=1.): """ :param loc: 预测的偏移量 dx,dy,dw,dh (9*h*w,4) :param score: 预测的正样本的概率(9*h*w,2) :param anchor: anchorbox坐标(9*h*w,4) :param img_size: :param scale: :return: """ if self.mode == "training": n_pre_nms = self.n_train_pre_nms n_post_nms = self.n_train_post_nms else: n_pre_nms = self.n_test_pre_nms n_post_nms = self.n_test_post_nms anchor = torch.from_numpy(anchor) if loc.is_cuda: anchor = anchor.cuda() # -----------------------------------# # 将RPN网络预测结果转化成建议框(x1,y1,x2,y2) anchor + loc = proposal 因为loc放的是偏移量 # -----------------------------------# roi = loc2bbox(anchor, loc) # -----------------------------------# # 防止建议框超出图像边缘 # -----------------------------------# roi[:, [0, 2]] = torch.clamp(roi[:, [0, 2]], min=0, max=img_size[1]) roi[:, [1, 3]] = torch.clamp(roi[:, [1, 3]], min=0, max=img_size[0]) # -----------------------------------# # 建议框的宽高的最小值不可以小于16 # -----------------------------------# min_size = self.min_size * scale keep = torch.where(((roi[:, 2] - roi[:, 0]) >= min_size) & ((roi[:, 3] - roi[:, 1]) >= min_size))[0] roi = roi[keep, :] score = score[keep] # -----------------------------------# # 根据得分进行排序,取出建议框 # -----------------------------------# order = torch.argsort(score, descending=True) if n_pre_nms > 0: order = order[:n_pre_nms] roi = roi[order, :] score = score[order] # -----------------------------------# # 对建议框进行非极大抑制 # -----------------------------------# keep = nms(roi, score, self.nms_thresh) keep = keep[:n_post_nms] # 只取了300个proposal roi = roi[keep] return roi
def __call__(self, loc, score, anchor, img_size, scale=1.): if self.mode == "training": n_pre_nms = self.n_train_pre_nms n_post_nms = self.n_train_post_nms else: n_pre_nms = self.n_test_pre_nms n_post_nms = self.n_test_post_nms anchor = torch.from_numpy(anchor) if loc.is_cuda: anchor = anchor.cuda() #-----------------------------------# # 将RPN网络预测结果转化成建议框 #-----------------------------------# roi = loc2bbox(anchor, loc) #-----------------------------------# # 防止建议框超出图像边缘 #-----------------------------------# roi[:, [0, 2]] = torch.clamp(roi[:, [0, 2]], min=0, max=img_size[1]) roi[:, [1, 3]] = torch.clamp(roi[:, [1, 3]], min=0, max=img_size[0]) #-----------------------------------# # 建议框的宽高的最小值不可以小于16 #-----------------------------------# min_size = self.min_size * scale keep = torch.where(((roi[:, 2] - roi[:, 0]) >= min_size) & ((roi[:, 3] - roi[:, 1]) >= min_size))[0] roi = roi[keep, :] score = score[keep] #-----------------------------------# # 根据得分进行排序,取出建议框 #-----------------------------------# order = torch.argsort(score, descending=True) if n_pre_nms > 0: order = order[:n_pre_nms] roi = roi[order, :] score = score[order] #-----------------------------------# # 对建议框进行非极大抑制 #-----------------------------------# keep = nms(roi, score, self.nms_thresh) keep = keep[:n_post_nms] roi = roi[keep] return roi
def __call__(self, loc, score, anchor, img_size, scale=1.): if self.mode == 'training': n_pre_nms = self.n_train_pre_nms n_post_nms = self.n_train_post_nms else: n_pre_nms = self.n_test_pre_nms n_post_nms = self.n_test_post_nms #将RPN网络预测结果转化为建议框 roi = loc2bbox(anchor, loc) #利用slice进行分割,防止建议框超出图像边缘 roi[:, slice(0, 4, 2)] = np.clip(roi[:, slice(0, 4, 2)], 0, img_size[1]) roi[:, slice(1, 4, 2)] = np.clip(roi[:, slice(1, 4, 2)], 0, img_size[0]) #宽高的最小值不可以小于16 min_size = self.min_size * scale #计算高宽 ws = roi[:, 2] - roi[:, 0] hs = roi[:, 3] - roi[:, 1] #防止建议框过小 keep = np.where((hs >= min_size) & (ws >= min_size))[0] roi = roi[keep, :] score = score[keep] #取出成绩最好的一些建议框 oder = score.ravel().argsort()[::-1] if n_pre_nms > 0: oder = oder[:n_pre_nms] roi = roi[oder, :] score = score[oder] keep = nms( torch.from_numpy(roi).cuda(), torch.from_numpy(score).cuda(), self.nms_thresh) keep = keep[:n_post_nms] roi = roi[keep.cpu().numpy()] return roi