def __init__(self): super().__init__() self.conv1 = nn.Conv2d(1, 32, 3, 1) self.conv2 = LayerChoice( [nn.Conv2d(32, 64, 3, 1), DepthwiseSeparableConv(32, 64)]) self.dropout1 = nn.Dropout(.25) self.dropout2 = nn.Dropout(0.5) self.dropout_choice = InputChoice(2, 1) self.fc = LayerChoice([ nn.Sequential( nn.Linear(9216, 64), nn.ReLU(), nn.Linear(64, 10), ), nn.Sequential( nn.Linear(9216, 128), nn.ReLU(), nn.Linear(128, 10), ), nn.Sequential( nn.Linear(9216, 256), nn.ReLU(), nn.Linear(256, 10), ) ]) self.rpfc = nn.Linear(10, 10)
def __init__(self, value_choice=True): super().__init__() self.conv1 = nn.Conv2d(1, 32, 3, 1) self.conv2 = LayerChoice( [nn.Conv2d(32, 64, 3, 1), DepthwiseSeparableConv(32, 64)]) self.dropout1 = LayerChoice( [nn.Dropout(.25), nn.Dropout(.5), nn.Dropout(.75)]) self.dropout2 = nn.Dropout(0.5) if value_choice: hidden = nn.ValueChoice([32, 64, 128]) else: hidden = 64 self.fc1 = nn.Linear(9216, hidden) self.fc2 = nn.Linear(hidden, 10) self.rpfc = nn.Linear(10, 10) self.input_ch = InputChoice(2, 1)
def __init__(self): super().__init__() ch1 = ValueChoice([16, 32]) kernel = ValueChoice([3, 5]) self.conv1 = nn.Conv2d(1, ch1, kernel, padding=kernel // 2) self.batch_norm = nn.BatchNorm2d(ch1) self.conv2 = nn.Conv2d(ch1, 64, 3) self.dropout1 = LayerChoice( [nn.Dropout(.25), nn.Dropout(.5), nn.Dropout(.75)]) self.fc = nn.Linear(64, 10)
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))
def _make_blocks(self, blocks, in_channels, channels): result = [] for i in range(blocks): stride = 2 if i == 0 else 1 inp = in_channels if i == 0 else channels oup = channels base_mid_channels = channels // 2 mid_channels = int(base_mid_channels) # prepare for scale self._layerchoice_count += 1 choice_block = LayerChoice([ ShuffleNetBlock(inp, oup, mid_channels=mid_channels, ksize=3, stride=stride, affine=self._affine), ShuffleNetBlock(inp, oup, mid_channels=mid_channels, ksize=5, stride=stride, affine=self._affine), ShuffleNetBlock(inp, oup, mid_channels=mid_channels, ksize=7, stride=stride, affine=self._affine), ShuffleXceptionBlock(inp, oup, mid_channels=mid_channels, stride=stride, affine=self._affine) ], label="LayerChoice" + str(self._layerchoice_count)) result.append(choice_block) if stride == 2: self._feature_map_size //= 2 return result
def __init__(self, width_stages=[24,40,80,96,192,320], n_cell_stages=[4,4,4,4,4,1], stride_stages=[2,2,2,1,2,1], width_mult=1, n_classes=1000, dropout_rate=0, bn_param=(0.1, 1e-3)): """ Parameters ---------- width_stages: str width (output channels) of each cell stage in the block n_cell_stages: str number of cells in each cell stage stride_strages: str stride of each cell stage in the block width_mult : int the scale factor of width """ super(SearchMobileNet, self).__init__() input_channel = putils.make_divisible(32 * width_mult, 8) first_cell_width = putils.make_divisible(16 * width_mult, 8) for i in range(len(width_stages)): width_stages[i] = putils.make_divisible(width_stages[i] * width_mult, 8) # first conv first_conv = ops.ConvLayer(3, input_channel, kernel_size=3, stride=2, use_bn=True, act_func='relu6', ops_order='weight_bn_act') # first block first_block_conv = ops.OPS['3x3_MBConv1'](input_channel, first_cell_width, 1) first_block = first_block_conv input_channel = first_cell_width blocks = [first_block] stage_cnt = 0 for width, n_cell, s in zip(width_stages, n_cell_stages, stride_stages): for i in range(n_cell): if i == 0: stride = s else: stride = 1 op_candidates = [ops.OPS['3x3_MBConv3'](input_channel, width, stride), ops.OPS['3x3_MBConv6'](input_channel, width, stride), ops.OPS['5x5_MBConv3'](input_channel, width, stride), ops.OPS['5x5_MBConv6'](input_channel, width, stride), ops.OPS['7x7_MBConv3'](input_channel, width, stride), ops.OPS['7x7_MBConv6'](input_channel, width, stride)] if stride == 1 and input_channel == width: # if it is not the first one op_candidates += [ops.OPS['Zero'](input_channel, width, stride)] conv_op = LayerChoice(op_candidates, label="s{}_c{}".format(stage_cnt, i)) else: conv_op = LayerChoice(op_candidates, label="s{}_c{}".format(stage_cnt, i)) # shortcut if stride == 1 and input_channel == width: # if not first cell shortcut = ops.IdentityLayer(input_channel, input_channel) else: shortcut = None inverted_residual_block = ops.MobileInvertedResidualBlock(conv_op, shortcut, op_candidates) blocks.append(inverted_residual_block) input_channel = width stage_cnt += 1 # feature mix layer last_channel = putils.make_devisible(1280 * width_mult, 8) if width_mult > 1.0 else 1280 feature_mix_layer = ops.ConvLayer(input_channel, last_channel, kernel_size=1, use_bn=True, act_func='relu6', ops_order='weight_bn_act', ) classifier = ops.LinearLayer(last_channel, n_classes, dropout_rate=dropout_rate) self.first_conv = first_conv self.blocks = nn.ModuleList(blocks) self.feature_mix_layer = feature_mix_layer self.global_avg_pooling = nn.AdaptiveAvgPool2d(1) self.classifier = classifier # set bn param self.set_bn_param(momentum=bn_param[0], eps=bn_param[1])