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

#609 Ci fix

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:bugfix/infra-000_ci
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
  1. """
  2. A base for a detection network built according to the following scheme:
  3. * constructed from nested arch_params;
  4. * inside arch_params each nested level (module) has an explicit type and its required parameters
  5. * each module accepts in_channels and other parameters
  6. * each module defines out_channels property on construction
  7. """
  8. from typing import Union
  9. from torch import nn
  10. from omegaconf import DictConfig
  11. from super_gradients.training.utils.utils import HpmStruct, get_param
  12. from super_gradients.training.models.sg_module import SgModule
  13. import super_gradients.common.factories.detection_modules_factory as det_factory
  14. class CustomizableDetector(SgModule):
  15. """
  16. A customizable detector with backbone -> neck -> heads
  17. Each submodule with its parameters must be defined explicitly.
  18. Modules should follow the interface of BaseDetectionModule
  19. """
  20. def __init__(self, arch_params: Union[HpmStruct, DictConfig], in_channels: int = 3):
  21. """
  22. :param type_mapping: can be passed to resolve string type names in arch_params to actual types
  23. """
  24. super().__init__()
  25. factory = det_factory.DetectionModulesFactory()
  26. # move num_classes into heads params
  27. if get_param(arch_params, "num_classes"):
  28. arch_params.heads = factory.insert_module_param(arch_params.heads, "num_classes", arch_params.num_classes)
  29. self.arch_params = arch_params
  30. self.backbone = factory.get(factory.insert_module_param(arch_params.backbone, "in_channels", in_channels))
  31. self.neck = factory.get(factory.insert_module_param(arch_params.neck, "in_channels", self.backbone.out_channels))
  32. self.heads = factory.get(factory.insert_module_param(arch_params.heads, "in_channels", self.neck.out_channels))
  33. self._initialize_weights(arch_params)
  34. def forward(self, x):
  35. x = self.backbone(x)
  36. x = self.neck(x)
  37. return self.heads(x)
  38. def _initialize_weights(self, arch_params: Union[HpmStruct, DictConfig]):
  39. bn_eps = get_param(arch_params, "bn_eps", None)
  40. bn_momentum = get_param(arch_params, "bn_momentum", None)
  41. inplace_act = get_param(arch_params, "inplace_act", True)
  42. for m in self.modules():
  43. t = type(m)
  44. if t is nn.BatchNorm2d:
  45. m.eps = bn_eps if bn_eps else m.eps
  46. m.momentum = bn_momentum if bn_momentum else m.momentum
  47. elif inplace_act and t in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU, nn.Mish]:
  48. m.inplace = True
  49. def prep_model_for_conversion(self, input_size: Union[tuple, list] = None, **kwargs):
  50. for module in self.modules():
  51. if module != self and hasattr(module, "prep_model_for_conversion"):
  52. module.prep_model_for_conversion(input_size, **kwargs)
  53. def replace_head(self, new_num_classes: int = None, new_head: nn.Module = None):
  54. if new_num_classes is None and new_head is None:
  55. raise ValueError("At least one of new_num_classes, new_head must be given to replace output layer.")
  56. if new_head is not None:
  57. self.heads = new_head
  58. else:
  59. factory = det_factory.DetectionModulesFactory()
  60. self.arch_params.heads = factory.insert_module_param(self.arch_params.heads, "num_classes", new_num_classes)
  61. self.heads = factory.get(factory.insert_module_param(self.arch_params.heads, "in_channels", self.neck.out_channels))
  62. self._initialize_weights(self.arch_params)
Discard
Tip!

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