def _reshape_with_axis_(input, axis=1): if not (axis > 0 and axis < len(input.shape)): raise ValueError("The axis should be smaller than " "the arity of input and bigger than 0.") new_shape = [ -1, reduce(lambda x, y: x * y, input.shape[axis:len(input.shape)]) ] out = nn.reshape(x=input, shape=new_shape) return out
def multi_box_head(inputs, image, base_size, num_classes, aspect_ratios, min_ratio=None, max_ratio=None, min_sizes=None, max_sizes=None, steps=None, step_w=None, step_h=None, offset=0.5, variance=[0.1, 0.1, 0.2, 0.2], flip=True, clip=False, kernel_size=1, pad=0, stride=1, name=None): """ **Prior_boxes** Generate prior boxes for SSD(Single Shot MultiBox Detector) algorithm. The details of this algorithm, please refer the section 2.2 of SSD paper (SSD: Single Shot MultiBox Detector) <https://arxiv.org/abs/1512.02325>`_ . Args: inputs(list|tuple): The list of input Variables, the format of all Variables is NCHW. image(Variable): The input image data of PriorBoxOp, the layout is NCHW. base_size(int): the base_size is used to get min_size and max_size according to min_ratio and max_ratio. num_classes(int): The number of classes. aspect_ratios(list|tuple): the aspect ratios of generated prior boxes. The length of input and aspect_ratios must be equal. min_ratio(int): the min ratio of generated prior boxes. max_ratio(int): the max ratio of generated prior boxes. min_sizes(list|tuple|None): If `len(inputs) <=2`, min_sizes must be set up, and the length of min_sizes should equal to the length of inputs. Default: None. max_sizes(list|tuple|None): If `len(inputs) <=2`, max_sizes must be set up, and the length of min_sizes should equal to the length of inputs. Default: None. steps(list|tuple): If step_w and step_h are the same, step_w and step_h can be replaced by steps. step_w(list|tuple): Prior boxes step across width. If step_w[i] == 0.0, the prior boxes step across width of the inputs[i] will be automatically calculated. Default: None. step_h(list|tuple): Prior boxes step across height, If step_h[i] == 0.0, the prior boxes step across height of the inputs[i] will be automatically calculated. Default: None. offset(float): Prior boxes center offset. Default: 0.5 variance(list|tuple): the variances to be encoded in prior boxes. Default:[0.1, 0.1, 0.2, 0.2]. flip(bool): Whether to flip aspect ratios. Default:False. clip(bool): Whether to clip out-of-boundary boxes. Default: False. kernel_size(int): The kernel size of conv2d. Default: 1. pad(int|list|tuple): The padding of conv2d. Default:0. stride(int|list|tuple): The stride of conv2d. Default:1, name(str): Name of the prior box layer. Default: None. Returns: mbox_loc(Variable): The predicted boxes' location of the inputs. The layout is [N, H*W*Priors, 4]. where Priors is the number of predicted boxes each position of each input. mbox_conf(Variable): The predicted boxes' confidence of the inputs. The layout is [N, H*W*Priors, C]. where Priors is the number of predicted boxes each position of each input and C is the number of Classes. boxes(Variable): the output prior boxes of PriorBox. The layout is [num_priors, 4]. num_priors is the total box count of each position of inputs. Variances(Variable): the expanded variances of PriorBox. The layout is [num_priors, 4]. num_priors is the total box count of each position of inputs Examples: .. code-block:: python mbox_locs, mbox_confs, box, var = layers.multi_box_head( inputs=[conv1, conv2, conv3, conv4, conv5, conv5], image=images, num_classes=21, min_ratio=20, max_ratio=90, aspect_ratios=[[2.], [2., 3.], [2., 3.], [2., 3.], [2.], [2.]], base_size=300, offset=0.5, flip=True, clip=True) """ def _prior_box_(input, image, min_sizes, max_sizes, aspect_ratios, variance, flip=False, clip=False, step_w=0.0, step_h=0.0, offset=0.5, name=None): helper = LayerHelper("prior_box", **locals()) dtype = helper.input_dtype() attrs = { 'min_sizes': min_sizes, 'aspect_ratios': aspect_ratios, 'variances': variance, 'flip': flip, 'clip': clip, 'step_w': step_w, 'step_h': step_h, 'offset': offset } if len(max_sizes) > 0 and max_sizes[0] > 0: attrs['max_sizes'] = max_sizes box = helper.create_tmp_variable(dtype) var = helper.create_tmp_variable(dtype) helper.append_op( type="prior_box", inputs={ "Input": input, "Image": image }, outputs={ "Boxes": box, "Variances": var }, attrs=attrs, ) box.stop_gradient = True var.stop_gradient = True return box, var def _reshape_with_axis_(input, axis=1): if not (axis > 0 and axis < len(input.shape)): raise ValueError("The axis should be smaller than " "the arity of input and bigger than 0.") new_shape = [ -1, reduce(lambda x, y: x * y, input.shape[axis:len(input.shape)]) ] out = nn.reshape(x=input, shape=new_shape) return out def _is_list_or_tuple_(data): return (isinstance(data, list) or isinstance(data, tuple)) def _is_list_or_tuple_and_equal(data, length, err_info): if not (_is_list_or_tuple_(data) and len(data) == length): raise ValueError(err_info) if not _is_list_or_tuple_(inputs): raise ValueError('inputs should be a list or tuple.') num_layer = len(inputs) if num_layer <= 2: assert min_sizes is not None and max_sizes is not None assert len(min_sizes) == num_layer and len(max_sizes) == num_layer elif min_sizes is None and max_sizes is None: min_sizes = [] max_sizes = [] step = int(math.floor(((max_ratio - min_ratio)) / (num_layer - 2))) for ratio in xrange(min_ratio, max_ratio + 1, step): min_sizes.append(base_size * ratio / 100.) max_sizes.append(base_size * (ratio + step) / 100.) min_sizes = [base_size * .10] + min_sizes max_sizes = [base_size * .20] + max_sizes if aspect_ratios: _is_list_or_tuple_and_equal( aspect_ratios, num_layer, 'aspect_ratios should be list or tuple, and the length of inputs ' 'and aspect_ratios should be the same.') if step_h: _is_list_or_tuple_and_equal( step_h, num_layer, 'step_h should be list or tuple, and the length of inputs and ' 'step_h should be the same.') if step_w: _is_list_or_tuple_and_equal( step_w, num_layer, 'step_w should be list or tuple, and the length of inputs and ' 'step_w should be the same.') if steps: _is_list_or_tuple_and_equal( steps, num_layer, 'steps should be list or tuple, and the length of inputs and ' 'step_w should be the same.') step_w = steps step_h = steps mbox_locs = [] mbox_confs = [] box_results = [] var_results = [] for i, input in enumerate(inputs): min_size = min_sizes[i] max_size = max_sizes[i] if not _is_list_or_tuple_(min_size): min_size = [min_size] if not _is_list_or_tuple_(max_size): max_size = [max_size] aspect_ratio = [] if aspect_ratios is not None: aspect_ratio = aspect_ratios[i] if not _is_list_or_tuple_(aspect_ratio): aspect_ratio = [aspect_ratio] box, var = _prior_box_(input, image, min_size, max_size, aspect_ratio, variance, flip, clip, step_w[i] if step_w else 0.0, step_h[i] if step_w else 0.0, offset) box_results.append(box) var_results.append(var) num_boxes = box.shape[2] # get loc num_loc_output = num_boxes * 4 mbox_loc = nn.conv2d(input=input, num_filters=num_loc_output, filter_size=kernel_size, padding=pad, stride=stride) mbox_loc = nn.transpose(mbox_loc, perm=[0, 2, 3, 1]) new_shape = [ mbox_loc.shape[0], mbox_loc.shape[1] * mbox_loc.shape[2] * mbox_loc.shape[3] / 4, 4 ] mbox_loc_flatten = nn.reshape(mbox_loc, shape=new_shape) mbox_locs.append(mbox_loc_flatten) # get conf num_conf_output = num_boxes * num_classes conf_loc = nn.conv2d(input=input, num_filters=num_conf_output, filter_size=kernel_size, padding=pad, stride=stride) conf_loc = nn.transpose(conf_loc, perm=[0, 2, 3, 1]) new_shape = [ conf_loc.shape[0], conf_loc.shape[1] * conf_loc.shape[2] * conf_loc.shape[3] / num_classes, num_classes ] conf_loc_flatten = nn.reshape(conf_loc, shape=new_shape) mbox_confs.append(conf_loc_flatten) if len(box_results) == 1: box = box_results[0] var = var_results[0] mbox_locs_concat = mbox_locs[0] mbox_confs_concat = mbox_confs[0] else: reshaped_boxes = [] reshaped_vars = [] for i in range(len(box_results)): reshaped_boxes.append(_reshape_with_axis_(box_results[i], axis=3)) reshaped_vars.append(_reshape_with_axis_(var_results[i], axis=3)) box = tensor.concat(reshaped_boxes) var = tensor.concat(reshaped_vars) mbox_locs_concat = tensor.concat(mbox_locs, axis=1) mbox_confs_concat = tensor.concat(mbox_confs, axis=1) box.stop_gradient = True var.stop_gradient = True return mbox_locs_concat, mbox_confs_concat, box, var
def __reshape_to_2d(var): return nn.reshape(x=var, shape=[-1, var.shape[-1]])
def detection_output(loc, scores, prior_box, prior_box_var, background_label=0, nms_threshold=0.3, nms_top_k=400, keep_top_k=200, score_threshold=0.01, nms_eta=1.0): """ **Detection Output Layer for Single Shot Multibox Detector (SSD).** This operation is to get the detection results by performing following two steps: 1. Decode input bounding box predictions according to the prior boxes. 2. Get the final detection results by applying multi-class non maximum suppression (NMS). Please note, this operation doesn't clip the final output bounding boxes to the image window. Args: loc(Variable): A 3-D Tensor with shape [N, M, 4] represents the predicted locations of M bounding bboxes. N is the batch size, and each bounding box has four coordinate values and the layout is [xmin, ymin, xmax, ymax]. scores(Variable): A 3-D Tensor with shape [N, M, C] represents the predicted confidence predictions. N is the batch size, C is the class number, M is number of bounding boxes. For each category there are total M scores which corresponding M bounding boxes. prior_box(Variable): A 2-D Tensor with shape [M, 4] holds M boxes, each box is represented as [xmin, ymin, xmax, ymax], [xmin, ymin] is the left top coordinate of the anchor box, if the input is image feature map, they are close to the origin of the coordinate system. [xmax, ymax] is the right bottom coordinate of the anchor box. prior_box_var(Variable): A 2-D Tensor with shape [M, 4] holds M group of variance. background_label(float): The index of background label, the background label will be ignored. If set to -1, then all categories will be considered. nms_threshold(float): The threshold to be used in NMS. nms_top_k(int): Maximum number of detections to be kept according to the confidences aftern the filtering detections based on score_threshold. keep_top_k(int): Number of total bboxes to be kept per image after NMS step. -1 means keeping all bboxes after NMS step. score_threshold(float): Threshold to filter out bounding boxes with low confidence score. If not provided, consider all boxes. nms_eta(float): The parameter for adaptive NMS. Returns: Variable: The detection outputs is a LoDTensor with shape [No, 6]. Each row has six values: [label, confidence, xmin, ymin, xmax, ymax]. `No` is the total number of detections in this mini-batch. For each instance, the offsets in first dimension are called LoD, the offset number is N + 1, N is the batch size. The i-th image has `LoD[i + 1] - LoD[i]` detected results, if it is 0, the i-th image has no detected results. If all images have not detected results, all the elements in LoD are 0, and output tensor only contains one value, which is -1. Examples: .. code-block:: python pb = layers.data(name='prior_box', shape=[10, 4], append_batch_size=False, dtype='float32') pbv = layers.data(name='prior_box_var', shape=[10, 4], append_batch_size=False, dtype='float32') loc = layers.data(name='target_box', shape=[2, 21, 4], append_batch_size=False, dtype='float32') scores = layers.data(name='scores', shape=[2, 21, 10], append_batch_size=False, dtype='float32') nmsed_outs = fluid.layers.detection_output(scores=scores, loc=loc, prior_box=pb, prior_box_var=pbv) """ helper = LayerHelper("detection_output", **locals()) decoded_box = box_coder(prior_box=prior_box, prior_box_var=prior_box_var, target_box=loc, code_type='decode_center_size') old_shape = scores.shape scores = nn.reshape(x=scores, shape=(-1, old_shape[-1])) scores = nn.softmax(input=scores) scores = nn.reshape(x=scores, shape=old_shape) scores = nn.transpose(scores, perm=[0, 2, 1]) scores.stop_gradient = True nmsed_outs = helper.create_tmp_variable(dtype=decoded_box.dtype) helper.append_op(type="multiclass_nms", inputs={ 'Scores': scores, 'BBoxes': decoded_box }, outputs={'Out': nmsed_outs}, attrs={ 'background_label': 0, 'nms_threshold': nms_threshold, 'nms_top_k': nms_top_k, 'keep_top_k': keep_top_k, 'score_threshold': score_threshold, 'nms_eta': 1.0 }) nmsed_outs.stop_gradient = True return nmsed_outs
def ssd_loss(location, confidence, gt_box, gt_label, prior_box, prior_box_var=None, background_label=0, overlap_threshold=0.5, neg_pos_ratio=3.0, neg_overlap=0.5, loc_loss_weight=1.0, conf_loss_weight=1.0, match_type='per_prediction', mining_type='max_negative', normalize=True, sample_size=None): """ **Multi-box loss layer for object dection algorithm of SSD** This layer is to compute dection loss for SSD given the location offset predictions, confidence predictions, prior boxes and ground-truth boudding boxes and labels, and the type of hard example mining. The returned loss is a weighted sum of the localization loss (or regression loss) and confidence loss (or classification loss) by performing the following steps: 1. Find matched boundding box by bipartite matching algorithm. 1.1 Compute IOU similarity between ground-truth boxes and prior boxes. 1.2 Compute matched boundding box by bipartite matching algorithm. 2. Compute confidence for mining hard examples 2.1. Get the target label based on matched indices. 2.2. Compute confidence loss. 3. Apply hard example mining to get the negative example indices and update the matched indices. 4. Assign classification and regression targets 4.1. Encoded bbox according to the prior boxes. 4.2. Assign regression targets. 4.3. Assign classification targets. 5. Compute the overall objective loss. 5.1 Compute confidence loss. 5.1 Compute localization loss. 5.3 Compute the overall weighted loss. Args: location (Variable): The location predictions are a 3D Tensor with shape [N, Np, 4], N is the batch size, Np is total number of predictions for each instance. 4 is the number of coordinate values, the layout is [xmin, ymin, xmax, ymax]. confidence (Variable): The confidence predictions are a 3D Tensor with shape [N, Np, C], N and Np are the same as they are in `location`, C is the class number. gt_box (Variable): The ground-truth boudding boxes (bboxes) are a 2D LoDTensor with shape [Ng, 4], Ng is the total number of ground-truth bboxes of mini-batch input. gt_label (Variable): The ground-truth labels are a 2D LoDTensor with shape [Ng, 1]. prior_box (Variable): The prior boxes are a 2D Tensor with shape [Np, 4]. prior_box_var (Variable): The variance of prior boxes are a 2D Tensor with shape [Np, 4]. background_label (int): The index of background label, 0 by default. overlap_threshold (float): If match_type is 'per_prediction', use `overlap_threshold` to determine the extra matching bboxes when finding matched boxes. 0.5 by default. neg_pos_ratio (float): The ratio of the negative boxes to the positive boxes, used only when mining_type is 'max_negative', 3.0 by defalut. neg_overlap (float): The negative overlap upper bound for the unmatched predictions. Use only when mining_type is 'max_negative', 0.5 by default. loc_loss_weight (float): Weight for localization loss, 1.0 by default. conf_loss_weight (float): Weight for confidence loss, 1.0 by default. match_type (str): The type of matching method during training, should be 'bipartite' or 'per_prediction', 'per_prediction' by defalut. mining_type (str): The hard example mining type, should be 'hard_example' or 'max_negative', now only support `max_negative`. normalize (bool): Whether to normalize the SSD loss by the total number of output locations, True by defalut. sample_size (int): The max sample size of negative box, used only when mining_type is 'hard_example'. Returns: Variable: The weighted sum of the localization loss and confidence loss, with shape [N * Np, 1], N and Np are the same as they are in `location`. Raises: ValueError: If mining_type is 'hard_example', now only support mining type of `max_negative`. Examples: .. code-block:: python pb = layers.data( name='prior_box', shape=[10, 4], append_batch_size=False, dtype='float32') pbv = layers.data( name='prior_box_var', shape=[10, 4], append_batch_size=False, dtype='float32') loc = layers.data(name='target_box', shape=[10, 4], dtype='float32') scores = layers.data(name='scores', shape=[10, 21], dtype='float32') gt_box = layers.data( name='gt_box', shape=[4], lod_level=1, dtype='float32') gt_label = layers.data( name='gt_label', shape=[1], lod_level=1, dtype='float32') loss = layers.ssd_loss(loc, scores, gt_box, gt_label, pb, pbv) """ helper = LayerHelper('ssd_loss', **locals()) if mining_type != 'max_negative': raise ValueError("Only support mining_type == max_negative now.") num, num_prior, num_class = confidence.shape def __reshape_to_2d(var): return nn.reshape(x=var, shape=[-1, var.shape[-1]]) # 1. Find matched boundding box by prior box. # 1.1 Compute IOU similarity between ground-truth boxes and prior boxes. iou = iou_similarity(x=gt_box, y=prior_box) # 1.2 Compute matched boundding box by bipartite matching algorithm. matched_indices, matched_dist = bipartite_match(iou, match_type, overlap_threshold) # 2. Compute confidence for mining hard examples # 2.1. Get the target label based on matched indices gt_label = nn.reshape(x=gt_label, shape=gt_label.shape + (1, )) gt_label.stop_gradient = True target_label, _ = target_assign(gt_label, matched_indices, mismatch_value=background_label) # 2.2. Compute confidence loss. # Reshape confidence to 2D tensor. confidence = __reshape_to_2d(confidence) target_label = tensor.cast(x=target_label, dtype='int64') target_label = __reshape_to_2d(target_label) target_label.stop_gradient = True conf_loss = nn.softmax_with_cross_entropy(confidence, target_label) # 3. Mining hard examples conf_loss = nn.reshape(x=conf_loss, shape=(num, num_prior)) conf_loss.stop_gradient = True neg_indices = helper.create_tmp_variable(dtype='int32') dtype = matched_indices.dtype updated_matched_indices = helper.create_tmp_variable(dtype=dtype) helper.append_op(type='mine_hard_examples', inputs={ 'ClsLoss': conf_loss, 'LocLoss': None, 'MatchIndices': matched_indices, 'MatchDist': matched_dist, }, outputs={ 'NegIndices': neg_indices, 'UpdatedMatchIndices': updated_matched_indices }, attrs={ 'neg_pos_ratio': neg_pos_ratio, 'neg_dist_threshold': neg_pos_ratio, 'mining_type': mining_type, 'sample_size': sample_size, }) # 4. Assign classification and regression targets # 4.1. Encoded bbox according to the prior boxes. encoded_bbox = box_coder(prior_box=prior_box, prior_box_var=prior_box_var, target_box=gt_box, code_type='encode_center_size') # 4.2. Assign regression targets target_bbox, target_loc_weight = target_assign( encoded_bbox, updated_matched_indices, mismatch_value=background_label) # 4.3. Assign classification targets target_label, target_conf_weight = target_assign( gt_label, updated_matched_indices, negative_indices=neg_indices, mismatch_value=background_label) # 5. Compute loss. # 5.1 Compute confidence loss. target_label = __reshape_to_2d(target_label) target_label = tensor.cast(x=target_label, dtype='int64') conf_loss = nn.softmax_with_cross_entropy(confidence, target_label) target_conf_weight = __reshape_to_2d(target_conf_weight) conf_loss = conf_loss * target_conf_weight # the target_label and target_conf_weight do not have gradient. target_label.stop_gradient = True target_conf_weight.stop_gradient = True # 5.2 Compute regression loss. location = __reshape_to_2d(location) target_bbox = __reshape_to_2d(target_bbox) loc_loss = nn.smooth_l1(location, target_bbox) target_loc_weight = __reshape_to_2d(target_loc_weight) loc_loss = loc_loss * target_loc_weight # the target_bbox and target_loc_weight do not have gradient. target_bbox.stop_gradient = True target_loc_weight.stop_gradient = True # 5.3 Compute overall weighted loss. loss = conf_loss_weight * conf_loss + loc_loss_weight * loc_loss # reshape to [N, Np], N is the batch size and Np is the prior box number. loss = nn.reshape(x=loss, shape=[-1, num_prior]) loss = nn.reduce_sum(loss, dim=1, keep_dim=True) if normalize: normalizer = nn.reduce_sum(target_loc_weight) loss = loss / normalizer return loss
def multi_box_head(inputs, image, base_size, num_classes, aspect_ratios, min_ratio=None, max_ratio=None, min_sizes=None, max_sizes=None, steps=None, step_w=None, step_h=None, offset=0.5, variance=[0.1, 0.1, 0.2, 0.2], flip=True, clip=False, kernel_size=1, pad=0, stride=1, name=None): """ **Prior_boxes** Generate prior boxes for SSD(Single Shot MultiBox Detector) algorithm. The details of this algorithm, please refer the section 2.2 of SSD paper (SSD: Single Shot MultiBox Detector) <https://arxiv.org/abs/1512.02325>`_ . Args: inputs(list|tuple): The list of input Variables, the format of all Variables is NCHW. image(Variable): The input image data of PriorBoxOp, the layout is NCHW. base_size(int): the base_size is used to get min_size and max_size according to min_ratio and max_ratio. num_classes(int): The number of classes. aspect_ratios(list|tuple): the aspect ratios of generated prior boxes. The length of input and aspect_ratios must be equal. min_ratio(int): the min ratio of generated prior boxes. max_ratio(int): the max ratio of generated prior boxes. min_sizes(list|tuple|None): If `len(inputs) <=2`, min_sizes must be set up, and the length of min_sizes should equal to the length of inputs. Default: None. max_sizes(list|tuple|None): If `len(inputs) <=2`, max_sizes must be set up, and the length of min_sizes should equal to the length of inputs. Default: None. steps(list|tuple): If step_w and step_h are the same, step_w and step_h can be replaced by steps. step_w(list|tuple): Prior boxes step across width. If step_w[i] == 0.0, the prior boxes step across width of the inputs[i] will be automatically calculated. Default: None. step_h(list|tuple): Prior boxes step across height, If step_h[i] == 0.0, the prior boxes step across height of the inputs[i] will be automatically calculated. Default: None. offset(float): Prior boxes center offset. Default: 0.5 variance(list|tuple): the variances to be encoded in prior boxes. Default:[0.1, 0.1, 0.2, 0.2]. flip(bool): Whether to flip aspect ratios. Default:False. clip(bool): Whether to clip out-of-boundary boxes. Default: False. kernel_size(int): The kernel size of conv2d. Default: 1. pad(int|list|tuple): The padding of conv2d. Default:0. stride(int|list|tuple): The stride of conv2d. Default:1, name(str): Name of the prior box layer. Default: None. Returns: mbox_loc(Variable): The predicted boxes' location of the inputs. The layout is [N, H*W*Priors, 4]. where Priors is the number of predicted boxes each position of each input. mbox_conf(Variable): The predicted boxes' confidence of the inputs. The layout is [N, H*W*Priors, C]. where Priors is the number of predicted boxes each position of each input and C is the number of Classes. boxes(Variable): the output prior boxes of PriorBox. The layout is [num_priors, 4]. num_priors is the total box count of each position of inputs. Variances(Variable): the expanded variances of PriorBox. The layout is [num_priors, 4]. num_priors is the total box count of each position of inputs Examples: .. code-block:: python mbox_locs, mbox_confs, box, var = layers.multi_box_head( inputs=[conv1, conv2, conv3, conv4, conv5, conv5], image=images, num_classes=21, min_ratio=20, max_ratio=90, aspect_ratios=[[2.], [2., 3.], [2., 3.], [2., 3.], [2.], [2.]], base_size=300, offset=0.5, flip=True, clip=True) """ def _reshape_with_axis_(input, axis=1): if not (axis > 0 and axis < len(input.shape)): raise ValueError("The axis should be smaller than " "the arity of input and bigger than 0.") new_shape = [ -1, reduce(lambda x, y: x * y, input.shape[axis:len(input.shape)]) ] out = nn.reshape(x=input, shape=new_shape) return out def _is_list_or_tuple_(data): return (isinstance(data, list) or isinstance(data, tuple)) def _is_list_or_tuple_and_equal(data, length, err_info): if not (_is_list_or_tuple_(data) and len(data) == length): raise ValueError(err_info) if not _is_list_or_tuple_(inputs): raise ValueError('inputs should be a list or tuple.') num_layer = len(inputs) if num_layer <= 2: assert min_sizes is not None and max_sizes is not None assert len(min_sizes) == num_layer and len(max_sizes) == num_layer elif min_sizes is None and max_sizes is None: min_sizes = [] max_sizes = [] step = int(math.floor(((max_ratio - min_ratio)) / (num_layer - 2))) for ratio in xrange(min_ratio, max_ratio + 1, step): min_sizes.append(base_size * ratio / 100.) max_sizes.append(base_size * (ratio + step) / 100.) min_sizes = [base_size * .10] + min_sizes max_sizes = [base_size * .20] + max_sizes if aspect_ratios: _is_list_or_tuple_and_equal( aspect_ratios, num_layer, 'aspect_ratios should be list or tuple, and the length of inputs ' 'and aspect_ratios should be the same.') if step_h: _is_list_or_tuple_and_equal( step_h, num_layer, 'step_h should be list or tuple, and the length of inputs and ' 'step_h should be the same.') if step_w: _is_list_or_tuple_and_equal( step_w, num_layer, 'step_w should be list or tuple, and the length of inputs and ' 'step_w should be the same.') if steps: _is_list_or_tuple_and_equal( steps, num_layer, 'steps should be list or tuple, and the length of inputs and ' 'step_w should be the same.') step_w = steps step_h = steps mbox_locs = [] mbox_confs = [] box_results = [] var_results = [] for i, input in enumerate(inputs): min_size = min_sizes[i] max_size = max_sizes[i] if not _is_list_or_tuple_(min_size): min_size = [min_size] if not _is_list_or_tuple_(max_size): max_size = [max_size] aspect_ratio = [] if aspect_ratios is not None: aspect_ratio = aspect_ratios[i] if not _is_list_or_tuple_(aspect_ratio): aspect_ratio = [aspect_ratio] step = [step_w[i] if step_w else 0.0, step_h[i] if step_w else 0.0] box, var = prior_box(input, image, min_size, max_size, aspect_ratio, variance, flip, clip, step, offset) box_results.append(box) var_results.append(var) num_boxes = box.shape[2] # get loc num_loc_output = num_boxes * 4 mbox_loc = nn.conv2d( input=input, num_filters=num_loc_output, filter_size=kernel_size, padding=pad, stride=stride) mbox_loc = nn.transpose(mbox_loc, perm=[0, 2, 3, 1]) new_shape = [ mbox_loc.shape[0], mbox_loc.shape[1] * mbox_loc.shape[2] * mbox_loc.shape[3] / 4, 4 ] mbox_loc_flatten = nn.reshape(mbox_loc, shape=new_shape) mbox_locs.append(mbox_loc_flatten) # get conf num_conf_output = num_boxes * num_classes conf_loc = nn.conv2d( input=input, num_filters=num_conf_output, filter_size=kernel_size, padding=pad, stride=stride) conf_loc = nn.transpose(conf_loc, perm=[0, 2, 3, 1]) new_shape = [ conf_loc.shape[0], conf_loc.shape[1] * conf_loc.shape[2] * conf_loc.shape[3] / num_classes, num_classes ] conf_loc_flatten = nn.reshape(conf_loc, shape=new_shape) mbox_confs.append(conf_loc_flatten) if len(box_results) == 1: box = box_results[0] var = var_results[0] mbox_locs_concat = mbox_locs[0] mbox_confs_concat = mbox_confs[0] else: reshaped_boxes = [] reshaped_vars = [] for i in range(len(box_results)): reshaped_boxes.append(_reshape_with_axis_(box_results[i], axis=3)) reshaped_vars.append(_reshape_with_axis_(var_results[i], axis=3)) box = tensor.concat(reshaped_boxes) var = tensor.concat(reshaped_vars) mbox_locs_concat = tensor.concat(mbox_locs, axis=1) mbox_confs_concat = tensor.concat(mbox_confs, axis=1) box.stop_gradient = True var.stop_gradient = True return mbox_locs_concat, mbox_confs_concat, box, var
def detection_output(loc, scores, prior_box, prior_box_var, background_label=0, nms_threshold=0.3, nms_top_k=400, keep_top_k=200, score_threshold=0.01, nms_eta=1.0): """ **Detection Output Layer for Single Shot Multibox Detector (SSD).** This operation is to get the detection results by performing following two steps: 1. Decode input bounding box predictions according to the prior boxes. 2. Get the final detection results by applying multi-class non maximum suppression (NMS). Please note, this operation doesn't clip the final output bounding boxes to the image window. Args: loc(Variable): A 3-D Tensor with shape [N, M, 4] represents the predicted locations of M bounding bboxes. N is the batch size, and each bounding box has four coordinate values and the layout is [xmin, ymin, xmax, ymax]. scores(Variable): A 3-D Tensor with shape [N, M, C] represents the predicted confidence predictions. N is the batch size, C is the class number, M is number of bounding boxes. For each category there are total M scores which corresponding M bounding boxes. prior_box(Variable): A 2-D Tensor with shape [M, 4] holds M boxes, each box is represented as [xmin, ymin, xmax, ymax], [xmin, ymin] is the left top coordinate of the anchor box, if the input is image feature map, they are close to the origin of the coordinate system. [xmax, ymax] is the right bottom coordinate of the anchor box. prior_box_var(Variable): A 2-D Tensor with shape [M, 4] holds M group of variance. background_label(float): The index of background label, the background label will be ignored. If set to -1, then all categories will be considered. nms_threshold(float): The threshold to be used in NMS. nms_top_k(int): Maximum number of detections to be kept according to the confidences aftern the filtering detections based on score_threshold. keep_top_k(int): Number of total bboxes to be kept per image after NMS step. -1 means keeping all bboxes after NMS step. score_threshold(float): Threshold to filter out bounding boxes with low confidence score. If not provided, consider all boxes. nms_eta(float): The parameter for adaptive NMS. Returns: Variable: The detection outputs is a LoDTensor with shape [No, 6]. Each row has six values: [label, confidence, xmin, ymin, xmax, ymax]. `No` is the total number of detections in this mini-batch. For each instance, the offsets in first dimension are called LoD, the offset number is N + 1, N is the batch size. The i-th image has `LoD[i + 1] - LoD[i]` detected results, if it is 0, the i-th image has no detected results. If all images have not detected results, all the elements in LoD are 0, and output tensor only contains one value, which is -1. Examples: .. code-block:: python pb = layers.data(name='prior_box', shape=[10, 4], append_batch_size=False, dtype='float32') pbv = layers.data(name='prior_box_var', shape=[10, 4], append_batch_size=False, dtype='float32') loc = layers.data(name='target_box', shape=[2, 21, 4], append_batch_size=False, dtype='float32') scores = layers.data(name='scores', shape=[2, 21, 10], append_batch_size=False, dtype='float32') nmsed_outs = fluid.layers.detection_output(scores=scores, loc=loc, prior_box=pb, prior_box_var=pbv) """ helper = LayerHelper("detection_output", **locals()) decoded_box = box_coder( prior_box=prior_box, prior_box_var=prior_box_var, target_box=loc, code_type='decode_center_size') old_shape = scores.shape scores = nn.reshape(x=scores, shape=(-1, old_shape[-1])) scores = nn.softmax(input=scores) scores = nn.reshape(x=scores, shape=old_shape) scores = nn.transpose(scores, perm=[0, 2, 1]) scores.stop_gradient = True nmsed_outs = helper.create_tmp_variable(dtype=decoded_box.dtype) helper.append_op( type="multiclass_nms", inputs={'Scores': scores, 'BBoxes': decoded_box}, outputs={'Out': nmsed_outs}, attrs={ 'background_label': 0, 'nms_threshold': nms_threshold, 'nms_top_k': nms_top_k, 'keep_top_k': keep_top_k, 'score_threshold': score_threshold, 'nms_eta': 1.0 }) nmsed_outs.stop_gradient = True return nmsed_outs
def ssd_loss(location, confidence, gt_box, gt_label, prior_box, prior_box_var=None, background_label=0, overlap_threshold=0.5, neg_pos_ratio=3.0, neg_overlap=0.5, loc_loss_weight=1.0, conf_loss_weight=1.0, match_type='per_prediction', mining_type='max_negative', normalize=True, sample_size=None): """ **Multi-box loss layer for object dection algorithm of SSD** This layer is to compute dection loss for SSD given the location offset predictions, confidence predictions, prior boxes and ground-truth boudding boxes and labels, and the type of hard example mining. The returned loss is a weighted sum of the localization loss (or regression loss) and confidence loss (or classification loss) by performing the following steps: 1. Find matched boundding box by bipartite matching algorithm. 1.1 Compute IOU similarity between ground-truth boxes and prior boxes. 1.2 Compute matched boundding box by bipartite matching algorithm. 2. Compute confidence for mining hard examples 2.1. Get the target label based on matched indices. 2.2. Compute confidence loss. 3. Apply hard example mining to get the negative example indices and update the matched indices. 4. Assign classification and regression targets 4.1. Encoded bbox according to the prior boxes. 4.2. Assign regression targets. 4.3. Assign classification targets. 5. Compute the overall objective loss. 5.1 Compute confidence loss. 5.1 Compute localization loss. 5.3 Compute the overall weighted loss. Args: location (Variable): The location predictions are a 3D Tensor with shape [N, Np, 4], N is the batch size, Np is total number of predictions for each instance. 4 is the number of coordinate values, the layout is [xmin, ymin, xmax, ymax]. confidence (Variable): The confidence predictions are a 3D Tensor with shape [N, Np, C], N and Np are the same as they are in `location`, C is the class number. gt_box (Variable): The ground-truth boudding boxes (bboxes) are a 2D LoDTensor with shape [Ng, 4], Ng is the total number of ground-truth bboxes of mini-batch input. gt_label (Variable): The ground-truth labels are a 2D LoDTensor with shape [Ng, 1]. prior_box (Variable): The prior boxes are a 2D Tensor with shape [Np, 4]. prior_box_var (Variable): The variance of prior boxes are a 2D Tensor with shape [Np, 4]. background_label (int): The index of background label, 0 by default. overlap_threshold (float): If match_type is 'per_prediction', use `overlap_threshold` to determine the extra matching bboxes when finding matched boxes. 0.5 by default. neg_pos_ratio (float): The ratio of the negative boxes to the positive boxes, used only when mining_type is 'max_negative', 3.0 by defalut. neg_overlap (float): The negative overlap upper bound for the unmatched predictions. Use only when mining_type is 'max_negative', 0.5 by default. loc_loss_weight (float): Weight for localization loss, 1.0 by default. conf_loss_weight (float): Weight for confidence loss, 1.0 by default. match_type (str): The type of matching method during training, should be 'bipartite' or 'per_prediction', 'per_prediction' by defalut. mining_type (str): The hard example mining type, should be 'hard_example' or 'max_negative', now only support `max_negative`. normalize (bool): Whether to normalize the SSD loss by the total number of output locations, True by defalut. sample_size (int): The max sample size of negative box, used only when mining_type is 'hard_example'. Returns: Variable: The weighted sum of the localization loss and confidence loss, with shape [N * Np, 1], N and Np are the same as they are in `location`. Raises: ValueError: If mining_type is 'hard_example', now only support mining type of `max_negative`. Examples: .. code-block:: python pb = layers.data( name='prior_box', shape=[10, 4], append_batch_size=False, dtype='float32') pbv = layers.data( name='prior_box_var', shape=[10, 4], append_batch_size=False, dtype='float32') loc = layers.data(name='target_box', shape=[10, 4], dtype='float32') scores = layers.data(name='scores', shape=[10, 21], dtype='float32') gt_box = layers.data( name='gt_box', shape=[4], lod_level=1, dtype='float32') gt_label = layers.data( name='gt_label', shape=[1], lod_level=1, dtype='float32') loss = layers.ssd_loss(loc, scores, gt_box, gt_label, pb, pbv) """ helper = LayerHelper('ssd_loss', **locals()) if mining_type != 'max_negative': raise ValueError("Only support mining_type == max_negative now.") num, num_prior, num_class = confidence.shape def __reshape_to_2d(var): return nn.reshape(x=var, shape=[-1, var.shape[-1]]) # 1. Find matched boundding box by prior box. # 1.1 Compute IOU similarity between ground-truth boxes and prior boxes. iou = iou_similarity(x=gt_box, y=prior_box) # 1.2 Compute matched boundding box by bipartite matching algorithm. matched_indices, matched_dist = bipartite_match(iou, match_type, overlap_threshold) # 2. Compute confidence for mining hard examples # 2.1. Get the target label based on matched indices gt_label = nn.reshape(x=gt_label, shape=gt_label.shape + (1, )) gt_label.stop_gradient = True target_label, _ = target_assign( gt_label, matched_indices, mismatch_value=background_label) # 2.2. Compute confidence loss. # Reshape confidence to 2D tensor. confidence = __reshape_to_2d(confidence) target_label = tensor.cast(x=target_label, dtype='int64') target_label = __reshape_to_2d(target_label) target_label.stop_gradient = True conf_loss = nn.softmax_with_cross_entropy(confidence, target_label) # 3. Mining hard examples conf_loss = nn.reshape(x=conf_loss, shape=(num, num_prior)) conf_loss.stop_gradient = True neg_indices = helper.create_tmp_variable(dtype='int32') dtype = matched_indices.dtype updated_matched_indices = helper.create_tmp_variable(dtype=dtype) helper.append_op( type='mine_hard_examples', inputs={ 'ClsLoss': conf_loss, 'LocLoss': None, 'MatchIndices': matched_indices, 'MatchDist': matched_dist, }, outputs={ 'NegIndices': neg_indices, 'UpdatedMatchIndices': updated_matched_indices }, attrs={ 'neg_pos_ratio': neg_pos_ratio, 'neg_dist_threshold': neg_pos_ratio, 'mining_type': mining_type, 'sample_size': sample_size, }) # 4. Assign classification and regression targets # 4.1. Encoded bbox according to the prior boxes. encoded_bbox = box_coder( prior_box=prior_box, prior_box_var=prior_box_var, target_box=gt_box, code_type='encode_center_size') # 4.2. Assign regression targets target_bbox, target_loc_weight = target_assign( encoded_bbox, updated_matched_indices, mismatch_value=background_label) # 4.3. Assign classification targets target_label, target_conf_weight = target_assign( gt_label, updated_matched_indices, negative_indices=neg_indices, mismatch_value=background_label) # 5. Compute loss. # 5.1 Compute confidence loss. target_label = __reshape_to_2d(target_label) target_label = tensor.cast(x=target_label, dtype='int64') conf_loss = nn.softmax_with_cross_entropy(confidence, target_label) target_conf_weight = __reshape_to_2d(target_conf_weight) conf_loss = conf_loss * target_conf_weight # the target_label and target_conf_weight do not have gradient. target_label.stop_gradient = True target_conf_weight.stop_gradient = True # 5.2 Compute regression loss. location = __reshape_to_2d(location) target_bbox = __reshape_to_2d(target_bbox) loc_loss = nn.smooth_l1(location, target_bbox) target_loc_weight = __reshape_to_2d(target_loc_weight) loc_loss = loc_loss * target_loc_weight # the target_bbox and target_loc_weight do not have gradient. target_bbox.stop_gradient = True target_loc_weight.stop_gradient = True # 5.3 Compute overall weighted loss. loss = conf_loss_weight * conf_loss + loc_loss_weight * loc_loss # reshape to [N, Np], N is the batch size and Np is the prior box number. loss = nn.reshape(x=loss, shape=[-1, num_prior]) loss = nn.reduce_sum(loss, dim=1, keep_dim=True) if normalize: normalizer = nn.reduce_sum(target_loc_weight) loss = loss / normalizer return loss