我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用torch.nn.ReLU()。
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, dim_in, dim_z, config='pendulum'): super(AE, self).__init__() _, _, dec = load_config(config) # TODO, refactor encoder to allow output of dim_z instead of dim_z * 2 self.encoder = nn.Sequential( nn.Linear(dim_in, 800), nn.BatchNorm1d(800), nn.ReLU(), nn.Linear(800, 800), nn.BatchNorm1d(800), nn.ReLU(), nn.Linear(800, dim_z), nn.BatchNorm1d(dim_z), nn.Sigmoid() ) self.decoder = dec(dim_z, dim_in)
def __init__(self, window_sizes, cov_dim, mem_dim): super(NewConvModule, self).__init__() self.window_sizes = window_sizes self.cov_dim = cov_dim self.mem_dim = mem_dim self.linear1 = nn.Linear(len(window_sizes) * mem_dim, mem_dim) self.relu1 = nn.ReLU() self.tanh1 = nn.Tanh()
def __init__(self, num_points = 2500): super(STN3d, self).__init__() self.num_points = num_points self.conv1 = nn.Conv1d(3, 64, 1) self.conv2 = nn.Conv1d(64, 128, 1) self.conv3 = nn.Conv1d(128, 1024, 1) self.mp1 = nn.MaxPool1d(num_points) self.fc1 = nn.Linear(1024, 512) self.fc2 = nn.Linear(512, 256) self.fc3 = nn.Linear(256, 9) self.relu = nn.ReLU() self.bn1 = nn.BatchNorm1d(64) self.bn2 = nn.BatchNorm1d(128) self.bn3 = nn.BatchNorm1d(1024) self.bn4 = nn.BatchNorm1d(512) self.bn5 = nn.BatchNorm1d(256)
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, 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, n_layers=2, h_size=512): super(ResLSTM, self).__init__() print('Building AlexNet + LSTM model...') self.h_size = h_size self.n_layers = n_layers resnet = models.resnet50(pretrained=True) self.conv = nn.Sequential(*list(resnet.children())[:-1]) self.lstm = nn.LSTM(1280, h_size, dropout=0.2, num_layers=n_layers) self.fc = nn.Sequential( nn.Linear(h_size, 64), nn.ReLU(), nn.Dropout(0.2), nn.Linear(64, 1) )
def __init__(self, h_size=512, n_layers=3): super(DenseLSTM, self).__init__() print('Building DenseNet + LSTM model...') self.h_size = h_size self.n_layers = n_layers densenet = models.densenet201(pretrained=True) self.conv = nn.Sequential(*list(densenet.children())[:-1]) self.lstm = nn.LSTM(23040, h_size, dropout=0.2, num_layers=n_layers) self.fc = nn.Sequential( nn.Linear(512, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, 1) )
def __init__(self, n_layers=2, h_size=420): super(AlexLSTM, self).__init__() print('Building AlexNet + LSTM model...') self.h_size = h_size self.n_layers = n_layers alexnet = models.alexnet(pretrained=True) self.conv = nn.Sequential(*list(alexnet.children())[:-1]) self.lstm = nn.LSTM(1280, h_size, dropout=0.2, num_layers=n_layers) self.fc = nn.Sequential( nn.Linear(h_size, 64), nn.ReLU(), nn.Dropout(0.2), nn.Linear(64, 1) )
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 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 __init__(self): super(C3D_net,self).__init__() self.conv1=nn.Conv3d(3,64,kernel_size=(3,3,3),stride=1,padding=(1,1,1)) self.relu=nn.ReLU() self.maxpool1=nn.MaxPool3d(kernel_size=(1,2,2),stride=(1,2,2)) self.conv2=nn.Conv3d(64,128,kernel_size=(3,3,3),stride=1,padding=(1,1,1)) self.maxpool2=nn.MaxPool3d(kernel_size=(2,2,2),stride=(2,2,2)) self.conv3=nn.Conv3d(128,256,kernel_size=(3,3,3),stride=1,padding=(1,1,1)) self.maxpool3=nn.MaxPool3d(kernel_size=(2,2,2),stride=(2,2,2)) self.conv4=nn.Conv3d(256,256,kernel_size=(3,3,3),stride=1,padding=(1,1,1)) self.maxpool4=nn.MaxPool3d(kernel_size=(2,2,2),stride=(2,2,2)) self.conv5=nn.Conv3d(256,256,kernel_size=(3,3,3),stride=1,padding=(1,1,1)) self.maxpool5=nn.MaxPool3d(kernel_size=(2,2,2),stride=(2,2,2)) self.num_out_maxpool5=2304 self.fc6=nn.Linear(self.num_out_maxpool5,2048)#TBA self.fc7=nn.Linear(2048,2048) #self.dropout=nn.Dropout(p=0.5) self.fc8=nn.Linear(2048,101) self._initialize_weights()
def __init__(self, z_dim, transition_dim): super(GatedTransition, self).__init__() # initialize the six linear transformations used in the neural network self.lin_gate_z_to_hidden = nn.Linear(z_dim, transition_dim) self.lin_gate_hidden_to_z = nn.Linear(transition_dim, z_dim) self.lin_proposed_mean_z_to_hidden = nn.Linear(z_dim, transition_dim) self.lin_proposed_mean_hidden_to_z = nn.Linear(transition_dim, z_dim) self.lin_sig = nn.Linear(z_dim, z_dim) self.lin_z_to_mu = nn.Linear(z_dim, z_dim) # modify the default initialization of lin_z_to_mu # so that it's starts out as the identity function self.lin_z_to_mu.weight.data = torch.eye(z_dim) self.lin_z_to_mu.bias.data = torch.zeros(z_dim) # initialize the three non-linearities used in the neural network self.relu = nn.ReLU() self.sigmoid = nn.Sigmoid() self.softplus = nn.Softplus()
def __init__(self, config): super(SNLIClassifier, self).__init__() self.config = config self.embed = nn.Embedding(config.n_embed, config.d_embed) self.projection = Linear(config.d_embed, config.d_proj) self.embed_bn = BatchNorm(config.d_proj) self.embed_dropout = nn.Dropout(p=config.embed_dropout) self.encoder = SPINN(config) if config.spinn else Encoder(config) feat_in_size = config.d_hidden * ( 2 if self.config.birnn and not self.config.spinn else 1) self.feature = Feature(feat_in_size, config.mlp_dropout) self.mlp_dropout = nn.Dropout(p=config.mlp_dropout) self.relu = nn.ReLU() mlp_in_size = 4 * feat_in_size mlp = [nn.Linear(mlp_in_size, config.d_mlp), self.relu, nn.BatchNorm1d(config.d_mlp), self.mlp_dropout] for i in range(config.n_mlp_layers - 1): mlp.extend([nn.Linear(config.d_mlp, config.d_mlp), self.relu, nn.BatchNorm1d(config.d_mlp), self.mlp_dropout]) mlp.append(nn.Linear(config.d_mlp, config.d_out)) self.out = nn.Sequential(*mlp)
def __init__(self, hidden_size, output_size, r_factor=2, dropout_p=0.5): super(AttnDecoderRNN, self).__init__() self.r_factor = r_factor self.prenet = nn.Sequential( nn.Linear(output_size, 2 * hidden_size), nn.ReLU(), nn.Dropout(dropout_p), nn.Linear(2 * hidden_size, hidden_size), nn.ReLU(), nn.Dropout(dropout_p) ) self.linear_dec = nn.Linear(2 * hidden_size, 2 * hidden_size) self.gru_att = nn.GRU(hidden_size, 2 * hidden_size, batch_first=True) self.attn = nn.Linear(2 * hidden_size, 1) # TODO: change name... self.short_cut = nn.Linear(4 * hidden_size, 2 * hidden_size) self.gru_dec1 = nn.GRU(4 * hidden_size, 2 * hidden_size, num_layers=1, batch_first=True) self.gru_dec2 = nn.GRU(2 * hidden_size, 2 * hidden_size, num_layers=1, batch_first=True) self.out = nn.Linear(2 * hidden_size, r_factor * output_size)
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): """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, in_planes, out_planes, stride, dropRate,layer_index): super(BasicBlock, self).__init__() self.bn1 = nn.BatchNorm2d(in_planes) self.relu1 = nn.ReLU(inplace=True) self.conv1 = nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(out_planes) self.relu2 = nn.ReLU(inplace=True) self.conv2 = nn.Conv2d(out_planes, out_planes, kernel_size=3, stride=1, padding=1, bias=False) self.droprate = dropRate self.equalInOut = (in_planes == out_planes) self.convShortcut = (not self.equalInOut) and nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, padding=0, bias=False) or None # If the layer is being trained or not self.active = True # The layer index relative to the rest of the net self.layer_index = layer_index
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 build_mlp(input_dim, hidden_dims, output_dim, use_batchnorm=False, dropout=0): layers = [] D = input_dim if dropout > 0: layers.append(nn.Dropout(p=dropout)) if use_batchnorm: layers.append(nn.BatchNorm1d(input_dim)) for dim in hidden_dims: layers.append(nn.Linear(D, dim)) if use_batchnorm: layers.append(nn.BatchNorm1d(dim)) if dropout > 0: layers.append(nn.Dropout(p=dropout)) layers.append(nn.ReLU(inplace=True)) D = dim layers.append(nn.Linear(D, output_dim)) return nn.Sequential(*layers)
def _make_grouped_conv1x1(self, in_channels, out_channels, groups, batch_norm=True, relu=False): modules = OrderedDict() conv = conv1x1(in_channels, out_channels, groups=groups) modules['conv1x1'] = conv if batch_norm: modules['batch_norm'] = nn.BatchNorm2d(out_channels) if relu: modules['relu'] = nn.ReLU() if len(modules) > 1: return nn.Sequential(modules) else: return conv
def __init__(self, in_channels, d1, d2, skip=False, stride = 1): super(ResidualBlock, self).__init__() self.skip = skip self.conv1 = nn.Conv2d(in_channels, d1, 1, stride = stride,bias = False) self.bn1 = nn.BatchNorm2d(d1) self.relu = nn.ReLU(inplace=True) self.conv2 = nn.Conv2d(d1, d1, 3, padding = 1,bias = False) self.bn2 = nn.BatchNorm2d(d1) self.conv3 = nn.Conv2d(d1, d2, 1,bias = False) self.bn3 = nn.BatchNorm2d(d2) if not self.skip: self.conv4 = nn.Conv2d(in_channels, d2, 1, stride=stride,bias = False) self.bn4 = nn.BatchNorm2d(d2)
def __init__(self, in_channels, out_channels, batch_size): super(UpProj_Block, self).__init__() self.batch_size = batch_size self.conv1 = nn.Conv2d(in_channels, out_channels, (3,3)) self.conv2 = nn.Conv2d(in_channels, out_channels, (2,3)) self.conv3 = nn.Conv2d(in_channels, out_channels, (3,2)) self.conv4 = nn.Conv2d(in_channels, out_channels, (2,2)) self.conv5 = nn.Conv2d(in_channels, out_channels, (3,3)) self.conv6 = nn.Conv2d(in_channels, out_channels, (2,3)) self.conv7 = nn.Conv2d(in_channels, out_channels, (3,2)) self.conv8 = nn.Conv2d(in_channels, out_channels, (2,2)) self.bn1_1 = nn.BatchNorm2d(out_channels) self.bn1_2 = nn.BatchNorm2d(out_channels) self.bn2 = nn.BatchNorm2d(out_channels) self.relu = nn.ReLU(inplace=True) self.conv9 = nn.Conv2d(out_channels, out_channels , 3,padding = 1)
def __init__(self, X, Y, hidden_layer_sizes): super(Net, self).__init__() # Initialize linear layer with least squares solution X_ = np.hstack([X, np.ones((X.shape[0],1))]) Theta = np.linalg.solve(X_.T.dot(X_), X_.T.dot(Y)) self.lin = nn.Linear(X.shape[1], Y.shape[1]) W,b = self.lin.parameters() W.data = torch.Tensor(Theta[:-1,:].T) b.data = torch.Tensor(Theta[-1,:]) # Set up non-linear network of # Linear -> BatchNorm -> ReLU -> Dropout layers layer_sizes = [X.shape[1]] + hidden_layer_sizes layers = reduce(operator.add, [[nn.Linear(a,b), nn.BatchNorm1d(b), nn.ReLU(), nn.Dropout(p=0.2)] for a,b in zip(layer_sizes[0:-1], layer_sizes[1:])]) layers += [nn.Linear(layer_sizes[-1], Y.shape[1])] self.net = nn.Sequential(*layers) self.sig = Parameter(torch.ones(1, Y.shape[1]).cuda())
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, 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 __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_channels, n_filters, k_size, stride, padding, bias=True): super(deconv2DBatchNormRelu, self).__init__() self.dcbr_unit = nn.Sequential(nn.ConvTranspose2d(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, in_channels, n_filters, stride=1, downsample=None): super(residualBottleneck, self).__init__() self.convbn1 = nn.Conv2DBatchNorm(in_channels, n_filters, k_size=1, bias=False) self.convbn2 = nn.Conv2DBatchNorm(n_filters, n_filters, k_size=3, padding=1, stride=stride, bias=False) self.convbn3 = nn.Conv2DBatchNorm(n_filters, n_filters * 4, k_size=1, bias=False) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride
def _augment_module_pre(net: nn.Module) -> (dict, list): callback_dict = OrderedDict() # not necessarily ordered, but this can help some readability. forward_hook_remove_func_list = [] for x, y in net.named_modules(): if not isinstance(y, nn.Sequential) and y is not net: if isinstance(y, nn.ReLU): callback_dict[x] = {} forward_hook_remove_func_list.append( y.register_forward_hook(partial(_forward_hook, module_name=x, callback_dict=callback_dict))) def remove_handles(): for x in forward_hook_remove_func_list: x.remove() return callback_dict, remove_handles
def __init__(self, in_features, out_features, relu=True): super(FC, self).__init__() self.fc = nn.Linear(in_features, out_features) self.relu = nn.ReLU(inplace=True) if relu else None
def __init__(self, in_planes, scale=1.0, activation=nn.ReLU(True)): super(block, self).__init__() self.scale = scale self.activation = activation or (lambda x: x)
def __init__(self, in_planes, scale=1.0, activation=nn.ReLU(True)): super(block35, self).__init__(in_planes, scale, activation) self.Branch_0 = nn.Sequential(OrderedDict([ ('Conv2d_1x1', conv_bn(in_planes, 32, 1)) ])) self.Branch_1 = nn.Sequential(OrderedDict([ ('Conv2d_0a_1x1', conv_bn(in_planes, 32, 1)), ('Conv2d_0b_3x3', conv_bn(32, 32, 3, padding=1)) ])) self.Branch_2 = nn.Sequential(OrderedDict([ ('Conv2d_0a_1x1', conv_bn(in_planes, 32, 1)), ('Conv2d_0b_3x3', conv_bn(32, 48, 3, padding=1)), ('Conv2d_0c_3x3', conv_bn(48, 64, 3, padding=1)) ])) self.Conv2d_1x1 = conv_bn(128, in_planes, 1)
def __init__(self, in_planes, scale=1.0, activation=nn.ReLU(True)): super(block17, self).__init__(in_planes, scale, activation) self.Branch_0 = nn.Sequential(OrderedDict([ ('Conv2d_1x1', conv_bn(in_planes, 192, 1)) ])) self.Branch_1 = nn.Sequential(OrderedDict([ ('Conv2d_0a_1x1', conv_bn(in_planes, 128, 1)), ('Conv2d_0b_1x7', conv_bn(128, 160, (1, 7), padding=(0, 3))), ('Conv2d_0c_7x1', conv_bn(160, 192, (7, 1), padding=(3, 0))) ])) self.Conv2d_1x1 = conv_bn(384, in_planes, 1)