def __init__(self, feat_stride_fpn, scales, ratios, output_score, rpn_pre_nms_top_n, rpn_post_nms_top_n, threshold, rpn_min_size_fpn): super(ProposalFPNOperator, self).__init__() self._feat_stride_fpn = np.fromstring(feat_stride_fpn[1:-1], dtype=int, sep=',') self.fpn_keys = [] fpn_stride = [] for s in self._feat_stride_fpn: self.fpn_keys.append('stride%s'%s) fpn_stride.append(int(s)) self._scales = np.fromstring(scales[1:-1], dtype=float, sep=',') self._ratios = np.fromstring(ratios[1:-1], dtype=float, sep=',') self._anchors_fpn = dict(zip(self.fpn_keys, generate_anchors_fpn(base_size=fpn_stride, scales=self._scales, ratios=self._ratios))) self._num_anchors = dict(zip(self.fpn_keys, [anchors.shape[0] for anchors in self._anchors_fpn.values()])) self._output_score = output_score self._rpn_pre_nms_top_n = rpn_pre_nms_top_n self._rpn_post_nms_top_n = rpn_post_nms_top_n self._threshold = threshold self._rpn_min_size_fpn = dict(zip(self.fpn_keys, np.fromstring(rpn_min_size_fpn[1:-1], dtype=int, sep=','))) self._bbox_pred = nonlinear_pred if DEBUG: print 'feat_stride: {}'.format(self._feat_stride_fpn) print 'anchors: {}'.format(self._anchors_fpn) print 'num_anchors: {}'.format(self._num_anchors)
def __init__(self, output_score, rpn_pre_nms_top_n, rpn_post_nms_top_n, threshold, rpn_min_size_fpn): super(ProposalFPNOperator, self).__init__() #self._feat_stride_fpn = np.fromstring(feat_stride_fpn[1:-1], dtype=int, sep=',') self._feat_stride_fpn = config.RPN_FEAT_STRIDE self.fpn_keys = [] fpn_stride = [] fpn_base_size = [] for s in self._feat_stride_fpn: self.fpn_keys.append('stride%s'%s) fpn_stride.append(int(s)) fpn_base_size.append(config.RPN_ANCHOR_CFG[str(s)]['BASE_SIZE']) #self._scales = np.fromstring(scales[1:-1], dtype=float, sep=',') #self._ratios = np.fromstring(ratios[1:-1], dtype=float, sep=',') #print('ProposalFPN scales', self._scales) #print('ProposalFPN ratios', self._ratios) self._anchors_fpn = dict(zip(self.fpn_keys, generate_anchors_fpn())) self._num_anchors = dict(zip(self.fpn_keys, [anchors.shape[0] for anchors in self._anchors_fpn.values()])) self._output_score = output_score self._rpn_pre_nms_top_n = rpn_pre_nms_top_n self._rpn_post_nms_top_n = rpn_post_nms_top_n self._threshold = threshold self._rpn_min_size_fpn = dict(zip(self.fpn_keys, np.fromstring(rpn_min_size_fpn[1:-1], dtype=int, sep=','))) self._bbox_pred = nonlinear_pred if DEBUG: print 'feat_stride: {}'.format(self._feat_stride_fpn) print 'anchors: {}'.format(self._anchors_fpn) print 'num_anchors: {}'.format(self._num_anchors)
def __init__(self, args, nms=0.4, verbose=False): self.threshold = args.threshold self.scales = args.scales self.nms_threshold = nms self.ctx_id = args.gpu self.margin = np.array([-args.face_margin, -args.face_margin, args.face_margin, args.face_margin]) self._feat_stride_fpn = [int(k) for k in anchor_shape.keys()] self.anchor_cfg = anchor_shape self.fpn_keys = [f'stride{s}' for s in self._feat_stride_fpn] anchors_fpn_list = generate_anchors_fpn(cfg=self.anchor_cfg) self._anchors_fpn = dict( zip(self.fpn_keys, np.asarray(anchors_fpn_list, dtype=np.float32))) self._num_anchors = dict( zip(self.fpn_keys, [anchors.shape[0] for anchors in self._anchors_fpn.values()])) if self.ctx_id < 0: self.ctx = mx.cpu() self.nms = cpu_nms_wrapper(self.nms_threshold) else: self.ctx = mx.gpu(self.ctx_id) self.nms = gpu_nms_wrapper(self.nms_threshold, self.ctx_id) sym, arg_params, aux_params = mx.model.load_checkpoint( args.retina_model, 0) self.model = mx.mod.Module(sym, context=self.ctx, label_names=None) self.model.bind( data_shapes=[('data', (1, 3, 640, 640))], for_training=False) self.model.set_params(arg_params, aux_params)
def generate_anchors(self): _ratio = (1., ) anchor_cfg = { '32': { 'SCALES': (32, 16), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '16': { 'SCALES': (8, 4), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '8': { 'SCALES': (2, 1), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, } self.fpn_keys = [32, 16, 8] self._anchors_fpn = dict( zip(self.fpn_keys, generate_anchors_fpn(dense_anchor=False, cfg=anchor_cfg))) self._num_anchors = dict( zip(self.fpn_keys, [anchors.shape[0] for anchors in self._anchors_fpn.values()]))
def __init__(self, prefix, epoch, ctx_id=0, test_mode=False): self.ctx_id = ctx_id self.test_mode = test_mode self.fpn_keys = [] fpn_stride = [] fpn_base_size = [] self._feat_stride_fpn = [32, 16, 8] for s in self._feat_stride_fpn: self.fpn_keys.append('stride%s' % s) fpn_stride.append(int(s)) fpn_base_size.append(16) self._scales = np.array([32, 16, 8, 4, 2, 1]) self._ratios = np.array([1.0] * len(self._feat_stride_fpn)) self._anchors_fpn = dict( zip( self.fpn_keys, generate_anchors_fpn(base_size=fpn_base_size, scales=self._scales, ratios=self._ratios))) self._num_anchors = dict( zip(self.fpn_keys, [anchors.shape[0] for anchors in self._anchors_fpn.values()])) self._rpn_pre_nms_top_n = 1000 # self._rpn_post_nms_top_n = rpn_post_nms_top_n # self.score_threshold = 0.05 self.nms_threshold = 0.3 self._bbox_pred = nonlinear_pred sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch) if self.ctx_id >= 0: self.ctx = mx.gpu(self.ctx_id) self.nms = gpu_nms_wrapper(self.nms_threshold, self.ctx_id) else: self.ctx = mx.cpu() self.nms = cpu_nms_wrapper(self.nms_threshold) # self.nms = py_nms_wrapper(self.nms_threshold) # self.pixel_means = np.array([103.939, 116.779, 123.68]) #BGR,VGG16 self.pixel_means = np.array([0.0, 0.0, 0.0]) #BGR,R50 if not self.test_mode: image_size = (640, 640) self.model = mx.mod.Module(symbol=sym, context=self.ctx, label_names=None) self.model.bind(data_shapes=[('data', (1, 3, image_size[0], image_size[1]))], for_training=False) self.model.set_params(arg_params, aux_params) else: from rcnn.core.module import MutableModule image_size = (2400, 2400) data_shape = [('data', (1, 3, image_size[0], image_size[1]))] self.model = MutableModule(symbol=sym, data_names=['data'], label_names=None, context=self.ctx, max_data_shapes=data_shape) self.model.bind(data_shape, None, for_training=False) self.model.set_params(arg_params, aux_params)
def __init__(self, gpu=0, test_mode=False): self.ctx_id = gpu self.ctx = mx.gpu(self.ctx_id) self.fpn_keys = [] fpn_stride = [] fpn_base_size = [] self._feat_stride_fpn = [32, 16, 8] for s in self._feat_stride_fpn: self.fpn_keys.append('stride%s' % s) fpn_stride.append(int(s)) fpn_base_size.append(16) self._scales = np.array([32, 16, 8, 4, 2, 1]) self._ratios = np.array([1.0] * len(self._feat_stride_fpn)) self._anchors_fpn = dict( zip( self.fpn_keys, generate_anchors_fpn(base_size=fpn_base_size, scales=self._scales, ratios=self._ratios))) self._num_anchors = dict( zip(self.fpn_keys, [anchors.shape[0] for anchors in self._anchors_fpn.values()])) self._rpn_pre_nms_top_n = 1000 # self._rpn_post_nms_top_n = rpn_post_nms_top_n # self.score_threshold = 0.05 self.nms_threshold = config.TEST.NMS self._bbox_pred = nonlinear_pred base_path = os.path.dirname(__file__) sym, arg_params, aux_params = mx.model.load_checkpoint( base_path + '/model/model-v2.0/e2e', 1) self.nms = gpu_nms_wrapper(self.nms_threshold, self.ctx_id) self.pixel_means = np.array([103.939, 116.779, 123.68]) # BGR if not test_mode: image_size = (640, 640) self.model = mx.mod.Module(symbol=sym, context=self.ctx, label_names=None) self.model.bind(data_shapes=[('data', (1, 3, image_size[0], image_size[1]))], for_training=False) self.model.set_params(arg_params, aux_params) else: from rcnn.core.module import MutableModule image_size = (640, 640) data_shape = [('data', (1, 3, image_size[0], image_size[1]))] self.model = MutableModule(symbol=sym, data_names=['data'], label_names=None, context=self.ctx, max_data_shapes=data_shape) self.model.bind(data_shape, None, for_training=False) self.model.set_params(arg_params, aux_params) print('init ssh success')
def __init__(self, model_weights, use_gpu_nms=True, nms=0.4, decay4=0.5): self.decay4 = decay4 self.nms_threshold = nms self.fpn_keys = [] self.anchor_cfg = None self.preprocess = False _ratio = (1., ) self._feat_stride_fpn = [32, 16, 8] self.anchor_cfg = { '32': { 'SCALES': (32, 16), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '16': { 'SCALES': (8, 4), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '8': { 'SCALES': (2, 1), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, } for s in self._feat_stride_fpn: self.fpn_keys.append('stride%s' % s) self._anchors_fpn = dict( zip(self.fpn_keys, generate_anchors_fpn(dense_anchor=False, cfg=self.anchor_cfg))) for k in self._anchors_fpn: v = self._anchors_fpn[k].astype(np.float32) self._anchors_fpn[k] = v self._num_anchors = dict( zip(self.fpn_keys, [anchors.shape[0] for anchors in self._anchors_fpn.values()])) if use_gpu_nms: self.nms = gpu_nms_wrapper(self.nms_threshold, 0) else: self.nms = cpu_nms_wrapper(self.nms_threshold) pixel_means = [0.0, 0.0, 0.0] pixel_stds = [1.0, 1.0, 1.0] pixel_scale = 1.0 self.pixel_means = np.array(pixel_means, dtype=np.float32) self.pixel_stds = np.array(pixel_stds, dtype=np.float32) self.pixel_scale = float(pixel_scale) self.bbox_stds = [1.0, 1.0, 1.0, 1.0] self.scales = [1024, 1980] self.model = tf.function(RetinaFaceNetwork(model_weights).model, input_signature=(tf.TensorSpec( shape=[None, None, None, 3], dtype=np.float32), ))
def __init__(self, prefix, epoch, ctx_id=0): self.ctx_id = ctx_id self.ctx = mx.gpu(self.ctx_id) self.fpn_keys = [] fpn_stride = [] fpn_base_size = [] self._feat_stride_fpn = [32, 16, 8] for s in self._feat_stride_fpn: self.fpn_keys.append('stride%s'%s) fpn_stride.append(int(s)) fpn_base_size.append(16) self._scales = np.array([32,16,8,4,2,1]) self._ratios = np.array([1.0]*len(self._feat_stride_fpn)) self._anchors_fpn = dict(zip(self.fpn_keys, generate_anchors_fpn(base_size=fpn_base_size, scales=self._scales, ratios=self._ratios))) self._num_anchors = dict(zip(self.fpn_keys, [anchors.shape[0] for anchors in self._anchors_fpn.values()])) self._rpn_pre_nms_top_n = 1000 #self._rpn_post_nms_top_n = rpn_post_nms_top_n #self.score_threshold = 0.05 self.nms_threshold = 0.3 self._bbox_pred = nonlinear_pred sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch) #all_layers = sym.get_internals() #outs = [] #for stride in self._feat_stride_fpn: # _name = 'rpn_cls_score_stride%s_output' % stride # rpn_cls_score = all_layers[_name] # rpn_cls_score_reshape = mx.symbol.Reshape(data=rpn_cls_score, # shape=(0, 2, -1, 0), # name="rpn_cls_score_reshape_stride%d" % stride) # rpn_cls_prob = mx.symbol.SoftmaxActivation(data=rpn_cls_score_reshape, # mode="channel", # name="rpn_cls_prob_stride%d" % stride) # rpn_cls_prob_reshape = mx.symbol.Reshape(data=rpn_cls_prob, # shape=(0, 2 * num_anchors, -1, 0), # name='rpn_cls_prob_reshape_stride%d' % stride) # outs.append(rpn_cls_prob_reshape) # _name = 'rpn_bbox_pred_stride%s_output' % stride # rpn_bbox_pred = all_layers[_name] # outs.append(rpn_bbox_pred) #sym = mx.sym.Group(outs) self.model = mx.mod.Module(symbol=sym, context=self.ctx, label_names = None) image_size = (640, 640) self.model.bind(data_shapes=[('data', (1, 3, image_size[0], image_size[1]))], for_training=False) self.model.set_params(arg_params, aux_params) self.nms = gpu_nms_wrapper(self.nms_threshold, self.ctx_id) pass
def __init__(self, prefix, epoch, ctx_id=0, test_mode=False): self.ctx_id = ctx_id self.ctx = mx.gpu(self.ctx_id) self.fpn_keys = [] fpn_stride = [] fpn_base_size = [] self._feat_stride_fpn = [32, 16, 8] for s in self._feat_stride_fpn: self.fpn_keys.append('stride%s'%s) fpn_stride.append(int(s)) fpn_base_size.append(16) self._scales = np.array([32,16,8,4,2,1]) self._ratios = np.array([1.0]*len(self._feat_stride_fpn)) self._anchors_fpn = dict(zip(self.fpn_keys, generate_anchors_fpn(base_size=fpn_base_size, scales=self._scales, ratios=self._ratios))) self._num_anchors = dict(zip(self.fpn_keys, [anchors.shape[0] for anchors in self._anchors_fpn.values()])) self._rpn_pre_nms_top_n = 1000 #self._rpn_post_nms_top_n = rpn_post_nms_top_n #self.score_threshold = 0.05 self.nms_threshold = 0.3 self._bbox_pred = nonlinear_pred sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch) self.nms = gpu_nms_wrapper(self.nms_threshold, self.ctx_id) self.pixel_means = np.array([103.939, 116.779, 123.68]) #BGR if not test_mode: image_size = (640, 640) self.model = mx.mod.Module(symbol=sym, context=self.ctx, label_names = None) self.model.bind(data_shapes=[('data', (1, 3, image_size[0], image_size[1]))], for_training=False) self.model.set_params(arg_params, aux_params) else: from rcnn.core.module import MutableModule image_size = (2400, 2400) data_shape = [('data', (1,3,image_size[0], image_size[1]))] self.model = MutableModule(symbol=sym, data_names=['data'], label_names=None, context=self.ctx, max_data_shapes=data_shape) self.model.bind(data_shape, None, for_training=False) self.model.set_params(arg_params, aux_params)
def __init__(self, prefix, epoch, ctx_id=0, network='net3', nms=0.4, nocrop=False, decay4=0.5, vote=False): self.ctx_id = ctx_id self.network = network self.decay4 = decay4 self.nms_threshold = nms self.vote = vote self.nocrop = nocrop self.debug = False self.fpn_keys = [] self.anchor_cfg = None pixel_means = [0.0, 0.0, 0.0] pixel_stds = [1.0, 1.0, 1.0] pixel_scale = 1.0 self.preprocess = False _ratio = (1., ) fmc = 3 if network == 'ssh' or network == 'vgg': pixel_means = [103.939, 116.779, 123.68] self.preprocess = True elif network == 'net3': _ratio = (1., ) elif network == 'net3a': _ratio = (1., 1.5) elif network == 'net6': #like pyramidbox or s3fd fmc = 6 elif network == 'net5': #retinaface fmc = 5 elif network == 'net5a': fmc = 5 _ratio = (1., 1.5) elif network == 'net4': fmc = 4 elif network == 'net4a': fmc = 4 _ratio = (1., 1.5) elif network == 'x5': fmc = 5 pixel_means = [103.52, 116.28, 123.675] pixel_stds = [57.375, 57.12, 58.395] elif network == 'x3': fmc = 3 pixel_means = [103.52, 116.28, 123.675] pixel_stds = [57.375, 57.12, 58.395] elif network == 'x3a': fmc = 3 _ratio = (1., 1.5) pixel_means = [103.52, 116.28, 123.675] pixel_stds = [57.375, 57.12, 58.395] else: assert False, 'network setting error %s' % network if fmc == 3: self._feat_stride_fpn = [32, 16, 8] self.anchor_cfg = { '32': { 'SCALES': (32, 16), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '16': { 'SCALES': (8, 4), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '8': { 'SCALES': (2, 1), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, } elif fmc == 4: self._feat_stride_fpn = [32, 16, 8, 4] self.anchor_cfg = { '32': { 'SCALES': (32, 16), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '16': { 'SCALES': (8, 4), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '8': { 'SCALES': (2, 1), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '4': { 'SCALES': (2, 1), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, } elif fmc == 6: self._feat_stride_fpn = [128, 64, 32, 16, 8, 4] self.anchor_cfg = { '128': { 'SCALES': (32, ), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '64': { 'SCALES': (16, ), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '32': { 'SCALES': (8, ), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '16': { 'SCALES': (4, ), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '8': { 'SCALES': (2, ), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '4': { 'SCALES': (1, ), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, } elif fmc == 5: self._feat_stride_fpn = [64, 32, 16, 8, 4] self.anchor_cfg = {} _ass = 2.0**(1.0 / 3) _basescale = 1.0 for _stride in [4, 8, 16, 32, 64]: key = str(_stride) value = { 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 } scales = [] for _ in range(3): scales.append(_basescale) _basescale *= _ass value['SCALES'] = tuple(scales) self.anchor_cfg[key] = value print(self._feat_stride_fpn, self.anchor_cfg) for s in self._feat_stride_fpn: self.fpn_keys.append('stride%s' % s) dense_anchor = False #self._anchors_fpn = dict(zip(self.fpn_keys, generate_anchors_fpn(base_size=fpn_base_size, scales=self._scales, ratios=self._ratios))) self._anchors_fpn = dict( zip( self.fpn_keys, generate_anchors_fpn(dense_anchor=dense_anchor, cfg=self.anchor_cfg))) for k in self._anchors_fpn: v = self._anchors_fpn[k].astype(np.float32) self._anchors_fpn[k] = v self._num_anchors = dict( zip(self.fpn_keys, [anchors.shape[0] for anchors in self._anchors_fpn.values()])) #self._bbox_pred = nonlinear_pred #self._landmark_pred = landmark_pred sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch) if self.ctx_id >= 0: self.ctx = mx.gpu(self.ctx_id) self.nms = gpu_nms_wrapper(self.nms_threshold, self.ctx_id) else: self.ctx = mx.cpu() self.nms = cpu_nms_wrapper(self.nms_threshold) self.pixel_means = np.array(pixel_means, dtype=np.float32) self.pixel_stds = np.array(pixel_stds, dtype=np.float32) self.pixel_scale = float(pixel_scale) print('means', self.pixel_means) self.use_landmarks = False if len(sym) // len(self._feat_stride_fpn) >= 3: self.use_landmarks = True print('use_landmarks', self.use_landmarks) self.cascade = 0 if float(len(sym)) // len(self._feat_stride_fpn) > 3.0: self.cascade = 1 print('cascade', self.cascade) #self.bbox_stds = [0.1, 0.1, 0.2, 0.2] #self.landmark_std = 0.1 self.bbox_stds = [1.0, 1.0, 1.0, 1.0] self.landmark_std = 1.0 if self.debug: c = len(sym) // len(self._feat_stride_fpn) sym = sym[(c * 0):] self._feat_stride_fpn = [32, 16, 8] print('sym size:', len(sym)) image_size = (640, 640) self.model = mx.mod.Module(symbol=sym, context=self.ctx, label_names=None) self.model.bind(data_shapes=[('data', (1, 3, image_size[0], image_size[1]))], for_training=False) self.model.set_params(arg_params, aux_params)
def __init__(self, prefix, epoch, ctx_id=0, network='net3', nms=0.4, nocrop=False, decay4=0.5, vote=False): self.ctx_id = ctx_id self.network = network self.decay4 = decay4 self.nms_threshold = nms self.vote = vote self.nocrop = nocrop self.debug = False self.fpn_keys = [] self.anchor_cfg = None self.preprocess = False _ratio = (1., ) fmc = 3 if network == 'ssh' or network == 'vgg': self.preprocess = True elif network == 'net3': _ratio = (1., ) elif network == 'net3a': _ratio = (1., 1.5) elif network == 'net6': #like pyramidbox or s3fd fmc = 6 elif network == 'net5': #retinaface fmc = 5 elif network == 'net5a': fmc = 5 _ratio = (1., 1.5) elif network == 'net4': fmc = 4 elif network == 'net4a': fmc = 4 _ratio = (1., 1.5) else: assert False, 'network setting error %s' % network if fmc == 3: self._feat_stride_fpn = [32, 16, 8] self.anchor_cfg = { '32': { 'SCALES': (32, 16), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '16': { 'SCALES': (8, 4), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '8': { 'SCALES': (2, 1), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, } elif fmc == 4: self._feat_stride_fpn = [32, 16, 8, 4] self.anchor_cfg = { '32': { 'SCALES': (32, 16), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '16': { 'SCALES': (8, 4), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '8': { 'SCALES': (2, 1), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '4': { 'SCALES': (2, 1), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, } elif fmc == 6: self._feat_stride_fpn = [128, 64, 32, 16, 8, 4] self.anchor_cfg = { '128': { 'SCALES': (32, ), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '64': { 'SCALES': (16, ), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '32': { 'SCALES': (8, ), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '16': { 'SCALES': (4, ), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '8': { 'SCALES': (2, ), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '4': { 'SCALES': (1, ), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, } elif fmc == 5: self._feat_stride_fpn = [64, 32, 16, 8, 4] self.anchor_cfg = {} _ass = 2.0**(1.0 / 3) _basescale = 1.0 for _stride in [4, 8, 16, 32, 64]: key = str(_stride) value = { 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 } scales = [] for _ in range(3): scales.append(_basescale) _basescale *= _ass value['SCALES'] = tuple(scales) self.anchor_cfg[key] = value print(self._feat_stride_fpn, self.anchor_cfg) for s in self._feat_stride_fpn: self.fpn_keys.append('stride%s' % s) dense_anchor = False self._anchors_fpn = dict( zip( self.fpn_keys, generate_anchors_fpn(dense_anchor=dense_anchor, cfg=self.anchor_cfg))) for k in self._anchors_fpn: v = self._anchors_fpn[k].astype(np.float32) self._anchors_fpn[k] = v self._num_anchors = dict( zip(self.fpn_keys, [anchors.shape[0] for anchors in self._anchors_fpn.values()])) sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch) if self.ctx_id >= 0: self.ctx = mx.gpu(self.ctx_id) self.nms = gpu_nms_wrapper(self.nms_threshold, self.ctx_id) else: self.ctx = mx.cpu() self.nms = cpu_nms_wrapper(self.nms_threshold) self.use_landmarks = False if len(sym) // len(self._feat_stride_fpn) == 3: self.use_landmarks = True print('use_landmarks', self.use_landmarks) image_size = (640, 640) self.model = mx.mod.Module(symbol=sym, context=self.ctx, label_names=None) self.model.bind(data_shapes=[('data', (1, 3, image_size[0], image_size[1]))], for_training=False) self.model.set_params(arg_params, aux_params)
def postprocess(net_out, threshold, ctx_id, im_scale, im_info): # im_info = [640, 640] flip = False decay4 = 0.5 vote = False fpn_keys = [] anchor_cfg = None bbox_stds = [1.0, 1.0, 1.0, 1.0] # im_scale = 1.0 landmark_std = 1.0 nms_threshold = 0.4 proposals_list = [] scores_list = [] landmarks_list = [] strides_list = [] use_landmarks = True if ctx_id >= 0: nms = gpu_nms_wrapper(nms_threshold, ctx_id) else: nms = cpu_nms_wrapper(nms_threshold) use_landmarks = True _ratio = (1., ) _feat_stride_fpn = [32, 16, 8] anchor_cfg = { '32': { 'SCALES': (32, 16), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '16': { 'SCALES': (8, 4), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '8': { 'SCALES': (2, 1), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, } for s in _feat_stride_fpn: fpn_keys.append('stride%s' % s) dense_anchor = False _anchors_fpn = dict( zip(fpn_keys, generate_anchors_fpn(dense_anchor=dense_anchor, cfg=anchor_cfg))) for k in _anchors_fpn: v = _anchors_fpn[k].astype(np.float32) _anchors_fpn[k] = v _num_anchors = dict( zip(fpn_keys, [anchors.shape[0] for anchors in _anchors_fpn.values()])) sym_idx = 0 for _idx, s in enumerate(_feat_stride_fpn): # print(sym_idx) _key = 'stride%s' % s # print(_key) stride = int(s) scores = net_out[sym_idx] #.asnumpy() scores = scores[:, _num_anchors['stride%s' % s]:, :, :] bbox_deltas = net_out[sym_idx + 1] # .asnumpy() height, width = bbox_deltas.shape[2], bbox_deltas.shape[3] A = _num_anchors['stride%s' % s] K = height * width anchors_fpn = _anchors_fpn['stride%s' % s] anchors = anchors_plane(height, width, stride, anchors_fpn) anchors = anchors.reshape((K * A, 4)) scores = scores.transpose((0, 2, 3, 1)).reshape((-1, 1)) bbox_deltas = bbox_deltas.transpose((0, 2, 3, 1)) bbox_pred_len = bbox_deltas.shape[3] // A bbox_deltas = bbox_deltas.reshape((-1, bbox_pred_len)) bbox_deltas[:, 0::4] = bbox_deltas[:, 0::4] * bbox_stds[0] bbox_deltas[:, 1::4] = bbox_deltas[:, 1::4] * bbox_stds[1] bbox_deltas[:, 2::4] = bbox_deltas[:, 2::4] * bbox_stds[2] bbox_deltas[:, 3::4] = bbox_deltas[:, 3::4] * bbox_stds[3] proposals = bbox_pred(anchors, bbox_deltas) proposals = clip_boxes(proposals, im_info[:2]) if stride == 4 and decay4 < 1.0: scores *= decay4 scores_ravel = scores.ravel() order = np.where(scores_ravel >= threshold)[0] proposals = proposals[order, :] scores = scores[order] if flip: oldx1 = proposals[:, 0].copy() oldx2 = proposals[:, 2].copy() proposals[:, 0] = im.shape[1] - oldx2 - 1 proposals[:, 2] = im.shape[1] - oldx1 - 1 #proposals[:,0:4] /= im_scale #print(proposals[:,0]) proposals[:, 0] /= im_scale[0] #print(pp) proposals[:, 1] /= im_scale[1] proposals[:, 2] /= im_scale[0] proposals[:, 3] /= im_scale[1] #print(proposals[:,0]) proposals_list.append(proposals) scores_list.append(scores) if nms_threshold < 0.0: _strides = np.empty(shape=(scores.shape), dtype=np.float32) _strides.fill(stride) strides_list.append(_strides) if not vote and use_landmarks: landmark_deltas = net_out[sym_idx + 2] #.asnumpy() # print(landmark_deltas) landmark_pred_len = landmark_deltas.shape[1] // A landmark_deltas = landmark_deltas.transpose((0, 2, 3, 1)).reshape( (-1, 5, landmark_pred_len // 5)) landmark_deltas *= landmark_std landmarks = landmark_pred(anchors, landmark_deltas) landmarks = landmarks[order, :] if flip: landmarks[:, :, 0] = im.shape[1] - landmarks[:, :, 0] - 1 order = [1, 0, 2, 4, 3] flandmarks = landmarks.copy() for idx, a in enumerate(order): flandmarks[:, idx, :] = landmarks[:, a, :] landmarks = flandmarks landmarks[:, :, 0:2] /= im_scale landmarks_list.append(landmarks) if use_landmarks: sym_idx += 3 else: sym_idx += 2 proposals = np.vstack(proposals_list) landmarks = None if proposals.shape[0] == 0: if use_landmarks: landmarks = np.zeros((0, 5, 2)) if nms_threshold < 0.0: return np.zeros((0, 6)), landmarks else: return np.zeros((0, 5)), landmarks scores = np.vstack(scores_list) scores_ravel = scores.ravel() order = scores_ravel.argsort()[::-1] proposals = proposals[order, :] scores = scores[order] if nms_threshold < 0.0: strides = np.vstack(strides_list) strides = strides[order] if not vote and use_landmarks: landmarks = np.vstack(landmarks_list) landmarks = landmarks[order].astype(np.float32, copy=False) if nms_threshold > 0.0: pre_det = np.hstack((proposals[:, 0:4], scores)).astype(np.float32, copy=False) if not vote: keep = nms(pre_det) det = np.hstack((pre_det, proposals[:, 4:])) det = det[keep, :] if use_landmarks: landmarks = landmarks[keep] else: det = np.hstack((pre_det, proposals[:, 4:])) det = bbox_vote(det, nms_threshold) elif nms_threshold < 0.0: det = np.hstack((proposals[:, 0:4], scores, strides)).astype(np.float32, copy=False) else: det = np.hstack((proposals[:, 0:4], scores)).astype(np.float32, copy=False) return det, landmarks
def __init__(self, network, gpu_id=0, nms=0.3, nocrop=False, decay4=0.5, vote=False): self.network = network self.gpu_id = gpu_id self.decay4 = decay4 self.nms_threshold = nms self.vote = vote self.nocrop = nocrop self.debug = False self.fpn_keys = [] self.anchor_cfg = None self.use_landmarks = True self.sess = None self.net = None # pixel_means=[0.0, 0.0, 0.0] pixel_means = config.PIXEL_MEANS pixel_stds = [1.0, 1.0, 1.0] pixel_scale = 1.0 self.preprocess = False dense_anchor = False _ratio = (1., ) image_size = (640, 640) self._feat_stride_fpn = config.RPN_FEAT_STRIDE self.anchor_cfg = config.RPN_ANCHOR_CFG for s in self._feat_stride_fpn: self.fpn_keys.append('stride%s' % s) self._anchors_fpn = dict( zip( self.fpn_keys, generate_anchors_fpn(dense_anchor=dense_anchor, cfg=self.anchor_cfg))) for k in self._anchors_fpn: v = self._anchors_fpn[k].astype(np.float32) self._anchors_fpn[k] = v ''' {'stride32': array([[-248., -248., 263., 263.],[-120., -120., 135., 135.]], dtype=float32), 'stride16': array([[-56., -56., 71., 71.],[-24., -24., 39., 39.]], dtype=float32), 'stride8': array([[-8., -8., 23., 23.],[ 0., 0., 15., 15.]], dtype=float32)} ''' # print('Retinaface: self.anchor_fpn=', self._anchors_fpn) self._num_anchors = dict( zip(self.fpn_keys, [anchors.shape[0] for anchors in self._anchors_fpn.values()])) #{'stride32': 2, 'stride16': 2, 'stride8': 2} # print('Retinaface: self._num_anchors=', self._num_anchors) if self.gpu_id >= 0: self.nms = gpu_nms_wrapper(self.nms_threshold, self.gpu_id) else: self.nms = cpu_nms_wrapper(self.nms_threshold) self.pixel_means = np.array(pixel_means, dtype=np.float32) self.pixel_stds = np.array(pixel_stds, dtype=np.float32) self.pixel_scale = float(pixel_scale) self.build_net()
def __init__(self, prefix, epoch, ctx_id=0, network='net3', nms=0.4, nocrop=False, decay4=0.5, vote=False): self.ctx_id = ctx_id self.network = network self.decay4 = decay4 self.nms_threshold = nms self.vote = vote self.nocrop = nocrop self.debug = False self.fpn_keys = [] self.anchor_cfg = None pixel_means = [0.0, 0.0, 0.0] pixel_stds = [1.0, 1.0, 1.0] pixel_scale = 1.0 self.preprocess = False _ratio = (1., ) fmc = 3 if fmc == 3: self._feat_stride_fpn = [32, 16, 8] self.anchor_cfg = { '32': { 'SCALES': (32, 16), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '16': { 'SCALES': (8, 4), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, '8': { 'SCALES': (2, 1), 'BASE_SIZE': 16, 'RATIOS': _ratio, 'ALLOWED_BORDER': 9999 }, } if self.debug: print(self._feat_stride_fpn, self.anchor_cfg) for s in self._feat_stride_fpn: self.fpn_keys.append('stride%s' % s) dense_anchor = False self._anchors_fpn = dict( zip( self.fpn_keys, generate_anchors_fpn(dense_anchor=dense_anchor, cfg=self.anchor_cfg))) for k in self._anchors_fpn: v = self._anchors_fpn[k].astype(np.float32) self._anchors_fpn[k] = v self._num_anchors = dict( zip(self.fpn_keys, [anchors.shape[0] for anchors in self._anchors_fpn.values()])) sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch) if self.ctx_id >= 0: self.ctx = mx.gpu(self.ctx_id) self.nms = gpu_nms_wrapper(self.nms_threshold, self.ctx_id) else: self.ctx = mx.cpu() self.nms = cpu_nms_wrapper(self.nms_threshold) self.pixel_means = np.array(pixel_means, dtype=np.float32) self.pixel_stds = np.array(pixel_stds, dtype=np.float32) self.pixel_scale = float(pixel_scale) if self.debug: print('means', self.pixel_means) self.use_landmarks = False if len(sym) // len(self._feat_stride_fpn) == 3: self.use_landmarks = True if self.debug: print('use_landmarks', self.use_landmarks) c = len(sym) // len(self._feat_stride_fpn) sym = sym[(c * 0):] self._feat_stride_fpn = [32, 16, 8] print('sym size:', len(sym)) image_size = (640, 640) self.model = mx.mod.Module(symbol=sym, context=self.ctx, label_names=None) self.model.bind(data_shapes=[('data', (1, 3, image_size[0], image_size[1]))], for_training=False) self.model.set_params(arg_params, aux_params)