Download approach/ResEmoteNet.py from canopus1008/FER_Model: direct link, hf CLI and curl.
- Browser
- Download file 3.24 kB
-
https://huggingface.co/canopus1008/FER_Model/resolve/main/approach/ResEmoteNet.py
- Command line
-
hf download hf://canopus1008/FER_Model/approach/ResEmoteNet.py
-
curl -L -o ResEmoteNet.py https://huggingface.co/canopus1008/FER_Model/resolve/main/approach/ResEmoteNet.py
3.24 kB
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| class SEBlock(nn.Module): | |
| def __init__(self, in_channels, reduction=16): | |
| super(SEBlock, self).__init__() | |
| self.avg_pool = nn.AdaptiveAvgPool2d(1) | |
| self.fc = nn.Sequential( | |
| nn.Linear(in_channels, in_channels // reduction, bias=False), | |
| nn.ReLU(inplace=True), | |
| nn.Linear(in_channels // reduction, in_channels, bias=False), | |
| nn.Sigmoid() | |
| ) | |
| def forward(self, x): | |
| b, c, _, _ = x.size() | |
| y = self.avg_pool(x).view(b, c) | |
| y = self.fc(y).view(b, c, 1, 1) | |
| return x * y.expand_as(x) | |
| class ResidualBlock(nn.Module): | |
| def __init__(self, in_ch, out_ch, stride=1): | |
| super(ResidualBlock, self).__init__() | |
| self.conv1 = nn.Conv2d(in_ch, out_ch, kernel_size=3, stride=stride, padding=1) | |
| self.bn1 = nn.BatchNorm2d(out_ch) | |
| self.conv2 = nn.Conv2d(out_ch, out_ch, kernel_size=3, stride=1, padding=1) | |
| self.bn2 = nn.BatchNorm2d(out_ch) | |
| self.shortcut = nn.Sequential() | |
| if stride != 1 or in_ch != out_ch: | |
| self.shortcut = nn.Sequential( | |
| nn.Conv2d(in_ch, out_ch, kernel_size=1, stride=stride, padding=0), | |
| nn.BatchNorm2d(out_ch) | |
| ) | |
| def forward(self, x): | |
| out = F.relu(self.bn1(self.conv1(x))) | |
| out = self.bn2(self.conv2(out)) | |
| out += self.shortcut(x) | |
| out = F.relu(out) | |
| return out | |
| class ResEmoteNet(nn.Module): | |
| def __init__(self): | |
| super(ResEmoteNet, self).__init__() | |
| self.conv1 = nn.Conv2d(3, 64, kernel_size=3, padding=1) | |
| self.bn1 = nn.BatchNorm2d(64) | |
| self.conv2 = nn.Conv2d(64, 128, kernel_size=3, padding=1) | |
| self.bn2 = nn.BatchNorm2d(128) | |
| self.conv3 = nn.Conv2d(128, 256, kernel_size=3, padding=1) | |
| self.bn3 = nn.BatchNorm2d(256) | |
| self.relu = nn.ReLU(inplace=True) | |
| self.se = SEBlock(256) | |
| self.res_block1 = ResidualBlock(256, 512, stride=2) | |
| self.res_block2 = ResidualBlock(512, 1024, stride=2) | |
| self.res_block3 = ResidualBlock(1024, 2048, stride=2) | |
| self.pool = nn.AdaptiveAvgPool2d((1, 1)) | |
| self.fc1 = nn.Linear(2048, 1024) | |
| self.fc2 = nn.Linear(1024, 512) | |
| self.fc3 = nn.Linear(512, 256) | |
| self.dropout1 = nn.Dropout(0.2) | |
| self.dropout2 = nn.Dropout(0.5) | |
| self.fc4 = nn.Linear(256, 7) | |
| def forward(self, x): | |
| x = F.relu(self.bn1(self.conv1(x))) | |
| x = F.max_pool2d(x, 2) | |
| x = self.dropout1(x) | |
| x = F.relu(self.bn2(self.conv2(x))) | |
| x = F.max_pool2d(x, 2) | |
| x = self.dropout1(x) | |
| x = F.relu(self.bn3(self.conv3(x))) | |
| x = F.max_pool2d(x, 2) | |
| x = self.se(x) | |
| x = self.res_block1(x) | |
| x = self.res_block2(x) | |
| x = self.res_block3(x) | |
| x = self.pool(x) | |
| x = x.view(x.size(0), -1) | |
| x = F.relu(self.fc1(x)) | |
| x = self.dropout2(x) | |
| x = F.relu(self.fc2(x)) | |
| x = self.dropout2(x) | |
| x = F.relu(self.fc3(x)) | |
| x = self.dropout2(x) | |
| x = self.fc4(x) | |
| return x |