# from torch import nn as nn # # # class EmotionModel(nn.Module): # def __init__(self, in_channels=1, num_classes=7): # super(EmotionModel, self).__init__() # self.conv1 = nn.Conv2d( # in_channels=in_channels, out_channels=256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1) # ) # self.relu1 = nn.ReLU() # self.pool1 = nn.MaxPool2d(kernel_size=(3, 3), stride=(2, 2), padding=(1, 1)) # self.drop1 = nn.Dropout2d(0.4) # # self.conv2 = nn.Conv2d(in_channels=256, out_channels=512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1)) # self.relu2 = nn.ReLU() # self.pool2 = nn.MaxPool2d(kernel_size=(3, 3), stride=(2, 2), padding=(1, 1)) # self.drop2 = nn.Dropout2d(0.4) # # self.conv3 = nn.Conv2d(in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1)) # self.relu3 = nn.ReLU() # self.pool3 = nn.MaxPool2d(kernel_size=(3, 3), stride=(2, 2), padding=(1, 1)) # self.drop3 = nn.Dropout2d(0.4) # # self.conv4 = nn.Conv2d( # in_channels=512, out_channels=512 * 4 * 4, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1) # ) # self.relu4 = nn.ReLU() # self.pool4 = nn.MaxPool2d(kernel_size=(3, 3), stride=(2, 2), padding=(1, 1)) # self.drop4 = nn.Dropout2d(0.4) # # self.fc1 = nn.Linear(in_features=512 * 4 * 4, out_features=512) # self.relu5 = nn.ReLU() # self.drop5 = nn.Dropout(0.3) # self.fc2 = nn.Linear(in_features=512, out_features=256) # self.relu6 = nn.ReLU() # self.drop6 = nn.Dropout(0.3) # self.fc3 = nn.Linear(in_features=256, out_features=num_classes) # self.softmax = nn.Softmax(dim=1) # # def forward(self, x): # x = self.conv1(x) # x = self.relu1(x) # x = self.pool1(x) # x = self.drop1(x) # x = self.conv2(x) # x = self.relu2(x) # x = self.pool2(x) # x = self.drop2(x) # x = self.conv3(x) # x = self.relu3(x) # x = self.pool3(x) # x = self.drop3(x) # x = self.conv4(x) # x = self.relu4(x) # x = self.pool4(x) # x = self.drop4(x) # x = x.view(-1, 512 * 4 * 4) # x = self.fc1(x) # x = self.relu5(x) # x = self.drop5(x) # x = self.fc2(x) # x = self.relu6(x) # x = self.drop6(x) # x = self.fc3(x) # x = self.softmax(x) # return x import torch.nn as nn class EmotionModel(nn.Module): def __init__(self, in_channels=1, num_classes=7): super(EmotionModel, self).__init__() self.conv1 = nn.Conv2d(in_channels, 256, kernel_size=3, stride=2, padding=1) self.bn1 = nn.BatchNorm2d(256) self.relu1 = nn.ReLU() self.pool1 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.drop1 = nn.Dropout2d(0.4) self.conv2 = nn.Conv2d(256, 512, kernel_size=3, stride=2, padding=1) self.bn2 = nn.BatchNorm2d(512) self.relu2 = nn.ReLU() self.pool2 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.drop2 = nn.Dropout2d(0.4) self.conv3 = nn.Conv2d(512, 512, kernel_size=3, stride=2, padding=1) self.bn3 = nn.BatchNorm2d(512) self.relu3 = nn.ReLU() self.pool3 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.drop3 = nn.Dropout2d(0.4) self.conv4 = nn.Conv2d(512, 512 * 4 * 4, kernel_size=3, stride=2, padding=1) self.bn4 = nn.BatchNorm2d(512 * 4 * 4) self.relu4 = nn.ReLU() self.pool4 = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.drop4 = nn.Dropout2d(0.4) self.fc1 = nn.Linear(512 * 4 * 4, 512) self.bn_fc1 = nn.BatchNorm1d(512) self.relu5 = nn.ReLU() self.drop5 = nn.Dropout(0.3) self.fc2 = nn.Linear(512, 256) self.bn_fc2 = nn.BatchNorm1d(256) self.relu6 = nn.ReLU() self.drop6 = nn.Dropout(0.3) self.fc3 = nn.Linear(256, num_classes) self.softmax = nn.Softmax(dim=1) def forward(self, x): x = self.conv1(x) x = self.bn1(x) x = self.relu1(x) x = self.pool1(x) x = self.drop1(x) x = self.conv2(x) x = self.bn2(x) x = self.relu2(x) x = self.pool2(x) x = self.drop2(x) x = self.conv3(x) x = self.bn3(x) x = self.relu3(x) x = self.pool3(x) x = self.drop3(x) x = self.conv4(x) x = self.bn4(x) x = self.relu4(x) x = self.pool4(x) x = self.drop4(x) x = x.view(-1, 512 * 4 * 4) x = self.fc1(x) x = self.bn_fc1(x) x = self.relu5(x) x = self.drop5(x) x = self.fc2(x) x = self.bn_fc2(x) x = self.relu6(x) x = self.drop6(x) x = self.fc3(x) x = self.softmax(x) return x