Example #1
0
    def __init__(self, n_nodes, channels_pp, channels_p, channels, reduction_p,
                 reduction):
        super().__init__()
        self.reduction = reduction
        self.n_nodes = n_nodes

        # If previous cell is reduction cell, current input size does not match with
        # output size of cell[k-2]. So the output[k-2] should be reduced by preprocessing.
        if reduction_p:
            self.preproc0 = ops.FactorizedReduce(channels_pp,
                                                 channels,
                                                 affine=False)
        else:
            self.preproc0 = ops.StdConv(channels_pp,
                                        channels,
                                        1,
                                        1,
                                        0,
                                        affine=False)
        self.preproc1 = ops.StdConv(channels_p,
                                    channels,
                                    1,
                                    1,
                                    0,
                                    affine=False)

        # generate dag
        self.mutable_ops = nn.ModuleList()
        for depth in range(2, self.n_nodes + 2):
            self.mutable_ops.append(
                Node(
                    "{}_n{}".format("reduce" if reduction else "normal",
                                    depth), depth, channels,
                    2 if reduction else 0))
Example #2
0
 def __init__(self, node_id, num_prev_nodes, channels,
              num_downsample_connect):
     super().__init__()
     self.ops = nn.ModuleList()
     choice_keys = []
     for i in range(num_prev_nodes):
         stride = 2 if i < num_downsample_connect else 1
         choice_keys.append("{}_p{}".format(node_id, i))
         self.ops.append(
             mutables.LayerChoice([
                 ops.PoolBN('max', channels, 3, stride, 1, affine=False),
                 ops.PoolBN('avg', channels, 3, stride, 1, affine=False),
                 nn.Identity() if stride == 1 else ops.FactorizedReduce(
                     channels, channels, affine=False),
                 ops.SepConv(channels, channels, 3, stride, 1,
                             affine=False),
                 ops.SepConv(channels, channels, 5, stride, 2,
                             affine=False),
                 ops.DilConv(
                     channels, channels, 3, stride, 2, 2, affine=False),
                 ops.DilConv(
                     channels, channels, 5, stride, 4, 2, affine=False)
             ],
                                  key=choice_keys[-1]))
     self.drop_path = ops.DropPath()
     self.input_switch = mutables.InputChoice(
         choose_from=choice_keys,
         n_chosen=2,
         key="{}_switch".format(node_id))
Example #3
0
 def __init__(self, node_id, num_prev_nodes, channels,
              num_downsample_connect):
     super().__init__()
     self.ops = nn.ModuleList()
     choice_keys = []
     for i in range(num_prev_nodes):
         stride = 2 if i < num_downsample_connect else 1
         choice_keys.append("{}_p{}".format(node_id, i))
         self.ops.append(
             LayerChoice(OrderedDict([
                 ("maxpool",
                  ops.PoolBN('max', channels, 3, stride, 1, affine=False)),
                 ("avgpool",
                  ops.PoolBN('avg', channels, 3, stride, 1, affine=False)),
                 ("skipconnect", nn.Identity() if stride == 1 else
                  ops.FactorizedReduce(channels, channels, affine=False)),
                 ("sepconv3x3",
                  ops.SepConv(channels,
                              channels,
                              3,
                              stride,
                              1,
                              affine=False)),
                 ("sepconv5x5",
                  ops.SepConv(channels,
                              channels,
                              5,
                              stride,
                              2,
                              affine=False)),
                 ("dilconv3x3",
                  ops.DilConv(channels,
                              channels,
                              3,
                              stride,
                              2,
                              2,
                              affine=False)),
                 ("dilconv5x5",
                  ops.DilConv(channels,
                              channels,
                              5,
                              stride,
                              4,
                              2,
                              affine=False))
             ]),
                         label=choice_keys[-1]))
     self.drop_path = ops.DropPath()
     self.input_switch = InputChoice(n_candidates=len(choice_keys),
                                     n_chosen=2,
                                     label="{}_switch".format(node_id))
Example #4
0
    def __init__(self, node_id, num_prev_nodes, channels, num_downsample_connect):
        '''
        Node("{}_n{}".format("reduce" if reduction else "normal", depth),
             depth, channels, 2 if reduction else 0)
        num_prev_nodes: 之前的节点个数
        '''

        super().__init__()
        self.ops = nn.ModuleList()
        choice_keys = []  # 记录 节点+边 组合的名称

        for i in range(num_prev_nodes):  # 枚举之前的节点
            stride = 2 if i < num_downsample_connect else 1
            # 统一设置stride
            # 如果是reduction cell, stride=2,
            # 如果是normal cell, stride=1
            choice_keys.append("{}_p{}".format(node_id, i))

            self.ops.append(
                mutables.LayerChoice(OrderedDict([
                    ("maxpool", ops.PoolBN('max', channels, 3, stride, 1, affine=False)),
                    ("avgpool", ops.PoolBN('avg', channels, 3, stride, 1, affine=False)),
                    ("skipconnect", nn.Identity() if stride == 1 else ops.FactorizedReduce(
                        channels, channels, affine=False)),
                    ("sepconv3x3", ops.SepConv(channels,
                                               channels, 3, stride, 1, affine=False)),
                    ("sepconv5x5", ops.SepConv(channels,
                                               channels, 5, stride, 2, affine=False)),
                    ("dilconv3x3", ops.DilConv(channels,
                                               channels, 3, stride, 2, 2, affine=False)),
                    ("dilconv5x5", ops.DilConv(channels,
                                               channels, 5, stride, 4, 2, affine=False))
                ]), key=choice_keys[-1]))

        self.drop_path = ops.DropPath()  # 以0.2的概率drop path

        self.input_switch = mutables.InputChoice(  # 控制连接方式, 维护choice_key就是为了这个使用
            choose_from=choice_keys, n_chosen=2, key="{}_switch".format(node_id))