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
  1. from typing import Tuple
  2. import numpy as np
  3. import torch
  4. from super_gradients.training.utils.bbox_formats.bbox_format import (
  5. BoundingBoxFormat,
  6. )
  7. __all__ = ["xyxy_to_xywh", "xywh_to_xyxy_inplace", "xyxy_to_xywh_inplace", "xywh_to_xyxy", "XYWHCoordinateFormat"]
  8. def xyxy_to_xywh(bboxes, image_shape: Tuple[int, int]):
  9. """
  10. Transforms bboxes inplace from XYXY format to XYWH format
  11. :param bboxes: BBoxes of shape (..., 4) in XYXY format
  12. :return: BBoxes of shape (..., 4) in XYWH format
  13. """
  14. x1, y1, x2, y2 = bboxes[..., 0], bboxes[..., 1], bboxes[..., 2], bboxes[..., 3]
  15. w = x2 - x1
  16. h = y2 - y1
  17. if torch.jit.is_scripting():
  18. return torch.stack([x1, y1, w, h], dim=-1)
  19. else:
  20. if torch.is_tensor(bboxes):
  21. return torch.stack([x1, y1, w, h], dim=-1)
  22. elif isinstance(bboxes, np.ndarray):
  23. return np.stack([x1, y1, w, h], axis=-1)
  24. else:
  25. raise RuntimeError(f"Only Torch tensor or Numpy array is supported. Received bboxes of type {str(type(bboxes))}")
  26. def xywh_to_xyxy(bboxes, image_shape: Tuple[int, int]):
  27. """
  28. Transforms bboxes from XYWH format to XYXY format
  29. :param bboxes: BBoxes of shape (..., 4) in XYWH format
  30. :return: BBoxes of shape (..., 4) in XYXY format
  31. """
  32. x1, y1, w, h = bboxes[..., 0], bboxes[..., 1], bboxes[..., 2], bboxes[..., 3]
  33. x2 = x1 + w
  34. y2 = y1 + h
  35. if torch.jit.is_scripting():
  36. return torch.stack([x1, y1, x2, y2], dim=-1)
  37. else:
  38. if torch.is_tensor(bboxes):
  39. return torch.stack([x1, y1, x2, y2], dim=-1)
  40. elif isinstance(bboxes, np.ndarray):
  41. return np.stack([x1, y1, x2, y2], axis=-1)
  42. else:
  43. raise RuntimeError(f"Only Torch tensor or Numpy array is supported. Received bboxes of type {str(type(bboxes))}")
  44. def xyxy_to_xywh_inplace(bboxes, image_shape: Tuple[int, int]):
  45. """
  46. Transforms bboxes inplace from XYXY format to XYWH format. This function operates in-place.
  47. :param bboxes: BBoxes of shape (..., 4) in XYXY format
  48. :return: BBoxes of shape (..., 4) in XYWH format
  49. """
  50. bboxes[..., 2:4] -= bboxes[..., 0:2]
  51. return bboxes
  52. def xywh_to_xyxy_inplace(bboxes, image_shape: Tuple[int, int]):
  53. """
  54. Transforms bboxes from XYWH format to XYXY format
  55. :param bboxes: BBoxes of shape (..., 4) in XYWH format
  56. :return: BBoxes of shape (..., 4) in XYXY format
  57. """
  58. bboxes[..., 2:4] += bboxes[..., 0:2]
  59. return bboxes
  60. class XYWHCoordinateFormat(BoundingBoxFormat):
  61. def __init__(self):
  62. self.format = "xywh"
  63. self.normalized = False
  64. def get_to_xyxy(self, inplace: bool):
  65. if inplace:
  66. return xywh_to_xyxy_inplace
  67. else:
  68. return xywh_to_xyxy
  69. def get_from_xyxy(self, inplace: bool):
  70. if inplace:
  71. return xyxy_to_xywh_inplace
  72. else:
  73. return xyxy_to_xywh
Discard
Tip!

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