How to save and load models of custom dataset in Detectron2?
Asked Answered
B

1

5

I have tried to save and load the model using: All keys are mapped but there is no prediction in output #1

from detectron2.modeling import build_model
model = build_model(cfg)
torch.save(model.state_dict(), 'checkpoint.pth') 
model.load_state_dict(torch.load(checkpoint_path,map_location='cpu'))

I also tried doing it using the official doc but can't understand the input format part

from detectron2.checkpoint import DetectionCheckpointer
DetectionCheckpointer(model).load(file_path_or_url)  # load a file, usually from cfg.MODEL.WEIGHTS
checkpointer = DetectionCheckpointer(model, save_dir="output")
checkpointer.save("model_999")  # save to output/model_999.pth
Biped answered 30/7, 2020 at 4:21 Comment(1)
Can you share the errors and more code for us to reproduce the errors?Alena
P
9
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file('COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml'))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # Set threshold for this model
cfg.MODEL.WEIGHTS = '/content/model_final.pth' # Set path model .pth
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1
predictor = DefaultPredictor(cfg)

My code to load custom model works.

Pivoting answered 18/9, 2020 at 3:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.