1 课程学习


2 作业题目

题目描述

必做题:
(1)  把模型改为resnet18,加载相应的模型权重(Lesson2的物料包中有),跑一下0.jpg和1.jpg,看一下输出结果。官方torchvision训练mobilenet和训练resnet的方式是一样的,所以数据预处理和数据后处理部分完全相同。
(2)  自己找2张其他图,用resnet18做下推理。
思考题:
(1)    以ResNet18为例,用time模块和for循环,对”./images/0.jpg”连续推理100次,统计时间开销,比如:
model_classify=ModelPipline()
 
import time
image=cv2.imread("./images/0.jpg")
t_all=0
for i in range(100):
    t_start=time.time()
    result=model_classify.predict(image)
    t_end=time.time()
    t_all+=t_end-t_start
print(t_all)
 
有CUDA的同学,改下代码:self.device=torch.device('cuda')。用上述相同方法测试时间开销。
(2)    在数据预处理和数据后处理的代码实现中,到处在用numpy, opencv, torch 对数组做相应变换,大家至少要把课程中出现的函数们给理解。

实现及结果

使用resnet18实现推理

import torch
import torchvision.models as models
import numpy as np
import cv2


class ModelPipline(object):
    def __init__(self):
        # 进入模型的图片大小:为数据预处理和后处理做准备
        self.inputs_size = (224, 224)
        # CPU or CUDA:为数据预处理和模型加载做准备
        self.device = torch.device('cpu')
        # 载入模型结构和模型权重
        self.model = self.get_model()
        # 载入标签,为数据后处理做准备
        label_names = open('./labels/imagenet_label.txt', 'r').readlines()
        self.label_names = [line.strip('\n') for line in label_names]

    def predict(self, image):
        # 数据预处理
        inputs = self.preprocess(image)
        # 数据进网络
        outputs = self.model(inputs)
        # 数据后处理
        results = self.postprocess(outputs)
        return results

    def get_model(self):
        # 上一节课的内容
        model = models.resnet18(num_classes=1000)
        pretrained_state_dict = torch.load('./weights/resnet18-5c106cde.pth',
                                           map_location=lambda storage, loc: storage)
        model.load_state_dict(pretrained_state_dict, strict=True)
        model.to(self.device)
        model.eval()
        return model

    def preprocess(self, image):
        # opencv默认读入是BGR,需要转为RGB,和训练时保持一致
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        # resize成模型输入的大小,和训练时保持一致
        image = cv2.resize(image, dsize=self.inputs_size)
        # 归一化和标准化,和训练时保持一致
        inputs = image / 255
        inputs = (inputs - np.array([0.485, 0.456, 0.406])) / np.array([0.229, 0.224, 0.225])
        ##以下是图像任务的通用处理
        # (H,W,C) ——> (C,H,W)
        inputs = inputs.transpose(2, 0, 1)
        # (C,H,W) ——> (1,C,H,W)
        inputs = inputs[np.newaxis, :, :, :]
        # NumpyArray ——> Tensor
        inputs = torch.from_numpy(inputs)
        # dtype float32
        inputs = inputs.type(torch.float32)
        # 与self.model放在相同硬件上
        inputs = inputs.to(self.device)
        return inputs

    def postprocess(self, outputs):
        # 取softmax得到每个类别的置信度
        outputs = torch.softmax(outputs, dim=1)
        # 取最高置信度的类别和分数
        score, label_id = torch.max(outputs, dim=1)
        # Tensor ——> float
        score, label_id = score.item(), label_id.item()
        # 查找标签名称
        label_name = self.label_names[label_id]
        return label_name, score


if __name__ == '__main__':
    model_classify = ModelPipline()

    image = cv2.imread('./images/0.jpg')
    result = model_classify.predict(image)
    print(result)

    image = cv2.imread('./images/1.jpg')
    result = model_classify.predict(image)
    print(result)

    image = cv2.imread('./images/2.jpg')
    result = model_classify.predict(image)
    print(result)

    image = cv2.imread('./images/3.jpg')
    result = model_classify.predict(image)
    print(result)

待推理的图像

pytorch多任务loss pytorch 多任务_pytorch多任务loss


推理结果

pytorch多任务loss pytorch 多任务_CUDA_02

CUDA和CPU的推理速度比较

这里比较了使用CUDA和CPU在推理速度上的差异,在代码上,主要改两处:
1 ModelPipline类的__init__属性

# 使用CPU
self.device = torch.device('cpu')
# 使用GPU/CUDA
self.device = torch.device('cuda:0')

2 主函数(这里为了更加直观地反映二者的差别,修改为1000次

if __name__ == '__main__':
    model_classify = ModelPipline()

    print(model_classify.device)

    image = cv2.imread('./images/0.jpg')
    t_all = 0
    for i in range(1000):
        t_start = time.time()
        result = model_classify.predict(image)
        t_end = time.time()
        t_all += t_end - t_start
    print(str(t_all) + 's')

这里进行了多次时间测试,并取平均值,结果如下
CPU的平均时间:11.377415478229523s
CUDA的平均时间:21.11991935968399s

可以看到,CUDA还是明显能够加速推理的。

CUDA多次推理时间:
12.196331024169922s
11.439291715621948s
11.822381734848022s
10.051657438278198s
CPU多次推理时间:
21.368716955184937s
21.317963123321533s
20.67442798614502s
21.118569374084473s