Skip to content

Commit fffd010

Browse files
committed
ruff format
1 parent 86d2537 commit fffd010

3 files changed

Lines changed: 22 additions & 24 deletions

File tree

src/model_api/models/anomaly.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,26 @@ def __init__(
7575

7676
def preprocess(self, inputs: np.ndarray) -> list[dict]:
7777
"""Data preprocess method for Anomalib models.
78-
78+
7979
Anomalib models typically expect inputs in [0,1] range as float32.
8080
"""
8181
original_shape = inputs.shape
82-
82+
8383
if self._is_dynamic:
8484
h, w, c = inputs.shape
8585
resized_shape = (w, h, c)
86-
86+
8787
# For anomalib models, convert to float32 and normalize to [0,1] if needed
8888
if inputs.dtype == np.uint8:
8989
processed_image = inputs.astype(np.float32) / 255.0
9090
else:
9191
processed_image = inputs.astype(np.float32)
92-
92+
9393
# Apply layout change but skip InputTransform (which might apply wrong normalization)
9494
processed_image = self._change_layout(processed_image)
9595
else:
9696
resized_shape = (self.w, self.h, self.c)
97-
# For fixed models, use standard preprocessing
97+
# For fixed models, use standard preprocessing
9898
if self.embedded_processing:
9999
processed_image = inputs[None]
100100
else:
@@ -104,15 +104,15 @@ def preprocess(self, inputs: np.ndarray) -> list[dict]:
104104
else:
105105
processed_image = inputs.astype(np.float32)
106106
processed_image = self._change_layout(processed_image)
107-
107+
108108
return [
109109
{self.image_blob_name: processed_image},
110110
{
111111
"original_shape": original_shape,
112112
"resized_shape": resized_shape,
113113
},
114114
]
115-
115+
116116
def postprocess(self, outputs: dict[str, np.ndarray], meta: dict[str, Any]) -> AnomalyResult:
117117
"""Post-processes the outputs and returns the results.
118118
@@ -128,7 +128,7 @@ def postprocess(self, outputs: dict[str, np.ndarray], meta: dict[str, Any]) -> A
128128
pred_mask: np.ndarray | None = None
129129
pred_boxes: np.ndarray | None = None
130130

131-
anomalib_keys = ['pred_score', 'pred_label', 'pred_mask', 'anomaly_map']
131+
anomalib_keys = ["pred_score", "pred_label", "pred_mask", "anomaly_map"]
132132
if not all(key in outputs for key in anomalib_keys):
133133
predictions = outputs[next(iter(self.outputs))]
134134

@@ -149,12 +149,12 @@ def postprocess(self, outputs: dict[str, np.ndarray], meta: dict[str, Any]) -> A
149149

150150
if pred_label == self.labels[0]: # normal
151151
npred_score = 1 - npred_score # Score of normal is 1 - score of anomaly
152-
pred_score=npred_score.item()
152+
pred_score = npred_score.item()
153153
else:
154-
pred_score = outputs['pred_score'].item()
155-
pred_label = str(outputs['pred_label'].item())
156-
anomaly_map = outputs['anomaly_map'].squeeze()
157-
pred_mask = outputs['pred_mask'].squeeze().astype(np.uint8)
154+
pred_score = outputs["pred_score"].item()
155+
pred_label = str(outputs["pred_label"].item())
156+
anomaly_map = outputs["anomaly_map"].squeeze()
157+
pred_mask = outputs["pred_mask"].squeeze().astype(np.uint8)
158158

159159
anomaly_map *= 255
160160
anomaly_map = np.round(anomaly_map).astype(np.uint8)
@@ -173,7 +173,6 @@ def postprocess(self, outputs: dict[str, np.ndarray], meta: dict[str, Any]) -> A
173173
if self.task == "detection":
174174
pred_boxes = self._get_boxes(pred_mask)
175175

176-
177176
return AnomalyResult(
178177
anomaly_map=anomaly_map,
179178
pred_boxes=pred_boxes,

src/model_api/models/image_model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ def __init__(self, inference_adapter: InferenceAdapter, configuration: dict = {}
7373
self.n, self.c, self.h, self.w = self.inputs[self.image_blob_name].shape
7474
else:
7575
self.n, self.h, self.w, self.c = self.inputs[self.image_blob_name].shape
76-
76+
7777
self._is_dynamic = False
7878
if self.h == -1 or self.w == -1 or self.n == -1:
7979
self._is_dynamic = True
8080
if self.n == -1:
8181
self.n = 1
82-
82+
8383
self.resize = RESIZE_TYPES[self.resize_type]
8484
self.input_transform = InputTransform(
8585
self.reverse_input_channels,
@@ -232,7 +232,7 @@ def preprocess(self, inputs: np.ndarray) -> list[dict]:
232232
else:
233233
processed_image = self.input_transform(inputs)
234234
processed_image = self._change_layout(processed_image)
235-
235+
236236
return [
237237
{self.image_blob_name: processed_image},
238238
{

src/model_api/models/model.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ def get_model_class(cls, name: str) -> Type:
125125
if name.lower() == subclass.__model__.lower():
126126
return subclass
127127
return cls.raise_error(
128-
f"There is no model with name {name} in list: "
129-
f"{', '.join([subclass.__model__ for subclass in subclasses])}",
128+
f"There is no model with name {name} in list: {', '.join([subclass.__model__ for subclass in subclasses])}",
130129
)
131130

132131
@classmethod
@@ -209,16 +208,16 @@ def detect_model_type(cls, inference_adapter) -> str:
209208
"""Detects model type on available information"""
210209
input_layers = inference_adapter.get_input_layers()
211210
output_layers = inference_adapter.get_output_layers()
212-
211+
213212
# Check for Anomalib model pattern: 1 input and specific output layer names
214213
if len(input_layers) == 1 and len(output_layers) == 4:
215-
expected_outputs = {'pred_score', 'pred_label', 'anomaly_map', 'pred_mask'}
214+
expected_outputs = {"pred_score", "pred_label", "anomaly_map", "pred_mask"}
216215
actual_outputs = set(output_layers.keys())
217216
if expected_outputs == actual_outputs:
218217
return "AnomalyDetection"
219-
220-
return 'uknown'
221-
218+
219+
return "uknown"
220+
222221
@classmethod
223222
def get_subclasses(cls) -> list[Any]:
224223
"""Retrieves all the subclasses of the model class given."""

0 commit comments

Comments
 (0)