BentoML
BentoML copied to clipboard
BentoML not loading pytorch object with .model module
Hi, I am trying to save a RetinaNet model with the following code:
net = torch.jit.load(_my_model_path, map_location=torch.device('cpu'))
def getstate():
pass
net.__getstate__ = getstate
def __init__(self):
super().__init__()
net.__init__ = __init__
#those extra init and getstate functions were necessary to get the code working. the bolded text should have two underscores on either side but is not formatting properly
detector = RetinaNetDetector(network=net, anchor_generator=anchor_generator, debug=False)
detector.set_box_selector_parameters(
score_thresh=0.02,
topk_candidates_per_level=1000,
nms_thresh=0.22,
detections_per_img=1,
)
detector.set_sliding_window_inferer(
roi_size=(192,192,96),
overlap=0.25,
sw_batch_size=1,
mode="gaussian",
# device="cpu",
device=device,
)
detector.eval()
bentoml.pytorch.save_model(model_name, detector, signatures={"__call__": {"batchable": False},
"__init__": {"batchable": False},
"model": {"batchable": False}})
detector is of type monai.apps.detection.networks.retinanet_detector.RetinaNetDetector. I am then trying to load the model to a runner in a different file using:
bento_model = bentoml.pytorch.get(model_name)
torch_model_runner = bento_model.to_runner()
svc = bentoml.Service(model_name, runners=[torch_model_runner])
But when I call the runner like this:
prediction = await torch_model_runner.async_run(data)
I get this error:
"{"time": "2023-04-18 17:43:51,259", "name": "bentoml._internal.server.runner_app", "level": "ERROR", "message": "Exception on runner 'smao_230409' method '__call__'\nTraceback (most recent call last):\n File \"/usr/local/lib/python3.8/dist-packages/bentoml/_internal/server/runner_app.py\", line 331, in _run\n ret = await runner_method.async_run(*params.args, **params.kwargs)\n File \"/usr/local/lib/python3.8/dist-packages/bentoml/_internal/runner/runner.py\", line 55, in async_run\n return await self.runner._runner_handle.async_run_method(self, *args, **kwargs)\n File \"/usr/local/lib/python3.8/dist-packages/bentoml/_internal/runner/runner_handle/local.py\", line 59, in async_run_method\n return await anyio.to_thread.run_sync(\n File \"/usr/local/lib/python3.8/dist-packages/anyio/to_thread.py\", line 31, in run_sync\n return await get_asynclib().run_sync_in_worker_thread(\n File \"/usr/local/lib/python3.8/dist-packages/anyio/_backends/_asyncio.py\", line 937, in run_sync_in_worker_thread\n return await future\n File \"/usr/local/lib/python3.8/dist-packages/anyio/_backends/_asyncio.py\", line 867, in run\n result = context.run(func, *args)\n File \"/usr/local/lib/python3.8/dist-packages/bentoml/_internal/runner/runnable.py\", line 139, in method\n return self.func(obj, *args, **kwargs)\n File \"/usr/local/lib/python3.8/dist-packages/bentoml/_internal/frameworks/common/pytorch.py\", line 107, in _run\n return getattr(self.model, method_name)(\n File \"/usr/local/lib/python3.8/dist-packages/bentoml/_internal/runner/runnable.py\", line 139, in method\n return self.func(obj, *args, **kwargs)\n File \"/usr/local/lib/python3.8/dist-packages/bentoml/_internal/frameworks/common/pytorch.py\", line 107, in _run\n return getattr(self.model, method_name)(\nAttributeError: 'function' object has no attribute 'model'"}"
if I print dir(bento_model)
I see that is has an attribute of "_model" rather than "model", but it is equal to None. Am I doing something wrong in saving this model to the store and then loading it into a runner? I don't know how to get the model attribute to propagate all the way to the runner. This is for bentoml==1.0.13 but I could change to whatever version if it helps.
Thanks!