我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用torch.nn.AvgPool2d()。
def __init__(self): super(mnist_model, self).__init__() self.feats = nn.Sequential( nn.Conv2d(1, 32, 5, 1, 1), nn.MaxPool2d(2, 2), nn.ReLU(True), nn.BatchNorm2d(32), nn.Conv2d(32, 64, 3, 1, 1), nn.ReLU(True), nn.BatchNorm2d(64), nn.Conv2d(64, 64, 3, 1, 1), nn.MaxPool2d(2, 2), nn.ReLU(True), nn.BatchNorm2d(64), nn.Conv2d(64, 128, 3, 1, 1), nn.ReLU(True), nn.BatchNorm2d(128) ) self.classifier = nn.Conv2d(128, 10, 1) self.avgpool = nn.AvgPool2d(6, 6) self.dropout = nn.Dropout(0.5)
def __init__(self, num_classes=1000, block=Bottleneck, layers=[3, 4, 23, 3]): super(ResNet_imagenet, 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, 256, layers[2], stride=2) self.layer4 = self._make_layer(block, 512, layers[3], stride=2) self.avgpool = nn.AvgPool2d(7) self.fc = nn.Linear(512 * block.expansion, num_classes) init_model(self) self.regime = [ {'epoch': 0, 'optimizer': 'SGD', 'lr': 1e-1, 'weight_decay': 1e-4, 'momentum': 0.9}, {'epoch': 30, 'lr': 1e-2}, {'epoch': 60, 'lr': 1e-3, 'weight_decay': 0}, {'epoch': 90, 'lr': 1e-4} ]
def __init__(self, num_classes=10, block=BasicBlock, depth=18): super(ResNet_cifar10, self).__init__() self.inplanes = 16 n = int((depth - 2) / 6) self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(16) self.relu = nn.ReLU(inplace=True) self.maxpool = lambda x: x self.layer1 = self._make_layer(block, 16, n) self.layer2 = self._make_layer(block, 32, n, stride=2) self.layer3 = self._make_layer(block, 64, n, stride=2) self.layer4 = lambda x: x self.avgpool = nn.AvgPool2d(8) self.fc = nn.Linear(64, num_classes) init_model(self) self.regime = [ {'epoch': 0, 'optimizer': 'SGD', 'lr': 1e-1, 'weight_decay': 1e-4, 'momentum': 0.9}, {'epoch': 81, 'lr': 1e-2}, {'epoch': 122, 'lr': 1e-3, 'weight_decay': 0}, {'epoch': 164, 'lr': 1e-4} ]
def __init__(self, block, layers, num_classes=1000): self.inplanes = 64 super(ResNet, 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) 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, 512, layers[3], stride=2) self.avgpool = nn.AvgPool2d(7) self.fc = nn.Linear(512 * block.expansion, num_classes) 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_()
def __init__(self, in_channels, theta, p): """ Initialize the different parts of the TransitionBlock. Params ------ - in_channels: number of input channels. - theta: compression factor in the range [0, 1]. Set to 0.5 in the paper when using DenseNet-BC. """ super(TransitionLayer, self).__init__() self.p = p self.out_channels = int(floor(theta*in_channels)) self.bn = nn.BatchNorm2d(in_channels) self.conv = nn.Conv2d(in_channels, self.out_channels, kernel_size=1) self.pool = nn.AvgPool2d(2)
def generate_model(): class MyModel(nn.Module): def __init__(self, pretrained_model): super(MyModel, self).__init__() self.pretrained_model = pretrained_model self.layer1 = pretrained_model.layer1 self.layer2 = pretrained_model.layer2 self.layer3 = pretrained_model.layer3 self.layer4 = pretrained_model.layer4 pretrained_model.avgpool = nn.AvgPool2d(8) classifier = [ nn.Linear(pretrained_model.fc.in_features, 17), ] self.classifier = nn.Sequential(*classifier) pretrained_model.fc = self.classifier def forward(self, x): return F.sigmoid(self.pretrained_model(x)) return MyModel(torchvision.models.resnet50(pretrained=True))
def generate_model(): class MyModel(nn.Module): def __init__(self, pretrained_model): super(MyModel, self).__init__() self.pretrained_model = pretrained_model self.layer1 = pretrained_model.layer1 self.layer2 = pretrained_model.layer2 self.layer3 = pretrained_model.layer3 self.layer4 = pretrained_model.layer4 pretrained_model.avgpool = nn.AvgPool2d(8) classifier = [ nn.Linear(pretrained_model.fc.in_features, 17), ] self.classifier = nn.Sequential(*classifier) pretrained_model.fc = self.classifier def forward(self, x): return F.sigmoid(self.pretrained_model(x)) return MyModel(torchvision.models.resnet34(pretrained=True))
def generate_model(): class MyModel(nn.Module): def __init__(self, pretrained_model): super(MyModel, self).__init__() self.pretrained_model = pretrained_model self.layer1 = pretrained_model.layer1 self.layer2 = pretrained_model.layer2 self.layer3 = pretrained_model.layer3 self.layer4 = pretrained_model.layer4 pretrained_model.avgpool = nn.AvgPool2d(8) classifier = [ nn.Linear(pretrained_model.fc.in_features, 17), ] self.classifier = nn.Sequential(*classifier) pretrained_model.fc = self.classifier def forward(self, x): return F.sigmoid(self.pretrained_model(x)) return MyModel(torchvision.models.resnet101(pretrained=True))
def generate_model(): class MyModel(nn.Module): def __init__(self, pretrained_model): super(MyModel, self).__init__() self.pretrained_model = pretrained_model self.layer1 = pretrained_model.layer1 self.layer2 = pretrained_model.layer2 self.layer3 = pretrained_model.layer3 self.layer4 = pretrained_model.layer4 pretrained_model.avgpool = nn.AvgPool2d(8) classifier = [ nn.Linear(pretrained_model.fc.in_features, 17), ] self.classifier = nn.Sequential(*classifier) pretrained_model.fc = self.classifier def forward(self, x): return self.pretrained_model(x) return MyModel(torchvision.models.resnet18(pretrained=True))
def __init__(self, block, layers, num_classes=1000): self.inplanes = 64 super(ResNet, 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=0, ceil_mode=True) # change 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, 512, layers[3], stride=2) self.avgpool = nn.AvgPool2d(7) self.fc = nn.Linear(512 * block.expansion, num_classes) 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_()
def __init__(self,out_size,gpu_id,num_seg): super(VC_inception_v4,self).__init__() sys.path.insert(0,'../tool/models_zoo/') from inceptionv4.pytorch_load import inceptionv4 self.inception_v4=inceptionv4(pretrained=True).cuda() mod=[nn.Dropout(p=0.8)]#.cuda(self.gpu_id)] mod.append(nn.Linear(1536,101))#.cuda(self.gpu_id)) new_fc=nn.Sequential(*mod)#.cuda(self.gpu_id) self.inception_v4.classif=new_fc self.num_seg=num_seg #self.resnet101.fc=nn.Linear(2048,101).cuda(gpu_id) self.avg_pool2d=nn.AvgPool2d(kernel_size=(3,1))#.cuda(self.gpu_id) # for params in self.inception_v4.parameters(): # params.requires_grad=False # for params in self.inception_v4.features[21].parameters(): # params.requires_grad=True
def __init__(self): super().__init__() self.conv1 = nn.Conv2d(1, 256, 3, padding=1) self.bn1 = nn.BatchNorm2d(256) self.max1 = nn.MaxPool2d(3) self.conv2 = nn.Conv2d(256, 512, 3, padding=1) self.bn2 = nn.BatchNorm2d(512) self.max2 = nn.MaxPool2d(3) self.conv3 = nn.Conv2d(512, 1024, 3, padding=1) self.bn3 = nn.BatchNorm2d(1024) self.avg1 = nn.AvgPool2d(3) self.fc_mu = nn.Linear(1024, latent_space_size) self.fc_sig = nn.Linear(1024, latent_space_size)
def __init__(self, in_planes, out_planes, pool_method, stride): super(Block, self).__init__() self.branches = nn.ModuleList([ nn.Sequential( _make_conv(in_planes, out_planes, kernel_size=1, padding=0), _make_conv(out_planes, out_planes, stride=stride) ), nn.Sequential( _make_conv(in_planes, out_planes, kernel_size=1, padding=0), _make_conv(out_planes, out_planes), _make_conv(out_planes, out_planes, stride=stride)) ]) if pool_method == 'Avg': assert stride == 1 self.branches.append( _make_conv(in_planes, out_planes, kernel_size=1, padding=0)) self.branches.append(nn.Sequential( nn.AvgPool2d(kernel_size=3, stride=1, padding=1), _make_conv(in_planes, out_planes, kernel_size=1, padding=0))) else: self.branches.append( nn.MaxPool2d(kernel_size=3, stride=stride, padding=1))
def _make_layers(self, cfg): layers = [] in_channels = 3 for x in cfg: if x == 'M': layers += [nn.MaxPool2d(kernel_size=2, stride=2)] else: layers += [nn.Conv2d(in_channels, x, kernel_size=3, padding=1), nn.BatchNorm2d(x), nn.ReLU(inplace=True)] in_channels = x layers += [nn.AvgPool2d(kernel_size=1, stride=1)] return nn.Sequential(*layers) # net = VGG('VGG11') # x = torch.randn(2,3,32,32) # print(net(Variable(x)).size())
def __init__(self): super(GoogLeNet, self).__init__() self.pre_layers = nn.Sequential( nn.Conv2d(3, 192, kernel_size=3, padding=1), nn.BatchNorm2d(192), nn.ReLU(True), ) self.a3 = Inception(192, 64, 96, 128, 16, 32, 32) self.b3 = Inception(256, 128, 128, 192, 32, 96, 64) self.maxpool = nn.MaxPool2d(3, stride=2, padding=1) self.a4 = Inception(480, 192, 96, 208, 16, 48, 64) self.b4 = Inception(512, 160, 112, 224, 24, 64, 64) self.c4 = Inception(512, 128, 128, 256, 24, 64, 64) self.d4 = Inception(512, 112, 144, 288, 32, 64, 64) self.e4 = Inception(528, 256, 160, 320, 32, 128, 128) self.a5 = Inception(832, 256, 160, 320, 32, 128, 128) self.b5 = Inception(832, 384, 192, 384, 48, 128, 128) self.avgpool = nn.AvgPool2d(8, stride=1) self.linear = nn.Linear(1024, 10)
def __init__(self, block, layers, num_classes=1000): self.inplanes = 64 super(ResNet, 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=1, 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, 512, layers[3], stride=2) self.avgpool = nn.AvgPool2d(14) self.fc = nn.Linear(512 * block.expansion, num_classes) 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_()
def __init__(self, block, layers, embedding_size=64): self.inplanes = 64 super(ResNet, 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) 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.avgpool = nn.AvgPool2d(7) self.fc_embed = nn.Linear(256 * block.expansion, embedding_size) 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_()
def __init__(self, block, layers, nb_classes=101, channel=20): self.inplanes = 64 super(ResNet, self).__init__() self.conv1_custom = nn.Conv2d(channel, 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, 256, layers[2], stride=2) self.layer4 = self._make_layer(block, 512, layers[3], stride=2) self.avgpool = nn.AvgPool2d(7) self.fc_custom = nn.Linear(512 * block.expansion, nb_classes) 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_()
def __init__(self, block, layers, channels, groups=1, num_classes=1000, downsample='basic'): super().__init__() assert len(layers) == 3 self.downsample_mode = downsample self.inplanes = 16 self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1, bias=False) self.layer1 = self._make_layer(block, channels, groups, layers[0]) self.layer2 = self._make_layer( block, channels * 2, groups, layers[1], stride=2) self.layer3 = self._make_layer( block, channels * 4, groups, layers[2], stride=2) self.avgpool = nn.AvgPool2d(8) self.fc1 = nn.Linear(block.out_channels( channels * 4, groups), num_classes) self.fc2 = nn.Linear(block.out_channels( channels * 4, groups), num_classes) 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_()
def add_poolconv(self, kernel, n_out, type='MAX'): assert(type in ['AVE', 'MAX']) n_last = self.n_in layers = [] # Pooling if type == 'MAX': layers.append(nn.MaxPool2d(kernel, padding=int(kernel/2), stride=self.in_stride)) elif type == 'AVE': layers.append(nn.AvgPool2d(kernel, padding=int(kernel/2), stride=self.in_stride)) # Conv - BN - Act layers.append(nn.Conv2d(n_last, n_out, kernel_size=1)) layers.append(nn.BatchNorm2d(n_out)) layers.append(self.act_func()) self.add_branch(nn.Sequential(*layers), n_out) return self
def __init__(self, block, layers, num_classes=1000): self.inplanes = 64 super(ResNet34, 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) 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, 512, layers[3], stride=2) self.avgpool = nn.AvgPool2d(7) self.fc_drop = nn.Dropout(p=0.75) self.fc = nn.Linear(512 * block.expansion, num_classes) 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_()
def __init__(self, classes=None, debug=False): super(RFCN, self).__init__() if classes is not None: self.classes = classes self.n_classes = len(classes) self.rpn = RPN() #self.psroi_pool = PSRoIPool(7,7,1.0/16,7,15) This is for test self.psroi_pool_cls = PSRoIPool(7,7, 1.0/16, 7, self.n_classes) self.psroi_pool_loc = PSRoIPool(7,7, 1.0/16, 7, 8) self.new_conv = Conv2d(512, 1024, 1, same_padding=False) self.rfcn_score = Conv2d(1024,7*7*8, 1,1, bn=False) self.rfcn_bbox = Conv2d(1024, 7*7*self.n_classes,1,1,bn=False) self.bbox_pred = nn.AvgPool2d((7,7),stride=(7,7)) self.cls_score = nn.AvgPool2d((7,7),stride=(7,7)) # loss self.cross_entropy = None self.loss_box = None # for log self.debug = debug
def __init__(self): super(Mixed_5b, self).__init__() self.branch0 = BasicConv2d(192, 96, kernel_size=1, stride=1) self.branch1 = nn.Sequential( BasicConv2d(192, 48, kernel_size=1, stride=1), BasicConv2d(48, 64, kernel_size=5, stride=1, padding=2) ) self.branch2 = nn.Sequential( BasicConv2d(192, 64, kernel_size=1, stride=1), BasicConv2d(64, 96, kernel_size=3, stride=1, padding=1), BasicConv2d(96, 96, kernel_size=3, stride=1, padding=1) ) self.branch3 = nn.Sequential( nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False), BasicConv2d(192, 64, kernel_size=1, stride=1) )
def __init__(self): super(Inception_A, self).__init__() self.branch0 = BasicConv2d(384, 96, kernel_size=1, stride=1) self.branch1 = nn.Sequential( BasicConv2d(384, 64, kernel_size=1, stride=1), BasicConv2d(64, 96, kernel_size=3, stride=1, padding=1) ) self.branch2 = nn.Sequential( BasicConv2d(384, 64, kernel_size=1, stride=1), BasicConv2d(64, 96, kernel_size=3, stride=1, padding=1), BasicConv2d(96, 96, kernel_size=3, stride=1, padding=1) ) self.branch3 = nn.Sequential( nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False), BasicConv2d(384, 96, kernel_size=1, stride=1) )
def __init__(self): super(Inception_B, self).__init__() self.branch0 = BasicConv2d(1024, 384, kernel_size=1, stride=1) self.branch1 = nn.Sequential( BasicConv2d(1024, 192, kernel_size=1, stride=1), BasicConv2d(192, 224, kernel_size=(1, 7), stride=1, padding=(0, 3)), BasicConv2d(224, 256, kernel_size=(7, 1), stride=1, padding=(3, 0)) ) self.branch2 = nn.Sequential( BasicConv2d(1024, 192, kernel_size=1, stride=1), BasicConv2d(192, 192, kernel_size=(7, 1), stride=1, padding=(3, 0)), BasicConv2d(192, 224, kernel_size=(1, 7), stride=1, padding=(0, 3)), BasicConv2d(224, 224, kernel_size=(7, 1), stride=1, padding=(3, 0)), BasicConv2d(224, 256, kernel_size=(1, 7), stride=1, padding=(0, 3)) ) self.branch3 = nn.Sequential( nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False), BasicConv2d(1024, 128, kernel_size=1, stride=1) )
def __init__(self): super(Inception_C, self).__init__() self.branch0 = BasicConv2d(1536, 256, kernel_size=1, stride=1) self.branch1_0 = BasicConv2d(1536, 384, kernel_size=1, stride=1) self.branch1_1a = BasicConv2d(384, 256, kernel_size=(1, 3), stride=1, padding=(0, 1)) self.branch1_1b = BasicConv2d(384, 256, kernel_size=(3, 1), stride=1, padding=(1, 0)) self.branch2_0 = BasicConv2d(1536, 384, kernel_size=1, stride=1) self.branch2_1 = BasicConv2d(384, 448, kernel_size=(3, 1), stride=1, padding=(1, 0)) self.branch2_2 = BasicConv2d(448, 512, kernel_size=(1, 3), stride=1, padding=(0, 1)) self.branch2_3a = BasicConv2d(512, 256, kernel_size=(1, 3), stride=1, padding=(0, 1)) self.branch2_3b = BasicConv2d(512, 256, kernel_size=(3, 1), stride=1, padding=(1, 0)) self.branch3 = nn.Sequential( nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False), BasicConv2d(1536, 256, kernel_size=1, stride=1) )
def __init__(self, block, k=1,n=2,num_classes=10): self.inplanes = 16 super(ResNet, self).__init__() self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(16) self.relu = nn.ReLU(inplace=True) self.layer1 = self._make_layer(block, 16*k, n) self.layer2 = self._make_layer(block, 32*k, n, stride=2) self.layer3 = self._make_layer(block, 64*k, n, stride=2) self.avgpool = nn.AvgPool2d(8) self.fc = nn.Linear(64*k, num_classes) 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_()
def __init__(self, block,J=2,N=32, k=1,n=4,num_classes=10): self.inplanes = 32*k self.ichannels = 32*k super(ResNet, self).__init__() self.nspace = N / (2 ** J) self.nfscat = (1 + 8 * J + 8 * 8 * J * (J - 1) / 2) self.bn0 = nn.BatchNorm2d(self.nfscat*3,eps=1e-5,affine=False) self.conv1 = nn.Conv2d(self.nfscat*3,self.ichannels, kernel_size=3, stride=1, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(self.ichannels) self.relu = nn.ReLU(inplace=True) self.layer2 = self._make_layer(block, 32*k, n) self.layer3 = self._make_layer(block, 64*k, n,stride=2) self.avgpool = nn.AvgPool2d(12) self.fc = nn.Linear(64*k, num_classes) 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): if m.affine: m.weight.data.fill_(1) m.bias.data.zero_()
def __init__(self, block, layers, num_classes=1000): self.inplanes = 16 super(PreResNet, self).__init__() self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1, bias=False) self.layer1 = self._make_layer(block, 16, layers[0]) self.layer2 = self._make_layer(block, 32, layers[1], stride=2) self.layer3 = self._make_layer(block, 64, layers[2], stride=2) self.bn1 = nn.BatchNorm2d(64*block.expansion) self.relu = nn.ReLU(inplace=True) self.fc1 = nn.Conv2d(64*block.expansion, 64*block.expansion, kernel_size=1, bias=False) self.bn2 = nn.BatchNorm2d(64*block.expansion) self.fc2 = nn.Conv2d(64*block.expansion, num_classes, kernel_size=1) # self.avgpool = nn.AvgPool2d(8) # self.fc = nn.Linear(64*block.expansion, num_classes) # 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_()
def __init__(self, block, layers, num_classes=1000): self.inplanes = 64 super(ResNet, 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) 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, 512, layers[3], stride=2) self.avgpool = nn.AvgPool2d(7) #self.fc = nn.Linear(512 * block.expansion, num_classes) self.out = nn.Linear(512 * block.expansion, num_classes) 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_()
def __init__(self, dim, pool='max', size=None, stride=None, verbose=False): super(PoolBlock, self).__init__() stride = size if stride is None else stride if size is not None: dim[1:] = np.floor(dim[1:] / stride) else: size = [int(x) for x in dim[1:]] dim[1:] = 1 if pool == 'max': self.pool = nn.MaxPool2d(size, size) elif pool == 'avg': self.pool = nn.AvgPool2d(size, size) self.n_params = 0 if verbose: pass
def __init__(self): super(Inception_C, self).__init__() self.branch0 = BasicConv2d(1536, 256, kernel_size=1, stride=1) self.branch1_0 = BasicConv2d(1536, 384, kernel_size=1, stride=1) self.branch1_1a = BasicConv2d(384, 256, kernel_size=(1,3), stride=1, padding=(0,1)) self.branch1_1b = BasicConv2d(384, 256, kernel_size=(3,1), stride=1, padding=(1,0)) self.branch2_0 = BasicConv2d(1536, 384, kernel_size=1, stride=1) self.branch2_1 = BasicConv2d(384, 448, kernel_size=(3,1), stride=1, padding=(1,0)) self.branch2_2 = BasicConv2d(448, 512, kernel_size=(1,3), stride=1, padding=(0,1)) self.branch2_3a = BasicConv2d(512, 256, kernel_size=(1,3), stride=1, padding=(0,1)) self.branch2_3b = BasicConv2d(512, 256, kernel_size=(3,1), stride=1, padding=(1,0)) self.branch3 = nn.Sequential( nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False), BasicConv2d(1536, 256, kernel_size=1, stride=1) )
def __init__(self, block, layers, num_classes=1000): self.inplanes = 64 super(ResNet, self).__init__() self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=True) #self.conv1 = nnl.SpatialConvolution(3, 64, 7, 7, 2, 2, 3, 3) 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, 256, layers[2], stride=2) self.layer4 = self._make_layer(block, 512, layers[3], stride=2) self.avgpool = nn.AvgPool2d(7) self.fc = nn.Linear(512 * block.expansion, num_classes) 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_()
def __init__(self, in_channels_left, out_channels_left, in_channels_right, out_channels_right): super(NormalCell, self).__init__() self.conv_prev_1x1 = nn.Sequential() self.conv_prev_1x1.add_module('relu', nn.ReLU()) self.conv_prev_1x1.add_module('conv', nn.Conv2d(in_channels_left, out_channels_left, 1, stride=1, bias=False)) self.conv_prev_1x1.add_module('bn', nn.BatchNorm2d(out_channels_left, eps=0.001, momentum=0.1, affine=True)) self.conv_1x1 = nn.Sequential() self.conv_1x1.add_module('relu', nn.ReLU()) self.conv_1x1.add_module('conv', nn.Conv2d(in_channels_right, out_channels_right, 1, stride=1, bias=False)) self.conv_1x1.add_module('bn', nn.BatchNorm2d(out_channels_right, eps=0.001, momentum=0.1, affine=True)) self.comb_iter_0_left = BranchSeparables(out_channels_right, out_channels_right, 5, 1, 2, bias=False) self.comb_iter_0_right = BranchSeparables(out_channels_left, out_channels_left, 3, 1, 1, bias=False) self.comb_iter_1_left = BranchSeparables(out_channels_left, out_channels_left, 5, 1, 2, bias=False) self.comb_iter_1_right = BranchSeparables(out_channels_left, out_channels_left, 3, 1, 1, bias=False) self.comb_iter_2_left = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False) self.comb_iter_3_left = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False) self.comb_iter_3_right = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False) self.comb_iter_4_left = BranchSeparables(out_channels_right, out_channels_right, 3, 1, 1, bias=False)
def __init__(self, in_channels_left, out_channels_left, in_channels_right, out_channels_right): super(ReductionCell0, self).__init__() self.conv_prev_1x1 = nn.Sequential() self.conv_prev_1x1.add_module('relu', nn.ReLU()) self.conv_prev_1x1.add_module('conv', nn.Conv2d(in_channels_left, out_channels_left, 1, stride=1, bias=False)) self.conv_prev_1x1.add_module('bn', nn.BatchNorm2d(out_channels_left, eps=0.001, momentum=0.1, affine=True)) self.conv_1x1 = nn.Sequential() self.conv_1x1.add_module('relu', nn.ReLU()) self.conv_1x1.add_module('conv', nn.Conv2d(in_channels_right, out_channels_right, 1, stride=1, bias=False)) self.conv_1x1.add_module('bn', nn.BatchNorm2d(out_channels_right, eps=0.001, momentum=0.1, affine=True)) self.comb_iter_0_left = BranchSeparablesReduction(out_channels_right, out_channels_right, 5, 2, 2, bias=False) self.comb_iter_0_right = BranchSeparablesReduction(out_channels_right, out_channels_right, 7, 2, 3, bias=False) self.comb_iter_1_left = MaxPoolPad() self.comb_iter_1_right = BranchSeparablesReduction(out_channels_right, out_channels_right, 7, 2, 3, bias=False) self.comb_iter_2_left = AvgPoolPad() self.comb_iter_2_right = BranchSeparablesReduction(out_channels_right, out_channels_right, 5, 2, 2, bias=False) self.comb_iter_3_right = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False) self.comb_iter_4_left = BranchSeparablesReduction(out_channels_right, out_channels_right, 3, 1, 1, bias=False) self.comb_iter_4_right = MaxPoolPad()
def __init__(self, in_channels_left, out_channels_left, in_channels_right, out_channels_right): super(ReductionCell1, self).__init__() self.conv_prev_1x1 = nn.Sequential() self.conv_prev_1x1.add_module('relu', nn.ReLU()) self.conv_prev_1x1.add_module('conv', nn.Conv2d(in_channels_left, out_channels_left, 1, stride=1, bias=False)) self.conv_prev_1x1.add_module('bn', nn.BatchNorm2d(out_channels_left, eps=0.001, momentum=0.1, affine=True)) self.conv_1x1 = nn.Sequential() self.conv_1x1.add_module('relu', nn.ReLU()) self.conv_1x1.add_module('conv', nn.Conv2d(in_channels_right, out_channels_right, 1, stride=1, bias=False)) self.conv_1x1.add_module('bn', nn.BatchNorm2d(out_channels_right, eps=0.001, momentum=0.1, affine=True)) self.comb_iter_0_left = BranchSeparables(out_channels_right, out_channels_right, 5, 2, 2, bias=False) self.comb_iter_0_right = BranchSeparables(out_channels_right, out_channels_right, 7, 2, 3, bias=False) self.comb_iter_1_left = nn.MaxPool2d(3, stride=2, padding=1) self.comb_iter_1_right = BranchSeparables(out_channels_right, out_channels_right, 7, 2, 3, bias=False) self.comb_iter_2_left = nn.AvgPool2d(3, stride=2, padding=1, count_include_pad=False) self.comb_iter_2_right = BranchSeparables(out_channels_right, out_channels_right, 5, 2, 2, bias=False) self.comb_iter_3_right = nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False) self.comb_iter_4_left = BranchSeparables(out_channels_right, out_channels_right, 3, 1, 1, bias=False) self.comb_iter_4_right = nn.MaxPool2d(3, stride=2, padding=1)