gpt4 book ai didi

python - 如何将 pytorch 模型保留在 redis 缓存中以更快地访问模型以进行视频流?

转载 作者:行者123 更新时间:2023-12-03 06:36:24 25 4
gpt4 key购买 nike

我有这个属于 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 中)并为每个请求调用它?

谢谢大家。

最佳答案

如果您只需要在 RAM 上保持模型状态,则不需要 Redis。您可以将 RAM 挂载为虚拟磁盘并将模型状态存储在那里。退房 tmpfs .

关于python - 如何将 pytorch 模型保留在 redis 缓存中以更快地访问模型以进行视频流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61708442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com