Beispiel #1
0
    def __init__(self, block, layers, k=10, zero_init_residual=True):
        super(KeyPoint, self).__init__()
        self.inplanes = 64
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, 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])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3_ = self._make_layer(block, k, layers[2], stride=2)
        # self.layer4_=self._make_layer(block,k,layers[3],stride=2)
        self.maxchannelpool = ChannelPool(k)
        self.fc_ = nn.Linear(196, 40)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
            elif isinstance(m, nn.BatchNorm2d):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)

        # Zero-initialize the last BN in each residual branch,
        # so that the residual branch starts with zeros, and each residual block behaves like an identity.
        # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
        if zero_init_residual:
            for m in self.modules():
                if isinstance(m, Bottleneck):
                    nn.init.constant_(m.bn3.weight, 0)
                elif isinstance(m, BasicBlock):
                    nn.init.constant_(m.bn2.weight, 0)
    def __init__(self,in_channel,out_channel):
        super(Attention, self).__init__()
        self.conv1_ = nn.Conv2d(in_channel,512,kernel_size=3,stride=1,padding=1)
        self.bn1 = nn.BatchNorm2d(128)
        self.conv_mid = nn.Conv2d(512,128,kernel_size=1,stride=1)
        self.conv2_ = nn.Conv2d(128,out_channel,kernel_size=3,stride=1,padding=1)

        self.channelpool = ChannelPool(out_channel)
Beispiel #3
0
 def __init__(self, K=6):
     super(attentionReid, self).__init__()
     self.K = K
     self.backbone = ResNet(50,
                            num_stages=4,
                            strides=(1, 2, 2, 1),
                            out_indices=(2, 3))
     self.backbone.init_weights(
         pretrained=
         'https://download.pytorch.org/models/resnet50-19c8e357.pth')
     self.attention = SattentionNet(num_features=128,
                                    seqlen=1,
                                    spanum=self.K)
     self.attention.reset_params()
     #self.GCN = GCN(2048,2048//K)
     self.lin = nn.Linear(2048, 2048)
     self.fc = nn.Linear(128 * self.K, 11)
     nn.init.xavier_normal_(self.lin.weight)
     nn.init.xavier_normal_(self.fc.weight)
     self.conv2 = nn.Conv2d(1024, self.K, 1, 1)
     self.channelpool = ChannelPool(self.K)
    def __init__(self, block, layers, num_classes=40, num_layers=6):
        self.inplanes = 64
        super(KeyPoint, self).__init__()

        self.conv1 = nn.Conv2d(3,
                               64,
                               kernel_size=7,
                               stride=2,
                               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)  # previous stride is 2
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.layer4_ = self._make_layer(block, num_layers, layers[3], stride=2)
        #self.convnext = nn.Conv2d(512*block.expansion,num_layers,kernel_size=1,stride=1,padding=0,bias=False)
        self.avgpool = nn.AvgPool2d(14)
        self.maxchannelpool = ChannelPool(num_layers)

        #self.dropout  = nn.Dropout(0.9)
        #self.fc_ = nn.Linear(256 * block.expansion, num_classes,bias=True)
        self.fc2 = nn.Linear(7 * 7, num_classes)
        #module needed
        #self.op1 = MultiHeadAttention(n_head=1, d_model=28*28, d_k=512, d_v=512, dropout=0.2)

        #50 以上的模型 expansion 为4  其他为1

        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_()
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)
    def __init__(self,
                 features,
                 num_classes=1000,
                 num_layers=5,
                 init_weights=True):
        super(VGG, self).__init__()
        self.features = features
        self.conv1 = nn.Conv2d(512,
                               num_layers,
                               kernel_size=1,
                               stride=1,
                               padding=0,
                               bias=False)
        self.conv2 = nn.Conv2d(256,
                               128,
                               kernel_size=1,
                               stride=1,
                               padding=0,
                               bias=False)
        self.conv3 = nn.Conv2d(128,
                               num_layers,
                               kernel_size=1,
                               stride=1,
                               padding=0,
                               bias=False)

        self.classifier = nn.Sequential(
            nn.Linear(512 * 7 * 7, 4096),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(True),
            nn.Dropout(),
            nn.Linear(4096, num_classes),
        )
        self.maxchannelpool = ChannelPool(num_layers)
        self.num_layers = num_layers
        if init_weights:
            self._initialize_weights()