<s> import os <EOL> import caffe <EOL> import yaml <EOL> from fast_rcnn . config import cfg <EOL> import numpy as np <EOL> import numpy . random as npr <EOL> from generate_anchors import generate_anchors <EOL> from utils . cython_bbox import bbox_overlaps <EOL> from fast_rcnn . bbox_transform import bbox_transform <EOL> DEBUG = False <EOL> class AnchorTargetLayer ( caffe . Layer ) : <EOL> """<STR_LIT>""" <EOL> def setup ( self , bottom , top ) : <EOL> layer_params = yaml . load ( self . param_str_ ) <EOL> anchor_scales = layer_params . get ( '<STR_LIT>' , ( <NUM_LIT:8> , <NUM_LIT:16> , <NUM_LIT:32> ) ) <EOL> self . _anchors = generate_anchors ( scales = np . array ( anchor_scales ) ) <EOL> self . _num_anchors = self . _anchors . shape [ <NUM_LIT:0> ] <EOL> self . _feat_stride = layer_params [ '<STR_LIT>' ] <EOL> if DEBUG : <EOL> print '<STR_LIT>' <EOL> print self . _anchors <EOL> print '<STR_LIT>' <EOL> print np . hstack ( ( <EOL> self . _anchors [ : , <NUM_LIT:2> : : <NUM_LIT:4> ] - self . _anchors [ : , <NUM_LIT:0> : : <NUM_LIT:4> ] , <EOL> self . _anchors [ : , <NUM_LIT:3> : : <NUM_LIT:4> ] - self . _anchors [ : , <NUM_LIT:1> : : <NUM_LIT:4> ] , <EOL> ) ) <EOL> self . _counts = cfg . EPS <EOL> self . _sums = np . zeros ( ( <NUM_LIT:1> , <NUM_LIT:4> ) ) <EOL> self . _squared_sums = np . zeros ( ( <NUM_LIT:1> , <NUM_LIT:4> ) ) <EOL> self . _fg_sum = <NUM_LIT:0> <EOL> self . _bg_sum = <NUM_LIT:0> <EOL> self . _count = <NUM_LIT:0> <EOL> self . _allowed_border = layer_params . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> height , width = bottom [ <NUM_LIT:0> ] . data . shape [ - <NUM_LIT:2> : ] <EOL> if DEBUG : <EOL> print '<STR_LIT>' , height , '<STR_LIT:width>' , width <EOL> A = self . _num_anchors <EOL> top [ <NUM_LIT:0> ] . reshape ( <NUM_LIT:1> , <NUM_LIT:1> , A * height , width ) <EOL> top [ <NUM_LIT:1> ] . reshape ( <NUM_LIT:1> , A * <NUM_LIT:4> , height , width ) <EOL> top [ <NUM_LIT:2> ] . reshape ( <NUM_LIT:1> , A * <NUM_LIT:4> , height , width ) <EOL> top [ <NUM_LIT:3> ] . reshape ( <NUM_LIT:1> , A * <NUM_LIT:4> , height , width ) <EOL> def forward ( self , bottom , top ) : <EOL> assert bottom [ <NUM_LIT:0> ] . data . shape [ <NUM_LIT:0> ] == <NUM_LIT:1> , '<STR_LIT>' <EOL> height , width = bottom [ <NUM_LIT:0> ] . data . shape [ - <NUM_LIT:2> : ] <EOL> gt_boxes = bottom [ <NUM_LIT:1> ] . data <EOL> im_info = bottom [ <NUM_LIT:2> ] . data [ <NUM_LIT:0> , : ] <EOL> if DEBUG : <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' . format ( im_info [ <NUM_LIT:0> ] , im_info [ <NUM_LIT:1> ] ) <EOL> print '<STR_LIT>' . format ( im_info [ <NUM_LIT:2> ] ) <EOL> print '<STR_LIT>' . format ( height , width ) <EOL> print '<STR_LIT>' , gt_boxes . shape <EOL> print '<STR_LIT>' , gt_boxes <EOL> shift_x = np . arange ( <NUM_LIT:0> , width ) * self . _feat_stride <EOL> shift_y = np . arange ( <NUM_LIT:0> , height ) * self . _feat_stride <EOL> shift_x , shift_y = np . meshgrid ( shift_x , shift_y ) <EOL> shifts = np . vstack ( ( shift_x . ravel ( ) , shift_y . ravel ( ) , <EOL> shift_x . ravel ( ) , shift_y . ravel ( ) ) ) . transpose ( ) <EOL> A = self . _num_anchors <EOL> K = shifts . shape [ <NUM_LIT:0> ] <EOL> all_anchors = ( self . _anchors . reshape ( ( <NUM_LIT:1> , A , <NUM_LIT:4> ) ) + <EOL> shifts . reshape ( ( <NUM_LIT:1> , K , <NUM_LIT:4> ) ) . transpose ( ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> ) ) ) <EOL> all_anchors = all_anchors . reshape ( ( K * A , <NUM_LIT:4> ) ) <EOL> total_anchors = int ( K * A ) <EOL> inds_inside = np . where ( <EOL> ( all_anchors [ : , <NUM_LIT:0> ] >= - self . _allowed_border ) & <EOL> ( all_anchors [ : , <NUM_LIT:1> ] >= - self . _allowed_border ) & <EOL> ( all_anchors [ : , <NUM_LIT:2> ] < im_info [ <NUM_LIT:1> ] + self . _allowed_border ) & <EOL> ( all_anchors [ : , <NUM_LIT:3> ] < im_info [ <NUM_LIT:0> ] + self . _allowed_border ) <EOL> ) [ <NUM_LIT:0> ] <EOL> if DEBUG : <EOL> print '<STR_LIT>' , total_anchors <EOL> print '<STR_LIT>' , len ( inds_inside ) <EOL> anchors = all_anchors [ inds_inside , : ] <EOL> if DEBUG : <EOL> print '<STR_LIT>' , anchors . shape <EOL> labels = np . empty ( ( len ( inds_inside ) , ) , dtype = np . float32 ) <EOL> labels . fill ( - <NUM_LIT:1> ) <EOL> overlaps = bbox_overlaps ( <EOL> np . ascontiguousarray ( anchors , dtype = np . float ) , <EOL> np . ascontiguousarray ( gt_boxes , dtype = np . float ) ) <EOL> argmax_overlaps = overlaps . argmax ( axis = <NUM_LIT:1> ) <EOL> max_overlaps = overlaps [ np . arange ( len ( inds_inside ) ) , argmax_overlaps ] <EOL> gt_argmax_overlaps = overlaps . argmax ( axis = <NUM_LIT:0> ) <EOL> gt_max_overlaps = overlaps [ gt_argmax_overlaps , <EOL> np . arange ( overlaps . shape [ <NUM_LIT:1> ] ) ] <EOL> gt_argmax_overlaps = np . where ( overlaps == gt_max_overlaps ) [ <NUM_LIT:0> ] <EOL> if not cfg . TRAIN . RPN_CLOBBER_POSITIVES : <EOL> labels [ max_overlaps < cfg . TRAIN . RPN_NEGATIVE_OVERLAP ] = <NUM_LIT:0> <EOL> labels [ gt_argmax_overlaps ] = <NUM_LIT:1> <EOL> labels [ max_overlaps >= cfg . TRAIN . RPN_POSITIVE_OVERLAP ] = <NUM_LIT:1> <EOL> if cfg . TRAIN . RPN_CLOBBER_POSITIVES : <EOL> labels [ max_overlaps < cfg . TRAIN . RPN_NEGATIVE_OVERLAP ] = <NUM_LIT:0> <EOL> num_fg = int ( cfg . TRAIN . RPN_FG_FRACTION * cfg . TRAIN . RPN_BATCHSIZE ) <EOL> fg_inds = np . where ( labels == <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if len ( fg_inds ) > num_fg : <EOL> disable_inds = npr . choice ( <EOL> fg_inds , size = ( len ( fg_inds ) - num_fg ) , replace = False ) <EOL> labels [ disable_inds ] = - <NUM_LIT:1> <EOL> num_bg = cfg . TRAIN . RPN_BATCHSIZE - np . sum ( labels == <NUM_LIT:1> ) <EOL> bg_inds = np . where ( labels == <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> if len ( bg_inds ) > num_bg : <EOL> disable_inds = npr . choice ( <EOL> bg_inds , size = ( len ( bg_inds ) - num_bg ) , replace = False ) <EOL> labels [ disable_inds ] = - <NUM_LIT:1> <EOL> bbox_targets = np . zeros ( ( len ( inds_inside ) , <NUM_LIT:4> ) , dtype = np . float32 ) <EOL> bbox_targets = _compute_targets ( anchors , gt_boxes [ argmax_overlaps , : ] ) <EOL> bbox_inside_weights = np . zeros ( ( len ( inds_inside ) , <NUM_LIT:4> ) , dtype = np . float32 ) <EOL> bbox_inside_weights [ labels == <NUM_LIT:1> , : ] = np . array ( cfg . TRAIN . RPN_BBOX_INSIDE_WEIGHTS ) <EOL> bbox_outside_weights = np . zeros ( ( len ( inds_inside ) , <NUM_LIT:4> ) , dtype = np . float32 ) <EOL> if cfg . TRAIN . RPN_POSITIVE_WEIGHT < <NUM_LIT:0> : <EOL> num_examples = np . sum ( labels >= <NUM_LIT:0> ) <EOL> positive_weights = np . ones ( ( <NUM_LIT:1> , <NUM_LIT:4> ) ) * <NUM_LIT:1.0> / num_examples <EOL> negative_weights = np . ones ( ( <NUM_LIT:1> , <NUM_LIT:4> ) ) * <NUM_LIT:1.0> / num_examples <EOL> else : <EOL> assert ( ( cfg . TRAIN . RPN_POSITIVE_WEIGHT > <NUM_LIT:0> ) & <EOL> ( cfg . TRAIN . RPN_POSITIVE_WEIGHT < <NUM_LIT:1> ) ) <EOL> positive_weights = ( cfg . TRAIN . RPN_POSITIVE_WEIGHT / <EOL> np . sum ( labels == <NUM_LIT:1> ) ) <EOL> negative_weights = ( ( <NUM_LIT:1.0> - cfg . TRAIN . RPN_POSITIVE_WEIGHT ) / <EOL> np . sum ( labels == <NUM_LIT:0> ) ) <EOL> bbox_outside_weights [ labels == <NUM_LIT:1> , : ] = positive_weights <EOL> bbox_outside_weights [ labels == <NUM_LIT:0> , : ] = negative_weights <EOL> if DEBUG : <EOL> self . _sums += bbox_targets [ labels == <NUM_LIT:1> , : ] . sum ( axis = <NUM_LIT:0> ) <EOL> self . _squared_sums += ( bbox_targets [ labels == <NUM_LIT:1> , : ] ** <NUM_LIT:2> ) . sum ( axis = <NUM_LIT:0> ) <EOL> self . _counts += np . sum ( labels == <NUM_LIT:1> ) <EOL> means = self . _sums / self . _counts <EOL> stds = np . sqrt ( self . _squared_sums / self . _counts - means ** <NUM_LIT:2> ) <EOL> print '<STR_LIT>' <EOL> print means <EOL> print '<STR_LIT>' <EOL> print stds <EOL> labels = _unmap ( labels , total_anchors , inds_inside , fill = - <NUM_LIT:1> ) <EOL> bbox_targets = _unmap ( bbox_targets , total_anchors , inds_inside , fill = <NUM_LIT:0> ) <EOL> bbox_inside_weights = _unmap ( bbox_inside_weights , total_anchors , inds_inside , fill = <NUM_LIT:0> ) <EOL> bbox_outside_weights = _unmap ( bbox_outside_weights , total_anchors , inds_inside , fill = <NUM_LIT:0> ) <EOL> if DEBUG : <EOL> print '<STR_LIT>' , np . max ( max_overlaps ) <EOL> print '<STR_LIT>' , np . sum ( labels == <NUM_LIT:1> ) <EOL> print '<STR_LIT>' , np . sum ( labels == <NUM_LIT:0> ) <EOL> self . _fg_sum += np . sum ( labels == <NUM_LIT:1> ) <EOL> self . _bg_sum += np . sum ( labels == <NUM_LIT:0> ) <EOL> self . _count += <NUM_LIT:1> <EOL> print '<STR_LIT>' , self . _fg_sum / self . _count <EOL> print '<STR_LIT>' , self . _bg_sum / self . _count <EOL> labels = labels . reshape ( ( <NUM_LIT:1> , height , width , A ) ) . transpose ( <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> labels = labels . reshape ( ( <NUM_LIT:1> , <NUM_LIT:1> , A * height , width ) ) <EOL> top [ <NUM_LIT:0> ] . reshape ( * labels . shape ) <EOL> top [ <NUM_LIT:0> ] . data [ ... ] = labels <EOL> bbox_targets = bbox_targets . reshape ( ( <NUM_LIT:1> , height , width , A * <NUM_LIT:4> ) ) . transpose ( <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> top [ <NUM_LIT:1> ] . reshape ( * bbox_targets . shape ) <EOL> top [ <NUM_LIT:1> ] . data [ ... ] = bbox_targets <EOL> bbox_inside_weights = bbox_inside_weights . reshape ( ( <NUM_LIT:1> , height , width , A * <NUM_LIT:4> ) ) . transpose ( <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> assert bbox_inside_weights . shape [ <NUM_LIT:2> ] == height <EOL> assert bbox_inside_weights . shape [ <NUM_LIT:3> ] == width <EOL> top [ <NUM_LIT:2> ] . reshape ( * bbox_inside_weights . shape ) <EOL> top [ <NUM_LIT:2> ] . data [ ... ] = bbox_inside_weights <EOL> bbox_outside_weights = bbox_outside_weights . reshape ( ( <NUM_LIT:1> , height , width , A * <NUM_LIT:4> ) ) . transpose ( <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> assert bbox_outside_weights . shape [ <NUM_LIT:2> ] == height <EOL> assert bbox_outside_weights . shape [ <NUM_LIT:3> ] == width <EOL> top [ <NUM_LIT:3> ] . reshape ( * bbox_outside_weights . shape ) <EOL> top [ <NUM_LIT:3> ] . data [ ... ] = bbox_outside_weights <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 _unmap ( data , count , inds , fill = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if len ( data . shape ) == <NUM_LIT:1> : <EOL> ret = np . empty ( ( count , ) , dtype = np . float32 ) <EOL> ret . fill ( fill ) <EOL> ret [ inds ] = data <EOL> else : <EOL> ret = np . empty ( ( count , ) + data . shape [ <NUM_LIT:1> : ] , dtype = np . float32 ) <EOL> ret . fill ( fill ) <EOL> ret [ inds , : ] = data <EOL> return ret <EOL> def _compute_targets ( ex_rois , gt_rois ) : <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:5> <EOL> return bbox_transform ( ex_rois , gt_rois [ : , : <NUM_LIT:4> ] ) . astype ( np . float32 , copy = False ) </s>
