我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用torch.nn.Conv2d()。
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): super(ImageTransformNet, self).__init__() self.conv1 = nn.Conv2d(3, 32, kernel_size=9, stride=1, padding=4) self.bn1 = nn.BatchNorm2d(32) self.conv2 = nn.Conv2d(32, 64, kernel_size=4, stride=2, padding=1) self.bn2 = nn.BatchNorm2d(64) self.conv3 = nn.Conv2d(64, 128, kernel_size=4, stride=2, padding=1) self.bn3 = nn.BatchNorm2d(128) self.res1 = ResidualBlock(128, 128) self.res2 = ResidualBlock(128, 128) self.res3 = ResidualBlock(128, 128) self.res4 = ResidualBlock(128, 128) self.res5 = ResidualBlock(128, 128) self.conv4 = nn.ConvTranspose2d(128, 64, kernel_size=4, stride=2, padding=1) self.bn4 = nn.BatchNorm2d(64) self.conv5 = nn.ConvTranspose2d(64, 32, kernel_size=4, stride=2, padding=1) self.bn5 = nn.BatchNorm2d(32) self.conv6 = nn.ConvTranspose2d(32, 3, kernel_size=9, stride=1, padding=4)
def __init__(self, num_inputs, action_space, use_gru): super(CNNPolicy, self).__init__() self.conv1 = nn.Conv2d(num_inputs, 32, 8, stride=4) self.conv2 = nn.Conv2d(32, 64, 4, stride=2) self.conv3 = nn.Conv2d(64, 32, 3, stride=1) self.linear1 = nn.Linear(32 * 7 * 7, 512) if use_gru: self.gru = nn.GRUCell(512, 512) self.critic_linear = nn.Linear(512, 1) if action_space.__class__.__name__ == "Discrete": num_outputs = action_space.n self.dist = Categorical(512, num_outputs) elif action_space.__class__.__name__ == "Box": num_outputs = action_space.shape[0] self.dist = DiagGaussian(512, num_outputs) else: raise NotImplementedError self.train() self.reset_parameters()
def __init__(self, in_planes, cardinality=32, bottleneck_width=4, stride=1): super(Block, self).__init__() group_width = cardinality * bottleneck_width self.conv1 = nn.Conv2d(in_planes, group_width, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(group_width) self.conv2 = nn.Conv2d(group_width, group_width, kernel_size=3, stride=stride, padding=1, groups=cardinality, bias=False) self.bn2 = nn.BatchNorm2d(group_width) self.conv3 = nn.Conv2d(group_width, self.expansion*group_width, kernel_size=1, bias=False) self.bn3 = nn.BatchNorm2d(self.expansion*group_width) self.shortcut = nn.Sequential() if stride != 1 or in_planes != self.expansion*group_width: self.shortcut = nn.Sequential( nn.Conv2d(in_planes, self.expansion*group_width, kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(self.expansion*group_width) )
def __init__(self, in_channels, n_filters, k_size, stride, padding, bias=True): super(conv2DBatchNorm, self).__init__() self.cb_unit = nn.Sequential(nn.Conv2d(int(in_channels), int(n_filters), kernel_size=k_size, padding=padding, stride=stride, bias=bias), nn.BatchNorm2d(int(n_filters)),)
def conv_bn(in_planes, out_planes, kernel_size, stride=1, padding=0, bias=False): "convolution with batchnorm, relu" return nn.Sequential( nn.Conv2d(in_planes, out_planes, kernel_size, stride=stride, padding=padding, bias=False), nn.BatchNorm2d(out_planes, eps=1e-3), nn.ReLU() )
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, in_channels=256, out_channels=256, stride=1, cardinality=32): """ Constructor Args: in_channels: input channel dimensionality out_channels: output channel dimensionality stride: conv stride. Replaces pooling layer. cardinality: num of convolution groups. """ super(DResNeXtBottleneck, self).__init__() D = out_channels // 2 self.conv_reduce = nn.Conv2d(in_channels, D, kernel_size=1, stride=1, padding=0, bias=False) self.conv_conv = nn.Conv2d(D, D, kernel_size=3, stride=stride, padding=1, groups=cardinality, bias=False) self.conv_expand = nn.Conv2d(D, out_channels, kernel_size=1, stride=1, padding=0, bias=False) self.shortcut = nn.Sequential() if in_channels != out_channels: self.shortcut.add_module('shortcut_conv', nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, padding=0, bias=False))
def __init__(self, inplanes, planes, cardinality, base_width, stride=1, downsample=None): super(ResNeXtBottleneck, self).__init__() D = int(math.floor(planes * (base_width/64.0))) C = cardinality self.conv_reduce = nn.Conv2d(inplanes, D*C, kernel_size=1, stride=1, padding=0, bias=False) self.bn_reduce = nn.BatchNorm2d(D*C) self.conv_conv = nn.Conv2d(D*C, D*C, kernel_size=3, stride=stride, padding=1, groups=cardinality, bias=False) self.bn = nn.BatchNorm2d(D*C) self.conv_expand = nn.Conv2d(D*C, planes*4, kernel_size=1, stride=1, padding=0, bias=False) self.bn_expand = nn.BatchNorm2d(planes*4) self.downsample = downsample
def _make_layer(self, block, planes, blocks, stride=1): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(planes * block.expansion), ) layers = [] layers.append(block(self.inplanes, planes, self.cardinality, self.base_width, stride, downsample)) self.inplanes = planes * block.expansion for i in range(1, blocks): layers.append(block(self.inplanes, planes, self.cardinality, self.base_width)) return nn.Sequential(*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=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 _make_layer(self, block, planes, blocks, stride=1): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential( nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(planes * block.expansion), ) layers = [] layers.append(block(self.inplanes, planes, stride, downsample)) self.inplanes = planes * block.expansion for i in range(1, blocks): layers.append(block(self.inplanes, planes)) return nn.Sequential(*layers)
def __init__(self, dim=64): super(Generator, self).__init__() conv_bn_relu = conv_norm_act dconv_bn_relu = dconv_norm_act self.ls = nn.Sequential(nn.ReflectionPad2d(3), conv_bn_relu(3, dim * 1, 7, 1), conv_bn_relu(dim * 1, dim * 2, 3, 2, 1), conv_bn_relu(dim * 2, dim * 4, 3, 2, 1), ResiduleBlock(dim * 4, dim * 4), ResiduleBlock(dim * 4, dim * 4), ResiduleBlock(dim * 4, dim * 4), ResiduleBlock(dim * 4, dim * 4), ResiduleBlock(dim * 4, dim * 4), ResiduleBlock(dim * 4, dim * 4), ResiduleBlock(dim * 4, dim * 4), ResiduleBlock(dim * 4, dim * 4), ResiduleBlock(dim * 4, dim * 4), dconv_bn_relu(dim * 4, dim * 2, 3, 2, 1, 1), dconv_bn_relu(dim * 2, dim * 1, 3, 2, 1, 1), nn.ReflectionPad2d(3), nn.Conv2d(dim, 3, 7, 1), nn.Tanh())
def __init__(self): super(GlobalFeatNet, self).__init__() self.conv1 = nn.Conv2d(512, 512, kernel_size=3, stride=2, padding=1) self.bn1 = nn.BatchNorm2d(512) self.conv2 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1) self.bn2 = nn.BatchNorm2d(512) self.conv3 = nn.Conv2d(512, 512, kernel_size=3, stride=2, padding=1) self.bn3 = nn.BatchNorm2d(512) self.conv4 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1) self.bn4 = nn.BatchNorm2d(512) self.fc1 = nn.Linear(25088, 1024) self.bn5 = nn.BatchNorm1d(1024) self.fc2 = nn.Linear(1024, 512) self.bn6 = nn.BatchNorm1d(512) self.fc3 = nn.Linear(512, 256) self.bn7 = nn.BatchNorm1d(256)
def __init__( self, ): super(Discriminator, self).__init__() self.conv1 = nn.Conv2d(3, 64, 4, 2, 1, bias=False) self.relu1 = nn.LeakyReLU(0.2, inplace=True) self.conv2 = nn.Conv2d(64, 64 * 2, 4, 2, 1, bias=False) self.bn2 = nn.BatchNorm2d(64 * 2) self.relu2 = nn.LeakyReLU(0.2, inplace=True) self.conv3 = nn.Conv2d(64 * 2, 64 * 4, 4, 2, 1, bias=False) self.bn3 = nn.BatchNorm2d(64 * 4) self.relu3 = nn.LeakyReLU(0.2, inplace=True) self.conv4 = nn.Conv2d(64 * 4, 64 * 8, 4, 2, 1, bias=False) self.bn4 = nn.BatchNorm2d(64 * 8) self.relu4 = nn.LeakyReLU(0.2, inplace=True) self.conv5 = nn.Conv2d(64 * 8, 1, 4, 1, 0, bias=False)
def build_conv_block(self, dim, padding_type, norm_layer, use_dropout): conv_block = [] p = 0 # TODO: support padding types assert(padding_type == 'zero') p = 1 # TODO: InstanceNorm conv_block += [nn.Conv2d(dim, dim, kernel_size=3, padding=p), norm_layer(dim, affine=True), nn.ReLU(True)] if use_dropout: conv_block += [nn.Dropout(0.5)] conv_block += [nn.Conv2d(dim, dim, kernel_size=3, padding=p), norm_layer(dim, affine=True)] return nn.Sequential(*conv_block)
def test_load_parameter_dict(self): l = nn.Linear(5, 5) block = nn.Container( conv=nn.Conv2d(3, 3, 3, bias=False) ) net = nn.Container( linear1=l, linear2=l, block=block, empty=None, ) param_dict = { 'linear1.weight': Variable(torch.ones(5, 5)), 'block.conv.bias': Variable(torch.range(1, 3)), } net.load_parameter_dict(param_dict) self.assertIs(net.linear1.weight, param_dict['linear1.weight']) self.assertIs(net.block.conv.bias, param_dict['block.conv.bias'])
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 _weights_init(model, pretrained): for m in model.modules(): if pretrained: if isinstance(m, nn.Linear): n = m.weight.size(1) m.weight.data.normal_(0, math.sqrt(2. / n)) m.bias.data.zero_() else: 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)) if m.bias is not None: m.bias.data.zero_() elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() elif isinstance(m, nn.Linear): n = m.weight.size(1) m.weight.data.normal_(0, math.sqrt(2. / n)) 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 __init__(self): super(VGG16Net, self).__init__() # conv: (input channel, output channel, height, width) self.conv1_1 = nn.Conv2d(3, 64, 3, padding=1) self.conv1_1_alias = _alias(0) self.conv1_2 = nn.Conv2d(64, 64, 3, padding=1) self.conv1_2_alias = _alias(2) self.conv2_1 = nn.Conv2d(64, 128, 3, padding=1) self.conv2_1_alias = _alias(5) self.conv2_2 = nn.Conv2d(128, 128, 3, padding=1) self.conv2_2_alias = _alias(7) self.conv3_1 = nn.Conv2d(128, 256, 3, padding=1) self.conv3_1_alias = _alias(10) self.conv3_2 = nn.Conv2d(256, 256, 3, padding=1) self.conv3_2_alias = _alias(12) self.conv3_3 = nn.Conv2d(256, 256, 3, padding=1) self.conv3_3_alias = _alias(14) self.conv4_1 = nn.Conv2d(256, 512, 3, padding=1) self.conv4_1_alias = _alias(17) self.conv4_2 = nn.Conv2d(512, 512, 3, padding=1) self.conv4_2_alias = _alias(19) self.conv4_3 = nn.Conv2d(512, 512, 3, padding=1) self.conv4_3_alias = _alias(21)
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): super(Vgg16Part, self).__init__() self.conv1_1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1) self.conv1_2 = nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1) self.conv2_1 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1) self.conv2_2 = nn.Conv2d(128, 128, kernel_size=3, stride=1, padding=1) self.conv3_1 = nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1) self.conv3_2 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1) self.conv3_3 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1) self.conv4_1 = nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1) self.conv4_2 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1) self.conv4_3 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1) self.conv5_1 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1) self.conv5_2 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1) self.conv5_3 = nn.Conv2d(512, 512, kernel_size=3, stride=1, padding=1)
def __init__(self): super(StylePart, self).__init__() self.conv1 = nn.Conv2d(3, 32, kernel_size=9, stride=1, padding=4) self.bn1 = nn.BatchNorm2d(32) self.conv2 = nn.Conv2d(32, 64, kernel_size=4, stride=2, padding=1) self.bn2 = nn.BatchNorm2d(64) self.conv3 = nn.Conv2d(64, 128, kernel_size=4, stride=2, padding=1) self.bn3 = nn.BatchNorm2d(128) self.res1 = ResBlock(128) self.res2 = ResBlock(128) self.res3 = ResBlock(128) self.res4 = ResBlock(128) self.res5 = ResBlock(128) self.deconv1 = nn.ConvTranspose2d(128, 64, kernel_size=4, stride=2, padding=1) self.bn4 = nn.BatchNorm2d(64) self.deconv2 = nn.ConvTranspose2d(64, 32, kernel_size=4, stride=2, padding=1) self.bn5 = nn.BatchNorm2d(32) self.deconv3 = nn.Conv2d(32, 3, kernel_size=9, stride=1, padding=4)
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 __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, args): super(CNN_Text,self).__init__() self.args = args V = args.embed_num D = args.embed_dim C = args.class_num Ci = 1 Co = args.kernel_num Ks = args.kernel_sizes self.embed = nn.Embedding(V, D) #self.convs1 = [nn.Conv2d(Ci, Co, (K, D)) for K in Ks] self.convs1 = nn.ModuleList([nn.Conv2d(Ci, Co, (K, D)) for K in Ks]) ''' self.conv13 = nn.Conv2d(Ci, Co, (3, D)) self.conv14 = nn.Conv2d(Ci, Co, (4, D)) self.conv15 = nn.Conv2d(Ci, Co, (5, D)) ''' self.dropout = nn.Dropout(args.dropout) self.fc1 = nn.Linear(len(Ks)*Co, C)
def add_extras(cfg, i, batch_norm=False): # Extra layers added to VGG for feature scaling layers = [] in_channels = i flag = False for k, v in enumerate(cfg): if in_channels != 'S': if v == 'S': layers += [nn.Conv2d(in_channels, cfg[k + 1], kernel_size=(1, 3)[flag], stride=2, padding=1)] else: layers += [nn.Conv2d(in_channels, v, kernel_size=(1, 3)[flag])] flag = not flag in_channels = v return layers
def __init__(self, num_inputs, action_space, small_net=False): """ Really I should be using inheritance for the small_net here """ super(ES, self).__init__() num_outputs = action_space.n self.small_net = small_net if self.small_net: self.linear1 = nn.Linear(num_inputs, 64) self.linear2 = nn.Linear(64, 64) self.actor_linear = nn.Linear(64, num_outputs) else: self.conv1 = nn.Conv2d(num_inputs, 32, 3, stride=2, padding=1) self.conv2 = nn.Conv2d(32, 32, 3, stride=2, padding=1) self.conv3 = nn.Conv2d(32, 32, 3, stride=2, padding=1) self.conv4 = nn.Conv2d(32, 32, 3, stride=2, padding=1) self.lstm = nn.LSTMCell(32*3*3, 256) self.actor_linear = nn.Linear(256, num_outputs) self.train()
def __init__(self, num_blocks, cardinality, bottleneck_width, num_classes=10): super(ResNeXt, self).__init__() self.cardinality = cardinality self.bottleneck_width = bottleneck_width self.in_planes = 64 self.conv1 = nn.Conv2d(3, 64, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(64) self.layer1 = self._make_layer(num_blocks[0], 1) self.layer2 = self._make_layer(num_blocks[1], 2) self.layer3 = self._make_layer(num_blocks[2], 2) # self.layer4 = self._make_layer(num_blocks[3], 2) self.linear = nn.Linear(cardinality*bottleneck_width*8, num_classes)
def init_params(net): '''Init layer parameters.''' for m in net.modules(): if isinstance(m, nn.Conv2d): init.kaiming_normal(m.weight, mode='fan_out') if m.bias: init.constant(m.bias, 0) elif isinstance(m, nn.BatchNorm2d): init.constant(m.weight, 1) init.constant(m.bias, 0) elif isinstance(m, nn.Linear): init.normal(m.weight, std=1e-3) if m.bias: init.constant(m.bias, 0)
def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(1, 10, kernel_size=5) self.conv2 = nn.Conv2d(10, 20, kernel_size=5) self.conv2_drop = nn.Dropout2d() self.fc1 = nn.Linear(320, 50) self.fc2 = nn.Linear(50, 10)
def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(3, 10, kernel_size=5) self.conv2 = nn.Conv2d(10, 20, kernel_size=5) self.bn2 = nn.BatchNorm2d(20) self.conv3 = nn.Conv2d(20, 30, kernel_size=5) self.bn3 = nn.BatchNorm2d(30) self.conv4 = nn.Conv2d(30, 30, kernel_size=5, stride=2) self.bn4 = nn.BatchNorm2d(30) self.fc1 = nn.Linear(750, 256) self.fc2 = nn.Linear(256, 2)
def __init__(self): super(Net, self).__init__() self.conv = nn.Conv2d(512, 2, kernel_size=1) self.avgpool = nn.AvgPool2d(13)
def __init__(self, feature_scale=4, n_classes=21, is_deconv=True, in_channels=3, is_batchnorm=True): super(unet, self).__init__() self.is_deconv = is_deconv self.in_channels = in_channels self.is_batchnorm = is_batchnorm self.feature_scale = feature_scale filters = [64, 128, 256, 512, 1024] filters = [int(x / self.feature_scale) for x in filters] # downsampling self.conv1 = unetConv2(self.in_channels, filters[0], self.is_batchnorm) self.maxpool1 = nn.MaxPool2d(kernel_size=2) self.conv2 = unetConv2(filters[0], filters[1], self.is_batchnorm) self.maxpool2 = nn.MaxPool2d(kernel_size=2) self.conv3 = unetConv2(filters[1], filters[2], self.is_batchnorm) self.maxpool3 = nn.MaxPool2d(kernel_size=2) self.conv4 = unetConv2(filters[2], filters[3], self.is_batchnorm) self.maxpool4 = nn.MaxPool2d(kernel_size=2) self.center = unetConv2(filters[3], filters[4], self.is_batchnorm) # upsampling self.up_concat4 = unetUp(filters[4], filters[3], self.is_deconv) self.up_concat3 = unetUp(filters[3], filters[2], self.is_deconv) self.up_concat2 = unetUp(filters[2], filters[1], self.is_deconv) self.up_concat1 = unetUp(filters[1], filters[0], self.is_deconv) # final conv (without any concat) self.final = nn.Conv2d(filters[0], n_classes, 1)
def __init__(self, feature_scale=4, n_classes=21, is_deconv=True, in_channels=3, is_batchnorm=True): super(pspnet, self).__init__() self.is_deconv = is_deconv self.in_channels = in_channels self.is_batchnorm = is_batchnorm self.feature_scale = feature_scale self.layers = [2, 2, 2, 2] # Currently hardcoded for ResNet-18 filters = [64, 128, 256, 512] filters = [x / self.feature_scale for x in filters] self.inplanes = filters[0] # Encoder self.convbnrelu1 = conv2DBatchNormRelu(in_channels=3, k_size=7, n_filters=64, padding=3, stride=2, bias=False) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) block = residualBlock self.encoder1 = self._make_layer(block, filters[0], self.layers[0]) self.encoder2 = self._make_layer(block, filters[1], self.layers[1], stride=2) self.encoder3 = self._make_layer(block, filters[2], self.layers[2], stride=2) self.encoder4 = self._make_layer(block, filters[3], self.layers[3], stride=2) self.avgpool = nn.AvgPool2d(7) # Decoder self.decoder4 = linknetUp(filters[3], filters[2]) self.decoder4 = linknetUp(filters[2], filters[1]) self.decoder4 = linknetUp(filters[1], filters[0]) self.decoder4 = linknetUp(filters[0], filters[0]) # Final Classifier self.finaldeconvbnrelu1 = nn.Sequential(nn.ConvTranspose2d(filters[0], 32/feature_scale, 3, 2, 1), nn.BatchNorm2d(32/feature_scale), nn.ReLU(inplace=True),) self.finalconvbnrelu2 = conv2DBatchNormRelu(in_channels=32/feature_scale, k_size=3, n_filters=32/feature_scale, padding=1, stride=1) self.finalconv3 = nn.Conv2d(32/feature_scale, n_classes, 2, 2, 0)
def _make_layer(self, block, planes, blocks, stride=1): downsample = None if stride != 1 or self.inplanes != planes * block.expansion: downsample = nn.Sequential(nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(planes * block.expansion),) layers = [] layers.append(block(self.inplanes, planes, stride, downsample)) self.inplanes = planes * block.expansion for i in range(1, blocks): layers.append(block(self.inplanes, planes)) return nn.Sequential(*layers)
def init_vgg16_params(self, vgg16): blocks = [self.down1, self.down2, self.down3, self.down4, self.down5] ranges = [[0, 4], [5, 9], [10, 16], [17, 23], [24, 29]] features = list(vgg16.features.children()) vgg_layers = [] for _layer in features: if isinstance(_layer, nn.Conv2d): vgg_layers.append(_layer) merged_layers = [] for idx, conv_block in enumerate(blocks): if idx < 2: units = [conv_block.conv1.cbr_unit, conv_block.conv2.cbr_unit] else: units = [conv_block.conv1.cbr_unit, conv_block.conv2.cbr_unit, conv_block.conv3.cbr_unit] for _unit in units: for _layer in _unit: if isinstance(_layer, nn.Conv2d): merged_layers.append(_layer) assert len(vgg_layers) == len(merged_layers) for l1, l2 in zip(vgg_layers, merged_layers): if isinstance(l1, nn.Conv2d) and isinstance(l2, nn.Conv2d): assert l1.weight.size() == l2.weight.size() assert l1.bias.size() == l2.bias.size() l2.weight.data = l1.weight.data l2.bias.data = l1.bias.data
def __init__(self, feature_scale=4, n_classes=21, is_deconv=True, in_channels=3, is_batchnorm=True): super(linknet, self).__init__() self.is_deconv = is_deconv self.in_channels = in_channels self.is_batchnorm = is_batchnorm self.feature_scale = feature_scale self.layers = [2, 2, 2, 2] # Currently hardcoded for ResNet-18 filters = [64, 128, 256, 512] filters = [x / self.feature_scale for x in filters] self.inplanes = filters[0] # Encoder self.convbnrelu1 = conv2DBatchNormRelu(in_channels=3, k_size=7, n_filters=64, padding=3, stride=2, bias=False) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) block = residualBlock self.encoder1 = self._make_layer(block, filters[0], self.layers[0]) self.encoder2 = self._make_layer(block, filters[1], self.layers[1], stride=2) self.encoder3 = self._make_layer(block, filters[2], self.layers[2], stride=2) self.encoder4 = self._make_layer(block, filters[3], self.layers[3], stride=2) self.avgpool = nn.AvgPool2d(7) # Decoder self.decoder4 = linknetUp(filters[3], filters[2]) self.decoder4 = linknetUp(filters[2], filters[1]) self.decoder4 = linknetUp(filters[1], filters[0]) self.decoder4 = linknetUp(filters[0], filters[0]) # Final Classifier self.finaldeconvbnrelu1 = nn.Sequential(nn.ConvTranspose2d(filters[0], 32/feature_scale, 3, 2, 1), nn.BatchNorm2d(32/feature_scale), nn.ReLU(inplace=True),) self.finalconvbnrelu2 = conv2DBatchNormRelu(in_channels=32/feature_scale, k_size=3, n_filters=32/feature_scale, padding=1, stride=1) self.finalconv3 = nn.Conv2d(32/feature_scale, n_classes, 2, 2, 0)
def __init__(self, in_channels, n_filters, k_size, stride, padding, bias=True): super(conv2DBatchNormRelu, self).__init__() self.cbr_unit = nn.Sequential(nn.Conv2d(int(in_channels), int(n_filters), kernel_size=k_size, padding=padding, stride=stride, bias=bias), nn.BatchNorm2d(int(n_filters)), nn.ReLU(inplace=True),)
def __init__(self, in_size, out_size, is_batchnorm): super(unetConv2, self).__init__() if is_batchnorm: self.conv1 = nn.Sequential(nn.Conv2d(in_size, out_size, 3, 1, 0), nn.BatchNorm2d(out_size), nn.ReLU(),) self.conv2 = nn.Sequential(nn.Conv2d(out_size, out_size, 3, 1, 0), nn.BatchNorm2d(out_size), nn.ReLU(),) else: self.conv1 = nn.Sequential(nn.Conv2d(in_size, out_size, 3, 1, 0), nn.ReLU(),) self.conv2 = nn.Sequential(nn.Conv2d(out_size, out_size, 3, 1, 0), nn.ReLU(),)
def __init__(self, prev_channels, out_channels, scale): super(FRRU, self).__init__() self.scale = scale self.prev_channels = prev_channels self.out_channels = out_channels self.conv1 = conv2DBatchNormRelu(prev_channels + 32, out_channels, k_size=3, stride=1, padding=1) self.conv2 = conv2DBatchNormRelu(out_channels, out_channels, k_size=3, stride=1, padding=1) self.conv_res = nn.Conv2d(out_channels, 32, kernel_size=1, stride=1, padding=0)
def init_vgg16_params(self, vgg16, copy_fc8=True): blocks = [self.conv_block1, self.conv_block2, self.conv_block3, self.conv_block4, self.conv_block5] ranges = [[0, 4], [5, 9], [10, 16], [17, 23], [24, 29]] features = list(vgg16.features.children()) for idx, conv_block in enumerate(blocks): for l1, l2 in zip(features[ranges[idx][0]:ranges[idx][1]], conv_block): if isinstance(l1, nn.Conv2d) and isinstance(l2, nn.Conv2d): # print idx, l1, l2 assert l1.weight.size() == l2.weight.size() assert l1.bias.size() == l2.bias.size() l2.weight.data = l1.weight.data l2.bias.data = l1.bias.data for i1, i2 in zip([0, 3], [0, 3]): l1 = vgg16.classifier[i1] l2 = self.classifier[i2] l2.weight.data = l1.weight.data.view(l2.weight.size()) l2.bias.data = l1.bias.data.view(l2.bias.size()) n_class = self.classifier[6].weight.size()[0] if copy_fc8: l1 = vgg16.classifier[6] l2 = self.classifier[6] l2.weight.data = l1.weight.data[:n_class, :].view(l2.weight.size()) l2.bias.data = l1.bias.data[:n_class] # FCN 8s