def __init__(self, in_channels, out_1_1, reduce_3_3, out_3_3, reduce_5_5, out_5_5, out_1_1_pool): super().__init__(self) self.branch1 = conv_block(in_channels, out_1_1, kernel_size=1) self.branch2 = nn.sequential(ConvBlock(in_channels,reduce_3_3,kernel_size=1), ConvBlock(reduce_3_3, out_3_3,kernel_size=3,padding=1)) self.branch3 = nn.sequential(ConvBlock(in_channels, reduce_5_5, kernel_size=1), ConvBlock(in_channels, out_5_5, kernel_size=5, padding=2)) self.branch4 = nn.sequential(nn.MaxPool2d(kernel_size=3, padding=1), ConvBlock(in_channels, out_1_1_pool, kernel_size=5, padding=2))
def __init__(self): super(Net, self).__init__() self.D2_123 = nn.sequential( nn.Conv2d(in_channels =3, out_channels =64, kernel_size=4, stride=2, padding=1), nn.LeakyReLU(0.2), nn.Conv2d(in_channels =64, out_channels =128, kernel_size=4, stride=2, padding=1), nn.BatchNorm2d(128), nn.LeakyReLU(0.2), nn.Conv2d(in_channels =128, out_channels =256, kernel_size=4, stride=2, padding=1), nn.BatchNorm2d(256), nn.LeakyReLU(0.2) ).to(device) self.D2ABC = nn.Sequential( nn.Conv2d(in_channels=7, out_channels=64, kernel_size=4, stride=2, padding=1), nn.BatchNorm2d(64), nn.LeakyReLU(0.2), nn.Conv2d(in_channels=64, out_channels=128, kernel_size=4, stride=2, padding=1), nn.BatchNorm2d(128), nn.LeakyReLU(0.2), nn.Conv2d(in_channels=128, out_channels=256, kernel_size=4, stride=2, padding=1), nn.BatchNorm2d(256), nn.LeakyReLU(0.2) ).to(device) self.D2_45 = nn.sequential( nn.Conv2d(in_channels =512, out_channels =512, kernel_size=4, stride=2, padding=1), nn.BatchNorm2d(512), nn.LeakyReLU(0.2), nn.ConvTranspose2d(in_channels =512, out_channels =1024, kernel_size=4, stride=2, padding=1), nn.BatchNorm2d(1024), nn.LeakyReLU(0.2) ).to(device) self.D2_67 = nn.sequential( nn.ConvTranspose2d(in_channels =(1024+108), out_channels =1024, kernel_size=1, stride=1), nn.BatchNorm2d(1024), nn.LeakyReLU(0.2), nn.ConvTranspose2d(in_channels =1024, out_channels =1, kernel_size=4, stride=4), nn.BatchNorm2d(1024), nn.LeakyReLU(0.2) ).to(device)
def __init__(self, backbone, transforms): super(MoCo, self).__init__() self.keys_encoder = backbone #todo: change backbone self.keys_encoder = nn.sequential( 'MLP', nn.Conv2d(512, 128, (1, 1), stride=(1, 1))) #for p in self.keys_encoder.parameters(): #Detach Gradients from keys network #p.requires_grad= False self.query_encoder = backbone self.keys_encoder = nn.sequential( 'MLP', nn.Conv2d(512, 128, (1, 1), stride=(1, 1))) self.classifier = nn.Sequential() #todo: implement MLP Head self.transforms = transforms
def load(path): #import everything import torch from torch import nn from torch import optim import trch.nn.functional as F from torchvision import datasets, transforms, models # define the model architecture model = models.resnet18(pretrained=True) # freez parameters for param in model.parameters(): param.requires_grad = False from collections import OrderDict model.classifier = nn.sequential(nn.Linear(1024, 256), nn.Relu(), nn.DropOut(0.2), nn.Linear(256, 2), nn.LogSoftmax(dim=1)) # load weights model.classifier.load_state_dict(torch.load(path, map_location="cpu")) # return weights loaded model return model
def __init__(self, input_nc=3, output_nc=3, ngf=64, n_downsampling=3, n_blocks=9, norm_layer=nn.BatchNorm2d, padding_type='reflect'): assert n_blocks >= 0, 'Number of blocks (n_blocks) must be >= 0' super(GlobalGenerator, self).__init__() activation = nn.ReLU(True) model = [ nn.ReflectionPad2d(3), nn.Conv2d(input_nc, ngf, kernel_size=7, padding=0), norm_layer(ngf), activation ] # Downsample for i in range(n_downsampling): mult = 2**i model += [ nn.Conv2d(ngf * mult, ngf * mult * 2, kernel_size=3, stride=2, padding=1), norm_layer(ngf * mult * 2), activation ] # Resnet Blocks mult = 2**n_downsampling for i in range(n_blocks): model += [ ResnetBlock(ngf * mult, padding_type=padding_type, activation=activation, norm_layer=norm_layer) ] # Upsampling for i in range(n_downsampling): mult = 2**(n_downsampling - 1) model += [ nn.ConvTranspose2d(ngf * mult, (ngf * mult) // 2, kernel_size=3, stride=2, padding=1, output_padding=1), norm_layer((ngf * mult) // 2), activation ] model += [ nn.ReflectionPad2d(3), nn.Conv2d(ngf, output_nc, kernel_size=7, padding=0), nn.Tanh() ] self.model = nn.sequential(*model)
def _make_layer_block(self, num_residual_layers, out_chan, stride): identity_down_sample = None layers = [] if stride != 1 | self.in_chans != out_chan * 4: identity_down_sample = nn.sequential( nn.Conv2d(self.in_chans, out_chan * 4, kernel_size=1, stride=stride), nn.BatchNorm2d(out_chan * 4)) layers.append( ResnetBlock(self.in_chans, out_chan, identity_down_sample, stride)) self.in_chans = out_chan * 4 for i in range(num_residual_layers - 1): layers.append(ResnetBlock(self.in_chans, out_chan)) return nn.sequential(*layers)
def initialize_model(features_length, num_classes, seq_length, dropout=0.5): input_dim = features_length batch_dim = seq_length output_dim = num_classes model_ft = nn.sequential(nn.GRU(input_dim, output_dim)) return model_ft, input_size
def DepthwiseConv(in_planes, planes, dilation=1): return nn.sequential( nn.Conv2d(in_planes, in_planes, kernel_size=3, padding=dilation, groups=in_planes, dilation=dilation, bias=False), nn.Conv2d(in_planes, planes, kernel_size=1), nn.BatchNorm2d(planes))
def _make_vgg_block(self, in_channels, out_channels, num_layers): layers = [ConvBlock(in_channels, out_channels)] for i in range(num_layers - 1): if i == 1: layers.append(ConvBlock(out_channels, out_channels, kernel_size=1, padding=0)) layers.append(ConvBlock(out_channels, out_channels)) return nn.sequential(*layers)
def __init__(self, net, normalizer=None, **attack_args): self._params = ut.ParamDict(**attack_args) self._attack_func = deepfool if normalizer is not None: self._model = nn.sequential(normalizer, net) else: self._model = net
def __init__(self, ctx, settings): super().__init__() layer_dict=collections.OrderedDict() for i in range(settings.num_units): bc = settings.init_g1*settings.init_g2 ic = settings.bottle_factor*bc oc = settings.bottle_factor*bc unit_settings = ShuffleNetUnitSettings(groups1 = settings.init_g1, groups2 = settings.init_g2, in_chan = ic, out_chan = oc, bottle_chan = bc) to_add=ShuffleNetUnit(ctx, unit_settings) layer_dict["shuffle_unit{}".format(i)]=to_add self.sequential = nn.sequential(layer_dict)
def __init__(self, cats, dps, model='resnet34', custom_head=None): super().__init__() model = getattr(models, model) self.body = nn.Sequential(*(list(model( pretrained=True).children())[:-2])) self.head = nn.sequential( AdaptiveConcatPool2d(), Flatten(), nn.BatchNorm1d(list(self.body.parameters())[-1].shape[0]), nn.Dropout(0.5), nn.Linear(list(self.body.parameters())[-1].shape[0], 512), nn.ReLU(), nn.BatchNorm1d(512), nn.Dropout(0.5), nn.Linear(512, cats)) if custom_head: self.head = custom_head
def __init__(self): super(fcn, self).__init__() self.layer1 = nn.sequential( nn.maxpool2d(kernel_size=(4, 4, 4)), nn.conv2d(25, 25, kernel_size=(3, 3, 3)), nn.conv2d(25, 25, kernel_size=(3, 3, 3), dilation=(1, 2, 2)), nn.conv2d(25, 25, kernel_size=(3, 3, 3), dilation=(2, 4, 4)), nn.maxunpool2d(25, 25, kernel_size=(4, 4, 4))) self.layer2 = nn.sequential( nn.maxpool2d(kernel_size=(4, 4, 4)), nn.conv2d(25, 25, kernel_size=(3, 3, 3)), nn.conv2d(25, 25, kernel_size=(3, 3, 3), dilation=(1, 2, 2)), nn.conv2d(25, 25, kernel_size=(3, 3, 3), dilation=(2, 4, 4)), nn.maxunpool2d(25, 25, kernel_size=(2, 2, 2))) self.layer3 = nn.sequential( nn.conv2d(3, 25, kernel_size=(3, 3, 3)), nn.conv2d(25, 25, kernel_size=(3, 3, 3), dilation=(1, 2, 2)), nn.conv2d(25, 25, kernel_size=(3, 3, 3), dilation=(2, 4, 4))) self.layer4 = nn.sequential( nn.conv2d(75, 75, kernel_size=(3, 3, 3), dilation=(2, 4, 4)), nn.conv2d(75, 100, kernel_size=(3, 3, 3), dilation=(2, 4, 4)), nn.conv2d(100, 100, kernel_size=(3, 3, 3), dilation=(2, 4, 4)), nn.conv2d(100, 100, kernel_size=(3, 3, 3), dilation=(2, 4, 4)), nn.conv2d(100, 2, kernel_size=(1, 1, 1)))
def get_model(pretrained): if pretrained: model = pretrainedmodels.__dict__['alexnet'](pretrained='imagenet') else: model = pretrainedmodels.__dict__['alexnet'](pretrained=None) #print the model here to know what's going on model.last_linear = nn.sequential( nn.BatchNorm1d(4096), nn.Dropout(p=0.25), nn.Linear(in_features=4096, out_features=2048), nn.ReLU(), nn.BatchNorm1d(2048, eps=1e-05, momentum=0.1), nn.Dropout(p=0.5), nn.Linear(in_features=2048, out_features=1), ) return model
def nn_class(structure='vgg16', hidder_layer1=512, hidden_layer2=256, output_layer=102, dropout=0.3, lr=0.001, power='gpu'): ''' This builds the CNN network classifier that takes the following: - CNN Network: (calsss) vgg16 or densnet121 - Two hidden layers: (int) 512, 256 - output_layer: (int) 120 - dropout: (float) 0.3 - learning_rate: (float) 0.001 - power: (image processor/str) gpu ''' #structure condition if structure == 'vgg16': model = models.vgg16(pretrained=True) model.name = 'vgg16' elif structure == 'densnet121': model = models.densenet121(pretrained=True) model.name = 'densnet121' else: print("Only {} or {} are available models".format( list(model_arch.keys()))) #freezing model's parameters for parameter in model.parameters: parameter.requires_grad = False #building classifier classifier = nn.sequential(nn.Linear(model_arch[structure], hidder_layer1), nn.ReLU(), nn.Dropout(dropout), nn.Linear(hidder_layer1, hidden_layer2), nn.ReLU(), nn.Dropout(dropout), nn.Linear(hidder_layer2, output_layer), nn.LogSoftmax(dim=1)) model.classifier = classifier criterion = nn.NLLLoss() optimizer = optim.Adam(model.classifier.parameters(), lr=lr) if torch.cuda.is_available() and power == 'gpu': model.cuda() else: model.cpu() return model, model.name, criterion, optimizer
def __init__(self): super(Net, self).__init__() self.G2_123 = nn.Sequential( nn.ConvTranspose2d(in_channels =(100+108), out_channels =1024, kernel_size=4, stride=4), nn.BatchNorm2d(1024), nn.ReLU(), nn.ConvTranspose2d(in_channels =1024, out_channels =512, kernel_size=4, stride=2, padding=1), nn.BatchNorm2d(512), nn.ReLU(), nn.ConvTranspose2d(in_channels =512, out_channels =256, kernel_size=4, stride=2, padding=1), nn.BatchNorm2d(256), nn.ReLU(), ).to(device) self.G2_ABC = nn.Sequential( nn.Conv2d(in_channels=7, out_channels=64, kernel_size=4, stride=2, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.Conv2d(in_channels=64, out_channels=128, kernel_size=4, stride=2, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.Conv2d(in_channels=128, out_channels=256, kernel_size=4, stride=2, padding=1), nn.BatchNorm2d(256), nn.ReLU(), ).to(device) self.G2_456 = nn.sequential( nn.ConvTranspose2d(in_channels =512, out_channels =128, kernel_size=4, stride=2, padding=1), nn.BatchNorm2d(128), nn.ReLU(), nn.ConvTranspose2d(in_channels =128, out_channels =64, kernel_size=4, stride=2, padding=1), nn.BatchNorm2d(64), nn.ReLU(), nn.ConvTranspose2d(in_channels =64, out_channels =3, kernel_size=4, stride=2, padding=1), nn.Tanh(), ).to(device)
import torch import torchvision import torch.nn as nn model = torchvision.resnet18(pretrained=True) model_conv_output = nn.sequential(*list(model.children()))[:-2] device = "cuda:0" model_conv_output.to(device) for model_conv_output.parameters(): model_conv_output.requires_grad = False
def PointwiseConv(in_planes, planes): return nn.sequential(nn.Conv2d(in_planes, planes, kernel_size=1), nn.BatchNorm2d(planes))
def __init__(self, conv_dim=64, c_dim=5, repeat_num=6): super(Generator, self).__int__() self._name = 'generator_wgan' layers = [] layers.append( nn.Conv2d(3 + c_dim, conv_dim, kernel_size=7, stride=1, padding=3, bias=False)) layers.append(nn.InstanceNorm2d(conv_dim, affline=True)) layers.append(nn.RelU(inplace=True)) # Down-Sampling curr_dim = conv_dim for i in range(2): layers.append( nn.Conv2d(curr_dim, curr_dim * 2, kernel_size=4, stride=2, padding=1, bias=False)) layers.append(nn.InstanceNorm2d(curr_dim * 2, affline=True)) layers.append(nn.RelU(inplace=True)) curr_dim = curr_dim * 2 # Bottleneck for i in range(repeat_num): layers.append(ResidualBlock(dim_in=curr_dim, dim_out=curr_dim)) # Up-Sampling for i in range(2): layers.append( nn.ConvTranspose2d(curr_dim, curr_dim // 2, kernel_size=4, stride=2, padding=1, bias=False)) layers.append(nn.InstanceNorm2d(curr_dim // 2, affline=True)) layers.append(nn.RelU(inplace=True)) curr_dim = curr_dim // 2 self.main = nn.Sequential(*layers) layers = [] layers.append( nn.Conv2d(curr_dim, 3, kernel_size=7, stride=1, padding=3, bias=False)) layers.append(nn.Tanh()) self.img_reg = nn.sequential(*layers) layers = [] layers.append( nn.Conv2d(curr_dim, 1, kernel_size=7, stride=1, padding=3, bias=False)) layers.append(nn.Sigmoid()) self.attetion_reg = nn.Sequential(*layers)
import torch.nn as nn from HistLayer import HistLayer as HLmod # N is batch size; D_in is input dimension; # H is hidden dimension; D_out is output dimension. N D_in D_out net = nn.sequential(HLmod(D_in))
def __init__(self): super(mynet, self).__init__() self.output = nn.sequential(nn.Linear(5, 10), nn.ReLU(), nn.Linear(10, 3))
import torch.optim as optim import torch.nn.functional as F import numpy as np import matplotlib.pyplot as plt import torchvision.transforms as transforms from torchvision.transforms.transforms import Normalize # Implement the sequential module for feature extraction torch.manual_seed(50) network1 = nn.sequential( nn.Conv2d(in_channels=1, out_channels=6, kernal_size=5), nn.relu(), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(in_channels=6, out_channels=12, kernal_size=5), nn.relu(), nn.MaxPool2d(kernel_size=2, stride=2), nn.flattern(start_dim=1), nn.linear(in_features=12 * 4 * 4, out_features=120), nn.relu(), nn.linear(in_features=20, out_features=60), nn.relu(), nn.linear(in_features=60, out_features=10)) torch.manual_seed(50) network2 = nn.sequential( nn.Conv2d(in_channels=1, out_channels=6, kernal_size=5), nn.relu(), nn.MaxPool2d(kernel_size=2, stride=2), nn.BatchNorm2d(6), nn.Conv2d(in_channels=6, out_channels=12, kernal_size=5), nn.relu(), nn.MaxPool2d(kernel_size=2, stride=2),