class ResNet(nn.Module): def __init__(self, block, num_blocks, num_classes=10, name="Model"): super(ResNet, self).__init__() self.in_planes = 64 self.name = name self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(64) self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1) self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2) self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2) self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2) self.linear = nn.Linear(512 * block.expansion, num_classes) def _make_layer(self, block, planes, num_blocks, stride): strides = [stride] + [1] * (num_blocks - 1) layers = [] for stride in strides: layers.append(block(self.in_planes, planes, stride)) self.in_planes = planes * block.expansion return nn.Sequential(*layers) def forward(self, x): out = F.relu(self.bn1(self.conv1(x))) out = self.layer1(out) out = self.layer2(out) out = self.layer3(out) out = self.layer4(out) out = F.avg_pool2d(out, 4) out = out.view(out.size(0), -1) out = self.linear(out) out = out.view(out.size(0), -1) return F.log_softmax(out, dim=-1) def summary(self, input_size): summary(self, input_size=input_size) def gotrain(self, optimizer, train_loader, test_loader, epochs, statspath, scheduler=None, batch_scheduler=False, L1lambda=0): self.trainer = ModelTrainer(self, optimizer, train_loader, test_loader, statspath, scheduler, batch_scheduler, L1lambda) self.trainer.run(epochs) def stats(self): return self.trainer.stats if self.trainer else None
def gotrain(self, optimizer, train_loader, test_loader, epochs, statspath, scheduler=None, batch_scheduler=False, L1lambda=0): self.trainer = ModelTrainer(self, optimizer, train_loader, test_loader, statspath, scheduler, batch_scheduler, L1lambda) self.trainer.run(epochs)
def gotrain(self, model, optimizer, train_loader, test_loader, epochs, statspath, criterion, writer, scheduler=None, batch_scheduler=False, L1lambda=0): self.trainer = ModelTrainer(model, optimizer, train_loader, test_loader, statspath, criterion, writer, scheduler, batch_scheduler, L1lambda) #print("hello") self.trainer.run(epochs)
class Net(nn.Module): """ Base network that defines helper functions, summary and mapping to device """ def conv2d(self, in_channels, out_channels, kernel_size=(3,3), dilation=1, groups=1, padding=1, bias=False, padding_mode="zeros"): return [nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, groups=groups, dilation=dilation, padding=padding, bias=bias, padding_mode=padding_mode)] def separable_conv2d(self, in_channels, out_channels, kernel_size=(3,3), dilation=1, padding=1, bias=False, padding_mode="zeros"): return [nn.Conv2d(in_channels=in_channels, out_channels=in_channels, kernel_size=kernel_size, groups=in_channels, dilation=dilation, padding=padding, bias=bias, padding_mode=padding_mode), nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=(1,1), bias=bias)] def activate(self, l, out_channels, bn=True, dropout=0, relu=True): if bn: l.append(nn.BatchNorm2d(out_channels)) if dropout>0: l.append(nn.Dropout(dropout)) if relu: l.append(nn.ReLU()) return nn.Sequential(*l) def create_conv2d(self, in_channels, out_channels, kernel_size=(3,3), dilation=1, groups=1, padding=1, bias=False, bn=True, dropout=0, relu=True, padding_mode="zeros"): return self.activate(self.conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, groups=groups, dilation=dilation, padding=padding, bias=bias, padding_mode=padding_mode), out_channels, bn, dropout, relu) def create_depthwise_conv2d(self, in_channels, out_channels, kernel_size=(3,3), dilation=1, padding=1, bias=False, bn=True, dropout=0, relu=True, padding_mode="zeros"): return self.activate(self.separable_conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, dilation=dilation, padding=padding, bias=bias, padding_mode=padding_mode), out_channels, bn, dropout, relu) def __init__(self, name="Model"): super(Net, self).__init__() self.trainer = None self.name = name def summary(self, input_size): #input_size=(1, 28, 28) summary(self, input_size=input_size) def gotrain(self, optimizer, train_loader, test_loader, epochs, statspath, scheduler=None, batch_scheduler=False, L1lambda=0): self.trainer = ModelTrainer(self, optimizer, train_loader, test_loader, statspath, scheduler, batch_scheduler, L1lambda) self.trainer.run(epochs) def stats(self): return self.trainer.stats if self.trainer else None
class Net(nn.Module): """ Base network that defines helper functions, summary and mapping to device """ def conv2d(self, in_channels, out_channels, kernel_size=(3, 3), dilation=1, groups=1, stride=1, padding=1, bias=False, padding_mode="zeros"): return [ nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, groups=groups, dilation=dilation, stride=stride, padding=padding, bias=bias, padding_mode=padding_mode) ] def separable_conv2d(self, in_channels, out_channels, kernel_size=(3, 3), dilation=1, stride=1, padding=1, bias=False, padding_mode="zeros"): return [ nn.Conv2d(in_channels=in_channels, out_channels=in_channels, kernel_size=kernel_size, groups=in_channels, dilation=dilation, stride=stride, padding=padding, bias=bias, padding_mode=padding_mode), nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=(1, 1), bias=bias) ] def activate(self, l, out_channels, bn=True, dropout=0, relu=True, max_pooling=0): if (max_pooling > 0): l.append(nn.MaxPool2d(2, 2)) if bn: l.append(nn.BatchNorm2d(out_channels)) if dropout > 0: l.append(nn.Dropout(dropout)) if relu: l.append(nn.ReLU()) return nn.Sequential(*l) def create_conv2d(self, in_channels, out_channels, kernel_size=(3, 3), dilation=1, groups=1, stride=1, padding=1, bias=False, bn=True, dropout=0, relu=True, padding_mode="zeros", max_pooling=0): return self.activate( self.conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, groups=groups, dilation=dilation, stride=stride, padding=padding, bias=bias, padding_mode=padding_mode), out_channels, bn, dropout, relu, max_pooling) def create_depthwise_conv2d(self, in_channels, out_channels, kernel_size=(3, 3), dilation=1, padding=1, bias=False, bn=True, dropout=0, relu=True, padding_mode="zeros"): return self.activate( self.separable_conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, dilation=dilation, padding=padding, bias=bias, padding_mode=padding_mode), out_channels, bn, dropout, relu) def __init__(self, name="Model"): super(Net, self).__init__() self.trainer = None self.name = name def summary(self, input_size): #input_size=(1, 28, 28) summary(self, input_size=input_size) def gotrain(self, optimizer, train_loader, test_loader, epochs, statspath, scheduler=None, batch_scheduler=False, criterion1=None, criterion2=None, L1lambda=0): self.trainer = ModelTrainer(self, optimizer, train_loader, test_loader, statspath, scheduler, batch_scheduler, criterion1=criterion1, criterion2=criterion2, L1lambda=L1lambda) self.trainer.run(epochs) # to initialize parameters def init_params(self): for m in self.modules(): if isinstance(m, nn.Conv2d): torch.nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, nn.ConvTranspose2d): torch.nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, nn.BatchNorm2d): torch.nn.init.constant_(m.weight, 1) torch.nn.init.constant_(m.bias, 0) print("Model parameters initialized")