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.3 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
  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(model=model,
  25. training_params={"max_epochs": 2,
  26. "lr_updates": [5, 6, 12],
  27. "lr_decay_factor": 0.01,
  28. "lr_mode": "step",
  29. "initial_lr": 0.01,
  30. "loss": "cross_entropy",
  31. "optimizer": "SGD",
  32. "optimizer_params": {"weight_decay": 1e-5, "momentum": 0.9},
  33. "train_metrics_list": [Accuracy()],
  34. "valid_metrics_list": [Accuracy()],
  35. "metric_to_watch": "Accuracy",
  36. "greater_metric_to_watch_is_better": True},
  37. train_loader=self.testcase_trainloader,
  38. valid_loader=self.testcase_validloader,
  39. )
  40. self.assertTrue(0 < trainer.best_metric.item() < 1)
  41. if __name__ == '__main__':
  42. unittest.main()
Tip!

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

Comments

Loading...