我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用torch.nn.MaxPool2d()。
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, bn=False): super(MCNN, self).__init__() self.branch1 = nn.Sequential(Conv2d( 1, 16, 9, same_padding=True, bn=bn), nn.MaxPool2d(2), Conv2d(16, 32, 7, same_padding=True, bn=bn), nn.MaxPool2d(2), Conv2d(32, 16, 7, same_padding=True, bn=bn), Conv2d(16, 8, 7, same_padding=True, bn=bn)) self.branch2 = nn.Sequential(Conv2d( 1, 20, 7, same_padding=True, bn=bn), nn.MaxPool2d(2), Conv2d(20, 40, 5, same_padding=True, bn=bn), nn.MaxPool2d(2), Conv2d(40, 20, 5, same_padding=True, bn=bn), Conv2d(20, 10, 5, same_padding=True, bn=bn)) self.branch3 = nn.Sequential(Conv2d( 1, 24, 5, same_padding=True, bn=bn), nn.MaxPool2d(2), Conv2d(24, 48, 3, same_padding=True, bn=bn), nn.MaxPool2d(2), Conv2d(48, 24, 3, same_padding=True, bn=bn), Conv2d(24, 12, 3, same_padding=True, bn=bn)) self.fuse = nn.Sequential(Conv2d( 30, 1, 1, same_padding=True, bn=bn))
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, 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 test_MaxUnpool2d_output_size(self): m = nn.MaxPool2d(3, stride=2, return_indices=True) mu = nn.MaxUnpool2d(3, stride=2) big_t = torch.rand(1, 1, 6, 6) big_t[0][0][4][4] = 100 output_big, indices_big = m(Variable(big_t)) self.assertRaises(RuntimeError, lambda: mu(output_big, indices_big)) small_t = torch.rand(1, 1, 5, 5) for i in range(0, 4, 2): for j in range(0, 4, 2): small_t[:,:,i,j] = 100 output_small, indices_small = m(Variable(small_t)) for h in range(3, 10): for w in range(3, 10): if 4 <= h <= 6 and 4 <= w <= 6: size = (h, w) if h == 5: size = torch.LongStorage(size) elif h == 6: size = torch.LongStorage((1, 1) + size) mu(output_small, indices_small, output_size=size) else: self.assertRaises(ValueError, lambda: mu(output_small, indices_small, (h, w)))
def vgg(cfg, i, batch_norm=False): layers = [] in_channels = i for v in cfg: if v == 'M': layers += [nn.MaxPool2d(kernel_size=2, stride=2)] elif v == 'C': layers += [nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True)] else: conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1) if batch_norm: layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)] else: layers += [conv2d, nn.ReLU(inplace=True)] in_channels = v pool5 = nn.MaxPool2d(kernel_size=3, stride=1, padding=1) conv6 = nn.Conv2d(512, 1024, kernel_size=3, padding=6, dilation=6) conv7 = nn.Conv2d(1024, 1024, kernel_size=1) layers += [pool5, conv6, nn.ReLU(inplace=True), conv7, nn.ReLU(inplace=True)] return layers
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, opt): super(resnet_mil, self).__init__() import model.resnet as resnet resnet = resnet.resnet101() resnet.load_state_dict(torch.load('/media/jxgu/d2tb/model/resnet/resnet101.pth')) self.conv = torch.nn.Sequential() self.conv.add_module("conv1", resnet.conv1) self.conv.add_module("bn1", resnet.bn1) self.conv.add_module("relu", resnet.relu) self.conv.add_module("maxpool", resnet.maxpool) self.conv.add_module("layer1", resnet.layer1) self.conv.add_module("layer2", resnet.layer2) self.conv.add_module("layer3", resnet.layer3) self.conv.add_module("layer4", resnet.layer4) self.l1 = nn.Sequential(nn.Linear(2048, 1000), nn.ReLU(True), nn.Dropout(0.5)) self.att_size = 7 self.pool_mil = nn.MaxPool2d(kernel_size=self.att_size, stride=0)
def _make_test_model(): import torch.nn as nn from inferno.extensions.layers.reshape import AsMatrix toy_net = nn.Sequential(nn.Conv2d(3, 128, 3, 1, 1), nn.ELU(), nn.MaxPool2d(2), nn.Conv2d(128, 128, 3, 1, 1), nn.ELU(), nn.MaxPool2d(2), nn.Conv2d(128, 256, 3, 1, 1), nn.ELU(), nn.AdaptiveAvgPool2d((1, 1)), AsMatrix(), nn.Linear(256, 10), nn.Softmax()) return toy_net
def build_graph_model(self): model = Graph() model\ .add_input_node('input')\ .add_node('conv1', Conv2D(3, 32, 3), 'input')\ .add_node('conv2', BNReLUConv2D(32, 32, 3), 'conv1')\ .add_node('pool1', nn.MaxPool2d(kernel_size=2, stride=2), 'conv2')\ .add_node('conv3', BNReLUConv2D(32, 32, 3), 'pool1')\ .add_node('pool2', nn.MaxPool2d(kernel_size=2, stride=2), 'conv3')\ .add_node('conv4', BNReLUConv2D(32, 32, 3), 'pool2')\ .add_node('pool3', nn.AdaptiveAvgPool2d(output_size=(1, 1)), 'conv4')\ .add_node('matrix', AsMatrix(), 'pool3')\ .add_node('linear', nn.Linear(32, self.NUM_CLASSES), 'matrix')\ .add_node('softmax', nn.Softmax(), 'linear')\ .add_output_node('output', 'softmax') return model
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 __init__(self): super(LeNetSeq, self).__init__() self.conv = nn.Sequential( nn.Conv2d(3, 6, 5), nn.ReLU(), nn.MaxPool2d(2), nn.Conv2d(6, 16, 5), nn.ReLU(), nn.MaxPool2d(2), ) self.fc = nn.Sequential( nn.Linear(16*5*5, 120), nn.ReLU(), nn.Linear(120, 84), nn.ReLU(), nn.Linear(84, 10) )
def __init__(self, bn=False): super(VGG16, self).__init__() self.conv1 = nn.Sequential(Conv2d(3, 64, 3, same_padding=True, bn=bn), Conv2d(64, 64, 3, same_padding=True, bn=bn), nn.MaxPool2d(2)) self.conv2 = nn.Sequential(Conv2d(64, 128, 3, same_padding=True, bn=bn), Conv2d(128, 128, 3, same_padding=True, bn=bn), nn.MaxPool2d(2)) network.set_trainable(self.conv1, requires_grad=False) network.set_trainable(self.conv2, requires_grad=False) self.conv3 = nn.Sequential(Conv2d(128, 256, 3, same_padding=True, bn=bn), Conv2d(256, 256, 3, same_padding=True, bn=bn), Conv2d(256, 256, 3, same_padding=True, bn=bn), nn.MaxPool2d(2)) self.conv4 = nn.Sequential(Conv2d(256, 512, 3, same_padding=True, bn=bn), Conv2d(512, 512, 3, same_padding=True, bn=bn), Conv2d(512, 512, 3, same_padding=True, bn=bn), nn.MaxPool2d(2)) self.conv5 = nn.Sequential(Conv2d(512, 512, 3, same_padding=True, bn=bn), Conv2d(512, 512, 3, same_padding=True, bn=bn), Conv2d(512, 512, 3, same_padding=True, bn=bn))
def __init__(self): """Init LeNet encoder.""" super(Generator, self).__init__() self.restored = False self.encoder = nn.Sequential( # 1st conv block # input [1 x 28 x 28] # output [64 x 12 x 12] nn.Conv2d(1, 64, 5, 1, 0, bias=False), nn.MaxPool2d(2), nn.ReLU(), # 2nd conv block # input [64 x 12 x 12] # output [50 x 4 x 4] nn.Conv2d(64, 50, 5, 1, 0, bias=False), nn.Dropout2d(), nn.MaxPool2d(2), nn.ReLU() ) self.fc1 = nn.Linear(50 * 4 * 4, 500)
def convLayer(opt, layer_pos, nInput, nOutput, k ): "3x3 convolution with padding" #if 'BN_momentum' in opt.keys(): # batchNorm = nn.BatchNorm2d(nOutput,momentum=opt['BN_momentum']) #else: # batchNorm = nn.BatchNorm2d(nOutput) seq = nn.Sequential( nn.Conv2d(nInput, nOutput, kernel_size=k, stride=1, padding=1, bias=True), #batchNorm, opt['bnorm2d'][layer_pos], nn.ReLU(True), nn.MaxPool2d(kernel_size=2, stride=2) ) if opt['useDropout']: # Add dropout module list_seq = list(seq.modules())[1:] list_seq.append(nn.Dropout(0.1)) seq = nn.Sequential(*list_seq) return seq
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 build_cnn(feat_dim=(1024, 14, 14), res_block_dim=128, num_res_blocks=0, proj_dim=512, pooling='maxpool2'): C, H, W = feat_dim layers = [] if num_res_blocks > 0: layers.append(nn.Conv2d(C, res_block_dim, kernel_size=3, padding=1)) layers.append(nn.ReLU(inplace=True)) C = res_block_dim for _ in range(num_res_blocks): layers.append(ResidualBlock(C)) if proj_dim > 0: layers.append(nn.Conv2d(C, proj_dim, kernel_size=1, padding=0)) layers.append(nn.ReLU(inplace=True)) C = proj_dim if pooling == 'maxpool2': layers.append(nn.MaxPool2d(kernel_size=2, stride=2)) H, W = H // 2, W // 2 return nn.Sequential(*layers), (C, H, W)
def _make_layers(in_channels, net_cfg): layers = [] if len(net_cfg) > 0 and isinstance(net_cfg[0], list): for sub_cfg in net_cfg: layer, in_channels = _make_layers(in_channels, sub_cfg) layers.append(layer) else: for item in net_cfg: if item == 'M': layers.append(nn.MaxPool2d(kernel_size=2, stride=2)) else: out_channels, ksize = item layers.append(net_utils.Conv2d_BatchNorm(in_channels, out_channels, ksize, same_padding=True)) # layers.append(net_utils.Conv2d(in_channels, out_channels, ksize, same_padding=True)) in_channels = out_channels return nn.Sequential(*layers), in_channels
def __init__(self): """Init LeNet encoder.""" super(LeNetEncoder, self).__init__() self.restored = False self.encoder = nn.Sequential( # 1st conv layer # input [1 x 28 x 28] # output [20 x 12 x 12] nn.Conv2d(1, 20, kernel_size=5), nn.MaxPool2d(kernel_size=2), nn.ReLU(), # 2nd conv layer # input [20 x 12 x 12] # output [50 x 4 x 4] nn.Conv2d(20, 50, kernel_size=5), nn.Dropout2d(), nn.MaxPool2d(kernel_size=2), nn.ReLU() ) self.fc1 = nn.Linear(50 * 4 * 4, 500)
def __init__(self): super().__init__() self.down1 = UNetDownBlock(self.n_channels, 64) self.down2 = UNetDownBlock( 64, 128) self.down3 = UNetDownBlock( 128, 256) self.down4 = UNetDownBlock( 256, 512) self.down5 = UNetDownBlock( 512,1024) self.pool1 = nn.MaxPool2d(2) self.pool2 = nn.MaxPool2d(2) self.pool3 = nn.MaxPool2d(2) self.pool4 = nn.MaxPool2d(2) self.up4 = UNetUpBlock(1024, 512) self.up3 = UNetUpBlock( 512, 256) self.up2 = UNetUpBlock( 256, 128) self.up1 = UNetUpBlock( 128, 64) self.classify = nn.Conv2d(64, self.n_classes, 1) return
def __init__(self): super().__init__() self.down1 = UNetDownBlock(self.n_channels, 64) self.down2 = UNetDownBlock( 64, 128) self.down3 = UNetDownBlock( 128, 256) self.down4 = UNetDownBlock( 256, 512) self.down5 = UNetDownBlock( 512,1024) self.pool1 = nn.MaxPool2d(2) self.pool2 = nn.MaxPool2d(2) self.pool3 = nn.MaxPool2d(2) self.pool4 = nn.MaxPool2d(2) self.up4 = UNetUpBlock(512+1024, 512, up='upsample') self.up3 = UNetUpBlock( 256+512, 256, up='upsample') self.up2 = UNetUpBlock( 128+256, 128, up='upsample') self.up1 = UNetUpBlock( 64+128, 64, up='upsample') self.classify = nn.Conv2d(64, self.n_classes, 1) return
def test_MaxUnpool2d_output_size(self): m = nn.MaxPool2d(3, stride=2, return_indices=True) mu = nn.MaxUnpool2d(3, stride=2) big_t = torch.rand(1, 1, 6, 6) big_t[0][0][4][4] = 100 output_big, indices_big = m(Variable(big_t)) self.assertRaises(RuntimeError, lambda: mu(output_big, indices_big)) small_t = torch.rand(1, 1, 5, 5) for i in range(0, 4, 2): for j in range(0, 4, 2): small_t[:, :, i, j] = 100 output_small, indices_small = m(Variable(small_t)) for h in range(3, 10): for w in range(3, 10): if 4 <= h <= 6 and 4 <= w <= 6: size = (h, w) if h == 5: size = torch.LongStorage(size) elif h == 6: size = torch.LongStorage((1, 1) + size) mu(output_small, indices_small, output_size=size) else: self.assertRaises(ValueError, lambda: mu(output_small, indices_small, (h, w)))
def build_base(cfg, i, batch_norm=False): layers = [] in_channels = i for v in cfg: if v == 'M': layers += [nn.MaxPool2d(kernel_size=2, stride=2)] elif v == 'C': layers += [nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True)] else: conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1) if batch_norm: layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)] else: layers += [conv2d, nn.ReLU(inplace=True)] in_channels = v return nn.Sequential(*layers)
def __init__(self, block, layers, num_classes): self.inplanes = 64 super(ResNet_Refine, self).__init__() self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = nn.BatchNorm2d(64, affine = affine_par) for i in self.bn1.parameters(): i.requires_grad = False self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1, 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=1, dilation=2) self.layer4 = self._make_layer(block, 512, layers[3], stride=1, dilation=4) self.layer5 = Residual_Refinement_Module(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, 0.01) elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() # for i in m.parameters(): # i.requires_grad = False
def __init__(self, block, layers, num_classes): 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, affine = affine_par) for i in self.bn1.parameters(): i.requires_grad = False self.relu = nn.ReLU(inplace=True) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1, 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=1, dilation=2) self.layer4 = self._make_layer(block, 512, layers[3], stride=1, dilation=4) self.layer5 = self._make_pred_layer(Classifier_Module, [6,12,18,24],[6,12,18,24],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, 0.01) elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() # for i in m.parameters(): # i.requires_grad = False
def _make_layers(in_channels, net_cfg): layers = [] if len(net_cfg) > 0 and isinstance(net_cfg[0], list): for sub_cfg in net_cfg: layer, in_channels = _make_layers(in_channels, sub_cfg) layers.append(layer) else: for item in net_cfg: if item == 'M': layers.append(nn.MaxPool2d(kernel_size=2, stride=2)) else: out_channels, ksize = item layers.append(utils_module.Conv2d_BatchNorm(in_channels, out_channels, ksize, same_padding=True)) # layers.append(net_utils.Conv2d(in_channels, out_channels, ksize, same_padding=True)) in_channels = out_channels return nn.Sequential(*layers), in_channels
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, hps: HyperParams): super().__init__(hps) self.pool = nn.MaxPool2d(2, 2) self.pool_top = nn.MaxPool2d(hps.top_scale, hps.top_scale) self.upsample = nn.UpsamplingNearest2d(scale_factor=2) self.upsample_top = nn.UpsamplingNearest2d(scale_factor=hps.top_scale) filter_sizes = [hps.filters_base * s for s in self.filter_factors] self.down, self.up = [], [] for i, nf in enumerate(filter_sizes): low_nf = hps.n_channels if i == 0 else filter_sizes[i - 1] self.down.append(self.module(hps, low_nf, nf)) setattr(self, 'down_{}'.format(i), self.down[-1]) if i != 0: self.up.append(self.module(hps, low_nf + nf, low_nf)) setattr(self, 'conv_up_{}'.format(i), self.up[-1]) self.conv_final = nn.Conv2d(filter_sizes[0], hps.n_classes, 1)
def __init__(self, hps): super().__init__(hps) s = hps.filters_base self.pool = nn.MaxPool2d(2, 2) self.upsample = nn.UpsamplingNearest2d(scale_factor=2) self.input_conv = BasicConv2d(hps.n_channels, s, 1) self.enc_1 = BasicConv2d(s * 1, s * 2, 3, padding=1) self.enc_2 = BasicConv2d(s * 2, s * 4, 3, padding=1) self.enc_3 = BasicConv2d(s * 4, s * 8, 3, padding=1) self.enc_4 = BasicConv2d(s * 8, s * 8, 3, padding=1) # https://github.com/pradyu1993/segnet - decoder lacks relu (???) self.dec_4 = BasicConv2d(s * 8, s * 8, 3, padding=1) self.dec_3 = BasicConv2d(s * 8, s * 4, 3, padding=1) self.dec_2 = BasicConv2d(s * 4, s * 2, 3, padding=1) self.dec_1 = BasicConv2d(s * 2, s * 1, 3, padding=1) self.conv_final = nn.Conv2d(s, hps.n_classes, 1)
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 make_layers(self, cfg, batch_norm = False): layers = [] in_channels = 3 idx_M = 0 for v in cfg: if v == 'M': idx_M += 1 if idx_M == 4: layers += [nn.MaxPool2d(kernel_size = 2, stride = 1)] else: layers += [nn.MaxPool2d(kernel_size = 2, stride = 2)] else: conv2d = nn.Conv2d(in_channels, v, kernel_size = 3, padding = 1) if batch_norm: layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace = True)] else: layers += [conv2d, nn.ReLU(inplace = True)] in_channels = v return nn.Sequential(*layers)
def __init__(self, conv_channels, input_nch=3, output_nch=2, use_bn=True): super(UNet, self).__init__() self.n_stages = len(conv_channels) # define convolution blocks down_convs = [] up_convs = [] self.max_pooling = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) in_nch = input_nch for i, out_nch in enumerate(conv_channels): down_convs.append(UNetConvBlock(in_nch, out_nch, use_bn=use_bn)) up_conv_in_ch = 2 * out_nch if i < self.n_stages - 1 else out_nch # first up conv with equal channels up_conv_out_ch = out_nch if i == 0 else in_nch # last up conv with channels equal to labels up_convs.insert(0, UNetConvBlock(up_conv_in_ch, up_conv_out_ch, use_bn=use_bn)) in_nch = out_nch self.down_convs = nn.ModuleList(down_convs) self.up_convs = nn.ModuleList(up_convs) # define output convolution self.out_conv = nn.Conv2d(conv_channels[0], output_nch, 1)