Пример #1
0
    def __init__(
        self,
        width_choice=(4, 5, 6),
        depth_choice=(4, 5, 6),
        kernel_choice=(3, 5, 7),
        num_cell_groups=[1, 4, 4, 4, 4, 4],
        expansions=[1, 6, 6, 6, 6, 6],
        schedule_cfg=None,
    ):
        super(MNasNetOFASearchSpace, self).__init__(schedule_cfg)
        self.genotype_type_name = "MnasnetOFAGenotype"

        self.num_cell_groups = num_cell_groups
        self.expansions = expansions

        self.block_names = sum(
            [["cell_{}".format(i) for i in range(len(num_cell_groups))]]
            + [
                [
                    "cell_{}_block_{}".format(i, j)
                    for j in range(self.num_cell_groups[i])
                ]
                for i in range(len(num_cell_groups))
            ],
            [],
        )

        self.genotype_type = utils.namedtuple_with_defaults(
            self.genotype_type_name, self.block_names, []
        )
        self.width_choice = width_choice
        self.depth_choice = depth_choice
        self.kernel_choice = kernel_choice
Пример #2
0
    def __init__(self,
                 num_dense_blocks=3,
                 first_ratio=2,
                 stem_channel=None,
                 bc_mode=True,
                 bc_ratio=4,
                 reduction=0.5,
                 transition_channels=None,
                 dynamic_transition=False):
        super(DenseSearchSpace, self).__init__()

        expect(
            (first_ratio is None) + (stem_channel is None) == 1,
            "One and only one of `first_ratio` and `stem_channel` should be specified.",
            ConfigException)
        expect(
            dynamic_transition
            or (transition_channels is None) + (reduction is None) == 1,
            "One and only one of `transition_channels` and `reduction` should be specified.",
            ConfigException)
        if dynamic_transition:
            expect(
                reduction is None and transition_channels is None,
                "When `dynamic_transition` is true, note that `reduction` and "
                "`transition_channels` should not be specified",
                ConfigException)

        self.num_dense_blocks = num_dense_blocks
        self.first_ratio = first_ratio
        self.stem_channel = stem_channel
        self.bc_mode = bc_mode
        self.bc_ratio = bc_ratio
        self.reduction = reduction
        self.transition_channels = transition_channels
        self.dynamic_transition = dynamic_transition

        self.genotype_type_name = "DenseGenotype"
        self.block_names = sum(
            [["stem"]] + \
            [["block_{}".format(i), "transition_{}".format(i)] if i != self.num_dense_blocks - 1
             else ["block_{}".format(i)] for i in range(self.num_dense_blocks)],
            [])
        self.genotype_type = utils.namedtuple_with_defaults(
            self.genotype_type_name, self.block_names, [])
Пример #3
0
 def __init__(
     self,
     width_choice=(4, 5, 6),
     depth_choice=(2, 3, 4),
     kernel_choice=(3, 5, 7),
     image_size_choice=[512, 384, 320, 256, 192],
     head_width_choice=(0.25, 0.5, 0.75),
     head_kernel_choice=[3],
     num_cell_groups=[1, 4, 4, 4, 4, 4],
     expansions=[1, 6, 6, 6, 6, 6],
     num_head=4,
     schedule_cfg=None,
 ):
     super().__init__(width_choice, depth_choice, kernel_choice,
                      image_size_choice, num_cell_groups, expansions,
                      schedule_cfg)
     self.head_width_choice = head_width_choice
     self.head_kernel_choice = head_kernel_choice
     self.num_head = num_head
     self.num_cell_groups = num_cell_groups
     self.block_names += ["head_{}".format(i) for i in range(num_head)]
     self.genotype_type = utils.namedtuple_with_defaults(
         self.genotype_type_name, self.block_names, [])
Пример #4
0
 def __setstate__(self, state):
     super(MNasNetOFASearchSpace, self).__setstate__(state)
     self.genotype_type = utils.namedtuple_with_defaults(
         self.genotype_type_name, self.block_names, []
     )
Пример #5
0
 def __setstate__(self, state):
     super(CellSearchSpace, self).__setstate__(state)
     self.genotype_type = utils.namedtuple_with_defaults(
         self.genotype_type_name, self.cell_group_names +
         [n + "_concat" for n in self.cell_group_names],
         self._default_concats)
Пример #6
0
    def __init__(
            self,
            # meta arch
            num_cell_groups=2,
            num_init_nodes=2,
            num_layers=8,
            cell_layout=None,
            reduce_cell_groups=(1, ),
            # cell arch
            num_steps=4,
            num_node_inputs=2,
            # concat for output
            concat_op="concat",
            concat_nodes=None,
            loose_end=False,
            shared_primitives=("none", "max_pool_3x3", "avg_pool_3x3",
                               "skip_connect", "sep_conv_3x3", "sep_conv_5x5",
                               "dil_conv_3x3", "dil_conv_5x5"),
            cell_shared_primitives=None,
            derive_without_none_op=False):

        super(CNNSearchSpace, self).__init__()

        ## inner-cell
        # number of nodes in every cell
        self.num_steps = num_steps

        # number of input nodes for every node in the cell
        self.num_node_inputs = num_node_inputs

        # primitive single-operand operations
        self.shared_primitives = shared_primitives

        # for now, fixed `concat_op` will not be in the rollout
        self.concat_op = concat_op

        self.concat_nodes = concat_nodes
        self.loose_end = loose_end
        if loose_end:
            expect(
                self.concat_nodes is None,
                "When `loose_end` is given, `concat_nodes` will be automatically determined, "
                "should not be explicitly specified.", ConfigException)

        ## overall structure/meta-arch: how to arrange cells
        # number of cell groups, different cell groups has different structure
        self.num_cell_groups = num_cell_groups

        # number of cell layers
        # if (by default) `num_layers` == 8: the cells of 2 cell groups 0(normal), 1(reduce)
        # will be arranged in order [0, 0, 1, 0, 1, 0, 0, 0]
        self.num_layers = num_layers

        # cell layout
        expect(
            self.num_cell_groups == 2 or cell_layout is not None,
            "`cell_layout` need to be explicitly specified when `num_cell_groups` != 2.",
            ConfigException)
        if cell_layout is not None:
            expect(
                len(cell_layout) == self.num_layers,
                "Length of `cell_layout` should equal `num_layers`")
            expect(
                np.max(cell_layout) == self.num_cell_groups - 1,
                "Max of elements of `cell_layout` should equal `num_cell_groups-1`"
            )
            self.cell_layout = cell_layout
        elif self.num_cell_groups == 2:
            # by default: cell 0: normal cel, cell 1: reduce cell
            self.cell_layout = [0] * self.num_layers
            self.cell_layout[self.num_layers // 3] = 1
            self.cell_layout[(2 * self.num_layers) // 3] = 1

        expect(
            self.num_cell_groups == 2 or reduce_cell_groups is not None,
            "`reduce_cell_groups` need to be explicitly specified when `num_cell_groups` !=  2.",
            ConfigException)
        self.reduce_cell_groups = reduce_cell_groups
        if self.reduce_cell_groups is None:
            # by default, 2 cell groups, the cell group 1 is the reduce cell group
            self.reduce_cell_groups = (1, )

        self.cell_group_names = ["{}_{}".format(
            "reduce" if i in self.reduce_cell_groups else "normal", i)\
                                 for i in range(self.num_cell_groups)]

        # different layerwise primitives
        if cell_shared_primitives is not None:
            expect(
                len(cell_shared_primitives) == self.num_cell_groups,
                "When `cell_shared_primitives` is specified, "
                "it length should equal `num_cell_groups`", ConfigException)
            self.cell_shared_primitives = cell_shared_primitives
            self.logger.info("`cell_shared_primitives` specified, "
                             "will ignore `shared_primitives` configuration.")
            self.shared_primitives = None  # avoid accidentally access this to cause subtle bugs
            self.cellwise_primitives = True
            self._num_primitives_list = [
                len(cps) for cps in self.cell_shared_primitives
            ]
        else:
            self.cell_shared_primitives = [self.shared_primitives
                                           ] * self.num_cell_groups
            self.cellwise_primitives = False
            self._num_primitives = len(self.shared_primitives)

        # check concat node
        if self.concat_nodes is not None:
            if isinstance(self.concat_nodes[0], int):
                # same concat node specification for every cell groups
                _concat_nodes = [self.concat_nodes] * self.num_cell_groups
            for i_cg in range(self.num_cell_groups):
                _num_steps = self.get_num_steps(i_cg)
                expect(np.max(_concat_nodes[i_cg]) < num_init_nodes + _num_steps,
                       "`concat_nodes` {} should be in range(0, {}+{}) for cell group {}"\
                       .format(_concat_nodes[i_cg], num_init_nodes, _num_steps, i_cg))

        self.genotype_type_name = "CNNGenotype"
        if self.concat_nodes is None and not self.loose_end:
            # For backward compatiblity, when concat all steps as output
            # specificy default concats
            self._default_concats = [
                list(
                    range(num_init_nodes,
                          num_init_nodes + self.get_num_steps(i_cg)))
                for i_cg in range(self.num_cell_groups)
            ]
        else:
            # concat_nodes specified or loose end
            self._default_concats = []

        self.genotype_type = utils.namedtuple_with_defaults(
            self.genotype_type_name, self.cell_group_names +
            [n + "_concat" for n in self.cell_group_names],
            self._default_concats)

        # init nodes(meta arch: connect from the last `num_init_nodes` cell's output)
        self.num_init_nodes = num_init_nodes
        self.derive_without_none_op = derive_without_none_op