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

initialize_with_dataloaders_test.py 2.1 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
  1. import unittest
  2. from super_gradients.training import models
  3. from super_gradients import Trainer
  4. import torch
  5. from torch.utils.data import TensorDataset, DataLoader
  6. from super_gradients.training.metrics import Accuracy
  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_train_with_dataloaders(self):
  22. trainer = Trainer(experiment_name="test_name")
  23. model = models.get("resnet18", num_classes=5)
  24. trainer.train(
  25. model=model,
  26. training_params={
  27. "max_epochs": 2,
  28. "lr_updates": [5, 6, 12],
  29. "lr_decay_factor": 0.01,
  30. "lr_mode": "step",
  31. "initial_lr": 0.01,
  32. "loss": "cross_entropy",
  33. "optimizer": "SGD",
  34. "optimizer_params": {"weight_decay": 1e-5, "momentum": 0.9},
  35. "train_metrics_list": [Accuracy()],
  36. "valid_metrics_list": [Accuracy()],
  37. "metric_to_watch": "Accuracy",
  38. "greater_metric_to_watch_is_better": True,
  39. },
  40. train_loader=self.testcase_trainloader,
  41. valid_loader=self.testcase_validloader,
  42. )
  43. self.assertTrue(0 < trainer.best_metric.item() < 1)
  44. if __name__ == "__main__":
  45. unittest.main()
Tip!

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

Comments

Loading...