예제 #1
0
    def __init__(self,
                 num_input_layers,
                 num_outputs,
                 loss_func,
                 reduce_error_func_dict,
                 hist_error_func_dict,
                 text_error_func_dict,
                 output_scaling,
                 resnet_type: str,
                 learning_rate,
                 cosine_annealing_steps,
                 weight_decay,
                 dtype=torch.float32,
                 track_ideal_metrics=False):
        super().__init__(num_input_layers,
                         num_outputs,
                         loss_func,
                         reduce_error_func_dict,
                         hist_error_func_dict,
                         text_error_func_dict,
                         output_scaling,
                         resnet_type,
                         learning_rate,
                         cosine_annealing_steps,
                         weight_decay,
                         dtype=torch.float32,
                         track_ideal_metrics=False)

        self.model = self.resnet_dict[resnet_type](pretrained=False,
                                                   num_classes=num_outputs)
        # altering resnet to fit more than 3 input layers
        if resnet_type.startswith('res'):
            self.model.conv1 = nn.Conv2d(num_input_layers,
                                         64,
                                         kernel_size=7,
                                         stride=2,
                                         padding=3,
                                         bias=False)
        if resnet_type.startswith('mobile'):
            self.model.features[0] = ConvBNReLU(self.num_input_layers,
                                                _make_divisible(32.0, 8),
                                                stride=2,
                                                norm_layer=nn.BatchNorm2d)
        self.plotter_val_data = None
        self.plotter_train_data = None
        self.type(dst_type=dtype)
예제 #2
0
    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 not inverted_residual_setting 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)]
        # building inverted residual blocks
        for expand_ratio, num_channels, num_layers, block_stride in inverted_residual_setting:
            output_channel = _make_divisible(num_channels * width_mult,
                                             round_nearest)
            for i in range(num_layers):
                stride = block_stride if i == 0 else 1
                features.append(
                    block(input_channel,
                          output_channel,
                          stride,
                          expand_ratio=expand_ratio))
                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 module in self.modules():
            if isinstance(module, nn.Conv2d):
                nn.init.kaiming_normal_(module.weight, mode='fan_out')
                if module.bias is not None:
                    nn.init.zeros_(module.bias)
            elif isinstance(module, nn.BatchNorm2d):
                nn.init.ones_(module.weight)
                nn.init.zeros_(module.bias)
            elif isinstance(module, nn.Linear):
                nn.init.normal_(module.weight, 0, 0.01)
                nn.init.zeros_(module.bias)
예제 #3
0
 def __init__(self, input_channels: int, squeeze_factor: int = 4):
     super().__init__()
     squeeze_channels = _make_divisible(input_channels // squeeze_factor, 8)
     self.fc1 = nn.Conv2d(input_channels, squeeze_channels, 1)
     self.fc2 = nn.Conv2d(squeeze_channels, input_channels, 1)
예제 #4
0
 def adjust_channels(channels: int, width_mult: float):
     return _make_divisible(channels * width_mult, 8)
예제 #5
0
    def __init__(self,
                 num_classes=1000,
                 width_mult=1.0,
                 version="v1",
                 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
            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(MobileNet, self).__init__()

        input_channel = 32
        if version == "v2":
            settings = [
                # 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],
            ]
            last_channel = 1280
            layer = mobilenet.InvertedResidual
        elif version == "v1":
            settings = [
                # t, c, n, s
                [1, 64, 1, 1],
                [1, 128, 2, 2],
                [1, 256, 2, 2],
                [1, 512, 6, 2],
                [1, 1024, 2, 2],
            ]
            last_channel = 1024
            layer = SepConvBNReLU
        self.settings = settings
        self.version = version

        # building first layer
        input_channel = mobilenet._make_divisible(input_channel * width_mult,
                                                  round_nearest)
        self.last_channel = mobilenet._make_divisible(
            last_channel * max(1.0, width_mult), round_nearest)
        self.conv1 = mobilenet.ConvBNReLU(3, input_channel, stride=2)
        # building inverted residual blocks
        for j, (t, c, n, s) in enumerate(settings):
            output_channel = mobilenet._make_divisible(c * width_mult,
                                                       round_nearest)
            layers = []
            for i in range(n):
                stride = s if i == 0 else 1
                layers.append(
                    layer(input_channel,
                          output_channel,
                          stride=stride,
                          expand_ratio=t))
                input_channel = output_channel
            self.add_module("layer{}".format(j + 1), nn.Sequential(*layers))
        # building last several layers
        if self.version == "v2":
            self.head_conv = mobilenet.ConvBNReLU(input_channel,
                                                  self.last_channel,
                                                  kernel_size=1)

        # 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)