自定义网络调用-Handlers

https://github.com/pytorch/serve/blob/v0.2.0/docs/README.md

自定义网络调用-Handlers_第1张图片

1、定义模型

from torchvision.models.resnet import ResNet, BasicBlock
model = ResNet(BasicBlock, [2, 2, 2, 2], 5) # resnet18

2、保存权重

torch.save(model.state_dict(), "model.pth" )

3、转为.pt

from torchvision.models.resnet import ResNet, BasicBlock

model = ResNet(BasicBlock, [2, 2, 2, 2], 5)
# model_name = "resnet18"
state_dict = torch.load("checkpoint/resnet18/resnet18-6-regular.pth")
model.load_state_dict(state_dict)
# model = model.cuda()
# print("model:", model)
# trace_module = torch.jit.trace(model, torch.rand(1, 3, 112, 112))
# print(trace_module.code)  # 查看模型结构
# output = trace_module(torch.ones(1, 3, 112, 112))  # 测试
# print(output)
# trace_module.save('model.pt')  # 模型保存

script_model = torch.jit.script(model)
torch.jit.save(script_model, "model.pt")

4、调用测试

torch-model-archiver --model-name resnet18 --version 1.0 --serialized-file model.pt --handler img_classifier --extra-files examples/image_classifier/index_to_name.json

5、自定义handler

ts->torch_handler

        拷贝image_classifier.py为img_classifer.py。

注册文件:

model_archiver->model_packaging_utils.py

自定义网络调用-Handlers_第2张图片

GitHub - xiahaifeng1995/PaDiM-Anomaly-Detection-Localization-master: This is an unofficial implementation of the paper “PaDiM: a Patch Distribution Modeling Framework for Anomaly Detection and Localization”.

你可能感兴趣的:(未分类,linux,ubuntu,python)