如何将我的 fastai resnet50/vision_learner 训练模型导出到 torchserve 中?

问题描述 投票:0回答:1

我的目标是将我用 Fastai 训练的模型部署到 Torchserve 中。我正在关注本教程,但卡在了他为 pytorch 创建模型类的部分。

他提到要在 Torchserve 中运行我们的模型,我们需要以下内容:

  1. 模范班
  2. 从pytorch导出的权重(pth文件)
  3. 处理程序

其中,我得到两个:重物和处理程序。然而,我陷入困境的是模型类。他创建了一个类文件,但我不知道他从哪里获得 DynamicUnet 作为该类的基础,也不知道他如何将该类与 unet_learner 混合以创建自定义 pytorch 模型类。你能帮我为在学习器vision_learner下训练的模型和resnet50的预训练模型建立一个模型类吗?

python machine-learning pytorch fast-ai torchserve
1个回答
0
投票

我自己找到了修复方法。事实证明,您可以进入 ??create_vision_model 然后进入 ??add_head 并将它们作为模型类放入 handler.py 内的“initialize”函数中;你应该得到这样的结果:

state_dict = torch.load(model_pt_path, map_location=self.device)
head = None
concat_pool = True
pool = True
lin_ftrs = None
ps = 0.5
first_bn = True
bn_final = False
lin_first = False
y_range = None
init = nn.init.kaiming_normal_
arch = resnet50
n_out = 9
pretrained = True
cut = None
n_in = 3
custom_head = None
# self.model = MyVisionModel()
meta = model_meta.get(arch, _default_meta)
model = arch(pretrained=pretrained)
body = create_body(model, n_in, pretrained, ifnone(cut, meta['cut']))
nf = num_features_model(nn.Sequential(*body.children())) if custom_head is None else None
if head is None:
    head = create_head(nf, n_out, concat_pool=concat_pool, pool=pool,
                       lin_ftrs=lin_ftrs, ps=ps, first_bn=first_bn, bn_final=bn_final, lin_first=lin_first,
                       y_range=y_range)
self.model = nn.Sequential(body, head)
self.model.load_state_dict(state_dict)
self.model.to(self.device)
self.model.eval()

logger.debug("Model file {0} loaded successfully".format(model_pt_path))
self.initialized = True
© www.soinside.com 2019 - 2024. All rights reserved.