I am creating a small interface to serve a mmdetection model. Everything runs locally.
My model is something like this:
from mmdet.apis import init_detector, inference_detector
class Model:
def __init__(
self,
config_path: str,
checkpoint_path: str,
):
self.cfg = Config.fromfile(config_path)
self.checkpoint_path = checkpoint_path
self.load()
def load(self):
self.model = init_detector(self.cfg, self.checkpoint_path, device="cpu")
def predict(self, x):
return inference_detector(self.model, x)
This model and other functions get imported into my streamlit app via
from model import *
This works well when running it for the first time on my streamlit app. However, if I edit the code and rerun the app, there is an issue with the mmdet registry, as there is an attempt to store the same name in the registry.
This error initially happened already at the import level. I have fixed this by avoiding the import if Model is already in sys.modules:
import sys
if "Model" not in sys.modules:
import model
globals().update(vars(model))
However, when I rerun streamlit and the Model instance gets recreated, this happens:
AssertionError: scope mmdet exists in runner registry
If I save the model as a session_state, then I get the same AssertionError when running inference_detector.
Any thoughts on how to avoid this?