(
        self,
        num_classes=1000,
        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,
        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

        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 = [
            M.ConvBnRelu2d(
                3, input_channel, kernel_size=3, padding=1, stride=2, bias=False
            )
        ]
        # building inverted residual blocks
        for t, c, n, s in inverted_residual_setting:
            output_channel = _make_divisible(c * width_mult, round_nearest)
            for i in range(n):
                stride = s if i == 0 else 1
                features.append(
                    block(input_channel, output_channel, stride, expand_ratio=t)
                )
                input_channel = output_channel
        # building last several layers
        features.append(
            M.ConvBnRelu2d(input_channel, self.last_channel, kernel_size=1, bias=False)
        )
        # make it M.Sequential
        self.features = M.Sequential(*features)

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

        self.quant = M.QuantStub()
        self.dequant = M.DequantStub()

        # weight initialization
        for m in self.modules():
            if isinstance(m, M.Conv2d):
                M.init.msra_normal_(m.weight, mode="fan_out")
                if m.bias is not None:
                    M.init.zeros_(m.bias)
            elif isinstance(m, M.BatchNorm2d):
                M.init.ones_(m.weight)
                M.init.zeros_(m.bias)
            elif isinstance(m, M.Linear):
                M.init.normal_(m.weight, 0, 0.01)
                M.init.zeros_(m.bias)
def __init__ ( self , num_classes = 1000 , width_mult = 1.0 , inverted_residual_setting = None , round_nearest = 8 , ) : super ( MobileNetV2 , self ) . __init__ ( ) block = InvertedResidual input_channel = 32 last_channel = 1280 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 = [ M . ConvBnRelu2d ( 3 , input_channel , kernel_size = 3 , padding = 1 , stride = 2 , bias = False ) ] # building inverted residual blocks for t , c , n , s in inverted_residual_setting : output_channel = _make_divisible ( c * width_mult , round_nearest ) for i in range ( n ) : stride = s if i == 0 else 1 features . append ( block ( input_channel , output_channel , stride , expand_ratio = t ) ) input_channel = output_channel # building last several layers features . append ( M . ConvBnRelu2d ( input_channel , self . last_channel , kernel_size = 1 , bias = False ) ) # make it M.Sequential self . features = M . Sequential ( * features ) # building classifier self . classifier = M . Sequential ( M . Dropout ( 0.2 ) , M . Linear ( self . last_channel , num_classes ) , ) self . classifier . disable_quantize ( ) self . quant = M . QuantStub ( ) self . dequant = M . DequantStub ( ) # weight initialization for m in self . modules ( ) : if isinstance ( m , M . Conv2d ) : M . init . msra_normal_ ( m . weight , mode = "fan_out" ) if m . bias is not None : M . init . zeros_ ( m . bias ) elif isinstance ( m , M . BatchNorm2d ) : M . init . ones_ ( m . weight ) M . init . zeros_ ( m . bias ) elif isinstance ( m , M . Linear ) : M . init . normal_ ( m . weight , 0 , 0.01 ) M . init . zeros_ ( m . bias )