File size: 4,982 Bytes
2972f68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# 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