def get_ground_truth_predictors(self, ground_truth, label_map, im=None): i = 0 #indexes the anchor boxes j = 0 total_boxes_per_gt = sum(self.anchor_nums) num_ground_truth_in_im = ground_truth.shape[0] inds = np.zeros((num_ground_truth_in_im, total_boxes_per_gt), dtype = np.int) #n index the the detection maps for n, anchor in enumerate(self.anchor_nums): offset = sum(self.num_pred_boxes[:n]) try: center_cells = (ground_truth[:,[0,1]]) // self.strides[n] except: print(ground_truth) assert False a = offset + self.anchor_nums[n]*(self.inp_dim//self.strides[n]*center_cells[:,1] + center_cells[:,0]) inds[:,sum(self.anchor_nums[:n])] = a for x in range(1, self.anchor_nums[n]): inds[:,sum(self.anchor_nums[:n]) + x] = a + x i += anchor j += self.num_pred_boxes[n] candidate_boxes = label_map[inds][:,:,:4] candidate_boxes = center_to_corner(candidate_boxes) ground_truth_boxes = center_to_corner(ground_truth.copy()[np.newaxis]).squeeze(0)[:,:4] candidate_boxes = candidate_boxes.transpose(0,2,1) ground_truth_boxes = ground_truth_boxes[:,:,np.newaxis] candidate_ious = bbox_iou(candidate_boxes, ground_truth_boxes, lib="numpy") prediction_boxes = np.zeros((num_ground_truth_in_im,1), dtype=np.int) for i in range(num_ground_truth_in_im): #get the the row and the column of the highest IoU max_iou_ind = np.argmax(candidate_ious) max_iou_row = max_iou_ind // total_boxes_per_gt max_iou_col = max_iou_ind % total_boxes_per_gt #get the index (in label map) of the box with maximum IoU max_iou_box = inds[max_iou_row, max_iou_col] #assign the bounding box to the appropriate gt prediction_boxes[max_iou_row] = max_iou_box #zero out all the IoUs for this box so it can't be reassigned to any other gt box_mask = (inds != max_iou_ind).reshape(-1,len(self.anchors)) candidate_ious *= box_mask #zero out all the values of the row representing gt that just got assigned so that it #doesn't participate in the process again candidate_ious[max_iou_row] *= 0 return (prediction_boxes)
def get_no_obj_candidates(self, ground_truth, label_map, ground_truth_predictors): total_boxes_per_gt = sum(self.anchor_nums) num_ground_truth_in_im = ground_truth.shape[0] inds = np.zeros((num_ground_truth_in_im, total_boxes_per_gt), dtype = np.int) inds = np.arange(sum(self.num_pred_boxes)).astype(int) inds = inds[np.newaxis].repeat(num_ground_truth_in_im, axis = 0) candidate_boxes = label_map[inds][:,:,:4] candidate_boxes = center_to_corner(candidate_boxes) ground_truth_boxes = center_to_corner(ground_truth.copy()[np.newaxis]).squeeze(0)[:,:4] candidate_boxes = candidate_boxes.transpose(0,2,1) ground_truth_boxes = ground_truth_boxes[:,:,np.newaxis] candidate_ious = bbox_iou(candidate_boxes, ground_truth_boxes, lib = "numpy") candidate_ious[:, ground_truth_predictors] = 1 max_ious_per_box = np.max(candidate_ious, 0) no_obj_cands = (np.nonzero(max_ious_per_box < 0.5)[0].astype(int)) return no_obj_cands
pd.read_csv(ground_truths_file, header=None, sep=' ')) except pd.errors.EmptyDataError: continue ground_truths[:, 1:] = center_to_corner_2d(ground_truths[:, 1:] * orig_im_dim[0]) # ground_truths[:, 1:] = ground_truths[:, 1:] * orig_im_dim[0] class_labels = ground_truths[:, 0] # x1y1x2y2 ground_truths = ground_truths[:, 1:] num_gts += ground_truths.shape[0] img = img_tmp.to(device) output = model(img) print('output ', output) output = center_to_corner(output) output = output.unsqueeze(0).view(-1, bbox_attrs) # keep = np.unique(np.asarray(nms(output, scores=output[:, 5], overlap=0.3)[0])) # print('keep ', keep) # output = output[keep, :] # print('output ', output) score_box = output[:, 5] print('score_box ', score_box) # inp_dim is model input size, im_dim is actual image size output = np.asarray(output.squeeze(0)) if output.shape[0] > 0: # Get x1y1x2y2, mask conf, class conf