code: def test_proposal_target_layer():
      from utils.rpn.proposal_target_layer import (
          ProposalTargetLayer as CntkProposalTargetLayer,
      )
      from utils.caffe_layers.proposal_target_layer import (
          ProposalTargetLayer as CaffeProposalTargetLayer,
      )
  
      num_rois = 400
      all_rois_shape_cntk = (num_rois, 4)
      num_gt_boxes = 50
      gt_boxes_shape_cntk = (num_gt_boxes, 5)
  
      # Create input tensors with values
      x1y1 = np.random.random_sample((num_rois, 2)) * 500
      wh = np.random.random_sample((num_rois, 2)) * 400
      x2y2 = x1y1 + wh + 50
      all_rois = np.hstack((x1y1, x2y2)).astype(np.float32)
  
      x1y1 = np.random.random_sample((num_gt_boxes, 2)) * 500
      wh = np.random.random_sample((num_gt_boxes, 2)) * 400
      x2y2 = x1y1 + wh + 50
      label = np.random.random_sample((num_gt_boxes, 1))
      label = label * 17.0
      gt_boxes = np.hstack((x1y1, x2y2, label)).astype(np.float32)
  
      # Create CNTK layer and call forward
      all_rois_var = input_variable(all_rois_shape_cntk)
      gt_boxes_var = input_variable(gt_boxes_shape_cntk)
  
      cntk_layer = user_function(
          CntkProposalTargetLayer(
              all_rois_var,
              gt_boxes_var,
              param_str="'num_classes': 17",
              deterministic=True,
          )
      )
      state, cntk_output = cntk_layer.forward(
          {all_rois_var: [all_rois], gt_boxes_var: [gt_boxes]}
      )
  
      roi_key = [
          k for k in cntk_output if "rpn_target_rois_raw" in str(k)
      ][0]
      labels_key = [
          k for k in cntk_output if "label_targets_raw" in str(k)
      ][0]
      bbox_key = [
          k for k in cntk_output if "bbox_targets_raw" in str(k)
      ][0]
      bbox_w_key = [
          k for k in cntk_output if "bbox_inside_w_raw" in str(k)
      ][0]
  
      cntk_rois = cntk_output[roi_key][0]
      cntk_labels_one_hot = cntk_output[labels_key][0]
      cntk_bbox_targets = cntk_output[bbox_key][0]
      cntk_bbox_inside_weights = cntk_output[bbox_w_key][0]
  
      cntk_labels = np.argmax(cntk_labels_one_hot, axis=1)
  
      # Create Caffe layer and call forward
      zeros = np.zeros((all_rois.shape[0], 1), dtype=gt_boxes.dtype)
      all_rois_caffe = np.hstack((zeros, all_rois))
  
      bottom = [np.array(all_rois_caffe), np.array(gt_boxes)]
      top = None  # handled through return statement in caffe layer for unit testing
  
      param_str = "'num_classes': 17"
      caffe_layer = CaffeProposalTargetLayer()
      caffe_layer.set_param_str(param_str)
      caffe_layer.setup(bottom, top)
      caffe_layer.set_deterministic_mode()
  
      (
          caffe_rois,
          caffe_labels,
          caffe_bbox_targets,
          caffe_bbox_inside_weights,
      ) = caffe_layer.forward(bottom, top)
      caffe_rois = caffe_rois[:, 1:]
  
      num_caffe_rois = caffe_rois.shape[0]
      cntk_rois = cntk_rois[:num_caffe_rois, :]
      cntk_labels = cntk_labels[:num_caffe_rois]
      cntk_bbox_targets = cntk_bbox_targets[:num_caffe_rois, :]
      cntk_bbox_inside_weights = cntk_bbox_inside_weights[
          :num_caffe_rois, :
      ]
  
      # assert that results are exactly the same
      assert cntk_rois.shape == caffe_rois.shape
      assert cntk_labels.shape == caffe_labels.shape
      assert cntk_bbox_targets.shape == caffe_bbox_targets.shape
      assert (
          cntk_bbox_inside_weights.shape
          == caffe_bbox_inside_weights.shape
      )
  
      caffe_labels = [int(x) for x in caffe_labels]
  
      # TODO: find source of randomness, sometimes this fails
      # assert np.allclose(cntk_rois, caffe_rois, rtol=0.0, atol=0.0)
      if not np.allclose(cntk_rois, caffe_rois, rtol=0.0, atol=0.0):
          print("rois differ in proposal_target_layer")
          # d1 = cntk_rois - caffe_rois
          # print(d1)
          # import pdb; pdb.set_trace()
  
      assert np.allclose(cntk_labels, caffe_labels, rtol=0.0, atol=0.0)
  
      # assert np.allclose(cntk_bbox_targets, caffe_bbox_targets, rtol=0.0, atol=0.0)
      if not np.allclose(
          cntk_bbox_targets, caffe_bbox_targets, rtol=0.0, atol=0.0
      ):
          print("bbox targets differ in proposal_target_layer")
          # d1 = cntk_rois - caffe_rois
          # print(d1)
          # import pdb; pdb.set_trace()
  
      assert np.allclose(
          cntk_bbox_inside_weights,
          caffe_bbox_inside_weights,
          rtol=0.0,
          atol=0.0,
      )
      print("Verified ProposalTargetLayer")
  
file_path: CNTK/Tests/EndToEndTests/CNTKv2Python/Examples/rpn_unit_test.py
imports: import os, sys
  import pytest
  import numpy
  from cntk import user_function
  from cntk.ops import input_variable
  from test_decorators import run_if_win35_linux35_linux36
label_desc: Use image node_id 2 for calling a global function with example usage: test_proposal_target_layer() without return types