code
stringlengths
17
6.64M
def ShuffleNetG2(): cfg = {'out_planes': [200, 400, 800], 'num_blocks': [4, 8, 4], 'groups': 2} return ShuffleNet(cfg)
def ShuffleNetG3(): cfg = {'out_planes': [240, 480, 960], 'num_blocks': [4, 8, 4], 'groups': 3} return ShuffleNet(cfg)
def test(): net = ShuffleNetG2() x = torch.randn(1, 3, 32, 32) y = net(x) print(y)
class VGG(nn.Module): def __init__(self, vgg_name): super(VGG, self).__init__() self.features = self._make_layers(cfg[vgg_name]) self.classifier = nn.Linear(512, 10) def forward(self, x): out = self.features(x) out = out.view(out.size(0), (- 1)) out = self.cla...
def test(): net = VGG('VGG11') x = torch.randn(2, 3, 32, 32) y = net(x) print(y.size())
def narcissus_gen(dataset_path=dataset_path, lab=lab): noise_size = 32 l_inf_r = (16 / 255) surrogate_model = ResNet18_201().cuda() generating_model = ResNet18_201().cuda() surrogate_epochs = 200 generating_lr_warmup = 0.1 warmup_round = 5 generating_lr_tri = 0.01 gen_round = 1000 ...
class DemandDataset(torch.utils.data.Dataset): def __init__(self, data_dir, cut_len=(16000 * 2)): self.cut_len = cut_len self.clean_dir = os.path.join(data_dir, 'clean') self.noisy_dir = os.path.join(data_dir, 'noisy') self.clean_wav_name = os.listdir(self.clean_dir) self....
def load_data(ds_dir, batch_size, n_cpu, cut_len): torchaudio.set_audio_backend('sox_io') train_dir = os.path.join(ds_dir, 'train') test_dir = os.path.join(ds_dir, 'test') train_ds = DemandDataset(train_dir, cut_len) test_ds = DemandDataset(test_dir, cut_len) train_dataset = torch.utils.data.D...
def exists(val): return (val is not None)
def default(val, d): return (val if exists(val) else d)
def calc_same_padding(kernel_size): pad = (kernel_size // 2) return (pad, (pad - ((kernel_size + 1) % 2)))
class Swish(nn.Module): def forward(self, x): return (x * x.sigmoid())
class GLU(nn.Module): def __init__(self, dim): super().__init__() self.dim = dim def forward(self, x): (out, gate) = x.chunk(2, dim=self.dim) return (out * gate.sigmoid())
class DepthWiseConv1d(nn.Module): def __init__(self, chan_in, chan_out, kernel_size, padding): super().__init__() self.padding = padding self.conv = nn.Conv1d(chan_in, chan_out, kernel_size, groups=chan_in) def forward(self, x): x = F.pad(x, self.padding) return self....
class Scale(nn.Module): def __init__(self, scale, fn): super().__init__() self.fn = fn self.scale = scale def forward(self, x, **kwargs): return (self.fn(x, **kwargs) * self.scale)
class PreNorm(nn.Module): def __init__(self, dim, fn): super().__init__() self.fn = fn self.norm = nn.LayerNorm(dim) def forward(self, x, **kwargs): x = self.norm(x) return self.fn(x, **kwargs)
class Attention(nn.Module): def __init__(self, dim, heads=8, dim_head=64, dropout=0.0, max_pos_emb=512): super().__init__() inner_dim = (dim_head * heads) self.heads = heads self.scale = (dim_head ** (- 0.5)) self.to_q = nn.Linear(dim, inner_dim, bias=False) self.t...
class FeedForward(nn.Module): def __init__(self, dim, mult=4, dropout=0.0): super().__init__() self.net = nn.Sequential(nn.Linear(dim, (dim * mult)), Swish(), nn.Dropout(dropout), nn.Linear((dim * mult), dim), nn.Dropout(dropout)) def forward(self, x): return self.net(x)
class ConformerConvModule(nn.Module): def __init__(self, dim, causal=False, expansion_factor=2, kernel_size=31, dropout=0.0): super().__init__() inner_dim = (dim * expansion_factor) padding = (calc_same_padding(kernel_size) if (not causal) else ((kernel_size - 1), 0)) self.net = n...
class ConformerBlock(nn.Module): def __init__(self, *, dim, dim_head=64, heads=8, ff_mult=4, conv_expansion_factor=2, conv_kernel_size=31, attn_dropout=0.0, ff_dropout=0.0, conv_dropout=0.0): super().__init__() self.ff1 = FeedForward(dim=dim, mult=ff_mult, dropout=ff_dropout) self.attn = ...
def pesq_loss(clean, noisy, sr=16000): try: pesq_score = pesq(sr, clean, noisy, 'wb') except: pesq_score = (- 1) return pesq_score
def batch_pesq(clean, noisy): pesq_score = Parallel(n_jobs=(- 1))((delayed(pesq_loss)(c, n) for (c, n) in zip(clean, noisy))) pesq_score = np.array(pesq_score) if ((- 1) in pesq_score): return None pesq_score = ((pesq_score - 1) / 3.5) return torch.FloatTensor(pesq_score).to('cuda')
class Discriminator(nn.Module): def __init__(self, ndf, in_channel=2): super().__init__() self.layers = nn.Sequential(nn.utils.spectral_norm(nn.Conv2d(in_channel, ndf, (4, 4), (2, 2), (1, 1), bias=False)), nn.InstanceNorm2d(ndf, affine=True), nn.PReLU(ndf), nn.utils.spectral_norm(nn.Conv2d(ndf, (...
def kaiming_init(m): if isinstance(m, nn.Linear): torch.nn.init.kaiming_normal_(m.weight) if (m.bias is not None): m.bias.data.fill_(0.01) if isinstance(m, nn.Conv2d): torch.nn.init.kaiming_normal_(m.weight) if (m.bias is not None): m.bias.data.fill_(0.0...
def power_compress(x): real = x[(..., 0)] imag = x[(..., 1)] spec = torch.complex(real, imag) mag = torch.abs(spec) phase = torch.angle(spec) mag = (mag ** 0.3) real_compress = (mag * torch.cos(phase)) imag_compress = (mag * torch.sin(phase)) return torch.stack([real_compress, imag...
def power_uncompress(real, imag): spec = torch.complex(real, imag) mag = torch.abs(spec) phase = torch.angle(spec) mag = (mag ** (1.0 / 0.3)) real_compress = (mag * torch.cos(phase)) imag_compress = (mag * torch.sin(phase)) return torch.stack([real_compress, imag_compress], (- 1))
class LearnableSigmoid(nn.Module): def __init__(self, in_features, beta=1): super().__init__() self.beta = beta self.slope = nn.Parameter(torch.ones(in_features)) self.slope.requiresGrad = True def forward(self, x): return (self.beta * torch.sigmoid((self.slope * x)))...
class Bottleneck(nn.Module): def __init__(self, in_planes, growth_rate): super(Bottleneck, self).__init__() self.bn1 = nn.BatchNorm2d(in_planes) self.conv1 = nn.Conv2d(in_planes, (4 * growth_rate), kernel_size=1, bias=False) self.bn2 = nn.BatchNorm2d((4 * growth_rate)) sel...
class Transition(nn.Module): def __init__(self, in_planes, out_planes): super(Transition, self).__init__() self.bn = nn.BatchNorm2d(in_planes) self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=1, bias=False) def forward(self, x): out = self.conv(F.relu(self.bn(x))) ...
class DenseNet(nn.Module): def __init__(self, block, nblocks, growth_rate=12, reduction=0.5, num_classes=10): super(DenseNet, self).__init__() self.growth_rate = growth_rate num_planes = (2 * growth_rate) self.conv1 = nn.Conv2d(3, num_planes, kernel_size=3, padding=1, bias=False) ...
def DenseNet121(): return DenseNet(Bottleneck, [6, 12, 24, 16], growth_rate=32)
def DenseNet169(): return DenseNet(Bottleneck, [6, 12, 32, 32], growth_rate=32)
def DenseNet201(): return DenseNet(Bottleneck, [6, 12, 48, 32], growth_rate=32)
def DenseNet161(): return DenseNet(Bottleneck, [6, 12, 36, 24], growth_rate=48)
def densenet_cifar(): return DenseNet(Bottleneck, [6, 12, 24, 16], growth_rate=12)
def test(): net = densenet_cifar() x = torch.randn(1, 3, 32, 32) y = net(x) print(y)
class BasicBlock(nn.Module): expansion = 1 def __init__(self, in_planes, planes, stride=1): super(BasicBlock, self).__init__() self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2...
class Root(nn.Module): def __init__(self, in_channels, out_channels, kernel_size=1): super(Root, self).__init__() self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=((kernel_size - 1) // 2), bias=False) self.bn = nn.BatchNorm2d(out_channels) def forward(s...
class Tree(nn.Module): def __init__(self, block, in_channels, out_channels, level=1, stride=1): super(Tree, self).__init__() self.level = level if (level == 1): self.root = Root((2 * out_channels), out_channels) self.left_node = block(in_channels, out_channels, str...
class DLA(nn.Module): def __init__(self, block=BasicBlock, num_classes=10): super(DLA, self).__init__() self.base = nn.Sequential(nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1, bias=False), nn.BatchNorm2d(16), nn.ReLU(True)) self.layer1 = nn.Sequential(nn.Conv2d(16, 16, kernel_size=...
def test(): net = DLA() print(net) x = torch.randn(1, 3, 32, 32) y = net(x) print(y.size())
class BasicBlock(nn.Module): expansion = 1 def __init__(self, in_planes, planes, stride=1): super(BasicBlock, self).__init__() self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2...
class Root(nn.Module): def __init__(self, in_channels, out_channels, kernel_size=1): super(Root, self).__init__() self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=((kernel_size - 1) // 2), bias=False) self.bn = nn.BatchNorm2d(out_channels) def forward(s...
class Tree(nn.Module): def __init__(self, block, in_channels, out_channels, level=1, stride=1): super(Tree, self).__init__() self.root = Root((2 * out_channels), out_channels) if (level == 1): self.left_tree = block(in_channels, out_channels, stride=stride) self.ri...
class SimpleDLA(nn.Module): def __init__(self, block=BasicBlock, num_classes=10): super(SimpleDLA, self).__init__() self.base = nn.Sequential(nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1, bias=False), nn.BatchNorm2d(16), nn.ReLU(True)) self.layer1 = nn.Sequential(nn.Conv2d(16, 16, ...
def test(): net = SimpleDLA() print(net) x = torch.randn(1, 3, 32, 32) y = net(x) print(y.size())
def swish(x): return (x * x.sigmoid())
def drop_connect(x, drop_ratio): keep_ratio = (1.0 - drop_ratio) mask = torch.empty([x.shape[0], 1, 1, 1], dtype=x.dtype, device=x.device) mask.bernoulli_(keep_ratio) x.div_(keep_ratio) x.mul_(mask) return x
class SE(nn.Module): 'Squeeze-and-Excitation block with Swish.' def __init__(self, in_channels, se_channels): super(SE, self).__init__() self.se1 = nn.Conv2d(in_channels, se_channels, kernel_size=1, bias=True) self.se2 = nn.Conv2d(se_channels, in_channels, kernel_size=1, bias=True) ...
class Block(nn.Module): 'expansion + depthwise + pointwise + squeeze-excitation' def __init__(self, in_channels, out_channels, kernel_size, stride, expand_ratio=1, se_ratio=0.0, drop_rate=0.0): super(Block, self).__init__() self.stride = stride self.drop_rate = drop_rate self....
class EfficientNet(nn.Module): def __init__(self, cfg, num_classes=1000): super(EfficientNet, self).__init__() self.cfg = cfg self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(32) self.layers = self._make_layers(in_chan...
def EfficientNetB0(): cfg = {'num_blocks': [1, 2, 2, 3, 3, 4, 1], 'expansion': [1, 6, 6, 6, 6, 6, 6], 'out_channels': [16, 24, 40, 80, 112, 192, 320], 'kernel_size': [3, 3, 5, 3, 5, 5, 3], 'stride': [1, 2, 2, 2, 1, 2, 1], 'dropout_rate': 0.2, 'drop_connect_rate': 0.2} return EfficientNet(cfg)
def test(): net = EfficientNetB0() x = torch.randn(2, 3, 32, 32) y = net(x) print(y.shape)
class Inception(nn.Module): def __init__(self, in_planes, n1x1, n3x3red, n3x3, n5x5red, n5x5, pool_planes): super(Inception, self).__init__() self.b1 = nn.Sequential(nn.Conv2d(in_planes, n1x1, kernel_size=1), nn.BatchNorm2d(n1x1), nn.ReLU(True)) self.b2 = nn.Sequential(nn.Conv2d(in_planes...
class GoogLeNet(nn.Module): 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, 1...
def test(): net = GoogLeNet() x = torch.randn(1, 3, 32, 32) y = net(x) print(y.size())
class LeNet(nn.Module): def __init__(self): super(LeNet, self).__init__() self.conv1 = nn.Conv2d(3, 6, 5) self.conv2 = nn.Conv2d(6, 16, 5) self.fc1 = nn.Linear(((16 * 5) * 5), 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10) def forward(self, x)...
class Block(nn.Module): 'Depthwise conv + Pointwise conv' def __init__(self, in_planes, out_planes, stride=1): super(Block, self).__init__() self.conv1 = nn.Conv2d(in_planes, in_planes, kernel_size=3, stride=stride, padding=1, groups=in_planes, bias=False) self.bn1 = nn.BatchNorm2d(in...
class MobileNet(nn.Module): cfg = [64, (128, 2), 128, (256, 2), 256, (512, 2), 512, 512, 512, 512, 512, (1024, 2), 1024] def __init__(self, num_classes=10): super(MobileNet, self).__init__() self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1, bias=False) self.bn1 = nn.Ba...
def test(): net = MobileNet() x = torch.randn(1, 3, 32, 32) y = net(x) print(y.size())
class Block(nn.Module): 'expand + depthwise + pointwise' def __init__(self, in_planes, out_planes, expansion, stride): super(Block, self).__init__() self.stride = stride planes = (expansion * in_planes) self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, stride=1, padding...
class MobileNetV2(nn.Module): cfg = [(1, 16, 1, 1), (6, 24, 2, 1), (6, 32, 3, 2), (6, 64, 4, 2), (6, 96, 3, 1), (6, 160, 3, 2), (6, 320, 1, 1)] def __init__(self, num_classes=10): super(MobileNetV2, self).__init__() self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1, bias=False)...
def test(): net = MobileNetV2() x = torch.randn(2, 3, 32, 32) y = net(x) print(y.size())
class SepConv(nn.Module): 'Separable Convolution.' def __init__(self, in_planes, out_planes, kernel_size, stride): super(SepConv, self).__init__() self.conv1 = nn.Conv2d(in_planes, out_planes, kernel_size, stride, padding=((kernel_size - 1) // 2), bias=False, groups=in_planes) self.bn...
class CellA(nn.Module): def __init__(self, in_planes, out_planes, stride=1): super(CellA, self).__init__() self.stride = stride self.sep_conv1 = SepConv(in_planes, out_planes, kernel_size=7, stride=stride) if (stride == 2): self.conv1 = nn.Conv2d(in_planes, out_planes,...
class CellB(nn.Module): def __init__(self, in_planes, out_planes, stride=1): super(CellB, self).__init__() self.stride = stride self.sep_conv1 = SepConv(in_planes, out_planes, kernel_size=7, stride=stride) self.sep_conv2 = SepConv(in_planes, out_planes, kernel_size=3, stride=strid...
class PNASNet(nn.Module): def __init__(self, cell_type, num_cells, num_planes): super(PNASNet, self).__init__() self.in_planes = num_planes self.cell_type = cell_type self.conv1 = nn.Conv2d(3, num_planes, kernel_size=3, stride=1, padding=1, bias=False) self.bn1 = nn.BatchN...
def PNASNetA(): return PNASNet(CellA, num_cells=6, num_planes=44)
def PNASNetB(): return PNASNet(CellB, num_cells=6, num_planes=32)
def test(): net = PNASNetB() x = torch.randn(1, 3, 32, 32) y = net(x) print(y)
class PreActBlock(nn.Module): 'Pre-activation version of the BasicBlock.' expansion = 1 def __init__(self, in_planes, planes, stride=1): super(PreActBlock, self).__init__() self.bn1 = nn.BatchNorm2d(in_planes) self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride,...
class PreActBottleneck(nn.Module): 'Pre-activation version of the original Bottleneck module.' expansion = 4 def __init__(self, in_planes, planes, stride=1): super(PreActBottleneck, self).__init__() self.bn1 = nn.BatchNorm2d(in_planes) self.conv1 = nn.Conv2d(in_planes, planes, ker...
class PreActResNet(nn.Module): def __init__(self, block, num_blocks, num_classes=10): super(PreActResNet, self).__init__() self.in_planes = 64 self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False) self.layer1 = self._make_layer(block, 64, num_blocks[0], str...
def PreActResNet18(): return PreActResNet(PreActBlock, [2, 2, 2, 2])
def PreActResNet34(): return PreActResNet(PreActBlock, [3, 4, 6, 3])
def PreActResNet50(): return PreActResNet(PreActBottleneck, [3, 4, 6, 3])
def PreActResNet101(): return PreActResNet(PreActBottleneck, [3, 4, 23, 3])
def PreActResNet152(): return PreActResNet(PreActBottleneck, [3, 8, 36, 3])
def test(): net = PreActResNet18() y = net(torch.randn(1, 3, 32, 32)) print(y.size())
class SE(nn.Module): 'Squeeze-and-Excitation block.' def __init__(self, in_planes, se_planes): super(SE, self).__init__() self.se1 = nn.Conv2d(in_planes, se_planes, kernel_size=1, bias=True) self.se2 = nn.Conv2d(se_planes, in_planes, kernel_size=1, bias=True) def forward(self, x)...
class Block(nn.Module): def __init__(self, w_in, w_out, stride, group_width, bottleneck_ratio, se_ratio): super(Block, self).__init__() w_b = int(round((w_out * bottleneck_ratio))) self.conv1 = nn.Conv2d(w_in, w_b, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(w_b) ...
class RegNet(nn.Module): def __init__(self, cfg, num_classes=10): super(RegNet, self).__init__() self.cfg = cfg self.in_planes = 64 self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(64) self.layer1 = self._make_...
def RegNetX_200MF(): cfg = {'depths': [1, 1, 4, 7], 'widths': [24, 56, 152, 368], 'strides': [1, 1, 2, 2], 'group_width': 8, 'bottleneck_ratio': 1, 'se_ratio': 0} return RegNet(cfg)
def RegNetX_400MF(): cfg = {'depths': [1, 2, 7, 12], 'widths': [32, 64, 160, 384], 'strides': [1, 1, 2, 2], 'group_width': 16, 'bottleneck_ratio': 1, 'se_ratio': 0} return RegNet(cfg)
def RegNetY_400MF(): cfg = {'depths': [1, 2, 7, 12], 'widths': [32, 64, 160, 384], 'strides': [1, 1, 2, 2], 'group_width': 16, 'bottleneck_ratio': 1, 'se_ratio': 0.25} return RegNet(cfg)
def test(): net = RegNetX_200MF() print(net) x = torch.randn(2, 3, 32, 32) y = net(x) print(y.shape)
class BasicBlock(nn.Module): expansion = 1 def __init__(self, in_planes, planes, stride=1): super(BasicBlock, self).__init__() self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2...
class Bottleneck(nn.Module): expansion = 4 def __init__(self, in_planes, planes, stride=1): super(Bottleneck, self).__init__() self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(planes) self.conv2 = nn.Conv2d(planes, planes, kernel_s...
class ResNet(nn.Module): def __init__(self, block, num_blocks, num_classes=10): super(ResNet, self).__init__() self.in_planes = 64 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(blo...
def ResNet18(): return ResNet(BasicBlock, [2, 2, 2, 2])
def ResNet18_11(): return ResNet(BasicBlock, [2, 2, 2, 2], num_classes=11)
def ResNet18_201(): return ResNet(BasicBlock, [2, 2, 2, 2], num_classes=201)
def ResNet34(): return ResNet(BasicBlock, [3, 4, 6, 3])
def ResNet50(): return ResNet(Bottleneck, [3, 4, 6, 3])
def ResNet101(): return ResNet(Bottleneck, [3, 4, 23, 3])
def ResNet152(): return ResNet(Bottleneck, [3, 8, 36, 3])
def test(): net = ResNet18() y = net(torch.randn(1, 3, 32, 32)) print(y.size())
class Block(nn.Module): 'Grouped convolution block.' expansion = 2 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...
class ResNeXt(nn.Module): 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,...
def ResNeXt29_2x64d(): return ResNeXt(num_blocks=[3, 3, 3], cardinality=2, bottleneck_width=64)