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

#309 Fix scale between rescaling batches

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:feature/SG-221-make_multiscale_keep_state
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  1. import unittest
  2. from super_gradients import SgModel, ClassificationTestDatasetInterface
  3. import torch
  4. from torch.utils.data import TensorDataset, DataLoader
  5. from super_gradients.training.metrics import Accuracy
  6. from super_gradients.training.exceptions.sg_model_exceptions import IllegalDataloaderInitialization
  7. class InitializeWithDataloadersTest(unittest.TestCase):
  8. def setUp(self):
  9. self.testcase_classes = [0, 1, 2, 3, 4]
  10. train_size, valid_size, test_size = 160, 20, 20
  11. channels, width, height = 3, 224, 224
  12. inp = torch.randn((train_size, channels, width, height))
  13. label = torch.randint(0, len(self.testcase_classes), size=(train_size,))
  14. self.testcase_trainloader = DataLoader(TensorDataset(inp, label))
  15. inp = torch.randn((valid_size, channels, width, height))
  16. label = torch.randint(0, len(self.testcase_classes), size=(valid_size,))
  17. self.testcase_validloader = DataLoader(TensorDataset(inp, label))
  18. inp = torch.randn((test_size, channels, width, height))
  19. label = torch.randint(0, len(self.testcase_classes), size=(test_size,))
  20. self.testcase_testloader = DataLoader(TensorDataset(inp, label))
  21. def test_interface_was_not_broken(self):
  22. model = SgModel("test_interface", model_checkpoints_location='local')
  23. dataset_params = {"batch_size": 10}
  24. dataset = ClassificationTestDatasetInterface(dataset_params=dataset_params)
  25. model.connect_dataset_interface(dataset)
  26. model.build_model("efficientnet_b0")
  27. train_params = {"max_epochs": 1, "lr_updates": [1], "lr_decay_factor": 0.1, "lr_mode": "step",
  28. "lr_warmup_epochs": 0, "initial_lr": 0.1, "loss": torch.nn.CrossEntropyLoss(),
  29. "optimizer": "SGD",
  30. "criterion_params": {}, "optimizer_params": {"weight_decay": 1e-4, "momentum": 0.9},
  31. "train_metrics_list": [Accuracy()], "valid_metrics_list": [Accuracy()],
  32. "metric_to_watch": "Accuracy",
  33. "greater_metric_to_watch_is_better": True}
  34. model.train(train_params)
  35. def test_initialization_rules(self):
  36. self.assertRaises(IllegalDataloaderInitialization, SgModel, "test_name", model_checkpoints_location='local',
  37. train_loader=self.testcase_trainloader)
  38. self.assertRaises(IllegalDataloaderInitialization, SgModel, "test_name", model_checkpoints_location='local',
  39. valid_loader=self.testcase_validloader)
  40. self.assertRaises(IllegalDataloaderInitialization, SgModel, "test_name", model_checkpoints_location='local',
  41. classes=self.testcase_classes)
  42. self.assertRaises(IllegalDataloaderInitialization, SgModel, "test_name", model_checkpoints_location='local',
  43. train_loader=self.testcase_trainloader, valid_loader=self.testcase_validloader)
  44. self.assertRaises(IllegalDataloaderInitialization, SgModel, "test_name", model_checkpoints_location='local',
  45. train_loader=self.testcase_trainloader, classes=self.testcase_classes)
  46. self.assertRaises(IllegalDataloaderInitialization, SgModel, "test_name", model_checkpoints_location='local',
  47. valid_loader=self.testcase_validloader, classes=self.testcase_classes)
  48. SgModel("test_name", model_checkpoints_location='local', train_loader=self.testcase_trainloader,
  49. valid_loader=self.testcase_validloader, classes=self.testcase_classes)
  50. SgModel("test_name", model_checkpoints_location='local', train_loader=self.testcase_trainloader,
  51. valid_loader=self.testcase_validloader, test_loader=self.testcase_testloader,
  52. classes=self.testcase_classes)
  53. def test_train_with_dataloaders(self):
  54. model = SgModel(experiment_name="test_name", model_checkpoints_location="local",
  55. train_loader=self.testcase_trainloader, valid_loader=self.testcase_validloader,
  56. classes=self.testcase_classes)
  57. model.build_model("resnet18")
  58. model.train(training_params={"max_epochs": 2,
  59. "lr_updates": [5, 6, 12],
  60. "lr_decay_factor": 0.01,
  61. "lr_mode": "step",
  62. "initial_lr": 0.01,
  63. "loss": "cross_entropy",
  64. "optimizer": "SGD",
  65. "optimizer_params": {"weight_decay": 1e-5, "momentum": 0.9},
  66. "train_metrics_list": [Accuracy()],
  67. "valid_metrics_list": [Accuracy()],
  68. "metric_to_watch": "Accuracy",
  69. "greater_metric_to_watch_is_better": True})
  70. self.assertTrue(0 < model.best_metric.item() < 1)
  71. if __name__ == '__main__':
  72. unittest.main()
Discard
Tip!

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