<s> import caffe <EOL> import yaml <EOL> import numpy as np <EOL> import numpy . random as npr <EOL> from fast_rcnn . config import cfg <EOL> from fast_rcnn . bbox_transform import bbox_transform <EOL> from utils . cython_bbox import bbox_overlaps <EOL> DEBUG = False <EOL> class ProposalTargetLayer ( caffe . Layer ) : <EOL> """<STR_LIT>""" <EOL> def setup ( self , bottom , top ) : <EOL> layer_params = yaml . load ( self . param_str_ ) <EOL> self . _num_classes = layer_params [ '<STR_LIT>' ] <EOL> top [ <NUM_LIT:0> ] . reshape ( <NUM_LIT:1> , <NUM_LIT:5> ) <EOL> top [ <NUM_LIT:1> ] . reshape ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> top [ <NUM_LIT:2> ] . reshape ( <NUM_LIT:1> , self . _num_classes * <NUM_LIT:4> ) <EOL> top [ <NUM_LIT:3> ] . reshape ( <NUM_LIT:1> , self . _num_classes * <NUM_LIT:4> ) <EOL> top [ <NUM_LIT:4> ] . reshape ( <NUM_LIT:1> , self . _num_classes * <NUM_LIT:4> ) <EOL> def forward ( self , bottom , top ) : <EOL> all_rois = bottom [ <NUM_LIT:0> ] . data <EOL> gt_boxes = bottom [ <NUM_LIT:1> ] . data <EOL> zeros = np . zeros ( ( gt_boxes . shape [ <NUM_LIT:0> ] , <NUM_LIT:1> ) , dtype = gt_boxes . dtype ) <EOL> all_rois = np . vstack ( <EOL> ( all_rois , np . hstack ( ( zeros , gt_boxes [ : , : - <NUM_LIT:1> ] ) ) ) <EOL> ) <EOL> assert np . all ( all_rois [ : , <NUM_LIT:0> ] == <NUM_LIT:0> ) , '<STR_LIT>' <EOL> num_images = <NUM_LIT:1> <EOL> rois_per_image = cfg . TRAIN . BATCH_SIZE / num_images <EOL> fg_rois_per_image = np . round ( cfg . TRAIN . FG_FRACTION * rois_per_image ) <EOL> labels , rois , bbox_targets , bbox_inside_weights = _sample_rois ( <EOL> all_rois , gt_boxes , fg_rois_per_image , <EOL> rois_per_image , self . _num_classes ) <EOL> if DEBUG : <EOL> print '<STR_LIT>' . format ( ( labels > <NUM_LIT:0> ) . sum ( ) ) <EOL> print '<STR_LIT>' . format ( ( labels == <NUM_LIT:0> ) . sum ( ) ) <EOL> self . _count += <NUM_LIT:1> <EOL> self . _fg_num += ( labels > <NUM_LIT:0> ) . sum ( ) <EOL> self . _bg_num += ( labels == <NUM_LIT:0> ) . sum ( ) <EOL> print '<STR_LIT>' . format ( self . _fg_num / self . _count ) <EOL> print '<STR_LIT>' . format ( self . _bg_num / self . _count ) <EOL> print '<STR_LIT>' . format ( float ( self . _fg_num ) / float ( self . _bg_num ) ) <EOL> top [ <NUM_LIT:0> ] . reshape ( * rois . shape ) <EOL> top [ <NUM_LIT:0> ] . data [ ... ] = rois <EOL> top [ <NUM_LIT:1> ] . reshape ( * labels . shape ) <EOL> top [ <NUM_LIT:1> ] . data [ ... ] = labels <EOL> top [ <NUM_LIT:2> ] . reshape ( * bbox_targets . shape ) <EOL> top [ <NUM_LIT:2> ] . data [ ... ] = bbox_targets <EOL> top [ <NUM_LIT:3> ] . reshape ( * bbox_inside_weights . shape ) <EOL> top [ <NUM_LIT:3> ] . data [ ... ] = bbox_inside_weights <EOL> top [ <NUM_LIT:4> ] . reshape ( * bbox_inside_weights . shape ) <EOL> top [ <NUM_LIT:4> ] . data [ ... ] = np . array ( bbox_inside_weights > <NUM_LIT:0> ) . astype ( np . float32 ) <EOL> def backward ( self , top , propagate_down , bottom ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def reshape ( self , bottom , top ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _get_bbox_regression_labels ( bbox_target_data , num_classes ) : <EOL> """<STR_LIT>""" <EOL> clss = bbox_target_data [ : , <NUM_LIT:0> ] <EOL> bbox_targets = np . zeros ( ( clss . size , <NUM_LIT:4> * num_classes ) , dtype = np . float32 ) <EOL> bbox_inside_weights = np . zeros ( bbox_targets . shape , dtype = np . float32 ) <EOL> inds = np . where ( clss > <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> for ind in inds : <EOL> cls = clss [ ind ] <EOL> start = <NUM_LIT:4> * cls <EOL> end = start + <NUM_LIT:4> <EOL> bbox_targets [ ind , start : end ] = bbox_target_data [ ind , <NUM_LIT:1> : ] <EOL> bbox_inside_weights [ ind , start : end ] = cfg . TRAIN . BBOX_INSIDE_WEIGHTS <EOL> return bbox_targets , bbox_inside_weights <EOL> def _compute_targets ( ex_rois , gt_rois , labels ) : <EOL> """<STR_LIT>""" <EOL> assert ex_rois . shape [ <NUM_LIT:0> ] == gt_rois . shape [ <NUM_LIT:0> ] <EOL> assert ex_rois . shape [ <NUM_LIT:1> ] == <NUM_LIT:4> <EOL> assert gt_rois . shape [ <NUM_LIT:1> ] == <NUM_LIT:4> <EOL> targets = bbox_transform ( ex_rois , gt_rois ) <EOL> if cfg . TRAIN . BBOX_NORMALIZE_TARGETS_PRECOMPUTED : <EOL> targets = ( ( targets - np . array ( cfg . TRAIN . BBOX_NORMALIZE_MEANS ) ) <EOL> / np . array ( cfg . TRAIN . BBOX_NORMALIZE_STDS ) ) <EOL> return np . hstack ( <EOL> ( labels [ : , np . newaxis ] , targets ) ) . astype ( np . float32 , copy = False ) <EOL> def _sample_rois ( all_rois , gt_boxes , fg_rois_per_image , rois_per_image , num_classes ) : <EOL> """<STR_LIT>""" <EOL> overlaps = bbox_overlaps ( <EOL> np . ascontiguousarray ( all_rois [ : , <NUM_LIT:1> : <NUM_LIT:5> ] , dtype = np . float ) , <EOL> np . ascontiguousarray ( gt_boxes [ : , : <NUM_LIT:4> ] , dtype = np . float ) ) <EOL> gt_assignment = overlaps . argmax ( axis = <NUM_LIT:1> ) <EOL> max_overlaps = overlaps . max ( axis = <NUM_LIT:1> ) <EOL> labels = gt_boxes [ gt_assignment , <NUM_LIT:4> ] <EOL> fg_inds = np . where ( max_overlaps >= cfg . TRAIN . FG_THRESH ) [ <NUM_LIT:0> ] <EOL> fg_rois_per_this_image = min ( fg_rois_per_image , fg_inds . size ) <EOL> if fg_inds . size > <NUM_LIT:0> : <EOL> fg_inds = npr . choice ( fg_inds , size = fg_rois_per_this_image , replace = False ) <EOL> bg_inds = np . where ( ( max_overlaps < cfg . TRAIN . BG_THRESH_HI ) & <EOL> ( max_overlaps >= cfg . TRAIN . BG_THRESH_LO ) ) [ <NUM_LIT:0> ] <EOL> bg_rois_per_this_image = rois_per_image - fg_rois_per_this_image <EOL> bg_rois_per_this_image = min ( bg_rois_per_this_image , bg_inds . size ) <EOL> if bg_inds . size > <NUM_LIT:0> : <EOL> bg_inds = npr . choice ( bg_inds , size = bg_rois_per_this_image , replace = False ) <EOL> keep_inds = np . append ( fg_inds , bg_inds ) <EOL> labels = labels [ keep_inds ] <EOL> labels [ fg_rois_per_this_image : ] = <NUM_LIT:0> <EOL> rois = all_rois [ keep_inds ] <EOL> bbox_target_data = _compute_targets ( <EOL> rois [ : , <NUM_LIT:1> : <NUM_LIT:5> ] , gt_boxes [ gt_assignment [ keep_inds ] , : <NUM_LIT:4> ] , labels ) <EOL> bbox_targets , bbox_inside_weights = _get_bbox_regression_labels ( bbox_target_data , num_classes ) <EOL> return labels , rois , bbox_targets , bbox_inside_weights </s>
