code: def test_proposal_layer(backend_default, fargs):
      np.random.seed(seed=0)
  
      # Get a backend for tensor allocation
      be = backend_default
      be.bsz = 1
  
      (
          _conv_size,
          im_shape_arr,
          SCALE,
          pre_nms_topN,
          post_nms_topN,
          nms_thresh,
          min_size,
      ) = fargs
  
      im_shape = be.zeros((2, 1), dtype=np.float32)
      im_shape[:] = np.array(im_shape_arr)
      im_scale = be.ones((1, 1), dtype=np.float32).fill(1.0 / 16.0)
      SCALE = be.ones((1, 1), dtype=np.float32).fill(SCALE)
  
      real_H = (
          np.round(im_shape.get()[1] * im_scale.get())
          .astype(int)
          .reshape((1,))[0]
      )
      real_W = (
          np.round(im_shape.get()[0] * im_scale.get())
          .astype(int)
          .reshape((1,))[0]
      )
  
      frcn_labels = be.zeros((21, 128), dtype=np.int32)
      frcn_labels_mask = be.zeros(frcn_labels.shape, dtype=np.int32)
      frcn_bbtargets = be.zeros((21 * 4, 128), dtype=np.float32)
      frcn_bbmask = be.zeros(frcn_bbtargets.shape, dtype=np.float32)
  
      gt_boxes = be.zeros((64, 4), dtype=np.float32)
      gt_boxes[:3, :] = np.array(
          [
              [262, 210, 323, 338],
              [164, 263, 252, 371],
              [240, 193, 294, 298],
          ]
      )
  
      gt_classes = be.zeros((64, 1), dtype=np.int32)
      gt_classes[:3, :] = np.array([[9], [9], [9]])
      num_gt_boxes = be.zeros((1, 1), dtype=np.int32).fill(3)
  
      num_scores = 2 * 9 * _conv_size * _conv_size
      rpn_obj_scores_dev = be.array(
          np.random.choice(
              num_scores * 2, size=num_scores, replace=False
          )
          / float(num_scores * 2.0)
      )
      rpn_bbox_deltas_dev = be.array(
          np.random.random((4 * 9 * _conv_size * _conv_size, 1))
      )
  
      RPN_1x1_obj = mock_layer(rpn_obj_scores_dev)
      RPN_1x1_bbox = mock_layer(rpn_bbox_deltas_dev)
  
      # Mock loader
      # mock RPN_1x1_obj and RPN_1x1_bbox
      # set inference to true to skip proposal target layer
      mock_loader = mock_dataloader(
          _conv_size,
          im_scale,
          im_shape,
          SCALE,
          gt_boxes,
          gt_classes,
          num_gt_boxes,
          frcn_labels,
          frcn_labels_mask,
          frcn_bbtargets,
          frcn_bbmask,
      )
  
      prop_layer = ProposalLayer(
          [[RPN_1x1_obj], [RPN_1x1_bbox]],
          mock_loader,
          pre_nms_N=pre_nms_topN,
          post_nms_N=post_nms_topN,
          nms_thresh=nms_thresh,
          min_bbox_size=min_size,
          num_rois=128,
          deterministic=True,
          inference=False,
          debug=True,
      )
  
      prop_layer.configure(mock_layer([]))
      prop_layer.allocate()
  
      # mock input (is not used)
      inputs = []
      inputs, dev_proposals = prop_layer.fprop(inputs, inference=False)
  
      # extract final proposals and scores from the layer without buffered memory like dev_proposals
      target_proposals = prop_layer.proposals
      target_scores = prop_layer.scores
  
      # Prepare PyCaffe Reference Layer
      prop_layer_ref = PyCaffeProposalLayer()
  
      # Re-initalize inputs to same as above
      rpn_obj_scores = rpn_obj_scores_dev.get()
      rpn_bbox_deltas = rpn_bbox_deltas_dev.get()
  
      # reshape from (4KHW, 1) -> (1, K4, H, W) format for pycaffe
      # NB: pycaffe uses A where we use K
      # rpn_bbox_deltas = rpn_bbox_deltas.reshape((4, -1, _conv_size, _conv_size))
      # rpn_bbox_deltas = rpn_bbbox_deltas[:, :, :real_H, :real_W].transpose((1, 0, 2, 3))
      # rpn_bbox_deltas = rpn_bbox_deltas.reshape((1, -1, real_H, real_W))
  
      # Skip unnecessecary reshaping (previously to match caffe)
      rpn_bbox_deltas = rpn_bbox_deltas.reshape(
          (4, -1, _conv_size, _conv_size)
      )
      rpn_bbox_deltas = (
          rpn_bbox_deltas[:, :, :real_H, :real_W].reshape((4, -1)).T
      )
  
      # reshape from (2KHW, 1) -> (1, K2, H, W)
      rpn_obj_scores = rpn_obj_scores.reshape(
          (2, -1, _conv_size, _conv_size)
      )
      rpn_obj_scores = rpn_obj_scores[:, :, :real_H, :real_W].transpose(
          (0, 1, 2, 3)
      )
      rpn_obj_scores = rpn_obj_scores.reshape((1, -1, real_H, real_W))
  
      bottom = [None, None, None]
      bottom[0] = rpn_obj_scores
      bottom[1] = rpn_bbox_deltas
      bottom[2] = [im_shape[1], im_shape[0], SCALE]
  
      top = [None, None]
  
      prop_layer_ref.setup(
          bottom,
          top,
          pre_nms_topN=pre_nms_topN,
          post_nms_topN=post_nms_topN,
          nms_thresh=nms_thresh,
          min_size=min_size,
      )
      prop_layer_ref.forward(bottom, top)
  
      # Compare proposals and scores from proposal layer
      assert np.allclose(
          top[0][:, 1:], target_proposals, atol=1e-5, rtol=1e-4
      )
      assert np.allclose(top[1], target_scores, atol=1e-5, rtol=1e-4)
  
      # Now testing proposal target layer
      t_bottom = [0, 1]
      # use target proposals from neon RPN
      zeros = np.zeros(
          (target_proposals.shape[0], 1), dtype=target_proposals.dtype
      )
      t_bottom[0] = np.hstack((zeros, target_proposals))
      # convert format of gt_boxes from (num_classes, 4) to (num_gt_boxes, 5)
      # concat the boxes and the classes and clip to num_gt_boxes and pass it in
      t_bottom[1] = np.hstack(
          (prop_layer.gt_boxes.get(), prop_layer.gt_classes.get())
      )[: prop_layer.num_gt_boxes.get()[0][0]]
  
      t_top = [None, None, None, None, None]
  
      prop_target_layer_ref = PyCaffeProposalTargetLayer()
      prop_target_layer_ref.setup(t_bottom, t_top, deterministic=True)
      prop_target_layer_ref.forward(t_bottom, t_top)
  
      frcn_bbtargets_reference = np.zeros(
          frcn_bbtargets.shape, dtype=np.float32
      )
      frcn_bbmask_reference = np.zeros(
          frcn_bbmask.shape, dtype=np.float32
      )
  
      frcn_bbtargets_reference[: t_top[2].shape[0]] = t_top[2].T
      frcn_bbmask_reference[: t_top[3].shape[0]] = t_top[3].T
  
      neon_labels = np.zeros((frcn_labels.shape[1],))
      label_mat = frcn_labels.get() * frcn_labels_mask.get()
  
      # Convert neon labels into
      for cls in range(frcn_labels.shape[0]):
          for idx, elem in enumerate(label_mat[cls]):
              if elem != 0:
                  neon_labels[idx] = cls
  
      # Test proposal layer targets against pycaffe layer
      assert np.alltrue(t_top[1] == neon_labels)  # target labels
      assert np.allclose(
          frcn_bbtargets_reference, frcn_bbtargets.get(), atol=1e-4
      )  # target bbox
      assert np.alltrue(frcn_bbmask_reference == frcn_bbmask.get())
  
file_path: neon/examples/faster-rcnn/tests/test_proposal_layer.py
imports: from __future__ import division
  from builtins import range
  from builtins import object
  import numpy
  import itertools
  from neon.backends import gen_backend
  from proposal_layer_ref import PyCaffeProposalLayer
  from proposal_target_layer_ref import PyCaffeProposalTargetLayer
  from proposal_layer import ProposalLayer
label_desc: Use image node_id 2 for calling a global function with example usage: test_proposal_layer(backend_default, fargs) without return types