def test_op_mrcnn_mask_target():
    if default_context().device_type != 'gpu':
        return

    num_rois = 2
    num_classes = 4
    mask_size = (3, 3)
    ctx = mx.gpu(0)
    # (B, N, 4)
    rois = mx.nd.array([[[2.3, 4.3, 2.2, 3.3],
                        [3.5, 5.5, 0.9, 2.4]]], ctx=ctx)
    gt_masks = mx.nd.arange(0, 4*32*32, ctx=ctx).reshape(1, 4, 32, 32)

    # (B, N)
    matches = mx.nd.array([[2, 0]], ctx=ctx)
    # (B, N)
    cls_targets = mx.nd.array([[2, 1]], ctx=ctx)

    mask_targets, mask_cls = mx.nd.contrib.mrcnn_mask_target(rois, gt_masks, matches, cls_targets,
                                                             num_rois=num_rois,
                                                             num_classes=num_classes,
                                                             mask_size=mask_size)

    # Ground truth outputs were generated with GluonCV's target generator
    # gluoncv.model_zoo.mask_rcnn.MaskTargetGenerator(1, num_rois, num_classes, mask_size)
    gt_mask_targets = mx.nd.array([[[[[2193.4    , 2193.7332 , 2194.0667 ],
                                      [2204.0667 , 2204.4    , 2204.7334 ],
                                      [2214.7334 , 2215.0667 , 2215.4    ]],
                                     [[2193.4    , 2193.7332 , 2194.0667 ],
                                      [2204.0667 , 2204.4    , 2204.7334 ],
                                      [2214.7334 , 2215.0667 , 2215.4    ]],
                                     [[2193.4    , 2193.7332 , 2194.0667 ],
                                      [2204.0667 , 2204.4    , 2204.7334 ],
                                      [2214.7334 , 2215.0667 , 2215.4    ]],
                                     [[2193.4    , 2193.7332 , 2194.0667 ],
                                      [2204.0667 , 2204.4    , 2204.7334 ],
                                      [2214.7334 , 2215.0667 , 2215.4    ]]],
                                    [[[ 185.     ,  185.33334,  185.66667],
                                      [ 195.66667,  196.00002,  196.33334],
                                      [ 206.33333,  206.66666,  207.     ]],
                                     [[ 185.     ,  185.33334,  185.66667],
                                      [ 195.66667,  196.00002,  196.33334],
                                      [ 206.33333,  206.66666,  207.     ]],
                                     [[ 185.     ,  185.33334,  185.66667],
                                      [ 195.66667,  196.00002,  196.33334],
                                      [ 206.33333,  206.66666,  207.     ]],
                                     [[ 185.     ,  185.33334,  185.66667],
                                      [ 195.66667,  196.00002,  196.33334],
                                      [  206.33333,  206.66666,  207.     ]]]]])

    gt_mask_cls = mx.nd.array([[0,0,1,0], [0,1,0,0]])
    gt_mask_cls = gt_mask_cls.reshape(1,2,4,1,1).broadcast_axes(axis=(3,4), size=(3,3))

    assert_almost_equal(mask_targets.asnumpy(), gt_mask_targets.asnumpy())
    assert_almost_equal(mask_cls.asnumpy(), gt_mask_cls.asnumpy())