Skip to content

Commit

Permalink
[Feature/OTX] Upgrade anomalib version in otx (#1275)
Browse files Browse the repository at this point in the history
* Upgrade anomalib version in otx

* update anomalib version in external

* improve caching in docker + fix nncf keys
  • Loading branch information
ashwinvaidya17 authored Sep 21, 2022
1 parent 020633b commit d7e7618
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 22 deletions.
15 changes: 6 additions & 9 deletions .ci/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,15 @@ FROM python_base_cuda as otx_development_env
# Install all OTX requirements. Serves as a way to cache the requirements

COPY ./requirements/anomaly.txt /tmp/otx/requirements/anomaly.txt
RUN pip install --no-cache-dir -r /tmp/otx/requirements/anomaly.txt

COPY ./requirements/base.txt /tmp/otx/requirements/base.txt
RUN pip install --no-cache-dir -r /tmp/otx/requirements/base.txt

COPY ./requirements/dev.txt /tmp/otx/requirements/dev.txt
RUN pip install --no-cache-dir -r /tmp/otx/requirements/dev.txt

COPY ./requirements/docs.txt /tmp/otx/requirements/docs.txt
RUN pip install --no-cache-dir -r /tmp/otx/requirements/docs.txt

COPY ./requirements/openvino.txt /tmp/otx/requirements/openvino.txt
RUN pip install --no-cache-dir -r /tmp/otx/requirements/openvino.txt

RUN pip install --no-cache-dir -r /tmp/otx/requirements/anomaly.txt \
-r /tmp/otx/requirements/base.txt \
-r /tmp/otx/requirements/dev.txt \
-r /tmp/otx/requirements/docs.txt \
-r /tmp/otx/requirements/openvino.txt

WORKDIR /home/validation
2 changes: 1 addition & 1 deletion external/anomaly/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
anomalib==0.3.5
anomalib==0.3.6
nncf==2.2.0
onnx==1.10.1
openmodelzoo-modelapi @ git+https://github.com/openvinotoolkit/open_model_zoo/@releases/2022/SCv1.1#egg=openmodelzoo-modelapi&subdirectory=demos/common/python
Expand Down
16 changes: 9 additions & 7 deletions otx/algorithms/anomaly/adapters/anomalib/callbacks/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def __init__(self, otx_dataset: DatasetEntity, labels: List[LabelEntity], task_t

def on_predict_epoch_end(self, _trainer: pl.Trainer, pl_module: AnomalyModule, outputs: List[Any]):
"""Call when the predict epoch ends."""
# TODO; refactor Ignore too many locals
# pylint: disable=too-many-locals
outputs = outputs[0]
pred_scores = np.hstack([output["pred_scores"].cpu() for output in outputs])
pred_labels = np.hstack([output["pred_labels"].cpu() for output in outputs])
Expand Down Expand Up @@ -85,11 +87,11 @@ def on_predict_epoch_end(self, _trainer: pl.Trainer, pl_module: AnomalyModule, o
numpy=anomaly_map_to_color_map(anomaly_map.squeeze(), normalize=False),
)
)
logger.info(
"\n\tMin: %.3f, Max: %.3f, Threshold: %.3f, Assigned Label '%s', %.3f",
pl_module.min_max.min.item(),
pl_module.min_max.max.item(),
pl_module.image_threshold.value.item(),
label.name,
pred_score,
log_string = (
f"\n\tThreshold: {pl_module.image_threshold.value.item():.3f},"
f" Assigned Label '{label.name}', {pred_score:.3f}"
)
if hasattr(pl_module, "normalization_metrics"):
for key, value in pl_module.normalization_metrics.state_dict().items():
log_string += f" {key}: {value}"
logger.info(log_string)
4 changes: 2 additions & 2 deletions otx/algorithms/anomaly/tasks/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ def save_model(self, output_model: ModelEntity) -> None:
def _set_metadata(self, output_model: ModelEntity):
output_model.set_data("image_threshold", self.model.image_threshold.value.cpu().numpy().tobytes())
output_model.set_data("pixel_threshold", self.model.pixel_threshold.value.cpu().numpy().tobytes())
output_model.set_data("min", self.model.min_max.min.cpu().numpy().tobytes())
output_model.set_data("max", self.model.min_max.max.cpu().numpy().tobytes())
output_model.set_data("min", self.model.normalization_metrics.state_dict()["min"].cpu().numpy().tobytes())
output_model.set_data("max", self.model.normalization_metrics.state_dict()["max"].cpu().numpy().tobytes())

@staticmethod
def _is_docker() -> bool:
Expand Down
4 changes: 2 additions & 2 deletions otx/algorithms/anomaly/tasks/nncf.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ def load_model(self, otx_model: Optional[ModelEntity]) -> AnomalyModule:
for key in model_data["model"].keys():
if key.startswith("model."):
new_key = key.replace("model.", "")
res = re.search(r"nncf_module\.(\w+)_backbone\.(.*)", new_key)
res = re.search(r"nncf_module\.(\w+)_feature_extractor\.(.*)", new_key)
if res:
new_key = f"nncf_module.{res.group(1)}_model.backbone.{res.group(2)}"
new_key = f"nncf_module.{res.group(1)}_model.feature_extractor.{res.group(2)}"
nncf_modules[new_key] = model_data["model"][key]
else:
pl_modules[key] = model_data["model"][key]
Expand Down
2 changes: 1 addition & 1 deletion requirements/anomaly.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Anomaly Requirements. #
anomalib==0.3.5
anomalib==0.3.6
pytorch-lightning>=1.6.0,<1.7.0

0 comments on commit d7e7618

Please sign in to comment.