# 轻松学Pytorch-全卷积神经网络实现表情识别

``` 1class ResidualBlock(torch.nn.Module):
2    def __init__(self, in_channels, out_channels, stride=1):
3        """
4        Args:
5          in_channels (int):  Number of input channels.
6          out_channels (int): Number of output channels.
7          stride (int):       Controls the stride.
8        """
9        super(ResidualBlock, self).__init__()
10
11        self.skip = torch.nn.Sequential()
12
13        if stride != 1 or in_channels != out_channels:
14            self.skip = torch.nn.Sequential(
15                torch.nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=stride, bias=False),
16                torch.nn.BatchNorm2d(out_channels))
17
18        self.block = torch.nn.Sequential(
19            torch.nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, padding=1, stride=stride, bias=False),
20            torch.nn.BatchNorm2d(out_channels),
21            torch.nn.ReLU(),
22            torch.nn.Conv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, padding=1, stride=1, bias=False),
23            torch.nn.BatchNorm2d(out_channels))
24
25    def forward(self, x):
26        out = self.block(x)
27        identity = self.skip(x)
28        out += identity
29        out = F.relu(out)
30        return out```

``` 1class EmotionsResNet(torch.nn.Module):
2    def __init__(self):
3        super(EmotionsResNet, self).__init__()
4        self.cnn_layers = torch.nn.Sequential(
5            # 卷积层 (64x64x3的图像)
6            ResidualBlock(3, 32, 1),
7            ResidualBlock(32, 64, 2),
8            ResidualBlock(64, 64, 2),
9            ResidualBlock(64, 128, 2),
10            ResidualBlock(128, 128, 2),
11            ResidualBlock(128, 256, 2),
12            ResidualBlock(256, 256, 2),
13            ResidualBlock(256, 8, 1),
14        )
15
16    def forward(self, x):
17        # stack convolution layers
18        x = self.cnn_layers(x)
19
20        # Nx5x1x1
21        B, C, H, W = x.size()
22        out = x.view(B, -1)
23        return out```

``` 1if __name__ == "__main__":
2    # create a complete CNN
3    model = EmotionsResNet()
4    print(model)
5
6    # 使用GPU
7    if train_on_gpu:
8        model.cuda()
9
10    ds = EmotionDataset("D:/facedb/emotion_dataset")
11    num_train_samples = ds.num_of_samples()
12    bs = 16
14
15    # 训练模型的次数
16    num_epochs = 15
17    # optimizer = torch.optim.SGD(model.parameters(), lr=0.001)
19    model.train()
20
21    # 损失函数
22    mse_loss = torch.nn.MSELoss()
23    cross_loss = torch.nn.CrossEntropyLoss()
24    index = 0
25    for epoch in  range(num_epochs):
26        train_loss = 0.0
27        for i_batch, sample_batched in enumerate(dataloader):
28            images_batch, emotion_batch = \
29                sample_batched['image'], sample_batched['emotion']
30            if train_on_gpu:
31                images_batch, emotion_batch= images_batch.cuda(), emotion_batch.cuda()
33
34            # forward pass: compute predicted outputs by passing inputs to the model
35            m_emotion_out_ = model(images_batch)
36            emotion_batch = emotion_batch.long()
37
38            # calculate the batch loss
39            loss = cross_loss(m_emotion_out_, emotion_batch)
40
41            # backward pass: compute gradient of the loss with respect to model parameters
42            loss.backward()
43
44            # perform a single optimization step (parameter update)
45            optimizer.step()
46
47            # update training loss
48            train_loss += loss.item()
49            if index % 100 == 0:
50                print('step: {} \tTraining Loss: {:.6f} '.format(index, loss.item()))
51            index += 1
52
53            # 计算平均损失
54        train_loss = train_loss / num_train_samples
55
56        # 显示训练集与验证集的损失函数
57        print('Epoch: {} \tTraining Loss: {:.6f} '.format(epoch, train_loss))
58
59    # save model
60    model.eval()
61    torch.save(model, 'face_emotions_model.pt')```

``` 1cnn_model = torch.load("./face_emotions_model.pt")
2print(cnn_model)
3# capture = cv.VideoCapture(0)
4capture = cv.VideoCapture("D:/images/video/example_dsh.mp4")
5
8while True:
10    if ret is not True:
11        break
12    frame = cv.flip(frame, 1)
13    h, w, c = frame.shape
14    blobImage = cv.dnn.blobFromImage(frame, 1.0, (300, 300), (104.0, 177.0, 123.0), False, False);
15    net.setInput(blobImage)
16    cvOut = net.forward()
17    # 绘制检测矩形
18    for detection in cvOut[0,0,:,:]:
19        score = float(detection[2])
20        if score > 0.5:
21            left = detection[3]*w
22            top = detection[4]*h
23            right = detection[5]*w
24            bottom = detection[6]*h
25
26            # roi and detect landmark
27            roi = frame[np.int32(top):np.int32(bottom),np.int32(left):np.int32(right),:]
28            rw = right - left
29            rh = bottom - top
30            img = cv.resize(roi, (64, 64))
31            img = (np.float32(img) / 255.0 - 0.5) / 0.5
32            img = img.transpose((2, 0, 1))
33            x_input = torch.from_numpy(img).view(1, 3, 64, 64)
34            emotion_ = cnn_model(x_input.cuda())
35            predict_ = torch.max(emotion_, 1)[1].cpu().detach().numpy()[0]
36            emotion_txt = emotion_labels[predict_]
37            # 绘制
38            cv.putText(frame, ("%s"%(emotion_txt)), (int(left), int(top)-15), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
39            cv.rectangle(frame, (int(left), int(top)), (int(right), int(bottom)), (255, 0, 0), thickness=2)
40            c = cv.waitKey(10)
41            if c == 27:
42                break
43            cv.imshow("face detection + emotion", frame)
44
45cv.waitKey(0)
46cv.destroyAllWindows()```

0 条评论

• ### 轻松学Pytorch –车辆类型与颜色识别

大家好，上一周没有给大家更新这个系列文章，不是我不想更新，而是很多数据需要我自己准备，做好处理，比如这次的车辆属性数据，基于BITVehicle_Dataset...

• ### 使用OpenCV 4.1.2的DNN模块部署深度学习模型

自3.3版本开始，OpenCV加入了对深度神经网络推理运算的支持模块-DNN模块，它支持多种深度学习框架的模型，如Tensorflow、Caffe、To...

• ### OpenCV中的图形绘制

OpenCV在Core模块中支持多种图形绘制与填充，方便开发者在图像对象识别与检测之后通过特定的图形轮廓加以显式表示。常见的几何形状包括线、矩形、圆形、椭圆，此...

• ### Leetcode打卡 | No.24 两两交换链表中的节点

欢迎和小詹一起定期刷leetcode，每周一和周五更新一题，每一题都吃透，欢迎一题多解，寻找最优解！这个记录帖哪怕只有一个读者，小詹也会坚持刷下去的！

• ### LeetCode 160： 相交链表 Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked ...

• ### LeetCode 160：相交链表 Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked ...

• ### Groovy 使用EqualsAndHashCode注解生成equals和hashcode方法

Groovy 1.8中有很多新的字节码生成注释。 其中一个是@EqualsAndHashCode注释。 使用此注释，为类生成equals()和hashCode(...

• ### 两个Go语言编程小技巧

我们可以使用一个for-range循环遍历一个尺寸为零的数组值来模拟for i in 0..N。比如，下面这个循环将打印出0到9这十个数字。

• ### Black Hat Europe 2017：安全专家发现5款最流行编程语言中的漏洞

没有不漏的锅，如果底层的编程语言如果出现问题，顶层的应用程序还能幸免于难吗？ 这周在 Black Hat Europe 2017 安全会议上，一名安全研究员公开...

• ### 人工智能要当老师了？“机器人”老师的时代已经到来

8月16日下午，由数据猿联合HCR慧辰资讯主办的“AI助力个性化教育—新模式 新探索”主题沙龙在北京隆重举行 记者 | 大文 官网 | www.datayuan...

### OpenCV学堂

OpenCV开发专家计算机视觉开发