[black]

parent c83fd608
Pipeline #46610 passed with stage
in 14 minutes and 1 second
# see https://docs.python.org/3/library/pkgutil.html
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
# see https://docs.python.org/3/library/pkgutil.html
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
# gets sphinx autodoc done right - don't remove it
def __appropriate__(*args):
"""Says object was actually declared here, an not on the import module.
......@@ -11,8 +10,10 @@ def __appropriate__(*args):
<https://github.com/sphinx-doc/sphinx/issues/3048>`
"""
for obj in args: obj.__module__ = __name__
for obj in args:
obj.__module__ = __name__
__appropriate__()
__all__ = [_ for _ in dir() if not _.startswith('_')]
__all__ = [_ for _ in dir() if not _.startswith("_")]
import torch
from .utils import make_conv_layers
CASIA_CONFIG = [32, 64, 'M', 64, 128, 'M', 96, 192, 'M', 128, 256, 'M', 160, 320]
CASIA_CONFIG = [32, 64, "M", 64, 128, "M", 96, 192, "M", 128, 256, "M", 160, 320]
class CASIANet(torch.nn.Module):
""" The class defining the CASIA-Net CNN model.
......@@ -61,6 +62,6 @@ class CASIANet(torch.nn.Module):
x = self.conv(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = torch.nn.functional.dropout(x, p = self.drop_rate, training=self.training)
x = torch.nn.functional.dropout(x, p=self.drop_rate, training=self.training)
out = self.classifier(x)
return out, x # x for feature
......@@ -8,7 +8,8 @@ import torch.nn.functional as F
from .utils import make_conv_layers
CNN8_CONFIG = [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M']
CNN8_CONFIG = [64, 64, "M", 128, 128, "M", 256, 256, "M", 512, 512, "M"]
class CNN8(nn.Module):
""" The class defining the CNN8 model.
......@@ -65,7 +66,6 @@ class CNN8(nn.Module):
x = self.conv(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = F.dropout(x, p = self.drop_rate, training=self.training)
x = F.dropout(x, p=self.drop_rate, training=self.training)
out = self.classifier(x)
return out, x # x for feature
......@@ -4,6 +4,7 @@
import torch
import torch.nn as nn
class ConditionalGAN_generator(nn.Module):
""" Class implementating the conditional GAN generator
......@@ -18,6 +19,7 @@ class ConditionalGAN_generator(nn.Module):
The sequential container
"""
def __init__(self, noise_dim, conditional_dim, channels=3, ngpu=1):
"""Init function
......@@ -42,7 +44,9 @@ class ConditionalGAN_generator(nn.Module):
self.main = nn.Sequential(
# input is Z, going into a convolution
nn.ConvTranspose2d((noise_dim + conditional_dim), ngf * 8, 4, 1, 0, bias=False),
nn.ConvTranspose2d(
(noise_dim + conditional_dim), ngf * 8, 4, 1, 0, bias=False
),
nn.BatchNorm2d(ngf * 8),
nn.ReLU(True),
# state size. (ngf*8) x 4 x 4
......@@ -80,9 +84,9 @@ class ConditionalGAN_generator(nn.Module):
"""
generator_input = torch.cat((z, y), 1)
#if isinstance(generator_input.data, torch.cuda.FloatTensor) and self.ngpu > 1:
# if isinstance(generator_input.data, torch.cuda.FloatTensor) and self.ngpu > 1:
# output = nn.parallel.data_parallel(self.main, generator_input, range(self.ngpu))
#else:
# else:
# output = self.main(generator_input)
# let's assume that we will never face the case where more than a GPU is used ...
......@@ -105,6 +109,7 @@ class ConditionalGAN_discriminator(nn.Module):
The sequential container
"""
def __init__(self, conditional_dim, channels=3, ngpu=1):
"""Init function
......@@ -142,10 +147,9 @@ class ConditionalGAN_discriminator(nn.Module):
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*8) x 4 x 4
nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False),
nn.Sigmoid()
nn.Sigmoid(),
)
def forward(self, images, y):
"""Forward function
......@@ -162,9 +166,9 @@ class ConditionalGAN_discriminator(nn.Module):
the output of the discriminator
"""
input_discriminator = torch.cat((images, y), 1)
#if isinstance(input_discriminator.data, torch.cuda.FloatTensor) and self.ngpu > 1:
# if isinstance(input_discriminator.data, torch.cuda.FloatTensor) and self.ngpu > 1:
# output = nn.parallel.data_parallel(self.main, input_discriminator, range(self.ngpu))
#else:
# else:
# output = self.main(input_discriminator)
# let's assume that we will never face the case where more than a GPU is used ...
......
......@@ -3,6 +3,7 @@
from torch import nn
class ConvAutoencoder(nn.Module):
"""
A class defining a simple convolutional autoencoder.
......@@ -13,6 +14,7 @@ class ConvAutoencoder(nn.Module):
returns the encoder output if true, the reconstructed image otherwise.
"""
def __init__(self, return_latent_embedding=False):
"""
Init function
......@@ -26,7 +28,8 @@ class ConvAutoencoder(nn.Module):
super(ConvAutoencoder, self).__init__()
self.return_latent_embedding = return_latent_embedding
self.encoder = nn.Sequential(nn.Conv2d(3, 16, 5, padding=2),
self.encoder = nn.Sequential(
nn.Conv2d(3, 16, 5, padding=2),
nn.ReLU(True),
nn.MaxPool2d(2),
nn.Conv2d(16, 16, 5, padding=2),
......@@ -37,9 +40,11 @@ class ConvAutoencoder(nn.Module):
nn.MaxPool2d(2),
nn.Conv2d(16, 16, 3, padding=2),
nn.ReLU(True),
nn.MaxPool2d(2))
nn.MaxPool2d(2),
)
self.decoder = nn.Sequential(nn.ConvTranspose2d(16, 16, 3, stride=2, padding=1),
self.decoder = nn.Sequential(
nn.ConvTranspose2d(16, 16, 3, stride=2, padding=1),
nn.ReLU(True),
nn.ConvTranspose2d(16, 16, 3, stride=2, padding=1),
nn.ReLU(True),
......@@ -48,7 +53,8 @@ class ConvAutoencoder(nn.Module):
nn.ConvTranspose2d(16, 3, 5, stride=2, padding=2),
nn.ReLU(True),
nn.ConvTranspose2d(3, 3, 2, stride=1, padding=1),
nn.Tanh())
nn.Tanh(),
)
def forward(self, x):
""" Propagate data through the network
......
......@@ -21,6 +21,7 @@ class DCGAN_generator(nn.Module):
The number of available GPU devices
"""
def __init__(self, ngpu):
"""Init function
......@@ -40,7 +41,7 @@ class DCGAN_generator(nn.Module):
self.main = nn.Sequential(
# input is Z, going into a convolution
nn.ConvTranspose2d( nz, ngf * 8, 4, 1, 0, bias=False),
nn.ConvTranspose2d(nz, ngf * 8, 4, 1, 0, bias=False),
nn.BatchNorm2d(ngf * 8),
nn.ReLU(True),
# state size. (ngf*8) x 4 x 4
......@@ -56,7 +57,7 @@ class DCGAN_generator(nn.Module):
nn.BatchNorm2d(ngf),
nn.ReLU(True),
# state size. (ngf) x 32 x 32
nn.ConvTranspose2d( ngf, nc, 4, 2, 1, bias=False),
nn.ConvTranspose2d(ngf, nc, 4, 2, 1, bias=False),
nn.Tanh()
# state size. (nc) x 64 x 64
)
......@@ -74,9 +75,9 @@ class DCGAN_generator(nn.Module):
the output of the generator (i.e. an image)
"""
#if isinstance(input.data, torch.cuda.FloatTensor) and self.ngpu > 1:
# if isinstance(input.data, torch.cuda.FloatTensor) and self.ngpu > 1:
# output = nn.parallel.data_parallel(self.main, input, range(self.ngpu))
#else:
# else:
# output = self.main(input)
# let's assume that we will never face the case where more than a GPU is used ...
......@@ -100,6 +101,7 @@ class DCGAN_discriminator(nn.Module):
The number of available GPU devices
"""
def __init__(self, ngpu):
"""Init function
......@@ -112,7 +114,6 @@ class DCGAN_discriminator(nn.Module):
super(DCGAN_discriminator, self).__init__()
self.ngpu = ngpu
# just to test - will soon be args
ndf = 64
nc = 3
......@@ -135,7 +136,7 @@ class DCGAN_discriminator(nn.Module):
nn.LeakyReLU(0.2, inplace=True),
# state size. (ndf*8) x 4 x 4
nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False),
nn.Sigmoid()
nn.Sigmoid(),
)
def forward(self, input):
......@@ -151,9 +152,9 @@ class DCGAN_discriminator(nn.Module):
the output of the generator (i.e. an image)
"""
#if isinstance(input.data, torch.cuda.FloatTensor) and self.ngpu > 1:
# if isinstance(input.data, torch.cuda.FloatTensor) and self.ngpu > 1:
# output = nn.parallel.data_parallel(self.main, input, range(self.ngpu))
#else:
# else:
# output = self.main(input)
# let's assume that we will never face the case where more than a GPU is used ...
......
......@@ -59,35 +59,37 @@ class DeepMSPAD(nn.Module):
# Mean of weight and bias for all filters
bias_values = temp_layer.bias.data.detach().numpy()
mean_weight = np.mean(temp_layer.weight.data.detach().numpy(),axis=1) # for 64 filters
mean_weight = np.mean(
temp_layer.weight.data.detach().numpy(), axis=1
) # for 64 filters
new_weight = np.zeros((64,num_channels,3,3))
new_weight = np.zeros((64, num_channels, 3, 3))
for i in range(num_channels):
new_weight[:,i,:,:]=mean_weight
new_weight[:, i, :, :] = mean_weight
# initialize new layer with required number of channels `num_channels`
features[0] = nn.Conv2d(num_channels, 64, kernel_size=(3, 3), stride=(1, 1), padding =(1, 1))
features[0] = nn.Conv2d(
num_channels, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)
)
features[0].weight.data = torch.Tensor(new_weight)
features[0].bias.data = torch.Tensor(bias_values) #check
features[0].bias.data = torch.Tensor(bias_values) # check
self.enc = nn.Sequential(*features)
self.linear1 = nn.Linear(25088,256)
self.linear1 = nn.Linear(25088, 256)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(p=0.5)
self.linear2 = nn.Linear(256,1)
self.linear2 = nn.Linear(256, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
""" Propagate data through the network
......@@ -105,7 +107,7 @@ class DeepMSPAD(nn.Module):
enc = self.enc(x)
x = enc.view(-1,25088)
x = enc.view(-1, 25088)
x = self.linear1(x)
......
......@@ -36,10 +36,9 @@ class DeepPixBiS(nn.Module):
self.enc = nn.Sequential(*features[0:8])
self.dec=nn.Conv2d(384, 1, kernel_size=1, padding=0)
self.linear=nn.Linear(14*14,1)
self.dec = nn.Conv2d(384, 1, kernel_size=1, padding=0)
self.linear = nn.Linear(14 * 14, 1)
def forward(self, x):
""" Propagate data through the network
......@@ -59,14 +58,14 @@ class DeepPixBiS(nn.Module):
"""
enc = self.enc(x)
dec=self.dec(enc)
dec = self.dec(enc)
dec=nn.Sigmoid()(dec)
dec = nn.Sigmoid()(dec)
dec_flat=dec.view(-1,14*14)
dec_flat = dec.view(-1, 14 * 14)
op=self.linear(dec_flat)
op = self.linear(dec_flat)
op=nn.Sigmoid()(op)
op = nn.Sigmoid()(op)
return dec,op
return dec, op
......@@ -5,18 +5,31 @@ import torch.nn as nn
import math
import torch
import logging
from bob.learn.pytorch.architectures.utils import BasicBlock, Bottleneck, conv3x3, _make_layer
from bob.learn.pytorch.architectures.utils import (
BasicBlock,
Bottleneck,
conv3x3,
_make_layer,
)
logger = logging.getLogger("bob.learn.pytorch")
__all__ = ['DltResNet', 'dltresnet18', 'dltresnet34', 'dltresnet50', 'dltresnet101',
'dltresnet152']
__all__ = [
"DltResNet",
"dltresnet18",
"dltresnet34",
"dltresnet50",
"dltresnet101",
"dltresnet152",
]
class DltResNet(nn.Module):
""" DltResNet architecture for training the audio embedding extractor"""
def __init__(self, block, layers, num_classes=1000, tp='cls', bn_dim=128, bn_dim2=128):
def __init__(
self, block, layers, num_classes=1000, tp="cls", bn_dim=128, bn_dim2=128
):
""" Initialization function
Parameters
......@@ -38,27 +51,50 @@ class DltResNet(nn.Module):
self.net_type = tp
self.inplanes = 64
super(DltResNet, self).__init__()
self.conv1 = nn.Conv2d(1, 16, kernel_size=5, stride=2, padding=2,
bias=False)
self.conv1 = nn.Conv2d(1, 16, kernel_size=5, stride=2, padding=2, bias=False)
self.bn1 = nn.BatchNorm2d(16)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(16, 32, kernel_size=(5,1), stride=(1,1), padding=(4,0),
bias=False, dilation=(2,1))
self.conv2 = nn.Conv2d(
16,
32,
kernel_size=(5, 1),
stride=(1, 1),
padding=(4, 0),
bias=False,
dilation=(2, 1),
)
self.bn2 = nn.BatchNorm2d(32)
self.maxpool = nn.MaxPool2d(kernel_size=(3, 1), stride=(2, 1), padding=0)
self.conv3 = nn.Conv2d(32, 64, kernel_size=(3,1), stride=(1,1), padding=(3,0),
bias=False, dilation=(3,1))
self.conv3 = nn.Conv2d(
32,
64,
kernel_size=(3, 1),
stride=(1, 1),
padding=(3, 0),
bias=False,
dilation=(3, 1),
)
self.bn3 = nn.BatchNorm2d(64)
self.layer1, self.inplanes = _make_layer(block, 64, layers[0], stride=2, inplanes=self.inplanes)
self.layer2, self.inplanes = _make_layer(block, 128, layers[1], stride=2, inplanes=self.inplanes)
self.layer3, self.inplanes = _make_layer(block, 256, layers[2], stride=2, inplanes=self.inplanes)
self.layer4, self.inplanes = _make_layer(block, 512, layers[3], stride=2, inplanes=self.inplanes)
self.conv4 = nn.Conv2d(256, 256, kernel_size=(1, 9), stride=1, padding=0,
bias=False)
self.layer1, self.inplanes = _make_layer(
block, 64, layers[0], stride=2, inplanes=self.inplanes
)
self.layer2, self.inplanes = _make_layer(
block, 128, layers[1], stride=2, inplanes=self.inplanes
)
self.layer3, self.inplanes = _make_layer(
block, 256, layers[2], stride=2, inplanes=self.inplanes
)
self.layer4, self.inplanes = _make_layer(
block, 512, layers[3], stride=2, inplanes=self.inplanes
)
self.conv4 = nn.Conv2d(
256, 256, kernel_size=(1, 9), stride=1, padding=0, bias=False
)
self.bn4 = nn.BatchNorm2d(256)
self.conv5 = nn.Conv2d(256, 512, kernel_size=(1, 9), stride=1, padding=0,
bias=False)
self.conv5 = nn.Conv2d(
256, 512, kernel_size=(1, 9), stride=1, padding=0, bias=False
)
self.bn5 = nn.BatchNorm2d(512)
self.fc = nn.Linear(1024, num_classes)
self.fc1 = nn.Linear(bn_dim, num_classes)
......@@ -71,13 +107,11 @@ class DltResNet(nn.Module):
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))
m.weight.data.normal_(0, math.sqrt(2.0 / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def forward(self, x):
""" Derived forward function implementation of :py:class:`torch.nn.Module` class
......@@ -91,17 +125,17 @@ class DltResNet(nn.Module):
py:class:`torch.Tensor`
"""
if self.net_type == 'prefc':
if self.net_type == "prefc":
return self.forward_cnn(x)
if self.net_type == 'cls': # without segment layer
if self.net_type == "cls": # without segment layer
return self.forward_cls(x)
if self.net_type == 'cls2': # with one segment layer
if self.net_type == "cls2": # with one segment layer
return self.forward_cls2(x)
if self.net_type == 'cls3': # with two segment layers
if self.net_type == "cls3": # with two segment layers
return self.forward_cls3(x)
if self.net_type == 'emb': # embedding from first segment layer
if self.net_type == "emb": # embedding from first segment layer
return self.forward_emb(x)
if self.net_type == 'emb2': # embedding from second segment layer
if self.net_type == "emb2": # embedding from second segment layer
return self.forward_emb2(x)
def forward_cnn(self, x):
......@@ -303,56 +337,91 @@ class DltResNet(nn.Module):
return x
def dltresnet18(num_classes=1251, tp='cls', bn_dim=128, bn_dim2=128):
def dltresnet18(num_classes=1251, tp="cls", bn_dim=128, bn_dim2=128):
""" Constructs a DltResNet-18 model.
Parameters
----------
"""
model = DltResNet(BasicBlock, [2, 2, 2, 2], num_classes=num_classes, tp=tp, bn_dim=bn_dim, bn_dim2=bn_dim2)
model = DltResNet(
BasicBlock,
[2, 2, 2, 2],
num_classes=num_classes,
tp=tp,
bn_dim=bn_dim,
bn_dim2=bn_dim2,
)
return model
def dltresnet34(num_classes=1251, tp='cls', bn_dim=128, bn_dim2=128):
def dltresnet34(num_classes=1251, tp="cls", bn_dim=128, bn_dim2=128):
""" Constructs a DltResNet-34 model.
Parameters
----------
"""
model = DltResNet(BasicBlock, [3, 4, 6, 3], num_classes=num_classes, tp=tp, bn_dim=bn_dim, bn_dim2=bn_dim2)
model = DltResNet(
BasicBlock,
[3, 4, 6, 3],
num_classes=num_classes,
tp=tp,
bn_dim=bn_dim,
bn_dim2=bn_dim2,
)
return model
def dltresnet50(num_classes=1251, tp='cls', bn_dim=128, bn_dim2=128):
def dltresnet50(num_classes=1251, tp="cls", bn_dim=128, bn_dim2=128):
""" Constructs a DltResNet-50 model.
Parameters
----------
"""
model = DltResNet(Bottleneck, [3, 4, 14, 3], num_classes=num_classes, tp=tp, bn_dim=bn_dim, bn_dim2=bn_dim2)
model = DltResNet(
Bottleneck,
[3, 4, 14, 3],
num_classes=num_classes,
tp=tp,
bn_dim=bn_dim,
bn_dim2=bn_dim2,
)
return model
def dltresnet101(num_classes=1251, tp='cls', bn_dim=128, bn_dim2=128):
def dltresnet101(num_classes=1251, tp="cls", bn_dim=128, bn_dim2=128):
""" Constructs a DltResNet-101 model.
Parameters
----------
"""
model = DltResNet(Bottleneck, [3, 4, 23, 3], num_classes=num_classes, tp=tp, bn_dim=bn_dim, bn_dim2=bn_dim2)
model = DltResNet(
Bottleneck,
[3, 4, 23, 3],
num_classes=num_classes,
tp=tp,
bn_dim=bn_dim,
bn_dim2=bn_dim2,
)
return model
def dltresnet152(num_classes=1251, tp='cls', bn_dim=128, bn_dim2=128):
def dltresnet152(num_classes=1251, tp="cls", bn_dim=128, bn_dim2=128):
""" Constructs a DltResNet-152 model.
Parameters
----------
"""
model = DltResNet(Bottleneck, [3, 8, 36, 3], num_classes=num_classes, tp=tp, bn_dim=bn_dim, bn_dim2=bn_dim2)
model = DltResNet(
Bottleneck,
[3, 8, 36, 3],
num_classes=num_classes,
tp=tp,
bn_dim=bn_dim,
bn_dim2=bn_dim2,
)
return model
......@@ -47,16 +47,15 @@ class FASNet(nn.Module):
self.enc = nn.Sequential(*features)
self.linear1=nn.Linear(25088,256)
self.linear1 = nn.Linear(25088, 256)
self.relu=nn.ReLU()
self.relu = nn.ReLU()
self.dropout= nn.Dropout(p=0.5)
self.dropout = nn.Dropout(p=0.5)
self.linear2=nn.Linear(256,1)
self.sigmoid= nn.Sigmoid()
self.linear2 = nn.Linear(256, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
""" Propagate data through the network
......@@ -75,16 +74,16 @@ class FASNet(nn.Module):
enc = self.enc(x)
x=enc.view(-1,25088)
x = enc.view(-1, 25088)
x=self.linear1(x)
x = self.linear1(x)
x=self.relu(x)
x = self.relu(x)
x=self.dropout(x)
x = self.dropout(x)
x=self.linear2(x)
x = self.linear2(x)
x=self.sigmoid(x)
x = self.sigmoid(x)
return x
......@@ -9,6 +9,7 @@ from .utils import MaxFeatureMap
from .utils import group
from .utils import resblock
class LightCNN9(nn.Module):
""" The class defining the light CNN with 9 layers
......@@ -50,10 +51,9 @@ class LightCNN9(nn.Module):
group(128, 128, 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True),
)
self.fc1 = MaxFeatureMap(8*8*128, 256, type=0)
self.fc1 = MaxFeatureMap(8 * 8 * 128, 256, type=0)
self.fc2 = nn.Linear(256, num_classes)
def forward(self, x):
""" Propagate data through the network
......@@ -88,6 +88,7 @@ class LightCNN29(nn.Module):
----------
"""
def __init__(self, block=resblock, layers=[1, 2, 3, 4], num_classes=79077):
""" Init function
......@@ -112,7 +113,7 @@ class LightCNN29(nn.Module):
self.block4 = self._make_layer(block, layers[3], 128, 128)
self.group4 = group(128, 128, 3, 1, 1)