(self, num_classes=1000, output_stride=8, width_mult=1.0, inverted_residual_setting=None, round_nearest=8)
MobileNet V2 main class

        Args:
            num_classes (int): Number of classes
            width_mult (float): Width multiplier - adjusts number of channels in each layer by this amount
            inverted_residual_setting: Network structure
            round_nearest (int): Round the number of channels in each layer to be a multiple of this number
            Set to 1 to turn off rounding
MobileNet V2 main class
MobileNet V2 main class
def __init__(self, num_classes=1000, output_stride=8, width_mult=1.0, inverted_residual_setting=None, round_nearest=8):
        """
        MobileNet V2 main class

        Args:
            num_classes (int): Number of classes
            width_mult (float): Width multiplier - adjusts number of channels in each layer by this amount
            inverted_residual_setting: Network structure
            round_nearest (int): Round the number of channels in each layer to be a multiple of this number
            Set to 1 to turn off rounding
        """
        super(MobileNetV2, self).__init__()
        block = InvertedResidual
        input_channel = 32
        last_channel = 1280
        self.output_stride = output_stride
        current_stride = 1
        if inverted_residual_setting is None:
            inverted_residual_setting = [
                # t, c, n, s
                [1, 16, 1, 1],
                [6, 24, 2, 2],
                [6, 32, 3, 2],
                [6, 64, 4, 2],
                [6, 96, 3, 1],
                [6, 160, 3, 2],
                [6, 320, 1, 1],
            ]

        # only check the first element, assuming user knows t,c,n,s are required
        if len(inverted_residual_setting) == 0 or len(inverted_residual_setting[0]) != 4:
            raise ValueError("inverted_residual_setting should be non-empty "
                             "or a 4-element list, got {}".format(inverted_residual_setting))

        # building first layer
        input_channel = _make_divisible(input_channel * width_mult, round_nearest)
        self.last_channel = _make_divisible(last_channel * max(1.0, width_mult), round_nearest)
        features = [ConvBNReLU(3, input_channel, stride=2)]
        current_stride *= 2
        dilation=1
        previous_dilation = 1

        # building inverted residual blocks
        for t, c, n, s in inverted_residual_setting:
            output_channel = _make_divisible(c * width_mult, round_nearest)
            previous_dilation = dilation
            if current_stride == output_stride:
                stride = 1
                dilation *= s
            else:
                stride = s
                current_stride *= s
            output_channel = int(c * width_mult)

            for i in range(n):
                if i==0:
                    features.append(block(input_channel, output_channel, stride, previous_dilation, expand_ratio=t))
                else:
                    features.append(block(input_channel, output_channel, 1, dilation, expand_ratio=t))
                input_channel = output_channel
        # building last several layers
        features.append(ConvBNReLU(input_channel, self.last_channel, kernel_size=1))
        # make it nn.Sequential
        self.features = nn.Sequential(*features)

        # building classifier
        self.classifier = nn.Sequential(
            nn.Dropout(0.2),
            nn.Linear(self.last_channel, num_classes),
        )

        # weight initialization
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out')
                if m.bias is not None:
                    nn.init.zeros_(m.bias)
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.ones_(m.weight)
                nn.init.zeros_(m.bias)
            elif isinstance(m, nn.Linear):
                nn.init.normal_(m.weight, 0, 0.01)
                nn.init.zeros_(m.bias)
def __init__ ( self , num_classes = 1000 , output_stride = 8 , width_mult = 1.0 , inverted_residual_setting = None , round_nearest = 8 ) : super ( MobileNetV2 , self ) . __init__ ( ) block = InvertedResidual input_channel = 32 last_channel = 1280 self . output_stride = output_stride current_stride = 1 if inverted_residual_setting is None : inverted_residual_setting = [ # t, c, n, s [ 1 , 16 , 1 , 1 ] , [ 6 , 24 , 2 , 2 ] , [ 6 , 32 , 3 , 2 ] , [ 6 , 64 , 4 , 2 ] , [ 6 , 96 , 3 , 1 ] , [ 6 , 160 , 3 , 2 ] , [ 6 , 320 , 1 , 1 ] , ] # only check the first element, assuming user knows t,c,n,s are required if len ( inverted_residual_setting ) == 0 or len ( inverted_residual_setting [ 0 ] ) != 4 : raise ValueError ( "inverted_residual_setting should be non-empty " "or a 4-element list, got {}" . format ( inverted_residual_setting ) ) # building first layer input_channel = _make_divisible ( input_channel * width_mult , round_nearest ) self . last_channel = _make_divisible ( last_channel * max ( 1.0 , width_mult ) , round_nearest ) features = [ ConvBNReLU ( 3 , input_channel , stride = 2 ) ] current_stride *= 2 dilation = 1 previous_dilation = 1 # building inverted residual blocks for t , c , n , s in inverted_residual_setting : output_channel = _make_divisible ( c * width_mult , round_nearest ) previous_dilation = dilation if current_stride == output_stride : stride = 1 dilation *= s else : stride = s current_stride *= s output_channel = int ( c * width_mult ) for i in range ( n ) : if i == 0 : features . append ( block ( input_channel , output_channel , stride , previous_dilation , expand_ratio = t ) ) else : features . append ( block ( input_channel , output_channel , 1 , dilation , expand_ratio = t ) ) input_channel = output_channel # building last several layers features . append ( ConvBNReLU ( input_channel , self . last_channel , kernel_size = 1 ) ) # make it nn.Sequential self . features = nn . Sequential ( * features ) # building classifier self . classifier = nn . Sequential ( nn . Dropout ( 0.2 ) , nn . Linear ( self . last_channel , num_classes ) , ) # weight initialization for m in self . modules ( ) : if isinstance ( m , nn . Conv2d ) : nn . init . kaiming_normal_ ( m . weight , mode = 'fan_out' ) if m . bias is not None : nn . init . zeros_ ( m . bias ) elif isinstance ( m , nn . BatchNorm2d ) : nn . init . ones_ ( m . weight ) nn . init . zeros_ ( m . bias ) elif isinstance ( m , nn . Linear ) : nn . init . normal_ ( m . weight , 0 , 0.01 ) nn . init . zeros_ ( m . bias )