code-loader 1.0.33__tar.gz → 1.0.38.dev6__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: code-loader
3
- Version: 1.0.33
3
+ Version: 1.0.38.dev6
4
4
  Summary:
5
5
  Home-page: https://github.com/tensorleap/code-loader
6
6
  License: MIT
@@ -4,9 +4,9 @@ from typing import Any, Callable, List, Optional, Dict, Union, Type
4
4
  import numpy as np
5
5
  import numpy.typing as npt
6
6
 
7
- from code_loader.contract.enums import DataStateType, DataStateEnum, LeapDataType, ConfusionMatrixValue
7
+ from code_loader.contract.enums import DataStateType, DataStateEnum, LeapDataType, ConfusionMatrixValue, MetricDirection
8
8
  from code_loader.contract.visualizer_classes import LeapImage, LeapText, LeapGraph, LeapHorizontalBar, \
9
- LeapTextMask, LeapImageMask, LeapImageWithBBox
9
+ LeapTextMask, LeapImageMask, LeapImageWithBBox, LeapImageWithHeatmap
10
10
 
11
11
  custom_latent_space_attribute = "custom_latent_space"
12
12
 
@@ -50,11 +50,12 @@ VisualizerCallableInterface = Union[
50
50
  Callable[..., LeapHorizontalBar],
51
51
  Callable[..., LeapImageMask],
52
52
  Callable[..., LeapTextMask],
53
- Callable[..., LeapImageWithBBox]
53
+ Callable[..., LeapImageWithBBox],
54
+ Callable[..., LeapImageWithHeatmap]
54
55
  ]
55
56
 
56
57
  VisualizerCallableReturnType = Union[LeapImage, LeapText, LeapGraph, LeapHorizontalBar,
57
- LeapImageMask, LeapTextMask, LeapImageWithBBox]
58
+ LeapImageMask, LeapTextMask, LeapImageWithBBox, LeapImageWithHeatmap]
58
59
 
59
60
  CustomCallableInterface = Callable[..., Any]
60
61
 
@@ -87,6 +88,11 @@ class MetricHandler:
87
88
  name: str
88
89
  function: Union[CustomCallableInterfaceMultiArgs, ConfusionMatrixCallableInterfaceMultiArgs]
89
90
  arg_names: List[str]
91
+ direction: Optional[MetricDirection] = MetricDirection.Downward
92
+
93
+ @dataclass
94
+ class RawInputsForHeatmap:
95
+ raw_input_by_vizualizer_arg_name: Dict[str, npt.NDArray[np.float32]]
90
96
 
91
97
 
92
98
  @dataclass
@@ -124,7 +130,7 @@ class MetadataHandler:
124
130
  class PredictionTypeHandler:
125
131
  name: str
126
132
  labels: List[str]
127
- channel_dim: Optional[int] = -1
133
+ channel_dim: int
128
134
 
129
135
 
130
136
  @dataclass
@@ -26,6 +26,12 @@ class LeapDataType(Enum):
26
26
  ImageMask = 'ImageMask'
27
27
  TextMask = 'TextMask'
28
28
  ImageWithBBox = 'ImageWithBBox'
29
+ ImageWithHeatmap = 'ImageWithHeatmap'
30
+
31
+
32
+ class MetricDirection(Enum):
33
+ Upward = "Upward"
34
+ Downward = "Downward"
29
35
 
30
36
 
31
37
  class DatasetMetadataType(Enum):
@@ -63,7 +63,7 @@ class CustomLayerInstance:
63
63
  class PredictionTypeInstance:
64
64
  name: str
65
65
  labels: List[str]
66
- channel_dim: Optional[int] = -1
66
+ channel_dim: int
67
67
 
68
68
 
69
69
  @dataclass
@@ -103,6 +103,7 @@ class BoundingBox:
103
103
  height: float # value between [0, 1], represent the percentage according to the image size.
104
104
  confidence: float
105
105
  label: str
106
+ rotation: float = 0.0 # value between [0, 360], represent the degree of rotation.
106
107
 
107
108
 
108
109
  @dataclass
@@ -135,6 +135,24 @@ class LeapTextMask:
135
135
  validate_type(type(label), str)
136
136
 
137
137
 
138
+ @dataclass
139
+ class LeapImageWithHeatmap:
140
+ image: npt.NDArray[np.float32]
141
+ heatmaps: npt.NDArray[np.float32]
142
+ labels: List[str]
143
+ type: LeapDataType = LeapDataType.ImageWithHeatmap
144
+
145
+ def __post_init__(self) -> None:
146
+ validate_type(self.type, LeapDataType.ImageWithHeatmap)
147
+ validate_type(type(self.heatmaps), np.ndarray)
148
+ validate_type(self.heatmaps.dtype, np.float32)
149
+ validate_type(type(self.image), np.ndarray)
150
+ validate_type(self.image.dtype, np.float32)
151
+ validate_type(type(self.labels), list)
152
+ for label in self.labels:
153
+ validate_type(type(label), str)
154
+
155
+
138
156
  map_leap_data_type_to_visualizer_class = {
139
157
  LeapDataType.Image.value: LeapImage,
140
158
  LeapDataType.Graph.value: LeapGraph,
@@ -142,5 +160,6 @@ map_leap_data_type_to_visualizer_class = {
142
160
  LeapDataType.HorizontalBar.value: LeapHorizontalBar,
143
161
  LeapDataType.ImageMask.value: LeapImageMask,
144
162
  LeapDataType.TextMask.value: LeapTextMask,
145
- LeapDataType.ImageWithBBox.value: LeapImageWithBBox
163
+ LeapDataType.ImageWithBBox.value: LeapImageWithBBox,
164
+ LeapDataType.ImageWithHeatmap.value: LeapImageWithHeatmap
146
165
  }
@@ -9,15 +9,15 @@ from code_loader.contract.datasetclasses import SectionCallableInterface, InputH
9
9
  PreprocessHandler, VisualizerCallableInterface, CustomLossHandler, CustomCallableInterface, PredictionTypeHandler, \
10
10
  MetadataSectionCallableInterface, UnlabeledDataPreprocessHandler, CustomLayerHandler, MetricHandler, \
11
11
  CustomCallableInterfaceMultiArgs, ConfusionMatrixCallableInterfaceMultiArgs, VisualizerCallableReturnType, \
12
- CustomMultipleReturnCallableInterfaceMultiArgs, DatasetBaseHandler, custom_latent_space_attribute
13
- from code_loader.contract.enums import LeapDataType, DataStateEnum, DataStateType
12
+ CustomMultipleReturnCallableInterfaceMultiArgs, DatasetBaseHandler, custom_latent_space_attribute, RawInputsForHeatmap
13
+ from code_loader.contract.enums import LeapDataType, DataStateEnum, DataStateType, MetricDirection
14
14
  from code_loader.contract.responsedataclasses import DatasetTestResultPayload
15
15
  from code_loader.contract.visualizer_classes import map_leap_data_type_to_visualizer_class
16
16
  from code_loader.utils import to_numpy_return_wrapper, get_shape
17
17
  from code_loader.visualizers.default_visualizers import DefaultVisualizer, \
18
18
  default_graph_visualizer, \
19
19
  default_image_visualizer, default_horizontal_bar_visualizer, default_word_visualizer, \
20
- default_image_mask_visualizer, default_text_mask_visualizer, default_raw_data_visualizer
20
+ default_image_mask_visualizer, default_text_mask_visualizer, default_raw_data_visualizer, default_image_heatmap_visualizer
21
21
 
22
22
 
23
23
  class LeapBinder:
@@ -31,6 +31,8 @@ class LeapBinder:
31
31
  def _extend_with_default_visualizers(self) -> None:
32
32
  self.set_visualizer(function=default_image_visualizer, name=DefaultVisualizer.Image.value,
33
33
  visualizer_type=LeapDataType.Image)
34
+ self.set_visualizer(function=default_image_heatmap_visualizer, name=DefaultVisualizer.ImageHeatmap.value,
35
+ visualizer_type=LeapDataType.ImageWithHeatmap)
34
36
  self.set_visualizer(function=default_graph_visualizer, name=DefaultVisualizer.Graph.value,
35
37
  visualizer_type=LeapDataType.Graph)
36
38
  self.set_visualizer(function=default_raw_data_visualizer, name=DefaultVisualizer.RawData.value,
@@ -50,10 +52,16 @@ class LeapBinder:
50
52
  heatmap_visualizer: Optional[Callable[..., npt.NDArray[np.float32]]] = None) -> None:
51
53
  arg_names = inspect.getfullargspec(function)[0]
52
54
  if heatmap_visualizer:
53
- if arg_names != inspect.getfullargspec(heatmap_visualizer)[0]:
54
- raise Exception(
55
- f'The argument names of the heatmap visualizer callback must match the visualizer callback '
56
- f'{str(arg_names)}')
55
+ visualizer_arg_names_set = set(arg_names)
56
+ heatmap_visualizer_inspection = inspect.getfullargspec(heatmap_visualizer)
57
+ heatmap_arg_names_set = set(heatmap_visualizer_inspection[0])
58
+ if visualizer_arg_names_set != heatmap_arg_names_set:
59
+ arg_names_difference = set(inspect.getfullargspec(heatmap_visualizer)[0]).difference(set(arg_names))
60
+ if len(arg_names_difference) != 1 or \
61
+ heatmap_visualizer_inspection.annotations[list(arg_names_difference)[0]] != RawInputsForHeatmap:
62
+ raise Exception(
63
+ f'The argument names of the heatmap visualizer callback must match the visualizer callback '
64
+ f'{str(arg_names)}')
57
65
 
58
66
  if visualizer_type.value not in map_leap_data_type_to_visualizer_class:
59
67
  raise Exception(
@@ -103,9 +111,10 @@ class LeapBinder:
103
111
  function: Union[CustomCallableInterfaceMultiArgs,
104
112
  CustomMultipleReturnCallableInterfaceMultiArgs,
105
113
  ConfusionMatrixCallableInterfaceMultiArgs],
106
- name: str) -> None:
114
+ name: str,
115
+ direction: Optional[MetricDirection] = MetricDirection.Downward) -> None:
107
116
  arg_names = inspect.getfullargspec(function)[0]
108
- self.setup_container.metrics.append(MetricHandler(name, function, arg_names))
117
+ self.setup_container.metrics.append(MetricHandler(name, function, arg_names, direction))
109
118
 
110
119
  def add_prediction(self, name: str, labels: List[str], channel_dim: int = -1) -> None:
111
120
  self.setup_container.prediction_types.append(PredictionTypeHandler(name, labels, channel_dim))
@@ -4,7 +4,7 @@ import numpy as np
4
4
  import numpy.typing as npt
5
5
 
6
6
  from code_loader.contract.visualizer_classes import LeapImage, LeapGraph, LeapHorizontalBar, LeapText, \
7
- LeapImageMask, LeapTextMask
7
+ LeapImageMask, LeapTextMask, LeapImageWithHeatmap
8
8
  from code_loader.utils import rescale_min_max
9
9
 
10
10
 
@@ -16,13 +16,23 @@ class DefaultVisualizer(Enum):
16
16
  ImageMask = 'ImageMask'
17
17
  TextMask = 'TextMask'
18
18
  RawData = 'RawData'
19
-
19
+ ImageHeatmap = 'ImageHeatmap'
20
+
20
21
 
21
22
  def default_image_visualizer(data: npt.NDArray[np.float32]) -> LeapImage:
22
23
  rescaled_data = rescale_min_max(data)
23
24
  return LeapImage(rescaled_data)
24
25
 
25
26
 
27
+ def default_image_heatmap_visualizer(image: npt.NDArray[np.float32], heatmaps: npt.NDArray[np.float32], labels: list) -> LeapImageWithHeatmap:
28
+ rescaled_image = rescale_min_max(image)
29
+ rescaled_heatmaps = []
30
+ for heatmap in heatmaps:
31
+ rescaled_heatmap = rescale_min_max(heatmap)
32
+ rescaled_heatmaps = np.append(rescaled_heatmaps, rescaled_heatmap)
33
+ return LeapImageWithHeatmap(rescaled_image, rescaled_heatmaps, labels)
34
+
35
+
26
36
  def default_graph_visualizer(data: npt.NDArray[np.float32]) -> LeapGraph:
27
37
  return LeapGraph(data)
28
38
 
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "code-loader"
3
- version = "1.0.33"
3
+ version = "1.0.38.dev6"
4
4
  description = ""
5
5
  authors = ["dorhar <doron.harnoy@tensorleap.ai>"]
6
6
  license = "MIT"
File without changes