当前位置:网站首页>Detectron2 save (according to maxap50) model during training_ best. PTH weight

Detectron2 save (according to maxap50) model during training_ best. PTH weight

2022-06-26 09:14:00 G fruit

Let me write it out front

The problem lies in GitHub Of detectron2 Of issues Put forward on , Someone solved ( As shown in the figure below )
Hint , Go to GitHub Upper issues Search for questions , Try to find 【closed】 Labeled , These are basically problems with solutions .
Just make a record here , For learning purposes only

Reference resources GitHub link :
Feature: a hook to automatically save the best model during training

 Insert picture description here

Added package

from detectron2.engine import HookBase

Function function

class BestCheckpointer(HookBase):
  def __init__(self):
      super().__init__()

  def after_step(self):
    # No way to use **kwargs

    ##ONly do this analys when trainer.iter is divisle by checkpoint_epochs
    curr_val = self.trainer.storage.latest().get('bbox/AP50', 0)
    ''' Minor changes have been made here '''
    import math
    if type(curr_val) != int:
        curr_val = curr_val[0]
        if math.isnan(curr_val):
            curr_val = 0

    try:
        _ = self.trainer.storage.history('max_bbox/AP50')
    except:
        self.trainer.storage.put_scalar('max_bbox/AP50', curr_val)

    max_val = self.trainer.storage.history('max_bbox/AP50')._data[-1][0]

    #print(curr_val, max_val)
    if curr_val > max_val:
        print("\n%s > %s To save !!\n"%(curr_val,max_val))
        self.trainer.storage.put_scalar('max_bbox/AP50', curr_val)
        self.trainer.checkpointer.save("model_best")
        #self.step(self.trainer.iter)

Usage method

    os.makedirs(cfg.OUTPUT_DIR, exist_ok=True)
    trainer = DefaultTrainer(cfg)
    trainer.register_hooks([BestCheckpointer()])
    trainer.resume_or_load(resume=False)
    print(cfg)
    trainer.train()

Realization effect

 Insert picture description here

 Insert picture description here

原网站

版权声明
本文为[G fruit]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170552523803.html