# Buggy code
def generate_proposal_labels(
    rpn_rois,
    gt_classes,
    is_crowd,
    gt_boxes,
    im_info,
    batch_size_per_im=256,
    fg_fraction=0.25,
    fg_thresh=0.25,
    bg_thresh_hi=0.5,
    bg_thresh_lo=0.0,
    bbox_reg_weights=[0.1, 0.1, 0.2, 0.2],
    class_nums=None,
    use_random=True,
):
    """
    ** Generate proposal labels Faster-RCNN **
    This operator can be, for given the GenerateProposalOp output bounding boxes and groundtruth,
    to sample foreground boxes and background boxes, and compute loss target.

    RpnRois is the output boxes of RPN and was processed by generate_proposal_op, these boxes
    were combined with groundtruth boxes and sampled according to batch_size_per_im and fg_fraction,
    If an instance with a groundtruth overlap greater than fg_thresh, then it was considered as a foreground sample.
    If an instance with a groundtruth overlap greater than bg_thresh_lo and lower than bg_thresh_hi,
    then it was considered as a background sample.
    After all foreground and background boxes are chosen (so called Rois),
    then we apply random sampling to make sure
    the number of foreground boxes is no more than batch_size_per_im * fg_fraction.

    For each box in Rois, we assign the classification (class label) and regression targets (box label) to it.
    Finally BboxInsideWeights and BboxOutsideWeights are used to specify whether it would contribute to training loss.

    Args:
        rpn_rois(Variable): A 2-D LoDTensor with shape [N, 4]. N is the number of the GenerateProposalOp's output, each element is a bounding box with [xmin, ymin, xmax, ymax] format.
        gt_classes(Variable): A 2-D LoDTensor with shape [M, 1]. M is the number of groundtruth, each element is a class label of groundtruth.
        is_crowd(Variable): A 2-D LoDTensor with shape [M, 1]. M is the number of groundtruth, each element is a flag indicates whether a groundtruth is crowd.
        gt_boxes(Variable): A 2-D LoDTensor with shape [M, 4]. M is the number of groundtruth, each element is a bounding box with [xmin, ymin, xmax, ymax] format.
        im_info(Variable): A 2-D LoDTensor with shape [B, 3]. B is the number of input images, each element consists of im_height, im_width, im_scale.

        batch_size_per_im(int): Batch size of rois per images.
        fg_fraction(float): Foreground fraction in total batch_size_per_im.
        fg_thresh(float): Overlap threshold which is used to chose foreground sample.
        bg_thresh_hi(float): Overlap threshold upper bound which is used to chose background sample.
        bg_thresh_lo(float): Overlap threshold lower bound which is used to chose background sample.
        bbox_reg_weights(list|tuple): Box regression weights.
        class_nums(int): Class number.
        use_random(bool): Use random sampling to choose foreground and background boxes.
    """

    helper = LayerHelper("generate_proposal_labels", **locals())

    rois = helper.create_variable_for_type_inference(dtype=rpn_rois.dtype)
    labels_int32 = helper.create_variable_for_type_inference(dtype=gt_classes.dtype)
    bbox_targets = helper.create_variable_for_type_inference(dtype=rpn_rois.dtype)
    bbox_inside_weights = helper.create_variable_for_type_inference(
        dtype=rpn_rois.dtype
    )
    bbox_outside_weights = helper.create_variable_for_type_inference(
        dtype=rpn_rois.dtype
    )

    helper.append_op(
        type="generate_proposal_labels",
        inputs={
            "RpnRois": rpn_rois,
            "GtClasses": gt_classes,
            "IsCrowd": is_crowd,
            "GtBoxes": gt_boxes,
            "ImInfo": im_info,
        },
        outputs={
            "Rois": rois,
            "LabelsInt32": labels_int32,
            "BboxTargets": bbox_targets,
            "BboxInsideWeights": bbox_inside_weights,
            "BboxOutsideWeights": bbox_outside_weights,
        },
        attrs={
            "batch_size_per_im": batch_size_per_im,
            "fg_fraction": fg_fraction,
            "fg_thresh": fg_thresh,
            "bg_thresh_hi": bg_thresh_hi,
            "bg_thresh_lo": bg_thresh_lo,
            "bbox_reg_weights": bbox_reg_weights,
            "class_nums": class_nums,
            "use_random": use_random,
        },
    )

    rois.stop_gradient = True
    labels_int32.stop_gradient = True
    bbox_targets.stop_gradient = True
    bbox_inside_weights.stop_gradient = True
    bbox_outside_weights.stop_gradient = True

    return rois, labels_int32, bbox_targets, bbox_inside_weights, bbox_outside_weights


# Traceback (ValueError)
W0114 15:51:07.217496 116714 device_context.cc:262] Please NOTE: device: 0, CUDA Capability: 70, Driver API Version: 9.0, Runtime API Version: 9.0
W0114 15:51:07.217563 116714 device_context.cc:270] device: 0, cuDNN Version: 7.0.
W0114 15:51:07.217572 116714 device_context.cc:294] WARNING: device: 0. The installed Paddle is compiled with CUDNN 7.1, but CUDNN version in your machine is 7.0, which may cause serious incompatible bug. Please recompile or reinstall Paddle with compatible CUDNN version.
Exception in thread Thread-1:
Traceback (most recent call last):
File "/home/paddle/anaconda2/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/home/paddle/anaconda2/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/home/paddle/anaconda2/lib/python2.7/site-packages/paddle/fluid/layers/io.py", line 563, in __provider_thread__
for tensors in func():
File "/home/paddle/anaconda2/lib/python2.7/site-packages/paddle/fluid/layers/io.py", line 610, in __tensor_provider__
for slots in paddle_reader():
File "/home/paddle/anaconda2/lib/python2.7/site-packages/paddle/fluid/data_feeder.py", line 287, in __reader_creator__
yield self.feed(item)
File "/home/paddle/anaconda2/lib/python2.7/site-packages/paddle/fluid/data_feeder.py", line 206, in feed
ret_dict[each_name] = each_converter.done()
File "/home/paddle/anaconda2/lib/python2.7/site-packages/paddle/fluid/data_feeder.py", line 92, in done
self._check_shape(arr.shape)
File "/home/paddle/anaconda2/lib/python2.7/site-packages/paddle/fluid/data_feeder.py", line 79, in _check_shape
format(self.shape, shape))
ValueError: Shape not match. What is defined in data layer is (-1L, 3L, 1333L, 1333L), but receive (1, 3, 800, 1205)

# Fixed code
def generate_proposal_labels(
    rpn_rois,
    gt_classes,
    is_crowd,
    gt_boxes,
    im_info,
    batch_size_per_im=256,
    fg_fraction=0.25,
    fg_thresh=0.25,
    bg_thresh_hi=0.5,
    bg_thresh_lo=0.0,
    bbox_reg_weights=[0.1, 0.1, 0.2, 0.2],
    class_nums=None,
    use_random=True,
):
    """
    ** Generate Proposal Labels of Faster-RCNN **
    This operator can be, for given the GenerateProposalOp output bounding boxes and groundtruth,
    to sample foreground boxes and background boxes, and compute loss target.

    RpnRois is the output boxes of RPN and was processed by generate_proposal_op, these boxes
    were combined with groundtruth boxes and sampled according to batch_size_per_im and fg_fraction,
    If an instance with a groundtruth overlap greater than fg_thresh, then it was considered as a foreground sample.
    If an instance with a groundtruth overlap greater than bg_thresh_lo and lower than bg_thresh_hi,
    then it was considered as a background sample.
    After all foreground and background boxes are chosen (so called Rois),
    then we apply random sampling to make sure
    the number of foreground boxes is no more than batch_size_per_im * fg_fraction.

    For each box in Rois, we assign the classification (class label) and regression targets (box label) to it.
    Finally BboxInsideWeights and BboxOutsideWeights are used to specify whether it would contribute to training loss.

    Args:
        rpn_rois(Variable): A 2-D LoDTensor with shape [N, 4]. N is the number of the GenerateProposalOp's output, each element is a bounding box with [xmin, ymin, xmax, ymax] format.
        gt_classes(Variable): A 2-D LoDTensor with shape [M, 1]. M is the number of groundtruth, each element is a class label of groundtruth.
        is_crowd(Variable): A 2-D LoDTensor with shape [M, 1]. M is the number of groundtruth, each element is a flag indicates whether a groundtruth is crowd.
        gt_boxes(Variable): A 2-D LoDTensor with shape [M, 4]. M is the number of groundtruth, each element is a bounding box with [xmin, ymin, xmax, ymax] format.
        im_info(Variable): A 2-D LoDTensor with shape [B, 3]. B is the number of input images, each element consists of im_height, im_width, im_scale.

        batch_size_per_im(int): Batch size of rois per images.
        fg_fraction(float): Foreground fraction in total batch_size_per_im.
        fg_thresh(float): Overlap threshold which is used to chose foreground sample.
        bg_thresh_hi(float): Overlap threshold upper bound which is used to chose background sample.
        bg_thresh_lo(float): Overlap threshold lower bound which is used to chose background sample.
        bbox_reg_weights(list|tuple): Box regression weights.
        class_nums(int): Class number.
        use_random(bool): Use random sampling to choose foreground and background boxes.
    """

    helper = LayerHelper("generate_proposal_labels", **locals())

    rois = helper.create_variable_for_type_inference(dtype=rpn_rois.dtype)
    labels_int32 = helper.create_variable_for_type_inference(dtype=gt_classes.dtype)
    bbox_targets = helper.create_variable_for_type_inference(dtype=rpn_rois.dtype)
    bbox_inside_weights = helper.create_variable_for_type_inference(
        dtype=rpn_rois.dtype
    )
    bbox_outside_weights = helper.create_variable_for_type_inference(
        dtype=rpn_rois.dtype
    )

    helper.append_op(
        type="generate_proposal_labels",
        inputs={
            "RpnRois": rpn_rois,
            "GtClasses": gt_classes,
            "IsCrowd": is_crowd,
            "GtBoxes": gt_boxes,
            "ImInfo": im_info,
        },
        outputs={
            "Rois": rois,
            "LabelsInt32": labels_int32,
            "BboxTargets": bbox_targets,
            "BboxInsideWeights": bbox_inside_weights,
            "BboxOutsideWeights": bbox_outside_weights,
        },
        attrs={
            "batch_size_per_im": batch_size_per_im,
            "fg_fraction": fg_fraction,
            "fg_thresh": fg_thresh,
            "bg_thresh_hi": bg_thresh_hi,
            "bg_thresh_lo": bg_thresh_lo,
            "bbox_reg_weights": bbox_reg_weights,
            "class_nums": class_nums,
            "use_random": use_random,
        },
    )

    rois.stop_gradient = True
    labels_int32.stop_gradient = True
    bbox_targets.stop_gradient = True
    bbox_inside_weights.stop_gradient = True
    bbox_outside_weights.stop_gradient = True

    return rois, labels_int32, bbox_targets, bbox_inside_weights, bbox_outside_weights