Esempio n. 1
0
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
Esempio n. 2
0
    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()
Esempio n. 3
0
 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())
Esempio n. 4
0
    def __init__(self, base_channel, num_classes):
        """Create layers.

        :param base_channel: base_channel
        :type base_channel: int
        :param num_class: number of class
        :type num_class: int
        """
        super(LinearClassificationHead, self).__init__()
        self.avgpool = ops.AdaptiveAvgPool2d(output_size=(1, 1))
        self.view = ops.View()
        self.linear = ops.Linear(in_features=base_channel,
                                 out_features=num_classes)
Esempio n. 5
0
    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)
Esempio n. 6
0
def _transform_op(init_layer):
    """Transform the torch op to Vega op."""
    if isinstance(init_layer, nn.Conv2d):
        in_channels = init_layer.in_channels
        out_channels = init_layer.out_channels
        kernel_size = init_layer.kernel_size[0]
        stride = init_layer.stride
        padding = init_layer.padding
        # bias = init_layer.bias
        new_layer = ops.Conv2d(in_channels=in_channels,
                               out_channels=out_channels,
                               kernel_size=kernel_size,
                               stride=stride,
                               padding=padding,
                               bias=False)
    elif isinstance(init_layer, nn.BatchNorm2d):
        num_features = init_layer.num_features
        new_layer = ops.BatchNorm2d(num_features=num_features)
    elif isinstance(init_layer, nn.ReLU):
        new_layer = ops.Relu()
    elif isinstance(init_layer, nn.MaxPool2d):
        kernel_size = init_layer.kernel_size
        stride = init_layer.stride
        # padding = init_layer.padding
        new_layer = ops.MaxPool2d(kernel_size=kernel_size, stride=stride)
    elif isinstance(init_layer, nn.AvgPool2d):
        kernel_size = init_layer.kernel_size
        stride = init_layer.stride
        padding = init_layer.padding
        new_layer = ops.AvgPool2d(kernel_size=kernel_size,
                                  stride=stride,
                                  padding=padding)
    elif isinstance(init_layer, P.ReduceMean):
        new_layer = ops.AdaptiveAvgPool2d()
    elif isinstance(init_layer, nn.Dense):
        in_features = init_layer.in_channels
        out_features = init_layer.out_channels
        # use_bias = init_layer.bias
        new_layer = ops.Linear(in_features=in_features,
                               out_features=out_features)
    elif isinstance(init_layer, nn.Dropout):
        prob = init_layer.p
        inplace = init_layer.inplace
        new_layer = ops.Dropout(prob=prob, inplace=inplace)
    elif isinstance(init_layer, nn.Flatten):
        new_layer = ops.View()
    else:
        raise ValueError("The op {} is not supported.".format(
            type(init_layer)))
    return new_layer
Esempio n. 7
0
    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)