def _generate(self, feature_map_shape, scales, aspect_ratios, input_size, device='cuda'): """ """ # shape(A,) ratios_sqrt = torch.sqrt(aspect_ratios) heights = scales * ratios_sqrt * self.base_anchor_size.to(device) widths = scales / ratios_sqrt * self.base_anchor_size.to(device) anchor_stride = [ input_size[0] / feature_map_shape[0], input_size[1] / feature_map_shape[1] ] y_ctrs = torch.arange( feature_map_shape[0], device=device ) * anchor_stride[0] + self.anchor_offset[0].to(device).float() x_ctrs = torch.arange( feature_map_shape[1], device=device ) * anchor_stride[1] + self.anchor_offset[1].to(device) y_ctrs = y_ctrs.float() x_ctrs = x_ctrs.float() # meshgrid # shape(H*W,) x_ctrs, y_ctrs = ops.meshgrid(x_ctrs, y_ctrs) # shape(K*A,) heights, x_ctrs = ops.meshgrid(heights, x_ctrs) widths, y_ctrs = ops.meshgrid(widths, y_ctrs) xmin = x_ctrs - 0.5 * (widths - 1) ymin = y_ctrs - 0.5 * (heights - 1) xmax = x_ctrs + 0.5 * (widths - 1) ymax = y_ctrs + 0.5 * (heights - 1) # (x,y,h,w) return torch.stack([xmin, ymin, xmax, ymax], dim=1)
def generate_voxels(self): """ generate all voxels in ground plane """ lattice_dims = self.grid_dims / self.voxel_size self.lattice_dims = lattice_dims x_inds = torch.arange(0, lattice_dims[0]).cuda() y_inds = torch.arange(0, lattice_dims[1]).cuda() z_inds = torch.arange(0, lattice_dims[2]).cuda() z_inds, x_inds = ops.meshgrid(z_inds, x_inds) y_inds1, z_inds = ops.meshgrid(y_inds, z_inds) y_inds2, x_inds = ops.meshgrid(y_inds, x_inds) y_inds = y_inds1 corner_coords = torch.stack([x_inds, y_inds, z_inds], dim=-1).float() corner_coords *= self.voxel_size center_offset = torch.tensor([0.5 * self.voxel_size] * 3).type_as(corner_coords) center_coords = corner_coords + center_offset high_interval = self.high_interval # bugs here # y_offset = (high_interval[0] + high_interval[1]) / 2 y_offset = high_interval[0] original_offset = [-0.5 * self.grid_dims[0], y_offset, self.z_offset] # original_offset = self.original_offset original_offset = torch.tensor(original_offset).type_as(center_coords) center_coords = center_coords + original_offset # tmp_coords = torch.tensor( # [[-16.53, 2.39, 58.49]]).type_as(center_coords) # center_coords = torch.cat([center_coords, tmp_coords]) self.voxel_centers = center_coords return center_coords
def generate(self, feature_map_list, im_shape): """ Args: feature_map_list, list of (stride, ratio) Returns: anchors """ anchors_list = [] scales, aspect_ratios = ops.meshgrid(self.scales, self.aspect_ratios) for feature_map_shape in feature_map_list: anchors_list.append( self._generate(feature_map_shape, scales, aspect_ratios, im_shape)) return torch.cat(anchors_list, dim=0)
def generate(self, feature_map_list, input_size, device='cuda'): """ Args: feature_map_list, list of (stride, ratio) Returns: anchors """ anchors_list = [] scales, aspect_ratios = ops.meshgrid(self.scales.to(device), self.aspect_ratios.to(device)) for feature_map_shape in feature_map_list: anchors_list.append( self._generate(feature_map_shape, scales, aspect_ratios, input_size, device)) return torch.cat(anchors_list, dim=0)
def nms_map(self, smoothed_fg_mask): """ supress the neibor """ directions = [-1, 0, 1] shape = smoothed_fg_mask.shape orig_index = (torch.arange(shape[0]).cuda().long(), torch.arange(shape[1]).cuda().long()) orig_index = ops.meshgrid(orig_index[1], orig_index[0]) orig_index = [orig_index[1], orig_index[0]] dest_indexes = [] for i in directions: for j in directions: dest_index = (orig_index[0] + directions[i], orig_index[1] + directions[j]) dest_indexes.append(dest_index) nms_filter = torch.ones_like(smoothed_fg_mask).byte() orig_fg_mask = smoothed_fg_mask # pad fg mask first to prevent out of boundary padded_smoothed_fg_mask = torch.zeros( (shape[0] + 1, shape[1] + 1)).type_as(smoothed_fg_mask) padded_smoothed_fg_mask[:-1, :-1] = smoothed_fg_mask # import ipdb # ipdb.set_trace() for dest_index in dest_indexes: nms_filter = nms_filter & ( orig_fg_mask >= padded_smoothed_fg_mask[dest_index].view_as(orig_fg_mask)) # surpress smoothed_fg_mask[~nms_filter] = 0 return smoothed_fg_mask
def _assign_classification_targets(self, match, gt_labels, inter_boxes, proposals): """ Generate cls map for segmentation loss Args: pass Returns: cls_map_targets """ # hard code pooling_size = 8 # get label for each bbox batch_size = match.shape[0] offset = torch.arange(0, batch_size) * gt_labels.size(1) match += offset.view(batch_size, 1).type_as(match) cls_targets_batch = gt_labels.view(-1)[match.view(-1)].view( batch_size, match.shape[1]) # set bg cls_targets_batch[match == -1] = 0 # generate map according to label for segmentation loss h = proposals[:, :, 3] - proposals[:, :, 1] w = proposals[:, :, 2] - proposals[:, :, 0] sub_bin_w = w / pooling_size sub_bin_h = h / pooling_size # pass offset_xmin = inter_boxes[:, :, 0] - proposals[:, :, 0] offset_xmax = inter_boxes[:, :, 2] - proposals[:, :, 0] offset_ymin = inter_boxes[:, :, 1] - proposals[:, :, 1] offset_ymax = inter_boxes[:, :, 3] - proposals[:, :, 1] offset_xmin_ind = (offset_xmin / sub_bin_w).round().int() offset_ymin_ind = (offset_ymin / sub_bin_h).round().int() offset_xmax_ind = (offset_xmax / sub_bin_w).round().int() offset_ymax_ind = (offset_ymax / sub_bin_h).round().int() # select not empty bbox from inter boxes # cond = (inter_boxes[:, :, 2] - inter_boxes[:, :, 0] + 1 > 0) & ( # inter_boxes[:, :, 3] - inter_boxes[:, :, 1] + 1 > 0) # import ipdb # ipdb.set_trace() num = pooling_size * pooling_size offset_xmin_ind = offset_xmin_ind.unsqueeze(-1).expand(-1, -1, num) offset_ymin_ind = offset_ymin_ind.unsqueeze(-1).expand(-1, -1, num) offset_xmax_ind = offset_xmax_ind.unsqueeze(-1).expand(-1, -1, num) offset_ymax_ind = offset_ymax_ind.unsqueeze(-1).expand(-1, -1, num) x = torch.range(0, pooling_size - 1).type_as(offset_xmin_ind) y = torch.range(0, pooling_size - 1).type_as(offset_xmin_ind) xx, yy = ops.meshgrid(x, y) coord = torch.stack([xx, yy], dim=-1) coord = coord.expand(inter_boxes.shape[0], inter_boxes.shape[1], -1, -1) # shape(N,M,49) cond = (coord[:, :, :, 0] < offset_xmax_ind) & (coord[:, :, :, 0] >= offset_xmin_ind) & ( coord[:, :, :, 1] < offset_ymax_ind) & (coord[:, :, :, 1] >= offset_ymin_ind) # torch.nonzero(cond) # cls_gate_map shape(N,M,49) # cond = cond.view(cond.shape[0], cond.shape[1], pooling_size, # pooling_size) cls_gate_map = cond.int() # reverse when bg # cls_gate_map[cls_targets_batch == 0] = ( # 1 - cls_gate_map)[cls_targets_batch == 0] return cls_gate_map.long()