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

#643 PPYolo-E

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:feature/SG-344-PP-Yolo-E-Training-Replicate-Recipe
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  1. import os
  2. import torch
  3. import torch.nn as nn
  4. import torch.nn.init as init
  5. def prefetch_dataset(dataset, num_workers=4, batch_size=32, device=None, half=False):
  6. if isinstance(dataset, list) and isinstance(dataset[0], torch.Tensor):
  7. tensors = dataset
  8. else:
  9. dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=False, drop_last=False, num_workers=num_workers, pin_memory=False)
  10. tensors = [t for t in dataloader]
  11. tensors = [torch.cat(t, dim=0) for t in zip(*tensors)]
  12. if device is not None:
  13. tensors = [t.to(device=device) for t in tensors]
  14. if half:
  15. tensors = [t.half() if t.is_floating_point() else t for t in tensors]
  16. return torch.utils.data.TensorDataset(*tensors)
  17. class PrefetchDataLoader:
  18. def __init__(self, dataloader, device, half=False):
  19. self.loader = dataloader
  20. self.iter = None
  21. self.device = device
  22. self.dtype = torch.float16 if half else torch.float32
  23. self.stream = torch.cuda.Stream()
  24. self.next_data = None
  25. def __len__(self):
  26. return len(self.loader)
  27. def async_prefech(self):
  28. try:
  29. self.next_data = next(self.iter)
  30. except StopIteration:
  31. self.next_data = None
  32. return
  33. with torch.cuda.stream(self.stream):
  34. if isinstance(self.next_data, torch.Tensor):
  35. self.next_data = self.next_data.to(dtype=self.dtype, device=self.device, non_blocking=True)
  36. elif isinstance(self.next_data, (list, tuple)):
  37. self.next_data = [
  38. t.to(dtype=self.dtype, device=self.device, non_blocking=True) if t.is_floating_point() else t.to(device=self.device, non_blocking=True)
  39. for t in self.next_data
  40. ]
  41. def __iter__(self):
  42. self.iter = iter(self.loader)
  43. self.async_prefech()
  44. while self.next_data is not None:
  45. torch.cuda.current_stream().wait_stream(self.stream)
  46. data = self.next_data
  47. self.async_prefech()
  48. yield data
  49. def init_params(net):
  50. """Init layer parameters."""
  51. for m in net.modules():
  52. if isinstance(m, nn.Conv2d):
  53. init.kaiming_normal(m.weight, mode="fan_out")
  54. # if m.bias:
  55. # init.constant(m.bias, -5)
  56. elif isinstance(m, nn.BatchNorm2d):
  57. init.constant(m.weight, 1)
  58. init.constant(m.bias, 0)
  59. elif isinstance(m, nn.Linear):
  60. init.normal(m.weight, std=1e-3)
  61. if m.bias:
  62. init.constant(m.bias, 0)
  63. def format_time(seconds):
  64. days = int(seconds / 3600 / 24)
  65. seconds = seconds - days * 3600 * 24
  66. hours = int(seconds / 3600)
  67. seconds = seconds - hours * 3600
  68. minutes = int(seconds / 60)
  69. seconds = seconds - minutes * 60
  70. secondsf = int(seconds)
  71. seconds = seconds - secondsf
  72. millis = int(seconds * 1000)
  73. f = ""
  74. i = 1
  75. if days > 0:
  76. f += str(days) + "D"
  77. i += 1
  78. if hours > 0 and i <= 2:
  79. f += str(hours) + "h"
  80. i += 1
  81. if minutes > 0 and i <= 2:
  82. f += str(minutes) + "m"
  83. i += 1
  84. if secondsf > 0 and i <= 2:
  85. f += str(secondsf) + "s"
  86. i += 1
  87. if millis > 0 and i <= 2:
  88. f += str(millis) + "ms"
  89. i += 1
  90. if f == "":
  91. f = "0ms"
  92. return f
  93. def is_better(new_metric, current_best_metric, metric_to_watch="acc"):
  94. """
  95. Determines which of the two metrics is better, the higher if watching acc or lower when watching loss
  96. :param new_metric: the new metric
  97. :param current_best_metric: the compared to metric
  98. :param metric_to_watch: acc or loss
  99. :return: bool, True if new metric is better than current
  100. """
  101. return metric_to_watch == "acc" and new_metric > current_best_metric or (metric_to_watch == "loss" and current_best_metric > new_metric)
  102. def makedirs_if_not_exists(dir_path: str):
  103. """
  104. make new directory in dir_path if it doesn't exists
  105. :param dir_path - full path of directory
  106. """
  107. if not os.path.exists(dir_path):
  108. os.makedirs(dir_path)
Discard
Tip!

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