text: # Copyright (c) 2017-present, Facebook, Inc.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  ##############################################################################
  
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from __future__ import unicode_literals
  
  import logging
  
  from detectron.datasets import json_dataset
  from detectron.datasets import roidb as roidb_utils
  from detectron.utils import blob as blob_utils
  import detectron.roi_data.fast_rcnn as fast_rcnn_roi_data
  
  logger = logging.getLogger(__name__)
  
  
  class GenerateProposalLabelsOp(object):
  
      def __init__(self,model):
          self.model = model
  
      def forward(self, inputs, outputs):
          """See modeling.detector.GenerateProposalLabels for inputs/outputs
          documentation.
          """
          # During training we reuse the data loader code. We populate roidb
          # entries on the fly using the rois generated by RPN.
          # im_info: [[im_height, im_width, im_scale], ...]
          rois = inputs[0].data  # 'rpn_rois'
          roidb = blob_utils.deserialize(inputs[1].data)
          im_info = inputs[2].data
          im_scales = im_info[:, 2]
          output_blob_names = fast_rcnn_roi_data.get_fast_rcnn_blob_names()
          # For historical consistency with the original Faster R-CNN
          # implementation we are *not* filtering crowd proposals.
          # This choice should be investigated in the future (it likely does
          # not matter).
          json_dataset.add_proposals(roidb, rois, im_scales, crowd_thresh=0,model=self.model)
          
          if 'is_source' in roidb[0]:
              print('pada_dc_weights: source,target: {} ({}) {} ({})'.format(
                    sum(entry['pada_roi_weights'].sum() for entry in roidb if entry['is_source']),
                    sum(len(entry['pada_roi_weights']) for entry in roidb if entry['is_source']),
                    sum(entry['pada_roi_weights'].sum() for entry in roidb if not entry['is_source']),
                    sum(len(entry['pada_roi_weights']) for entry in roidb if not entry['is_source'])
              ))
          
          roidb_utils.add_bbox_regression_targets(roidb)
          blobs = {k: [] for k in output_blob_names}
          fast_rcnn_roi_data.add_fast_rcnn_blobs(blobs, im_scales, roidb)
          
          if 'pada_roi_weights' in blobs:
              fading = self.model.da_fade_in.get_weight()
              blobs['avg_pada_weight'] = self.model.class_weight_db.avg_pada_weight ** fading
  
          for i, k in enumerate(output_blob_names):
              blob_utils.py_op_copy_blob(blobs[k], outputs[i])
  