def __init__(self, inplanes, planes, stride=1, dilation=1, downsample=None, activate=1, init=1.0): super(Bottleneck, self).__init__() self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, dilation=dilation, padding=dilation, bias=False) self.bn2 = nn.BatchNorm2d(planes) self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False) self.bn3 = nn.BatchNorm2d(planes * 4) # self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride self.relu1 = act.Activate(activate, init=init) self.relu2 = act.Activate(activate, init=init) self.relu3 = act.Activate(activate, init=init)
def __init__(self, inplanes, outplanes, stride=1, t=6, act_id=1, init_value=1.0): super(LinearBottleneck, self).__init__() self.conv1 = nn.Conv2d(inplanes, inplanes * t, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(inplanes * t) self.conv2 = nn.Conv2d(inplanes * t, inplanes * t, kernel_size=3, stride=stride, padding=1, bias=False, groups=inplanes * t) self.bn2 = nn.BatchNorm2d(inplanes * t) self.conv3 = nn.Conv2d(inplanes * t, outplanes, kernel_size=1, bias=False) self.bn3 = nn.BatchNorm2d(outplanes) # self.activation = activation(inplace=True) self.act_1 = act.Activate(act_id=act_id, init=init_value) self.act_2 = act.Activate(act_id=act_id, init=init_value) self.stride = stride self.t = t self.inplanes = inplanes self.outplanes = outplanes
def __init__(self, inplanes, planes, stride=1, dilation=1, downsample=None, activate=1, init=1.0): super(BasicBlock, self).__init__() self.conv1 = conv3x3(inplanes, planes, stride, dilation) self.bn1 = nn.BatchNorm2d(planes) # self.relu = nn.ReLU(inplace=True) self.conv2 = conv3x3(planes, planes, dilation=dilation) self.bn2 = nn.BatchNorm2d(planes) self.downsample = downsample self.stride = stride self.relu1 = act.Activate(activate, init=init) self.relu2 = act.Activate(activate, init=init)
def __init__(self, block, layers, num_classes=1000, pre_trained_model=None, activate=1, init=1.0): self.inplanes = 64 super(ResNet, self).__init__() self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=1, padding=3, bias=False) self.bn1 = nn.BatchNorm2d(64) # self.relu = nn.ReLU(inplace=True) # self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.layer1 = self._make_layer(block, 64, layers[0], stride=1) self.layer2 = self._make_layer(block, 128, layers[1], stride=1) self.layer3 = self._make_layer(block, 256, layers[2], stride=2) self.layer4 = self._make_layer(block, 512, layers[3], stride=2) self.avgpool = nn.AdaptiveAvgPool2d(1) self.fc = nn.Linear(512 * block.expansion, num_classes) self.relu1 = act.Activate(activate, init=init) if pre_trained_model is None: for m in self.modules(): if isinstance(m, nn.Conv2d): n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels m.weight.data.normal_(0, math.sqrt(2. / n)) elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() else: print("load init model... ") model_dict = self.state_dict() model_dict.update(pre_trained_model.state_dict()) self.load_state_dict(model_dict)
def __init__(self, scale=1.0, input_size=224, t=6, in_channels=3, num_classes=1000, act_id=1, init_value=1.0): """ MobileNet2 constructor. :param in_channels: (int, optional): number of channels in the input tensor. Default is 3 for RGB image inputs. :param input_size: :param num_classes: number of classes to predict. Default is 1000 for ImageNet. :param scale: :param t: :param activation: :param init_value: """ super(MobileNet2, self).__init__() self.scale = scale self.t = t # self.activation_type = activation # self.activation = activation(inplace=True) self.act_id = act_id self.init = init_value self.act_1 = act.Activate(act_id=self.act_id, init=self.init) self.act_2 = act.Activate(act_id=self.act_id, init=self.init) self.num_classes = num_classes self.num_of_channels = [32, 16, 24, 32, 64, 96, 160, 320] # assert (input_size % 32 == 0) self.c = [ _make_divisible(ch * self.scale, 8) for ch in self.num_of_channels ] self.n = [1, 1, 2, 3, 4, 3, 3, 1] self.s = [2, 1, 2, 2, 2, 1, 2, 1] self.conv1 = nn.Conv2d(in_channels, self.c[0], kernel_size=3, bias=False, stride=self.s[0], padding=1) self.bn1 = nn.BatchNorm2d(self.c[0]) self.bottlenecks = self._make_bottlenecks() # Last convolution has 1280 output channels for scale <= 1 self.last_conv_out_ch = 1280 if self.scale <= 1 else _make_divisible( 1280 * self.scale, 8) self.conv_last = nn.Conv2d(self.c[-1], self.last_conv_out_ch, kernel_size=1, bias=False) self.bn_last = nn.BatchNorm2d(self.last_conv_out_ch) self.avgpool = nn.AdaptiveAvgPool2d(1) self.dropout = nn.Dropout(p=0.2, inplace=True) # confirmed by paper authors self.fc = nn.Linear(self.last_conv_out_ch, self.num_classes) self.init_params()