def forward(self, features, batched_inputs): normalizer_fn, normalizer_params = odt.get_norm( self.cfg.NORM, self.is_training) activation_fn = odt.get_activation_fn(self.cfg.ACTIVATION_FN) with tf.variable_scope("FusionBackboneHookV2"): del batched_inputs end_points = list(features.items()) k0, v0 = end_points[0] mfeatures = [] shape0 = wmlt.combined_static_and_dynamic_shape(v0) for k, v in end_points[1:]: net = tf.image.resize_bilinear(v, shape0[1:3]) mfeatures.append(net) net = tf.add_n(mfeatures) / float(len(mfeatures)) ''' 与v2相比,使用sum代替concat ''' net = v0 + net level0 = int(k0[1:]) net = slim.conv2d(net, v0.get_shape().as_list()[-1], [3, 3], activation_fn=activation_fn, normalizer_fn=normalizer_fn, normalizer_params=normalizer_params, scope=f"smooth{level0}") res = features res[f'F{level0}'] = net return res
def __init__(self, cfg, is_mini=False, **kwargs): super().__init__(cfg, **kwargs) self.normalizer_fn, self.norm_params = odt.get_norm( self.cfg.MODEL.HRNET.NORM, self.is_training) self.activation_fn = odt.get_activation_fn( self.cfg.MODEL.HRNET.ACTIVATION_FN) self.is_mini = is_mini
def forward(self, features, batched_inputs): features = self.bh(features, batched_inputs) del batched_inputs res = OrderedDict() featuremap_keys = ["P3", "P4", "P5", "P6", "P7"] anchor_sizes = global_cfg.MODEL.ANCHOR_GENERATOR.SIZES anchor_ratios = global_cfg.MODEL.ANCHOR_GENERATOR.ASPECT_RATIOS normalizer_fn, normalizer_params = odt.get_norm( "evo_norm_s0", is_training=self.is_training) ref = features[featuremap_keys[1]] ref_shape = wmlt.combined_static_and_dynamic_shape(ref)[1:3] ref_size = anchor_sizes[1][0] nr = 0 with tf.name_scope("MakeAnchorsForRetinaNet"): for i, k in enumerate(featuremap_keys): net = features[k] for j, s in enumerate(anchor_sizes[i]): for k, r in enumerate(anchor_ratios[i][j]): net = slim.separable_conv2d( net, 32, kernel_size=3, padding="SAME", depth_multiplier=1, normalizer_fn=normalizer_fn, normalizer_params=normalizer_params, scope=f"sep_conv_{i}{j}{k}") target_shape = self.get_shape(ref_shape, ref_size, s, r) net = tf.image.resize_nearest_neighbor( net, target_shape) res[f"P{nr}"] = net nr += 1 return res
def __init__(self, out_channels, cfg, *args, **kwargs): super().__init__(cfg=cfg, *args, **kwargs) self.normalizer_fn, self.norm_params = odt.get_norm( self.cfg.MODEL.FPN.NORM, self.is_training) self.activation_fn = odt.get_activation_fn( self.cfg.MODEL.FPN.ACTIVATION_FN) self.out_channels = out_channels
def forward(self, features, batched_inputs): del batched_inputs res = OrderedDict() normalizer_fn, normalizer_params = odt.get_norm( "evo_norm_s0", is_training=self.is_training) normalizer_params['G'] = 8 with tf.variable_scope("NonLocalBackboneHookV3"): for k, v in features.items(): if k[0] not in ["C", "P"]: continue level = int(k[1:]) if level <= 2: res[k] = v continue h = self.base_size // (2**level) w = self.base_size // (2**level) v = wnnl.non_local_blockv4(v, inner_dims=[128, 128, 128], normalizer_fn=normalizer_fn, normalizer_params=normalizer_params, n_head=2, activation_fn=None, weighed_sum=False, scope=f"non_localv4_{level}", size=[h, w]) res[k] = v return res
def forward(self, features, batched_inputs): normalizer_fn, normalizer_params = odt.get_norm( "evo_norm_s0", is_training=self.is_training) res = [] with tf.variable_scope("BalanceBackboneHook"): del batched_inputs ref_index = 1 end_points = list(features) v0 = end_points[ref_index] mfeatures = [] with tf.name_scope("fusion"): shape0 = wmlt.combined_static_and_dynamic_shape(v0) for i, v in enumerate(end_points): if i == ref_index: net = v else: net = tf.image.resize_bilinear(v, shape0[1:3], name=f"resize{i}") mfeatures.append(net) net = tf.add_n(mfeatures) / float(len(mfeatures)) net = slim.conv2d(net, net.get_shape().as_list()[-1], [3, 3], activation_fn=None, normalizer_fn=normalizer_fn, normalizer_params=normalizer_params, scope=f"smooth") for i, v in enumerate(end_points): with tf.name_scope(f"merge{i}"): shape = wmlt.combined_static_and_dynamic_shape(v) v0 = tf.image.resize_bilinear(net, shape[1:3]) res.append(v + v0) return res
def __init__(self, cfg, parent, *args, **kwargs): ''' :param cfg: only the child part :param parent: :param args: :param kwargs: ''' super().__init__(cfg, *args, parent=parent, **kwargs) # Detectron2默认没有使用normalizer, 但在测试数据集上发现不使用normalizer网络不收敛 self.normalizer_fn, self.norm_params = odtk.get_norm( self.cfg.NORM, is_training=self.is_training) self.activation_fn = odtk.get_activation_fn(self.cfg.ACTIVATION_FN) self.norm_scope_name = odtk.get_norm_scope_name(self.cfg.NORM) '''self.left_pool = tfop.left_pool self.right_pool = tfop.right_pool self.bottom_pool = tfop.bottom_pool self.top_pool = tfop.top_pool''' '''self.left_pool = partial(wnnl.cnn_self_hattenation,scope="left_pool") self.right_pool = partial(wnnl.cnn_self_hattenation,scope="right_pool") self.bottom_pool = partial(wnnl.cnn_self_vattenation,scope="bottom_pool") self.top_pool = partial(wnnl.cnn_self_vattenation,scope="top_pool")''' self.left_pool = left_pool self.right_pool = right_pool self.bottom_pool = bottom_pool self.top_pool = top_pool
def __init__(self, cfg, *args, **kwargs): super().__init__(cfg, *args, **kwargs) self.out_channels = self.cfg.MODEL.SHUFFLENETS.OUT_CHANNELS self.normalizer_fn, self.norm_params = odt.get_norm( self.cfg.MODEL.SHUFFLENETS.NORM, self.is_training) self.activation_fn = odt.get_activation_fn( self.cfg.MODEL.SHUFFLENETS.ACTIVATION_FN)
def __init__(self,cfg,*args,**kwargs): if cfg.MODEL.PREPROCESS != "subimagenetmean": print("--------------------WARNING--------------------") print(f"Preprocess for resnet should be subimagenetmean not {cfg.MODEL.PREPROCESS}.") print("------------------END WARNING------------------") super().__init__(cfg,*args,**kwargs) self.normalizer_fn, self.norm_params = odt.get_norm(self.cfg.MODEL.RESNETS.NORM, self.is_training) self.activation_fn = odt.get_activation_fn(self.cfg.MODEL.RESNETS.ACTIVATION_FN) self.out_channels = cfg.MODEL.RESNETS.OUT_CHANNELS self.scope_name = "50"
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.rcnn_anchor_boxes = tf.reshape( tf.convert_to_tensor(self.cfg.MODEL.ANCHOR_GENERATOR.SIZES, dtype=tf.float32), [1, -1]) self.normalizer_fn, self.norm_params = odt.get_norm( self.cfg.MODEL.ROI_BOX_HEAD.NORM, self.is_training) self.activation_fn = odt.get_activation_fn( self.cfg.MODEL.ROI_BOX_HEAD.ACTIVATION_FN) self.norm_scope_name = odt.get_norm_scope_name( self.cfg.MODEL.ROI_BOX_HEAD.NORM)
def __init__(self, cfg, **kwargs): """ The following attributes are parsed from config: num_conv: the number of conv layers conv_dim: the dimension of the conv layers norm: normalization for the conv layers """ super(HighResolutionMaskHead, self).__init__(cfg, **kwargs) self.normalizer_fn, self.norm_params = odt.get_norm( self.cfg.MODEL.ROI_MASK_HEAD.NORM, self.is_training) self.activation_fn = odt.get_activation_fn( self.cfg.MODEL.ROI_MASK_HEAD.ACTIVATION_FN)
def __init__(self, cfg, parent, *args, **kwargs): ''' :param cfg: only child part :param parent: :param args: :param kwargs: ''' super().__init__(cfg, parent=parent, *args, **kwargs) self.normalizer_fn, self.norm_params = odtk.get_norm( self.cfg.NORM, is_training=self.is_training) self.activation_fn = odtk.get_activation_fn(self.cfg.ACTIVATION_FN)
def __init__(self, cfg, bottom_up, in_features, out_channels, fuse_type="sum", parent=None, *args, **kwargs): """ Args: bottom_up (Backbone): module representing the bottom up subnetwork. Must be a subclass of :class:`Backbone`. The multi-scale feature maps generated by the bottom up network, and listed in `in_features`, are used to generate WeightedFPN levels. in_features (list[str]): names of the input feature maps coming from the backbone to which WeightedFPN is attached. For example, if the backbone produces ["res2", "res3", "res4"], any *contiguous* sublist of these may be used; order must be from high to low resolution. out_channels (int): number of channels in the output feature maps. norm (str): the normalization to use. fuse_type (str): types for fusing the top down features and the lateral ones. It can be "sum" (default), which sums up element-wise; or "avg", which takes the element-wise mean of the two. """ stage = int(in_features[-1][1:]) super(WeightedFPN, self).__init__(cfg, parent=parent, *args, **kwargs) assert isinstance(bottom_up, Backbone) # Place convs into top-down order (from low to high resolution) # to make the top-down computation in forward clearer. self.in_features = in_features self.bottom_up = bottom_up self.out_channels = out_channels self._fuse_type = fuse_type self.scope = "WeightedFPN" self.use_depthwise = False self.interpolate_op = tf.image.resize_nearest_neighbor self.stage = stage #Detectron2默认没有使用normalizer, 但在测试数据集上发现不使用normalizer网络不收敛 self.normalizer_fn, self.normalizer_params = odt.get_norm( self.cfg.MODEL.TWOWAYFPN.NORM, self.is_training) self.hook0_before, self.hook0_after = build_backbone_hook_by_name( cfg.MODEL.TWOWAYFPN.BACKBONE_HOOK, cfg, parent=self) if len(cfg.MODEL.TWOWAYFPN.BACKBONE_HOOK) >= 4: self.hook1_before, self.hook1_after = build_backbone_hook_by_name( cfg.MODEL.TWOWAYFPN.BACKBONE_HOOK[2:], cfg, parent=self) else: self.hook1_before, self.hook1_after = build_backbone_hook_by_name( ["", ""], cfg, parent=self) self.activation_fn = odt.get_activation_fn( self.cfg.MODEL.TWOWAYFPN.ACTIVATION_FN)
def __init__(self, cfg, parent, *args, **kwargs): ''' :param cfg: only the child part :param parent: :param args: :param kwargs: ''' super().__init__(cfg, *args, parent=parent, **kwargs) self.normalizer_fn, self.norm_params = odtk.get_norm( self.cfg.NORM, is_training=self.is_training) self.activation_fn = odtk.get_activation_fn(self.cfg.ACTIVATION_FN) self.norm_scope_name = odtk.get_norm_scope_name(self.cfg.NORM) self.head_conv_dim = self.cfg.HEAD_CONV_DIM
def __init__(self, cfg, **kwargs): """ The following attributes are parsed from config: num_conv: the number of conv layers conv_dim: the dimension of the conv layers norm: normalization for the conv layers """ super(MaskRCNNConvUpsampleHead, self).__init__(cfg, **kwargs) #Detectron2默认没有使用normalizer, 使用测试数据发现是否使用normalizer并没有什么影响 self.normalizer_fn, self.norm_params = odt.get_norm( self.cfg.MODEL.ROI_MASK_HEAD.NORM, self.is_training) self.activation_fn = odt.get_activation_fn( self.cfg.MODEL.ROI_MASK_HEAD.ACTIVATION_FN)
def __init__( self, cfg,bottom_up, in_features, out_channels, top_block=None, fuse_type="sum", parent=None,*args,**kwargs ): """ Args: bottom_up (Backbone): module representing the bottom up subnetwork. Must be a subclass of :class:`Backbone`. The multi-scale feature maps generated by the bottom up network, and listed in `in_features`, are used to generate FPN levels. in_features (list[str]): names of the input feature maps coming from the backbone to which FPN is attached. For example, if the backbone produces ["res2", "res3", "res4"], any *contiguous* sublist of these may be used; order must be from high to low resolution. out_channels (int): number of channels in the output feature maps. norm (str): the normalization to use. top_block (nn.Module or None): if provided, an extra operation will be performed on the output of the last (smallest resolution) FPN output, and the result will extend the result list. The top_block further downsamples the feature map. It must have an attribute "num_levels", meaning the number of extra FPN levels added by this block, and "in_feature", which is a string representing its input feature (e.g., p5). fuse_type (str): types for fusing the top down features and the lateral ones. It can be "sum" (default), which sums up element-wise; or "avg", which takes the element-wise mean of the two. """ stage = int(in_features[-1][-1:]) super(FPNV2, self).__init__(cfg,parent=parent,*args,**kwargs) assert isinstance(bottom_up, Backbone) def get_feature_name(x): p = x.find(":") if p<0: return x else: return x[:p] # Place convs into top-down order (from low to high resolution) # to make the top-down computation in forward clearer. self.top_block = top_block self.in_features = [get_feature_name(x) for x in in_features] self.bottom_up = bottom_up self.out_channels = out_channels self._fuse_type = fuse_type self.scope = "FPN" self.use_depthwise = self.cfg.MODEL.FPN.USE_DEPTHWISE self.interpolate_op=tf.image.resize_nearest_neighbor self.stage = stage #Detectron2默认没有使用normalizer, 但在测试数据集上发现不使用normalizer网络不收敛 self.normalizer_fn,self.norm_params = odt.get_norm(self.cfg.MODEL.FPN.NORM,self.is_training) self.hook_before,self.hook_after = build_backbone_hook(cfg.MODEL.FPN,parent=self) self.activation_fn = odt.get_activation_fn(self.cfg.MODEL.FPN.ACTIVATION_FN)
def __init__(self, num_keypoints, cfg, parent, *args, **kwargs): ''' :param cfg: only the child part :param parent: :param args: :param kwargs: ''' super().__init__(cfg, *args, parent=parent, **kwargs) self.num_keypoints = num_keypoints self.normalizer_fn, self.norm_params = odtk.get_norm( self.cfg.NORM, is_training=self.is_training) self.activation_fn = odtk.get_activation_fn(self.cfg.ACTIVATION_FN) self.norm_scope_name = odtk.get_norm_scope_name(self.cfg.NORM) self.pred_paf_maps_outputs = [] self.pred_conf_maps_outputs = []
def __init__( self, cfg,bottom_up, in_features, parent=None,*args,**kwargs ): """ Args: bottom_up (Backbone): module representing the bottom up subnetwork. Must be a subclass of :class:`Backbone`. The multi-scale feature maps generated by the bottom up network, and listed in `in_features`, are used to generate DLA levels. in_features (list[str]): names of the input feature maps coming from the backbone to which DLA is attached. For example, if the backbone produces ["res2", "res3", "res4"], any *contiguous* sublist of these may be used; order must be from high to low resolution. norm (str): the normalization to use. """ stage = int(in_features[-1][-1:]) super(DLA, self).__init__(cfg,parent=parent,*args,**kwargs) assert isinstance(bottom_up, Backbone) def get_feature_name(x): p = x.find(":") if p<0: return x else: return x[:p] # Place convs into top-down order (from low to high resolution) # to make the top-down computation in forward clearer. self.in_features = [get_feature_name(x) for x in in_features] self.bottom_up = bottom_up self.scope = "DLA" self.interpolate_op=tf.image.resize_nearest_neighbor self.stage = stage #Detectron2默认没有使用normalizer, 但在测试数据集上发现不使用normalizer网络不收敛 self.normalizer_fn,self.norm_params = odt.get_norm(self.cfg.MODEL.DLA.NORM,self.is_training) self.hook_before,self.hook_after = build_backbone_hook(cfg.MODEL.DLA,parent=self) self.activation_fn = odt.get_activation_fn(self.cfg.MODEL.DLA.ACTIVATION_FN) self.out_channels = [ 64,64,128,256] self.conv_op = functools.partial(slim.conv2d,normalizer_fn=self.normalizer_fn, activation_fn=self.activation_fn, normalizer_params=self.norm_params) self.upsample_op = functools.partial(slim.conv2d_transpose, kernel_size=4, stride=2, activation_fn=self.activation_fn, normalizer_fn=self.normalizer_fn, normalizer_params=self.norm_params)
def __init__(self, cfg, parent, *args, **kwargs): super().__init__(cfg, parent, *args, **kwargs) self.anchor_generator = build_anchor_generator(cfg, parent=self, *args, **kwargs) num_cell_anchors = self.anchor_generator.num_cell_anchors assert len(set(num_cell_anchors) ) == 1, "all levers cell anchors num must be equal." self.num_cell_anchors = num_cell_anchors[0] self.box_dim = self.anchor_generator.box_dim self.normalizer_fn, self.norm_params = odtk.get_norm( self.cfg.MODEL.RPN.NORM, is_training=self.is_training) self.activation_fn = odtk.get_activation_fn( self.cfg.MODEL.RPN.ACTIVATION_FN) self.hook = build_hook_by_name(self.cfg.MODEL.RPN.HOOK, self.cfg, parent=self)
def forward(self, features, batched_inputs): normalizer_fn, normalizer_params = odt.get_norm( "evo_norm_s0", is_training=self.is_training) res = OrderedDict() with tf.variable_scope("BalanceNonLocalBackboneHook"): del batched_inputs ref_index = 1 end_points = list(features.items()) k0, v0 = end_points[ref_index] mfeatures = [] with tf.name_scope("fusion"): shape0 = wmlt.combined_static_and_dynamic_shape(v0) for i, (k, v) in enumerate(end_points): if i == ref_index: net = v else: net = tf.image.resize_bilinear(v, shape0[1:3], name=f"resize{i}") mfeatures.append(net) net = tf.add_n(mfeatures) / float(len(mfeatures)) net = slim.conv2d(net, net.get_shape().as_list()[-1], [3, 3], activation_fn=None, normalizer_fn=normalizer_fn, normalizer_params=normalizer_params, scope=f"smooth") for i, (k, v) in enumerate(end_points): with tf.variable_scope(f"merge{i}"): shape = wmlt.combined_static_and_dynamic_shape(v) v0 = tf.image.resize_bilinear(net, shape[1:3]) net = v + v0 if i > 0: net = wnnl.non_local_blockv1( net, inner_dims_multiplier=[1, 1, 1], normalizer_fn=normalizer_fn, normalizer_params=normalizer_params, activation_fn=None, weighed_sum=False) res[k] = net return res
def forward(self, features, batched_inputs): del batched_inputs res = OrderedDict() with tf.variable_scope("DeformConvBackboneHook"): normalizer_fn, normalizer_params = odt.get_norm( "BN", is_training=self.is_training) for k, v in features.items(): if k[0] not in ["C", "P"]: continue level = int(k[1:]) channel = v.get_shape().as_list()[-1] res[k] = wnnl.deform_conv2dv2( v, num_outputs=channel, kernel_size=3, scope=f"deform_conv2d{level}", normalizer_fn=normalizer_fn, normalizer_params=normalizer_params) return res
def forward(self, features, batched_inputs): low_features = self.parent.low_features normalizer_fn, normalizer_params = odt.get_norm( "evo_norm_s0", is_training=self.is_training) res = OrderedDict() with tf.variable_scope("BalanceBackboneHookV2"): del batched_inputs ref_index = 1 end_points = list(features.items()) k0, v0 = end_points[ref_index] mfeatures = [] with tf.name_scope("fusion"): shape0 = wmlt.combined_static_and_dynamic_shape(v0) for i, (k, v) in enumerate(end_points): if i == ref_index: net = v else: net = tf.image.resize_bilinear(v, shape0[1:3], name=f"resize{i}") mfeatures.append(net) net = tf.add_n(mfeatures) / float(len(mfeatures)) net = slim.conv2d(net, net.get_shape().as_list()[-1], [3, 3], activation_fn=None, normalizer_fn=normalizer_fn, normalizer_params=normalizer_params, scope=f"smooth") for i, (k, v) in enumerate(end_points): with tf.name_scope(f"smooth_low_feature{i}"): index = int(k[1:]) low_feature = low_features[f"C{index}"] channel = v.get_shape().as_list()[-1] low_feature = slim.conv2d(low_feature, channel, [1, 1], activation_fn=None, normalizer_fn=None) with tf.name_scope(f"merge{i}"): shape = wmlt.combined_static_and_dynamic_shape(v) v0 = tf.image.resize_bilinear(net, shape[1:3]) res[k] = tf.concat([v + v0, low_feature], axis=-1) return res
def __init__(self, num_anchors, cfg, parent, *args, **kwargs): ''' :param num_anchors: :param cfg: only the child part :param parent: :param args: :param kwargs: ''' super().__init__(cfg, *args, parent=parent, **kwargs) assert ( len(set(num_anchors)) == 1 ), "Using different number of anchors between levels is not currently supported!" self.num_anchors = num_anchors[0] self.normalizer_fn, self.norm_params = odtk.get_norm( self.cfg.NORM, is_training=self.is_training) self.activation_fn = odtk.get_activation_fn(self.cfg.ACTIVATION_FN) self.norm_scope_name = odtk.get_norm_scope_name(self.cfg.NORM) self.logits_pre_outputs = [] self.bbox_reg_pre_outputs = []
def forward(self, features, batched_inputs): del batched_inputs res = OrderedDict() normalizer_fn, normalizer_params = odt.get_norm( "evo_norm_s0", is_training=self.is_training) with tf.variable_scope("NonLocalBackboneHook"): for k, v in features.items(): if k[0] not in ["C", "P"]: continue level = int(k[1:]) if level <= 3: res[k] = v continue res[k] = wnnl.non_local_blockv1( v, inner_dims_multiplier=[1, 1, 1], normalizer_fn=normalizer_fn, normalizer_params=normalizer_params, activation_fn=None, weighed_sum=False) return res
def __init__(self, cfg, bottom_up, in_features, out_channels, parent=None, *args, **kwargs): """ Args: bottom_up (Backbone): module representing the bottom up subnetwork. Must be a subclass of :class:`Backbone`. The multi-scale feature maps generated by the bottom up network, and listed in `in_features`, are used to generate BIFPN levels. in_features (list[str]): names of the input feature maps coming from the backbone to which BIFPN is attached. For example, if the backbone produces ["res2", "res3", "res4"], any *contiguous* sublist of these may be used; order must be from high to low resolution. out_channels (int): number of channels in the output feature maps. """ stage = int(in_features[-1][1:]) super(BIFPN, self).__init__(cfg, parent=parent, *args, **kwargs) assert isinstance(bottom_up, Backbone) # Place convs into top-down order (from low to high resolution) # to make the top-down computation in forward clearer. self.in_features = in_features self.bottom_up = bottom_up self.out_channels = out_channels self.scope = "BIFPN" self.use_depthwise = False self.interpolate_op = tf.image.resize_nearest_neighbor self.stage = stage self.normalizer_fn, self.norm_params = odt.get_norm( self.cfg.MODEL.BIFPN.NORM, self.is_training) self.hook_before, self.hook_after = build_backbone_hook( cfg.MODEL.BIFPN, parent=self) self.activation_fn = odt.get_activation_fn( self.cfg.MODEL.BIFPN.ACTIVATION_FN)
def __init__(self, cfg, parent, *args, **kwargs): super().__init__(cfg, parent, *args, **kwargs) self.normalizer_fn, self.norm_params = odt.get_norm( self.cfg.MODEL.ROI_BOX_HEAD.NORM, self.is_training) self.activation_fn = odt.get_activation_fn( self.cfg.MODEL.ROI_BOX_HEAD.ACTIVATION_FN)