【问题标题】:how to keep pytorch model in redis cache to access model faster for video streaming?如何将 pytorch 模型保存在 redis 缓存中以更快地访问模型以进行视频流传输?
【发布时间】:2020-08-25 17:44:22
【问题描述】:

我有这个属于feature_extractor.py 的代码,它是here 中这个文件夹的一部分:

import torch
import torchvision.transforms as transforms
import numpy as np
import cv2
from .model import Net

class Extractor(object):
    def __init__(self, model_path, use_cuda=True):
        self.net = Net(reid=True)
        self.device = "cuda" if torch.cuda.is_available() and use_cuda else "cpu"
        state_dict = torch.load(model_path, map_location=lambda storage, loc: storage)['net_dict']
        self.net.load_state_dict(state_dict)
        print("Loading weights from {}... Done!".format(model_path))
        self.net.to(self.device)
        self.size = (64, 128)
        self.norm = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
        ])

    def _preprocess(self, im_crops):
        def _resize(im, size):
            return cv2.resize(im.astype(np.float32) / 255., size)

        im_batch = torch.cat([self.norm(_resize(im, self.size)).unsqueeze(0) for im in im_crops], dim=0).float()
        return im_batch

    def __call__(self, im_crops):
        im_batch = self._preprocess(im_crops)
        with torch.no_grad():
            im_batch = im_batch.to(self.device)
            features = self.net(im_batch)
        return features.cpu().numpy()


if __name__ == '__main__':
    img = cv2.imread("demo.jpg")[:, :, (2, 1, 0)]
    extr = Extractor("checkpoint/ckpt.t7")
    feature = extr(img)
    print(feature.shape)

现在假设有 200 个请求连续进行。为每个请求加载模型的过程使代码运行缓慢。

所以我认为将 pytorch 模型保存在缓存中可能是个好主意。我是这样修改的:

from redis import Redis
import msgpack as msg

r = Redis('111.222.333.444')

class Extractor(object):
    def __init__(self, model_path, use_cuda=True):
        try:
            self.net = msg.unpackb(r.get('REID_CKPT'))
        finally:
            self.net = Net(reid=True)
            self.device = "cuda" if torch.cuda.is_available() and use_cuda else "cpu"
            state_dict = torch.load(model_path, map_location=lambda storage, loc: storage)['net_dict']
            self.net.load_state_dict(state_dict)
            print("Loading weights from {}... Done!".format(model_path))
            self.net.to(self.device)
            packed_net = msg.packb(self.net)
            r.set('REID_CKPT', packed_net)

        self.size = (64, 128)
        self.norm = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
        ])

不幸的是出现了这个错误:

 File "msgpack/_packer.pyx", line 286, in msgpack._cmsgpack.Packer.pack
 File "msgpack/_packer.pyx", line 292, in msgpack._cmsgpack.Packer.pack
 File "msgpack/_packer.pyx", line 289, in msgpack._cmsgpack.Packer.pack
 File "msgpack/_packer.pyx", line 283, in msgpack._cmsgpack.Packer._pack
 TypeError: can not serialize 'Net' object

原因显然是因为它无法将 Net 对象(pytorch nn.Module 类)转换为字节。

如何有效地将 pytorch 模型保存在缓存中(或以某种方式将其保存在 RAM 中)并为每个请求调用它?

谢谢大家。

【问题讨论】:

  • 你检查Redis模块RedisAI了吗?
  • 不,我没有。不知道怎么用。

标签: python caching redis video-streaming pytorch


【解决方案1】:

如果您只需要在 RAM 上保存模型状态,则不需要 Redis。您可以改为将 RAM 挂载为虚拟磁盘并在其中存储模型状态。查看tmpfs

【讨论】:

    猜你喜欢
    • 2020-10-17
    • 2021-07-15
    • 2021-06-23
    • 2022-10-09
    • 2021-06-26
    • 1970-01-01
    • 2021-09-10
    • 2020-04-04
    • 2020-11-15
    相关资源
    最近更新 更多