How to save and load model_final.pth detectron2

I saved model_final.pth on my drive then I wrote this piece of code but it does not work. could somone check it ?

from detectron2.modeling import build_model
cfg = get_cfg()
model = build_model(cfg)
from detectron2.checkpoint import DetectionCheckpointer
DetectionCheckpointer(model).load("/content/gdrive/My Drive/model_final.pth")
checkpointer = DetectionCheckpointer(model, save_dir=“output”)“my_model”)

Inference :

cfg = get_cfg()
cfg.MODEL.WEIGHTS = “/content/output/my_model.pth”
cfg.DATASETS.TEST = (“my_dataset_test”, )
predictor = DefaultPredictor(cfg)
test_metadata = MetadataCatalog.get(“my_dataset_test”)

from detectron2.utils.visualizer import ColorMode
import glob

for imageName in glob.glob("/content/my_dataset/test/*jpg"):
im = cv2.imread(imageName)
outputs = predictor(im)
v = Visualizer(im[:, :, ::-1],
out = v.draw_instance_predictions(outputs[“instances”].to(“cpu”))
print(“pred_classes : \n”,outputs[“instances”].pred_classes)
print(“pred_boxes : \n”,outputs[“instances”].pred_boxes)
cv2_imshow(out.get_image()[:, :, ::-1])

What is not working in the code?
Could you post the error message?

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier. :wink:

While loading the model during inference it needs to be built by setting the eval_only parameter.

model = MyTrainer.build_model(cfg, eval_only=True)