python-wml 3.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of python-wml might be problematic. Click here for more details.

Files changed (164) hide show
  1. python_wml-3.0.0.dist-info/LICENSE +23 -0
  2. python_wml-3.0.0.dist-info/METADATA +51 -0
  3. python_wml-3.0.0.dist-info/RECORD +164 -0
  4. python_wml-3.0.0.dist-info/WHEEL +5 -0
  5. python_wml-3.0.0.dist-info/top_level.txt +1 -0
  6. wml/__init__.py +0 -0
  7. wml/basic_data_def/__init__.py +2 -0
  8. wml/basic_data_def/detection_data_def.py +279 -0
  9. wml/basic_data_def/io_data_def.py +2 -0
  10. wml/basic_img_utils.py +816 -0
  11. wml/img_patch.py +92 -0
  12. wml/img_utils.py +571 -0
  13. wml/iotoolkit/__init__.py +17 -0
  14. wml/iotoolkit/aic_keypoint.py +115 -0
  15. wml/iotoolkit/baidu_mask_toolkit.py +244 -0
  16. wml/iotoolkit/base_dataset.py +210 -0
  17. wml/iotoolkit/bboxes_statistics.py +515 -0
  18. wml/iotoolkit/build.py +0 -0
  19. wml/iotoolkit/cityscapes_toolkit.py +183 -0
  20. wml/iotoolkit/classification_data_statistics.py +25 -0
  21. wml/iotoolkit/coco_data_fwd.py +225 -0
  22. wml/iotoolkit/coco_keypoints.py +118 -0
  23. wml/iotoolkit/coco_keypoints_fmt2.py +103 -0
  24. wml/iotoolkit/coco_toolkit.py +397 -0
  25. wml/iotoolkit/coco_wholebody.py +269 -0
  26. wml/iotoolkit/common.py +108 -0
  27. wml/iotoolkit/crowd_pose.py +146 -0
  28. wml/iotoolkit/fast_labelme.py +110 -0
  29. wml/iotoolkit/image_folder.py +95 -0
  30. wml/iotoolkit/imgs_cache.py +58 -0
  31. wml/iotoolkit/imgs_reader_mt.py +73 -0
  32. wml/iotoolkit/labelme_base.py +102 -0
  33. wml/iotoolkit/labelme_json_to_img.py +49 -0
  34. wml/iotoolkit/labelme_toolkit.py +117 -0
  35. wml/iotoolkit/labelme_toolkit_fwd.py +733 -0
  36. wml/iotoolkit/labelmemckeypoints_dataset.py +169 -0
  37. wml/iotoolkit/lspet.py +48 -0
  38. wml/iotoolkit/mapillary_vistas_toolkit.py +269 -0
  39. wml/iotoolkit/mat_data.py +90 -0
  40. wml/iotoolkit/mckeypoints_statistics.py +28 -0
  41. wml/iotoolkit/mot_datasets.py +62 -0
  42. wml/iotoolkit/mpii.py +108 -0
  43. wml/iotoolkit/npmckeypoints_dataset.py +164 -0
  44. wml/iotoolkit/o365_to_coco.py +136 -0
  45. wml/iotoolkit/object365_toolkit.py +156 -0
  46. wml/iotoolkit/object365v2_toolkit.py +71 -0
  47. wml/iotoolkit/pascal_voc_data.py +51 -0
  48. wml/iotoolkit/pascal_voc_toolkit.py +194 -0
  49. wml/iotoolkit/pascal_voc_toolkit_fwd.py +473 -0
  50. wml/iotoolkit/penn_action.py +57 -0
  51. wml/iotoolkit/rawframe_dataset.py +129 -0
  52. wml/iotoolkit/rewrite_pascal_voc.py +28 -0
  53. wml/iotoolkit/semantic_data.py +49 -0
  54. wml/iotoolkit/split_file_by_type.py +29 -0
  55. wml/iotoolkit/sports_mot_datasets.py +78 -0
  56. wml/iotoolkit/vis_objectdetection_dataset.py +70 -0
  57. wml/iotoolkit/vis_torch_data.py +39 -0
  58. wml/iotoolkit/yolo_toolkit.py +38 -0
  59. wml/object_detection2/__init__.py +4 -0
  60. wml/object_detection2/basic_visualization.py +37 -0
  61. wml/object_detection2/bboxes.py +812 -0
  62. wml/object_detection2/data_process_toolkit.py +146 -0
  63. wml/object_detection2/keypoints.py +292 -0
  64. wml/object_detection2/mask.py +120 -0
  65. wml/object_detection2/metrics/__init__.py +3 -0
  66. wml/object_detection2/metrics/build.py +15 -0
  67. wml/object_detection2/metrics/classifier_toolkit.py +440 -0
  68. wml/object_detection2/metrics/common.py +71 -0
  69. wml/object_detection2/metrics/mckps_toolkit.py +338 -0
  70. wml/object_detection2/metrics/toolkit.py +1953 -0
  71. wml/object_detection2/npod_toolkit.py +361 -0
  72. wml/object_detection2/odtools.py +243 -0
  73. wml/object_detection2/standard_names.py +75 -0
  74. wml/object_detection2/visualization.py +956 -0
  75. wml/object_detection2/wmath.py +34 -0
  76. wml/semantic/__init__.py +0 -0
  77. wml/semantic/basic_toolkit.py +65 -0
  78. wml/semantic/mask_utils.py +156 -0
  79. wml/semantic/semantic_test.py +21 -0
  80. wml/semantic/structures.py +1 -0
  81. wml/semantic/toolkit.py +105 -0
  82. wml/semantic/visualization_utils.py +658 -0
  83. wml/threadtoolkit.py +50 -0
  84. wml/walgorithm.py +228 -0
  85. wml/wcollections.py +212 -0
  86. wml/wfilesystem.py +487 -0
  87. wml/wml_utils.py +657 -0
  88. wml/wstructures/__init__.py +4 -0
  89. wml/wstructures/common.py +9 -0
  90. wml/wstructures/keypoints_train_toolkit.py +149 -0
  91. wml/wstructures/kps_structures.py +579 -0
  92. wml/wstructures/mask_structures.py +1161 -0
  93. wml/wtorch/__init__.py +8 -0
  94. wml/wtorch/bboxes.py +104 -0
  95. wml/wtorch/classes_suppression.py +24 -0
  96. wml/wtorch/conv_module.py +181 -0
  97. wml/wtorch/conv_ws.py +144 -0
  98. wml/wtorch/data/__init__.py +16 -0
  99. wml/wtorch/data/_utils/__init__.py +45 -0
  100. wml/wtorch/data/_utils/collate.py +183 -0
  101. wml/wtorch/data/_utils/fetch.py +47 -0
  102. wml/wtorch/data/_utils/pin_memory.py +121 -0
  103. wml/wtorch/data/_utils/signal_handling.py +72 -0
  104. wml/wtorch/data/_utils/worker.py +227 -0
  105. wml/wtorch/data/base_data_loader_iter.py +93 -0
  106. wml/wtorch/data/dataloader.py +501 -0
  107. wml/wtorch/data/datapipes/__init__.py +1 -0
  108. wml/wtorch/data/datapipes/iter/__init__.py +12 -0
  109. wml/wtorch/data/datapipes/iter/batch.py +126 -0
  110. wml/wtorch/data/datapipes/iter/callable.py +92 -0
  111. wml/wtorch/data/datapipes/iter/listdirfiles.py +37 -0
  112. wml/wtorch/data/datapipes/iter/loadfilesfromdisk.py +30 -0
  113. wml/wtorch/data/datapipes/iter/readfilesfromtar.py +60 -0
  114. wml/wtorch/data/datapipes/iter/readfilesfromzip.py +63 -0
  115. wml/wtorch/data/datapipes/iter/sampler.py +94 -0
  116. wml/wtorch/data/datapipes/utils/__init__.py +0 -0
  117. wml/wtorch/data/datapipes/utils/common.py +65 -0
  118. wml/wtorch/data/dataset.py +354 -0
  119. wml/wtorch/data/datasets/__init__.py +4 -0
  120. wml/wtorch/data/datasets/common.py +53 -0
  121. wml/wtorch/data/datasets/listdirfilesdataset.py +36 -0
  122. wml/wtorch/data/datasets/loadfilesfromdiskdataset.py +30 -0
  123. wml/wtorch/data/distributed.py +135 -0
  124. wml/wtorch/data/multi_processing_data_loader_iter.py +866 -0
  125. wml/wtorch/data/sampler.py +267 -0
  126. wml/wtorch/data/single_process_data_loader_iter.py +24 -0
  127. wml/wtorch/data/test_data_loader.py +26 -0
  128. wml/wtorch/dataset_toolkit.py +67 -0
  129. wml/wtorch/depthwise_separable_conv_module.py +98 -0
  130. wml/wtorch/dist.py +591 -0
  131. wml/wtorch/dropblock/__init__.py +6 -0
  132. wml/wtorch/dropblock/dropblock.py +228 -0
  133. wml/wtorch/dropblock/dropout.py +40 -0
  134. wml/wtorch/dropblock/scheduler.py +48 -0
  135. wml/wtorch/ema.py +61 -0
  136. wml/wtorch/fc_module.py +73 -0
  137. wml/wtorch/functional.py +34 -0
  138. wml/wtorch/iter_dataset.py +26 -0
  139. wml/wtorch/loss.py +69 -0
  140. wml/wtorch/nets/__init__.py +0 -0
  141. wml/wtorch/nets/ckpt_toolkit.py +219 -0
  142. wml/wtorch/nets/fpn.py +276 -0
  143. wml/wtorch/nets/hrnet/__init__.py +0 -0
  144. wml/wtorch/nets/hrnet/config.py +2 -0
  145. wml/wtorch/nets/hrnet/hrnet.py +494 -0
  146. wml/wtorch/nets/misc.py +249 -0
  147. wml/wtorch/nets/resnet/__init__.py +0 -0
  148. wml/wtorch/nets/resnet/layers/__init__.py +17 -0
  149. wml/wtorch/nets/resnet/layers/aspp.py +144 -0
  150. wml/wtorch/nets/resnet/layers/batch_norm.py +231 -0
  151. wml/wtorch/nets/resnet/layers/blocks.py +111 -0
  152. wml/wtorch/nets/resnet/layers/wrappers.py +110 -0
  153. wml/wtorch/nets/resnet/r50_config.py +38 -0
  154. wml/wtorch/nets/resnet/resnet.py +691 -0
  155. wml/wtorch/nets/shape_spec.py +20 -0
  156. wml/wtorch/nets/simple_fpn.py +101 -0
  157. wml/wtorch/nms.py +109 -0
  158. wml/wtorch/nn.py +896 -0
  159. wml/wtorch/ocr_block.py +193 -0
  160. wml/wtorch/summary.py +331 -0
  161. wml/wtorch/train_toolkit.py +603 -0
  162. wml/wtorch/transformer_blocks.py +266 -0
  163. wml/wtorch/utils.py +719 -0
  164. wml/wtorch/wlr_scheduler.py +100 -0
@@ -0,0 +1,266 @@
1
+ import torch
2
+ import torch.nn as nn
3
+ from typing import Optional, Tuple, Type
4
+ import torch.nn.functional as F
5
+ from functools import partial
6
+
7
+
8
+ def window_partition(x: torch.Tensor, window_size: int) -> Tuple[torch.Tensor, Tuple[int, int]]:
9
+ """
10
+ Partition into non-overlapping windows with padding if needed.
11
+ Args:
12
+ x (tensor): input tokens with [B, H, W, C].
13
+ window_size (int): window size.
14
+
15
+ Returns:
16
+ windows: windows after partition with [B * num_windows, window_size, window_size, C].
17
+ (Hp, Wp): padded height and width before partition
18
+ """
19
+ B, H, W, C = x.shape
20
+
21
+ pad_h = (window_size - H % window_size) % window_size
22
+ pad_w = (window_size - W % window_size) % window_size
23
+ if pad_h > 0 or pad_w > 0:
24
+ x = F.pad(x, (0, 0, 0, pad_w, 0, pad_h))
25
+ Hp, Wp = H + pad_h, W + pad_w
26
+
27
+ x = x.view(B, Hp // window_size, window_size, Wp // window_size, window_size, C)
28
+ windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, C)
29
+ return windows, (Hp, Wp)
30
+
31
+
32
+ def window_unpartition(
33
+ windows: torch.Tensor, window_size: int, pad_hw: Tuple[int, int], hw: Tuple[int, int]
34
+ ) -> torch.Tensor:
35
+ """
36
+ Window unpartition into original sequences and removing padding.
37
+ Args:
38
+ windows (tensor): input tokens with [B * num_windows, window_size, window_size, C].
39
+ window_size (int): window size.
40
+ pad_hw (Tuple): padded height and width (Hp, Wp).
41
+ hw (Tuple): original height and width (H, W) before padding.
42
+
43
+ Returns:
44
+ x: unpartitioned sequences with [B, H, W, C].
45
+ """
46
+ Hp, Wp = pad_hw
47
+ H, W = hw
48
+ B = windows.shape[0] // (Hp * Wp // window_size // window_size)
49
+ x = windows.view(B, Hp // window_size, Wp // window_size, window_size, window_size, -1)
50
+ x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, Hp, Wp, -1)
51
+
52
+ if Hp > H or Wp > W:
53
+ x = x[:, :H, :W, :].contiguous()
54
+ return x
55
+
56
+ def get_rel_pos(q_size: int, k_size: int, rel_pos: torch.Tensor) -> torch.Tensor:
57
+ """
58
+ Get relative positional embeddings according to the relative positions of
59
+ query and key sizes.
60
+ Args:
61
+ q_size (int): size of query q.
62
+ k_size (int): size of key k.
63
+ rel_pos (Tensor): relative position embeddings (L, C).
64
+
65
+ Returns:
66
+ Extracted positional embeddings according to relative positions.
67
+ """
68
+ max_rel_dist = int(2 * max(q_size, k_size) - 1)
69
+ # Interpolate rel pos if needed.
70
+ if rel_pos.shape[0] != max_rel_dist:
71
+ # Interpolate rel pos.
72
+ rel_pos_resized = F.interpolate(
73
+ rel_pos.reshape(1, rel_pos.shape[0], -1).permute(0, 2, 1),
74
+ size=max_rel_dist,
75
+ mode="linear",
76
+ )
77
+ rel_pos_resized = rel_pos_resized.reshape(-1, max_rel_dist).permute(1, 0)
78
+ else:
79
+ rel_pos_resized = rel_pos
80
+
81
+ # Scale the coords with short length if shapes for q and k are different.
82
+ q_coords = torch.arange(q_size)[:, None] * max(k_size / q_size, 1.0)
83
+ k_coords = torch.arange(k_size)[None, :] * max(q_size / k_size, 1.0)
84
+ relative_coords = (q_coords - k_coords) + (k_size - 1) * max(q_size / k_size, 1.0)
85
+
86
+ return rel_pos_resized[relative_coords.long()]
87
+
88
+
89
+ def add_decomposed_rel_pos(
90
+ attn: torch.Tensor,
91
+ q: torch.Tensor,
92
+ rel_pos_h: torch.Tensor,
93
+ rel_pos_w: torch.Tensor,
94
+ q_size: Tuple[int, int],
95
+ k_size: Tuple[int, int],
96
+ ) -> torch.Tensor:
97
+ """
98
+ Calculate decomposed Relative Positional Embeddings from :paper:`mvitv2`.
99
+ https://github.com/facebookresearch/mvit/blob/19786631e330df9f3622e5402b4a419a263a2c80/mvit/models/attention.py # noqa B950
100
+ Args:
101
+ attn (Tensor): attention map.
102
+ q (Tensor): query q in the attention layer with shape (B, q_h * q_w, C).
103
+ rel_pos_h (Tensor): relative position embeddings (Lh, C) for height axis.
104
+ rel_pos_w (Tensor): relative position embeddings (Lw, C) for width axis.
105
+ q_size (Tuple): spatial sequence size of query q with (q_h, q_w).
106
+ k_size (Tuple): spatial sequence size of key k with (k_h, k_w).
107
+
108
+ Returns:
109
+ attn (Tensor): attention map with added relative positional embeddings.
110
+ """
111
+ q_h, q_w = q_size
112
+ k_h, k_w = k_size
113
+ Rh = get_rel_pos(q_h, k_h, rel_pos_h)
114
+ Rw = get_rel_pos(q_w, k_w, rel_pos_w)
115
+
116
+ B, _, dim = q.shape
117
+ r_q = q.reshape(B, q_h, q_w, dim)
118
+ rel_h = torch.einsum("bhwc,hkc->bhwk", r_q, Rh)
119
+ rel_w = torch.einsum("bhwc,wkc->bhwk", r_q, Rw)
120
+
121
+ attn = (
122
+ attn.view(B, q_h, q_w, k_h, k_w) + rel_h[:, :, :, :, None] + rel_w[:, :, :, None, :]
123
+ ).view(B, q_h * q_w, k_h * k_w)
124
+
125
+ return attn
126
+
127
+ class MLPBlock(nn.Module):
128
+ def __init__(
129
+ self,
130
+ embedding_dim: int,
131
+ mlp_dim: int,
132
+ act: Type[nn.Module] = nn.GELU,
133
+ ) -> None:
134
+ super().__init__()
135
+ self.lin1 = nn.Linear(embedding_dim, mlp_dim)
136
+ self.lin2 = nn.Linear(mlp_dim, embedding_dim)
137
+ self.act = act()
138
+
139
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
140
+ return self.lin2(self.act(self.lin1(x)))
141
+
142
+
143
+ class Attention(nn.Module):
144
+ """Multi-head Attention block with relative position embeddings."""
145
+
146
+ def __init__(
147
+ self,
148
+ dim: int,
149
+ num_heads: int = 8,
150
+ qkv_bias: bool = True,
151
+ use_rel_pos: bool = True,
152
+ rel_pos_zero_init: bool = True,
153
+ input_size: Optional[Tuple[int, int]] = None,
154
+ ) -> None:
155
+ """
156
+ Args:
157
+ dim (int): Number of input channels.
158
+ num_heads (int): Number of attention heads.
159
+ qkv_bias (bool): If True, add a learnable bias to query, key, value.
160
+ rel_pos (bool): If True, add relative positional embeddings to the attention map.
161
+ rel_pos_zero_init (bool): If True, zero initialize relative positional parameters.
162
+ input_size (tuple(int, int) or None): Input resolution for calculating the relative
163
+ positional parameter size. (H,W)
164
+ """
165
+ super().__init__()
166
+ self.num_heads = num_heads
167
+ head_dim = dim // num_heads
168
+ self.scale = head_dim**-0.5
169
+
170
+ self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
171
+ self.proj = nn.Linear(dim, dim)
172
+
173
+ self.use_rel_pos = use_rel_pos
174
+ if self.use_rel_pos:
175
+ assert (
176
+ input_size is not None
177
+ ), "Input size must be provided if using relative positional encoding."
178
+ # initialize relative positional embeddings
179
+ self.rel_pos_h = nn.Parameter(torch.zeros(2 * input_size[0] - 1, head_dim))
180
+ self.rel_pos_w = nn.Parameter(torch.zeros(2 * input_size[1] - 1, head_dim))
181
+
182
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
183
+ B, H, W, _ = x.shape
184
+ # qkv with shape (3, B, nHead, H * W, C)
185
+ qkv = self.qkv(x).reshape(B, H * W, 3, self.num_heads, -1).permute(2, 0, 3, 1, 4)
186
+ # q, k, v with shape (B * nHead, H * W, C)
187
+ q, k, v = qkv.reshape(3, B * self.num_heads, H * W, -1).unbind(0)
188
+
189
+ attn = (q * self.scale) @ k.transpose(-2, -1)
190
+
191
+ if self.use_rel_pos:
192
+ attn = add_decomposed_rel_pos(attn, q, self.rel_pos_h, self.rel_pos_w, (H, W), (H, W))
193
+
194
+ attn = attn.softmax(dim=-1)
195
+ x = (attn @ v).view(B, self.num_heads, H, W, -1).permute(0, 2, 3, 1, 4).reshape(B, H, W, -1)
196
+ x = self.proj(x)
197
+
198
+ return x
199
+
200
+ class TransformerBlock(nn.Module):
201
+ """Transformer blocks with support of window attention and residual propagation blocks"""
202
+
203
+ def __init__(
204
+ self,
205
+ dim: int = 768,
206
+ num_heads: int = 12,
207
+ mlp_ratio: float = 4.0,
208
+ qkv_bias: bool = True,
209
+ norm_layer: Type[nn.Module] = partial(torch.nn.LayerNorm, eps=1e-6),
210
+ act_layer: Type[nn.Module] = nn.GELU,
211
+ use_rel_pos: bool = True,
212
+ rel_pos_zero_init: bool = True,
213
+ window_size: int = 0,
214
+ input_size: Optional[Tuple[int, int]] = (64,64),
215
+ ) -> None:
216
+ """
217
+ Args:
218
+ dim (int): Number of input channels.
219
+ num_heads (int): Number of attention heads in each ViT block.
220
+ mlp_ratio (float): Ratio of mlp hidden dim to embedding dim.
221
+ qkv_bias (bool): If True, add a learnable bias to query, key, value.
222
+ norm_layer (nn.Module): Normalization layer.
223
+ act_layer (nn.Module): Activation layer.
224
+ use_rel_pos (bool): If True, add relative positional embeddings to the attention map.
225
+ rel_pos_zero_init (bool): If True, zero initialize relative positional parameters.
226
+ window_size (int): Window size for window attention blocks. If it equals 0, then
227
+ use global attention.
228
+ input_size (tuple(int, int) or None): Input resolution for calculating the relative
229
+ positional parameter size.
230
+ """
231
+ super().__init__()
232
+ self.norm1 = norm_layer(dim)
233
+ self.attn = Attention(
234
+ dim,
235
+ num_heads=num_heads,
236
+ qkv_bias=qkv_bias,
237
+ use_rel_pos=use_rel_pos,
238
+ rel_pos_zero_init=rel_pos_zero_init,
239
+ input_size=input_size if window_size == 0 else (window_size, window_size),
240
+ )
241
+
242
+ self.norm2 = norm_layer(dim)
243
+ self.mlp = MLPBlock(embedding_dim=dim, mlp_dim=int(dim * mlp_ratio), act=act_layer)
244
+
245
+ self.window_size = window_size
246
+
247
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
248
+ '''
249
+ x: [B,H,W,C]
250
+ '''
251
+ shortcut = x
252
+ x = self.norm1(x)
253
+ # Window partition
254
+ if self.window_size > 0:
255
+ H, W = x.shape[1], x.shape[2]
256
+ x, pad_hw = window_partition(x, self.window_size)
257
+
258
+ x = self.attn(x)
259
+ # Reverse window partition
260
+ if self.window_size > 0:
261
+ x = window_unpartition(x, self.window_size, pad_hw, (H, W))
262
+
263
+ x = shortcut + x
264
+ x = x + self.mlp(self.norm2(x))
265
+
266
+ return x