Commit f2fd52e4 authored by Guillaume HEUSCH's avatar Guillaume HEUSCH
Browse files

[architectures] started implementation of LightCNN 9-layers

parent 07aa13df
Pipeline #26302 passed with stage
in 7 minutes and 37 seconds
#!/usr/bin/env python
# encoding: utf-8
from .utils import MaxFeatureMap
class LightCNN9(nn.Module):
""" The class defining the light CNN with 9 layers
This class implements the CNN described in:
"Learning Face Representation From Scratch", D. Yi, Z. Lei, S. Liao and S.z. Li, 2014
Attributes
----------
features: :py:class:`torch.nn.Module`
The output of the convolutional / max layers
avgpool: :py:class:`torch.nn.Module`
The output of the average pooling layer (used as embedding)
classifier: :py:class:`torch.nn.Module`
The output of the last linear (logits)
"""
def __init__(self, num_classes=79077):
""" Init function
Parameters
----------
num_classes: int
The number of classes.
"""
super(LightCNN9, self).__init__()
self.features = nn.Sequential(
MaxFeatureMap(1, 48, 5, 1, 2),
nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True),
group(48, 96, 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True),
group(96, 192, 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True),
group(192, 128, 3, 1, 1),
group(128, 128, 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True),
)
self.fc1 = mfm(8*8*128, 256, type=0)
self.fc2 = nn.Linear(256, num_classes)
def forward(self, x):
""" Propagate data through the network
Parameters
----------
x: :py:class:`torch.Tensor`
The data to forward through the network
Returns
-------
out: :py:class:`torch.Tensor`
x: :py:class:`torch.Tensor`
"""
x = self.features(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
x = F.dropout(x, training=self.training)
out = self.fc2(x)
return out, x
......@@ -51,3 +51,62 @@ def weights_init(m):
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
class MaxFeatureMap(nn.Module):
""" Class defining the max feature map
Attributes
----------
out_channels: int
the number of output channels ?
filter: either :py:class:`torch.nn.Conv2D` or :py:class:`torch.nn.Linear`
"""
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, type=1):
""" Init function
Parameters
----------
in_channels: int
the number of input channels
out_channels: int
the number of output channels
kernel_size: int
The size of the kernel in the convolution
stride: int
The stride in the convolution
padding: int
The padding (default to)
type: int
??
"""
super(MaxFeatureMap, self).__init__()
self.out_channels = out_channels
if type == 1:
self.filter = nn.Conv2d(in_channels, 2*out_channels, kernel_size=kernel_size, stride=stride, padding=padding)
else:
self.filter = nn.Linear(in_channels, 2*out_channels)
def forward(self, x):
""" Forward function
Propagates data through the Max Feature Map
Parameters
----------
x: :py:class:`torch.Tensor`
The data to forward through the MFM
Returns
-------
py:class:`torch.Tensor`
"""
x = self.filter(x)
out = torch.split(x, self.out_channels, 1)
return torch.max(out[0], out[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