Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

train_logging_test.py 2.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  1. import unittest
  2. from super_gradients import Trainer
  3. from super_gradients.common.auto_logging.auto_logger import AutoLoggerConfig
  4. from super_gradients.training.dataloaders.dataloaders import classification_test_dataloader
  5. from super_gradients.training.metrics import Accuracy, Top5
  6. from super_gradients.training.models import ResNet18
  7. import os
  8. import logging
  9. from super_gradients.common.abstractions.abstract_logger import get_logger
  10. import shutil
  11. class SgTrainerLoggingTest(unittest.TestCase):
  12. def test_train_logging(self):
  13. trainer = Trainer("test_train_with_full_log")
  14. net = ResNet18(num_classes=5, arch_params={})
  15. train_params = {
  16. "max_epochs": 2,
  17. "lr_updates": [1],
  18. "lr_decay_factor": 0.1,
  19. "lr_mode": "StepLRScheduler",
  20. "lr_warmup_epochs": 0,
  21. "initial_lr": 0.1,
  22. "loss": "CrossEntropyLoss",
  23. "optimizer": "SGD",
  24. "optimizer_params": {"weight_decay": 1e-4, "momentum": 0.9},
  25. "train_metrics_list": [Accuracy(), Top5()],
  26. "valid_metrics_list": [Accuracy(), Top5()],
  27. "metric_to_watch": "Accuracy",
  28. "greater_metric_to_watch_is_better": True,
  29. }
  30. trainer.train(
  31. model=net,
  32. training_params=train_params,
  33. train_loader=classification_test_dataloader(batch_size=10),
  34. valid_loader=classification_test_dataloader(batch_size=10),
  35. )
  36. logfile_path = AutoLoggerConfig.get_log_file_path()
  37. assert os.path.exists(logfile_path) and os.path.getsize(logfile_path) > 0
  38. root_logger_handlers = logging.root.handlers
  39. assert any(isinstance(handler, logging.handlers.FileHandler) and handler.baseFilename == logfile_path for handler in root_logger_handlers)
  40. assert any(isinstance(handler, logging.StreamHandler) and handler.name == "console" for handler in root_logger_handlers)
  41. def test_logger_with_non_existing_deci_logs_dir(self):
  42. user_dir = os.path.expanduser(r"~")
  43. logs_dir_path = os.path.join(user_dir, "non_existing_deci_logs_dir")
  44. if os.path.exists(logs_dir_path):
  45. shutil.rmtree(logs_dir_path)
  46. module_name = "super_gradients.trainer.sg_trainer"
  47. _ = get_logger(module_name, logs_dir_path=logs_dir_path)
  48. root_logger_handlers = logging.root.handlers
  49. assert any(isinstance(handler, logging.StreamHandler) and handler.name == "console" for handler in root_logger_handlers)
  50. if __name__ == "__main__":
  51. unittest.main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...