How to load an already trained model to avoid retrain the same model #5317
Unanswered
VictorGimenez
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello there,
I am trying to load an existing model already trained by the method
DefaultTrainer(get_cfg()).train()
to avoid the need to retrain again the same configuration with the dataset. Here is my code to load the .yaml model:`cfg_filename = './config_model_test.yaml'
with open(cfg_filename, 'r') as f:
cfg_dict = yaml.safe_load(f)`
cfg.DATASETS.PRECOMPUTED_PROPOSAL_TOPK_TEST = cfg_dict['DATASETS']['PRECOMPUTED_PROPOSAL_TOPK_TEST']
cfg.DATASETS.PRECOMPUTED_PROPOSAL_TOPK_TRAIN = cfg_dict['DATASETS']['PRECOMPUTED_PROPOSAL_TOPK_TRAIN']
cfg.DATASETS.PROPOSAL_FILES_TEST = cfg_dict['DATASETS']['PROPOSAL_FILES_TEST']
cfg.DATASETS.PROPOSAL_FILES_TRAIN = cfg_dict['DATASETS']['PROPOSAL_FILES_TRAIN']
cfg.DATASETS.TEST = cfg_dict['DATASETS']['TEST']
cfg.DATASETS.TRAIN = cfg_dict['DATASETS']['TRAIN']
cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, 'model_final.pth')
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7
model = build_model(cfg)
model.load_state_dict(torch.load(cfg.MODEL.WEIGHTS, map_location=torch.device('cpu')))
predictor = DefaultPredictor(cfg)
v = VideoVisualizer(metadata, ColorMode.IMAGE)`
But I am getting the following error after the execution above:
RuntimeError: Error(s) in loading state_dict for GeneralizedRCNN: Missing key(s) in state_dict: "backbone.fpn_lateral2.weight", "backbone.fpn_lateral2.bias", "backbone.fpn_output2.weight", "backbone.fpn_output2.bias", "backbone.fpn_lateral3.weight", "backbone.fpn_lateral3.bias", "backbone.fpn_output3.weight", "backbone.fpn_output3.bias", "backbone.fpn_lateral4.weight", "backbone.fpn_lateral4.bias", "backbone.fpn_output4.weight", "backbone.fpn_output4.bias", "backbone.fpn_lateral5.weight", "backbone.fpn_lateral5.bias", "backbone.fpn_output5.weight", "backbone.fpn_output5.bias", "backbone.bottom_up.stem.conv1.weight", "backbone.bottom_up.stem.conv1.norm.weight", "backbone.bottom_up.stem.conv1.norm.bias", "backbone.bottom_up.res2.0.shortcut.weight", "backbone.bottom_up.res2.0.shortcut.norm.weight", "backbone.bottom_up.res2.0.shortcut.norm.bias", "backbone.bottom_up.res2.0.conv1.weight", "backbone.bottom_up.res2.0.conv1.norm.weight", "backbone.bottom_up.res2.0.conv1.norm.bias", "backbone.bottom_up.res2.0.conv2.weight", "backbone.bottom_up.res2.0.conv2.norm.weight", "backbone.bottom_up.res2.0.conv2.norm.bias", "backbone.bottom_up.res2.0.conv3.weight", "backbone.bottom_up.res2.0.conv3.norm.weight", "backbone.bottom_up.res2.0.conv3.norm.bias", "backbone.bottom_up.res2.1.conv1.weight", "backbone.bottom_up.res2.1.conv1.norm.weight", "backbone.bottom_up.res2.1.conv1.norm.bias", "backbone.bottom_up.res2.1.conv2.weight", "backbone.bottom_up.res2.1.conv2.norm.weight", "backbone.bottom_up.res2.1.conv2.norm.bias", "backbone.bottom_up.res2.1.conv3.weight", "backbone.bottom_up.res2.1.conv3.norm.weight", "backbone.bottom_up.res2.1.conv3.norm.bias", "backbone.bottom_up.res2.2.conv1.weight", "backbone.bottom_up.res2.2.conv1.norm.weight", "backbone.bottom_up.res2.2.conv1.norm.bias", "backbone.bottom_up.res2.2.conv2.weight", "backbone.bottom_up.res2.2.conv2.norm.weight", "backbone.bottom_up.res2.2.conv2.norm.bias", "backbone.bottom_up.res2.2.conv3.weight", "backbone.bottom_up.res2.2.conv3.norm.weight", "backbone.bottom_up.res2.2.conv3.norm.bias", "backbone.bottom_up.res3.0.shortcut.weight", "backbone.bottom_up.res3.0.shortcut.norm.weight", "backbone.bottom_up.res3.0.shortcut.norm.bias", "backbone.bottom_up.res3.0.conv1.weight", "backbone.bottom_up.res3.0.conv1.norm.weight", "backbone.bottom_up.res3.0.conv1.norm.bias", "backbone.bottom_up.res3.0.conv2.weight", "backbone.bottom_up.res3.0.conv2.norm.weight", "backbone.bottom_up.res3.0.conv2.norm.bias", "backbone.bottom_up.res3.0.conv3.weight", "backbone.bottom_up.res3.0.conv3.norm.weight", "backbone.bottom_up.res3.0.conv3.norm.bias", "backbone.bottom_up.res3.1.conv1.weight", "backbone.bottom_up.res3.1.conv1.norm.weight", "backbone.bottom_up.res3.1.conv1.norm.bias", "backbone.bottom_up.res3.1.conv2.weight", "backbone.bottom_up.res3.1.conv2.norm.weight", "backbone.bottom_up.res3.1.conv2.norm.bias", "backbone.bottom_up.res3.1.conv3.weight", "backbone.bottom_up.res3.1.conv3.norm.weight", "backbone.bottom_up.res3.1.conv3.norm.bias", "backbone.bottom_up.res3.2.conv1.weight", "backbone.bottom_up.res3.2.conv1.norm.weight", "backbone.bottom_up.res3.2.conv1.norm.bias", "backbone.bottom_up.res3.2.conv2.weight", "backbone.bottom_up.res3.2.conv2.norm.weight", "backbone.bottom_up.res3.2.conv2.norm.bias", "backbone.bottom_up.res3.2.conv3.weight", "backbone.bottom_up.res3.2.conv3.norm.weight", "backbone.bottom_up.res3.2.conv3.norm.bias", "backbone.bottom_up.res3.3.conv1.weight", "backbone.bottom_up.res3.3.conv1.norm.weight", "backbone.bottom_up.res3.3.conv1.norm.bias", "backbone.bottom_up.res3.3.conv2.weight", "backbone.bottom_up.res3.3.conv2.norm.weight", "backbone.bottom_up.res3.3.conv2.norm.bias", "backbone.bottom_up.res3.3.conv3.weight", "backbone.bottom_up.res3.3.conv3.norm.weight", "backbone.bottom_up.res3.3.conv3.norm.bias", "backbone.bottom_up.res4.0.shortcut.weight", "backbone.bottom_up.res4.0.shortcut.norm.weight", "backbone.bottom_up.res4.0.shortcut.norm.bias", "backbone.bottom_up.res4.0.conv1.weight", "backbone.bottom_up.res4.0.conv1.norm.weight", "backbone.bottom_up.res4.0.conv1.norm.bias", "backbone.bottom_up.res4.0.conv2.weight", "backbone.bottom_up.res4.0.conv2.norm.weight", "backbone.bottom_up.res4.0.conv2.norm.bias", "backbone.bottom_up.res4.0.conv3.weight", "backbone.bottom_up.res4.0.conv3.norm.weight", "backbone.bottom_up.res4.0.conv3.norm.bias", "backbone.bottom_up.res4.1.conv1.weight", "backbone.bottom_up.res4.1.conv1.norm.weight", "backbone.bottom_up.res4.1.conv1.norm.bias", "backbone.bottom_up.res4.1.conv2.weight", "backbone.bottom_up.res4.1.conv2.norm.weight", "backbone.bottom_up.res4.1.conv2.norm.bias", "backbone.bottom_up.res4.1.conv3.weight", "backbone.bottom_up.res4.1.conv3.norm.weight", "backbone.bottom_up.res4.1.conv3.norm.bias", "backbone.bottom_up.res4.2.conv1.weight", "backbone.bottom_up.res4.2.conv1.norm.weight", "backbone.bottom_up.res4.2.conv1.norm.bias", "backbone.bottom_up.res4.2.conv2.weight", "backbone.bottom_up.res4.2.conv2.norm.weight", "backbone.bottom_up.res4.2.conv2.norm.bias", "backbone.bottom_up.res4.2.conv3.weight", "backbone.bottom_up.res4.2.conv3.norm.weight", "backbone.bottom_up.res4.2.conv3.norm.bias", "backbone.bottom_up.res4.3.conv1.weight", "backbone.bottom_up.res4.3.conv1.norm.weight", "backbone.bottom_up.res4.3.conv1.norm.bias", "backbone.bottom_up.res4.3.conv2.weight", "backbone.bottom_up.res4.3.conv2.norm.weight", "backbone.bottom_up.res4.3.conv2.norm.bias", "backbone.bottom_up.res4.3.conv3.weight", "backbone.bottom_up.res4.3.conv3.norm.weight", "backbone.bottom_up.res4.3.conv3.norm.bias", "backbone.bottom_up.res4.4.conv1.weight", "backbone.bottom_up.res4.4.conv1.norm.weight", "backbone.bottom_up.res4.4.conv1.norm.bias", "backbone.bottom_up.res4.4.conv2.weight", "backbone.bottom_up.res4.4.conv2.norm.weight", "backbone.bottom_up.res4.4.conv2.norm.bias", "backbone.bottom_up.res4.4.conv3.weight", "backbone.bottom_up.res4.4.conv3.norm.weight", "backbone.bottom_up.res4.4.conv3.norm.bias", "backbone.bottom_up.res4.5.conv1.weight", "backbone.bottom_up.res4.5.conv1.norm.weight", "backbone.bottom_up.res4.5.conv1.norm.bias", "backbone.bottom_up.res4.5.conv2.weight", "backbone.bottom_up.res4.5.conv2.norm.weight", "backbone.bottom_up.res4.5.conv2.norm.bias", "backbone.bottom_up.res4.5.conv3.weight", "backbone.bottom_up.res4.5.conv3.norm.weight", "backbone.bottom_up.res4.5.conv3.norm.bias", "backbone.bottom_up.res5.0.shortcut.weight", "backbone.bottom_up.res5.0.shortcut.norm.weight", "backbone.bottom_up.res5.0.shortcut.norm.bias", "backbone.bottom_up.res5.0.conv1.weight", "backbone.bottom_up.res5.0.conv1.norm.weight", "backbone.bottom_up.res5.0.conv1.norm.bias", "backbone.bottom_up.res5.0.conv2.weight", "backbone.bottom_up.res5.0.conv2.norm.weight", "backbone.bottom_up.res5.0.conv2.norm.bias", "backbone.bottom_up.res5.0.conv3.weight", "backbone.bottom_up.res5.0.conv3.norm.weight", "backbone.bottom_up.res5.0.conv3.norm.bias", "backbone.bottom_up.res5.1.conv1.weight", "backbone.bottom_up.res5.1.conv1.norm.weight", "backbone.bottom_up.res5.1.conv1.norm.bias", "backbone.bottom_up.res5.1.conv2.weight", "backbone.bottom_up.res5.1.conv2.norm.weight", "backbone.bottom_up.res5.1.conv2.norm.bias", "backbone.bottom_up.res5.1.conv3.weight", "backbone.bottom_up.res5.1.conv3.norm.weight", "backbone.bottom_up.res5.1.conv3.norm.bias", "backbone.bottom_up.res5.2.conv1.weight", "backbone.bottom_up.res5.2.conv1.norm.weight", "backbone.bottom_up.res5.2.conv1.norm.bias", "backbone.bottom_up.res5.2.conv2.weight", "backbone.bottom_up.res5.2.conv2.norm.weight", "backbone.bottom_up.res5.2.conv2.norm.bias", "backbone.bottom_up.res5.2.conv3.weight", "backbone.bottom_up.res5.2.conv3.norm.weight", "backbone.bottom_up.res5.2.conv3.norm.bias", "proposal_generator.rpn_head.conv.weight", "proposal_generator.rpn_head.conv.bias", "proposal_generator.rpn_head.objectness_logits.weight", "proposal_generator.rpn_head.objectness_logits.bias", "proposal_generator.rpn_head.anchor_deltas.weight", "proposal_generator.rpn_head.anchor_deltas.bias", "roi_heads.box_head.fc1.weight", "roi_heads.box_head.fc1.bias", "roi_heads.box_head.fc2.weight", "roi_heads.box_head.fc2.bias", "roi_heads.box_predictor.cls_score.weight", "roi_heads.box_predictor.cls_score.bias", "roi_heads.box_predictor.bbox_pred.weight", "roi_heads.box_predictor.bbox_pred.bias", "roi_heads.mask_head.mask_fcn1.weight", "roi_heads.mask_head.mask_fcn1.bias", "roi_heads.mask_head.mask_fcn2.weight", "roi_heads.mask_head.mask_fcn2.bias", "roi_heads.mask_head.mask_fcn3.weight", "roi_heads.mask_head.mask_fcn3.bias", "roi_heads.mask_head.mask_fcn4.weight", "roi_heads.mask_head.mask_fcn4.bias", "roi_heads.mask_head.deconv.weight", "roi_heads.mask_head.deconv.bias", "roi_heads.mask_head.predictor.weight", "roi_heads.mask_head.predictor.bias". Unexpected key(s) in state_dict: "model", "trainer", "iteration".
To save my model I ran this codeblock below before this load codeblock at the top of the post:
`
cfg_dict = yaml.safe_load(cfg.dump())
cfg_filename = "config_model_with_212_train_images.yaml"
with open(cfg_filename, "w") as f:
yaml.dump(cfg_dict, f)
`
What I should do? Or what am I missing on my code?
Beta Was this translation helpful? Give feedback.
All reactions