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

#578 Feature/sg 516 support head replacement for local pretrained weights unknown dataset

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

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