def __init__(self, inp, hidden_dim, oup, kernel_size, stride, use_se=False, use_hs=False, momentum=0.1): """Init InvertedResidualSE.""" super(InvertedResidualSE, self).__init__() self.identity = stride == 1 and inp == oup self.ir_block = Sequential( # pw ops.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False), ops.BatchNorm2d(hidden_dim, momentum=momentum), ops.Hswish() if use_hs else ops.Relu(inplace=True), # dw ops.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim, bias=False), ops.BatchNorm2d(hidden_dim, momentum=momentum), # Squeeze-and-Excite SELayer(hidden_dim) if use_se else Sequential(), ops.Hswish() if use_hs else ops.Relu(inplace=True), # pw-linear ops.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False), ops.BatchNorm2d(oup, momentum=momentum), )
def transform_model(model): """Transform the torch model to Vega model.""" new_model_dict = OrderedDict() for name, module in model.name_cells().items(): if isinstance(module, atom_op): if isinstance(module, nn.Flatten): new_model_dict["mean"] = ops.AdaptiveAvgPool2d() new_model_dict[name] = _transform_op(module) sub_modules = OrderedDict() if isinstance(module, nn.SequentialCell): for sub_name, sub_module in module.name_cells().items(): if isinstance(sub_module, atom_block): sub_modules[sub_name] = _transform_block(sub_module) sub_modules[sub_name].update_parameters_name(name + "." + sub_name + ".") if isinstance(sub_module, atom_op): sub_modules[sub_name] = _transform_op(sub_module) sub_modules[sub_name].update_parameters_name(name + "." + sub_name + ".") new_model_dict[name] = Sequential(sub_modules) model = Sequential(new_model_dict) desc = model.to_desc() vega_model = NetworkDesc(desc).to_model() return vega_model
def __init__(self, cfgs, mode='small', input_channel=3, feat_channels=16, special_stride=1, num_classes=10, width_mult=1., block=InvertedResidualSE, momentum=0.1, is_prune_mode=False, **kwargs): """Init MobileNetV3. :params cfgs: cfgs for mobilenetv3 :type cfgs: list :params special_stride: the stride of the first InvertedResidualSE block. :type special_stride: int (1 for cifar10, 2 for imagenet) """ super(MobileNetV3, self).__init__() self.cfgs = cfgs # building first layer if not is_prune_mode: feat_channels = _make_divisible(feat_channels * width_mult, 8) else: feat_channels = int(feat_channels * width_mult) layers = [ ConvBnAct(input_channel, feat_channels, kernel_size=3, momentum=momentum, stride=special_stride, padding=1, activation='hswish') ] # buidling blocks # kernel_size, expand_ratio, output_channels, use_se, use_hs, stride for k, t, c, use_se, use_hs, s in self.cfgs: output_channel = _make_divisible( c * width_mult, 8) if not is_prune_mode else int(c * width_mult) hidden_dim = _make_divisible(t, 8) if not is_prune_mode else t layers.append( block(feat_channels, hidden_dim, output_channel, k, s, use_se, use_hs, momentum)) feat_channels = output_channel self.features = Sequential(*layers) # building last linear layer self.avgpool = ops.AdaptiveAvgPool2d((1, 1)) chn = 1280 if mode == 'large' else 1024 self.classifier = Sequential(ops.View(), ops.Linear(feat_channels, chn), ops.Hswish(), ops.Dropout(0.2), ops.Linear(chn, num_classes)) self._initialize_weights()
def __init__(self, encoding, n_class=1000): super(DNet, self).__init__() op_names = ["conv3", "conv1", "conv3_grp2", "conv3_grp4", "conv3_base1", "conv3_base32", "conv3_sep"] block_str, num_channel, macro_str = encoding.split('_') curr_channel, index = int(num_channel), 0 _big_model = "*" in block_str if _big_model: block_encoding_list = block_str.split('*') # stem self.layers = Sequential( create_op('conv3', 3, curr_channel // 2, stride=2), ops.Relu(), create_op('conv3', curr_channel // 2, curr_channel // 2), ops.Relu(), create_op('conv3', curr_channel // 2, curr_channel, stride=2), ops.Relu() ) # body if not _big_model: while index < len(macro_str): stride = 1 if macro_str[index] == '-': stride = 2 index += 1 channel_increase = int(macro_str[index]) block = EncodedBlock(block_str, curr_channel, op_names, stride, channel_increase) self.layers.append(block) curr_channel *= channel_increase index += 1 else: block_encoding_index = 0 while index < len(macro_str): stride = 1 if macro_str[index] == '-': stride = 2 index += 1 block_encoding_index += 1 channel_increase = int(macro_str[index]) block_encoding = block_encoding_list[block_encoding_index] block = EncodedBlock(block_encoding, curr_channel, op_names, stride, channel_increase) self.layers.append(block) curr_channel *= channel_increase index += 1 self.layers.append(ops.AdaptiveAvgPool2d((1, 1))) self.view = ops.View() self.fc = ops.Linear(in_features=curr_channel, out_features=n_class)
def __init__(self, in_chnls, cardinality, group_depth, stride): super(ResNeXt_Block, self).__init__() self.group_chnls = cardinality * group_depth self.conv1 = BN_Conv2d(in_chnls, self.group_chnls, 1, stride=1, padding=0) self.conv2 = BN_Conv2d(self.group_chnls, self.group_chnls, 3, stride=stride, padding=1, groups=cardinality) self.conv3 = ops.Conv2d(self.group_chnls, self.group_chnls * 2, 1, stride=1, padding=0) self.bn = ops.BatchNorm2d(self.group_chnls * 2) if stride != 1 or in_chnls != self.group_chnls * 2: self.short_cut = Sequential( ops.Conv2d(in_chnls, self.group_chnls * 2, 1, stride, bias=False), ops.BatchNorm2d(self.group_chnls * 2)) else: self.short_cut = None
def make_res_layer_from_code(block, inplanes, planes, blocks, stride=1, dilation=1, style='pytorch', with_cp=False, code=None): """Make res layer from code.""" if code is None: return make_res_layer(block, inplanes, planes, blocks, stride, dilation, style, with_cp) strides = map(int, code) layers = [] for stride in strides: layers.append( block(inplanes=inplanes, planes=planes, stride=stride, dilation=dilation, style=style, with_cp=with_cp)) inplanes = planes * block.expansion return Sequential(*layers)
def __init__(self, code='111-2111-211111-211', block='BottleneckBlock', in_channels=64, weight_file=None, out_layers=None): """Init SerialBackbone.""" super(SerialBackbone, self).__init__() self.inplanes = in_channels self.planes = self.inplanes self.weight_file = weight_file self.channels = [3] self.code = code.split('-') self.block = ClassFactory.get_cls(ClassType.NETWORK, block) self._make_stem_layer() self.layers = Sequential() if out_layers == -1 else OutDictSequential() self.make_cells()
def __init__(self, in_channels, out_channels, kernel_size, stride, padding, dilation=1, groups=1, bias=False): super(BN_Conv2d, self).__init__() self.seq = Sequential( ops.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, dilation=dilation, groups=groups, bias=bias), ops.BatchNorm2d(out_channels), ops.Relu() )
def __init__(self, channel, reduction=4): """Init SELayer.""" super(SELayer, self).__init__() self.avg_pool = ops.AdaptiveAvgPool2d(1) hidden_dim = _make_divisible(channel // reduction, 8) self.fc = Sequential(ops.Linear(channel, hidden_dim, use_bias=False), ops.Relu(inplace=True), ops.Linear(hidden_dim, channel, use_bias=False), ops.Hsigmoid())
def _make_layers(self, inplanes, d, blocks, stride, code): """Make layer.""" strides = map(int, code) layers = [] for stride in strides: layers.append(ResNeXt_Block( inplanes, self.cardinality, d, stride)) inplanes = self.cardinality * d * 2 return Sequential(*layers), inplanes
def make_layers(self, block, inplanes, planes, code=None): """Make ResNet layers.""" strides = list(map(int, code)) layers = [] layers.append(block(inplanes, planes, stride=strides[0])) inplanes = planes * block.expansion for stride in strides[1:]: layers.append(block(inplanes, planes, stride=stride)) inplanes = planes * block.expansion return Sequential(*layers), inplanes
def make_resnext_layer_from_code(block, inplanes, planes, cardinality=32, dilation=1, with_cp=False, code=None): """Make resnext layer from code.""" strides = list(map(int, code)) layers = [] layers.append(block(in_chnls=inplanes, cardinality=cardinality, group_depth=planes, stride=strides[0])) inplanes = planes * cardinality * 2 for stride in strides[1:]: layers.append(block(in_chnls=inplanes, cardinality=cardinality, group_depth=planes, stride=stride)) return Sequential(*layers), inplanes
def make_resnet_layer_from_code(block, inplanes, planes, dilation=1, with_cp=False, code=None): """Make resnet layer from code.""" strides = list(map(int, code)) layers = [] layers.append(block(inplanes=inplanes, planes=planes, stride=strides[0], dilation=dilation, with_cp=with_cp, downsample=True)) inplanes = planes * block.expansion for stride in strides[1:]: layers.append(block(inplanes=inplanes, planes=planes, stride=stride, dilation=dilation, with_cp=with_cp)) inplanes = planes * block.expansion return Sequential(*layers), inplanes
def __init__(self, encoding): super(DNetBackbone, self).__init__() op_names = ["conv3", "conv1", "conv3_grp2", "conv3_grp4", "conv3_base1", "conv3_base32", "conv3_sep"] # code with kangning block_str, num_channel, macro_str = encoding.split('_') curr_channel, index = int(num_channel), 0 _big_model = "*" in block_str if _big_model: block_encoding_list = block_str.split('*') # stem layers = [ create_op('conv3', 3, curr_channel // 2, stride=2), ops.Relu(), create_op('conv3', curr_channel // 2, curr_channel // 2), ops.Relu(), create_op('conv3', curr_channel // 2, curr_channel, stride=2), ops.Relu() ] # body if not _big_model: while index < len(macro_str): stride = 1 if macro_str[index] == '-': stride = 2 index += 1 channel_increase = int(macro_str[index]) block = EncodedBlock(block_str, curr_channel, op_names, stride, channel_increase) layers.append(block) curr_channel *= channel_increase index += 1 else: block_encoding_index = 0 while index < len(macro_str): stride = 1 if macro_str[index] == '-': stride = 2 index += 1 block_encoding_index += 1 channel_increase = int(macro_str[index]) block_encoding = block_encoding_list[block_encoding_index] block = EncodedBlock(block_encoding, curr_channel, op_names, stride, channel_increase) layers.append(block) curr_channel *= channel_increase index += 1 layers.append(ops.AdaptiveAvgPool2d((1, 1))) self.layers = Sequential(*layers)
def make_res_layer(block, inplanes, planes, blocks, stride=1, dilation=1, style='pytorch', with_cp=False): """Build resnet layer.""" downsample = None if stride != 1 or inplanes != planes * block.expansion: conv_layer = ops.Conv2d(inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False) norm_layer = ops.BatchNorm2d(planes * block.expansion) downsample = Sequential(conv_layer, norm_layer) layers = [] layers.append( block(inplanes=inplanes, planes=planes, stride=stride, dilation=dilation, downsample=downsample, style=style, with_cp=with_cp)) inplanes = planes * block.expansion for i in range(1, blocks): layers.append( block(inplanes=inplanes, planes=planes, stride=1, dilation=dilation, style=style, with_cp=with_cp)) return Sequential(*layers)
def _blocks(self, out_channels, desc_blocks): blocks = ModuleList() in_channels = 32 for i in range(desc_blocks): blocks.append( Sequential( ops.Conv2d(in_channels, out_channels, padding=1, kernel_size=3), ops.BatchNorm2d(out_channels), ops.Relu(inplace=True), )) in_channels = out_channels return blocks
def __init__(self, inplanes, planes, stride=1, dilation=1, downsample=None, style='pytorch', with_cp=False): """Init Bottleneck.""" super(Bottleneck, self).__init__() assert style in ['pytorch', 'caffe'] self.inplanes = inplanes self.planes = planes self.stride = stride self.dilation = dilation self.style = style self.with_cp = with_cp self.norm1 = ops.BatchNorm2d(planes) self.norm2 = ops.BatchNorm2d(planes) self.norm3 = ops.BatchNorm2d(planes * self.expansion) self.conv1 = ops.Conv2d(inplanes, planes, kernel_size=1, bias=False) self.with_modulated_dcn = False self.conv2 = ops.Conv2d( planes, planes, kernel_size=3, stride=stride, padding=dilation, dilation=dilation, bias=False, ) self.conv3 = ops.Conv2d(planes, planes * self.expansion, kernel_size=1, bias=False) self.relu = ops.Relu(inplace=True) if stride > 1 or downsample is not None: conv_layer = ops.Conv2d(inplanes, planes * self.expansion, kernel_size=1, stride=stride, bias=False) norm_layer = ops.BatchNorm2d(planes * self.expansion) self.downsample = Sequential(conv_layer, norm_layer) else: self.downsample = None
def _transfowm_model(model): """Transform the torch model to Vega model.""" new_model_dict = OrderedDict() for name, module in model.named_children(): # print("name:", name, "module:", module, "type:", type(module)) if isinstance(module, atom_op): new_model_dict[name] = _transsorm_op(module) sub_modules = OrderedDict() if isinstance(module, nn.Sequential): for sub_name, sub_module in module.named_children(): if isinstance(sub_module, atom_block): sub_modules[sub_name] = _transform_block(sub_module) if isinstance(sub_module, atom_op): sub_modules[sub_name] = _transsorm_op(sub_module) new_model_dict[name] = Sequential(sub_modules) return new_model_dict
def __init__(self, num_classes, backbone='ResNetBackbone', neck='FPN', **kwargs): """Create layers. :param num_class: number of class :type num_class: int """ backbone_cls = ClassFactory.get_instance(ClassType.NETWORK, backbone) neck_cls = ClassFactory.get_instance(ClassType.NETWORK, neck, in_channels=backbone_cls.out_channels) backbone_neck = Sequential() backbone_neck.append(backbone_cls, 'body') backbone_neck.append(neck_cls, 'fpn') super(FasterRCNN, self).__init__(backbone_neck, num_classes, **kwargs)
def __init__(self, inp, oup, stride, kernel=3, expand_ratio=1): """Construct InvertedResidual class. :param inp: input channel :param oup: output channel :param stride: stride :param kernel: kernel :param expand_ratio: channel increase multiplier """ super(InvertedConv, self).__init__() hidden_dim = round(inp * expand_ratio) conv = [] if expand_ratio > 1: conv = [ ops.Conv2d(in_channels=inp, out_channels=hidden_dim, kernel_size=1, stride=1, padding=0, bias=False), ops.BatchNorm2d(num_features=hidden_dim), ops.Relu6(inplace=True) ] conv = conv + [ ops.Conv2d(in_channels=hidden_dim, out_channels=hidden_dim, kernel_size=kernel, stride=stride, padding=kernel // 2, groups=hidden_dim, bias=False, depthwise=True), ops.BatchNorm2d(num_features=hidden_dim), ops.Relu6(inplace=True), ops.Conv2d(in_channels=hidden_dim, out_channels=oup, kernel_size=1, stride=1, padding=0, bias=False), ops.BatchNorm2d(num_features=oup) ] self.models = Sequential(*conv)
def __init__(self, inplanes, planes, stride=1, dilation=1, downsample=None, style='pytorch', with_cp=False): """Init BasicBlock.""" super(BasicBlock, self).__init__() self.expansion = 1 self.norm1 = ops.BatchNorm2d(planes) self.norm2 = ops.BatchNorm2d(planes) self.conv1 = ops.Conv2d(inplanes, planes, 3, stride=stride, padding=dilation, dilation=dilation, bias=False) self.conv2 = ops.Conv2d(planes, planes, 3, padding=1, bias=False) self.relu = ops.Relu(inplace=True) if stride > 1 or downsample is not None: conv_layer = ops.Conv2d(inplanes, planes * self.expansion, kernel_size=1, stride=stride, bias=False) norm_layer = ops.BatchNorm2d(planes) self.downsample = Sequential(conv_layer, norm_layer) else: self.downsample = None self.inplanes = inplanes self.planes = planes self.stride = stride self.dilation = dilation self.style = style assert not with_cp
def conv33_sep(in_channel, out_channel, stride): """Conv 3*3 sep.""" return Sequential( conv33(in_channel, in_channel, stride, groups=in_channel), conv11(in_channel, out_channel))
class SerialBackbone(Module): """Serial Net for spnas.""" def __init__(self, code='111-2111-211111-211', block='BottleneckBlock', in_channels=64, weight_file=None, out_layers=None): """Init SerialBackbone.""" super(SerialBackbone, self).__init__() self.inplanes = in_channels self.planes = self.inplanes self.weight_file = weight_file self.channels = [3] self.code = code.split('-') self.block = ClassFactory.get_cls(ClassType.NETWORK, block) self._make_stem_layer() self.layers = Sequential() if out_layers == -1 else OutDictSequential() self.make_cells() @property def out_channels(self): """Output Channel for Module.""" return self.layers.out_channels def load_state_dict(self, state_dict=None, strict=None): """Load and freeze backbone state.""" if isinstance(self.layers, Sequential): return super().load_state_dict(state_dict, strict or False) state_dict = { k.replace('backbone.', ''): v for k, v in state_dict.items() } state_dict = {k.replace('body.', ''): v for k, v in state_dict.items()} not_swap_keys = super().load_state_dict(state_dict, strict or False) if not_swap_keys: need_freeze_layers = [ name for name, parameter in self.named_parameters() if name not in not_swap_keys ] self.freeze(need_freeze_layers) else: self.freeze(['layers']) def _make_stem_layer(self): """Make stem layer.""" self.conv1 = ops.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = ops.BatchNorm2d(self.inplanes) self.relu = ops.Relu(inplace=True) self.maxpool = ops.MaxPool2d(kernel_size=3, stride=2, padding=1) def make_cells(self): """Make ResNet Cell.""" for i, code in enumerate(self.code): layer, planes = self.make_layers(self.block, self.inplanes, self.planes, code=code) self.channels.append(planes) self.inplanes = planes self.layers.append(layer) self.planes = self.planes * 2 def make_layers(self, block, inplanes, planes, code=None): """Make ResNet layers.""" strides = list(map(int, code)) layers = [] layers.append(block(inplanes, planes, stride=strides[0])) inplanes = planes * block.expansion for stride in strides[1:]: layers.append(block(inplanes, planes, stride=stride)) inplanes = planes * block.expansion return Sequential(*layers), inplanes
def create_op(opt_name, in_channel, out_channel, stride=1): """Create op.""" layer = OPS[opt_name](in_channel, out_channel, stride) bn = ops.BatchNorm2d(out_channel) return Sequential(layer, bn)