Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make logging period configurable via TRAINER.LOG_PERIOD #5394

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions detectron2/config/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,7 @@
# Do not commit any configs into it.
_C.GLOBAL = CN()
_C.GLOBAL.HACK = 1.0

# Period (measured in iterations) for writing logs during training
_C.TRAINER = CN()
_C.TRAINER.LOG_PERIOD = 20
2 changes: 1 addition & 1 deletion detectron2/engine/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def test_and_save_results():
if comm.is_main_process():
# Here the default print/log frequency of each writer is used.
# run writers in the end, so that evaluation metrics are written
ret.append(hooks.PeriodicWriter(self.build_writers(), period=20))
ret.append(hooks.PeriodicWriter(self.build_writers(), period=cfg.TRAINER.LOG_PERIOD))
return ret

def build_writers(self):
Expand Down
39 changes: 39 additions & 0 deletions tests/test_trainer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest
from detectron2.config import get_cfg
from detectron2.engine import DefaultTrainer
from detectron2.data import DatasetCatalog, MetadataCatalog

class TestTrainer(unittest.TestCase):
def test_log_period(self):
cfg = get_cfg()
cfg.TRAINER.LOG_PERIOD = 1

# Add minimum required config for trainer initialization
cfg.DATASETS.TRAIN = ("dummy_dataset",)
cfg.MODEL.DEVICE = "cpu"
cfg.OUTPUT_DIR = "."



def dummy_dataset():
return [{
"file_name": "dummy.jpg",
"height": 500,
"width": 500,
"image_id": 1,
"annotations": [{
"bbox": [0, 0, 10, 10],
"bbox_mode": 0, # XYXY_ABS
"category_id": 0,
"iscrowd": 0,
}]
}]

# Register a dummy dataset with one sample
DatasetCatalog.register("dummy_dataset", dummy_dataset)
MetadataCatalog.get("dummy_dataset").set(thing_classes=["dummy"])

trainer = DefaultTrainer(cfg)
hooks = trainer.build_hooks()
writer_hook = hooks[-1] # PeriodicWriter should be last hook
self.assertEqual(writer_hook._period, 1, "Log period from config not properly set")