Commit ee78d11d authored by Guillaume HEUSCH's avatar Guillaume HEUSCH

[test] fixed the download of pretrained FASNet (saves CI time)

parent 3b4888d0
Pipeline #28509 passed with stage
in 29 minutes and 42 seconds
......@@ -15,5 +15,3 @@ log*
results*
build/
record.txt
*lightcnn*
bob/learn/pytorch/preprocessor/audio/data
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
@author: Olegs Nikisins
"""
#==============================================================================
# Import here:
from torch import nn
import torch.nn.functional as F
#==============================================================================
# Define the network:
class TwoLayerMLP(nn.Module):
"""
A simple two-layer MLP for binary classification. The output activation
function is sigmoid.
Attributes
----------
in_features : int
Dimensionality of the input feature vectors.
n_hidden_relu : int
Number of ReLU units in the hidden layer of the MLP.
apply_sigmoid : bool
If set to ``True`` the sigmoid will be applied to the output of the
hidden FC layer. If ``False`` the sigmoid is not applied.
"""
def __init__(self, in_features, n_hidden_relu, apply_sigmoid = True):
super(TwoLayerMLP, self).__init__()
"""
Init method.
Parameters
----------
in_features : int
Dimensionality of the input feature vectors.
n_hidden_relu : int
Number of ReLU units in the hidden layer of the MLP.
apply_sigmoid : bool
If set to ``True`` the sigmoid will be applied to the output of the
hidden FC layer. If ``False`` the sigmoid is not applied.
Default: ``True``.
"""
self.in_features = in_features
self.n_hidden_relu = n_hidden_relu
self.apply_sigmoid = apply_sigmoid
self.fc1 = nn.Linear(in_features = self.in_features, out_features = self.n_hidden_relu, bias=True)
self.fc2 = nn.Linear(in_features = self.n_hidden_relu, out_features = 1, bias=True)
def forward(self, x):
"""
The forward method.
Parameters
----------
x : :py:class:`torch.Tensor`
The batch to forward through the network. Size of the input batch
is [batch_size, 1, self.in_features].
Returns
-------
x : :py:class:`torch.Tensor`
Output of the MLP, class probability.
"""
# input is a batch of the size: [batch_size, 1, self.in_features],
# convert it to the size [batch_size, self.in_features] as expected by FC layer:
x = x.squeeze()
# first fully connected activated by ReLu:
x = self.fc1(x)
x = F.relu(x)
# second fully connected activated by sigmoid:
x = self.fc2(x)
if not self.apply_sigmoid:
return x
x = F.sigmoid(x)
return x
......@@ -16,7 +16,6 @@ from .ConditionalGAN import ConditionalGAN_generator
from .ConditionalGAN import ConditionalGAN_discriminator
from .ConvAutoencoder import ConvAutoencoder
from .TwoLayerMLP import TwoLayerMLP
from .utils import weights_init
......
This diff is collapsed.
......@@ -96,7 +96,7 @@ def test_architectures():
a = numpy.random.rand(1, 3, 224, 224).astype("float32")
t = torch.from_numpy(a)
from ..architectures import DeepPixBiS
net = DeepPixBiS(pretrained=True)
net = DeepPixBiS(pretrained=False)
output = net.forward(t)
assert output[0].shape == torch.Size([1, 1, 14, 14])
assert output[1].shape == torch.Size([1, 1])
......@@ -291,7 +291,7 @@ class DummyDataSetFASNet(Dataset):
def test_FASNettrainer():
from ..architectures import FASNet
net = FASNet()
net = FASNet(pretrained=False)
dataloader={}
dataloader['train'] = torch.utils.data.DataLoader(DummyDataSetFASNet(), batch_size=32, shuffle=True)
......@@ -309,7 +309,7 @@ def test_FASNettrainer():
def test_FASNettrainer_cv():
from ..architectures import FASNet
net = FASNet()
net = FASNet(pretrained=False)
dataloader={}
dataloader['train'] = torch.utils.data.DataLoader(DummyDataSetFASNet(), batch_size=32, shuffle=True)
......@@ -455,24 +455,3 @@ def test_extractors():
data = numpy.random.rand(3, 224, 224).astype("uint8")
output = extractor(data)
assert output.shape[0] == 1
def test_two_layer_mlp():
"""
Test the TwoLayerMLP class.
"""
from bob.learn.pytorch.architectures import TwoLayerMLP
batch = torch.randn(10, 1, 100)
model = TwoLayerMLP(in_features = 100,
n_hidden_relu = 10,
apply_sigmoid = True)
output = model(batch)
assert list(output.shape) == [10, 1]
model.apply_sigmoid = False
output = model(batch)
assert list(output.shape) == [10, 1]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment