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

#546 Features/sg 409 check all params used

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:features/SG-409-check-all-params-used
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
  1. import warnings
  2. from typing import Tuple
  3. import numpy as np
  4. import torch
  5. from torch import Tensor
  6. from super_gradients.training.utils.bbox_formats.bbox_format import (
  7. BoundingBoxFormat,
  8. )
  9. __all__ = [
  10. "NormalizedXYXYCoordinateFormat",
  11. "normalized_xyxy_to_xyxy",
  12. "normalized_xyxy_to_xyxy_inplace",
  13. "xyxy_to_normalized_xyxy",
  14. "xyxy_to_normalized_xyxy_inplace",
  15. ]
  16. def normalized_xyxy_to_xyxy(bboxes, image_shape: Tuple[int, int]):
  17. """
  18. Convert unit-normalized XYXY bboxes to XYXY bboxes in pixel units.
  19. :param bboxes: BBoxes of shape (..., 4) in XYXY (unit-normalized) format
  20. :param image_shape: Image shape (rows,cols)
  21. :return: BBoxes of shape (..., 4) in XYXY (pixels) format
  22. """
  23. rows, cols = image_shape
  24. if torch.jit.is_scripting():
  25. scale = torch.tensor([cols, rows, cols, rows], dtype=bboxes.dtype, device=bboxes.device)
  26. scale = scale.reshape([1] * (len(bboxes.size()) - 1) + [4])
  27. else:
  28. if torch.is_tensor(bboxes):
  29. scale = torch.tensor([cols, rows, cols, rows], dtype=bboxes.dtype, device=bboxes.device)
  30. scale = scale.reshape([1] * (len(bboxes.size()) - 1) + [4])
  31. elif isinstance(bboxes, np.ndarray):
  32. scale = np.array([cols, rows, cols, rows], dtype=bboxes.dtype)
  33. scale = scale.reshape([1] * (len(bboxes.shape) - 1) + [4])
  34. else:
  35. raise RuntimeError(f"Only Torch tensor or Numpy array is supported. Received bboxes of type {str(type(bboxes))}")
  36. return bboxes * scale
  37. def xyxy_to_normalized_xyxy(bboxes: Tensor, image_shape: Tuple[int, int]) -> Tensor:
  38. """
  39. Convert bboxes from XYXY (pixels) format to XYXY (unit-normalized) format
  40. :param bboxes: BBoxes of shape (..., 4) in XYXY (pixels) format
  41. :param image_shape: Image shape (rows,cols)
  42. :return: BBoxes of shape (..., 4) in XYXY (unit-normalized) format
  43. """
  44. rows, cols = image_shape
  45. if torch.jit.is_scripting():
  46. scale = torch.tensor([cols, rows, cols, rows], dtype=bboxes.dtype, device=bboxes.device)
  47. scale = scale.reshape([1] * (len(bboxes.size()) - 1) + [4])
  48. else:
  49. if torch.is_tensor(bboxes):
  50. scale = torch.tensor([cols, rows, cols, rows], dtype=bboxes.dtype, device=bboxes.device)
  51. scale = scale.reshape([1] * (len(bboxes.size()) - 1) + [4])
  52. elif isinstance(bboxes, np.ndarray):
  53. scale = np.array([cols, rows, cols, rows], dtype=bboxes.dtype)
  54. else:
  55. raise RuntimeError(f"Only Torch tensor or Numpy array is supported. Received bboxes of type {str(type(bboxes))}")
  56. return bboxes / scale
  57. def normalized_xyxy_to_xyxy_inplace(bboxes, image_shape: Tuple[int, int]):
  58. """
  59. Convert unit-normalized XYXY bboxes to XYXY bboxes in pixel units. This function operates in-place.
  60. :param bboxes: BBoxes of shape (..., 4) in XYXY (unit-normalized) format
  61. :param image_shape: Image shape (rows,cols)
  62. :return: BBoxes of shape (..., 4) in XYXY (pixels) format
  63. """
  64. rows, cols = image_shape
  65. bboxes[..., 0:3:2] *= cols
  66. bboxes[..., 1:4:2] *= rows
  67. return bboxes
  68. def xyxy_to_normalized_xyxy_inplace(bboxes, image_shape: Tuple[int, int]):
  69. """
  70. Convert bboxes from XYXY (pixels) format to XYXY (unit-normalized) format. This function operates in-place.
  71. :param bboxes: BBoxes of shape (..., 4) in XYXY (pixels) format
  72. :param image_shape: Image shape (rows,cols)
  73. :return: BBoxes of shape (..., 4) in XYXY (unit-normalized) format
  74. """
  75. if not torch.jit.is_scripting():
  76. if torch.is_tensor(bboxes) and not torch.is_floating_point(bboxes):
  77. warnings.warn(
  78. f"Detected non floating-point ({bboxes.dtype}) input to xyxy_to_normalized_xyxy_inplace function. "
  79. f"This may cause rounding errors and lose of precision. You may want to convert your array to floating-point precision first."
  80. )
  81. if isinstance(bboxes, np.ndarray) and not np.issubdtype(bboxes.dtype, np.floating):
  82. warnings.warn(
  83. f"Detected non floating-point input ({bboxes.dtype}) to xyxy_to_normalized_xyxy_inplace function. "
  84. f"This may cause rounding errors and lose of precision. You may want to convert your array to floating-point precision first."
  85. )
  86. rows, cols = image_shape
  87. bboxes[..., 0:3:2] /= cols
  88. bboxes[..., 1:4:2] /= rows
  89. return bboxes
  90. class NormalizedXYXYCoordinateFormat(BoundingBoxFormat):
  91. """
  92. Normalized X1,Y1,X2,Y2 bounding boxes format
  93. """
  94. def __init__(self):
  95. super().__init__()
  96. self.format = "normalized_xyxy"
  97. self.normalized = True
  98. def get_to_xyxy(self, inplace: bool):
  99. if inplace:
  100. return normalized_xyxy_to_xyxy_inplace
  101. else:
  102. return normalized_xyxy_to_xyxy
  103. def get_from_xyxy(self, inplace: bool):
  104. if inplace:
  105. return xyxy_to_normalized_xyxy_inplace
  106. else:
  107. return xyxy_to_normalized_xyxy
Discard
Tip!

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