Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable direct export of ViTs #3736

Merged
merged 10 commits into from
Aug 2, 2024
17 changes: 10 additions & 7 deletions src/otx/algo/classification/vit.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ def head_forward_fn(self, x: torch.Tensor) -> torch.Tensor:
x = self.model.backbone.norm(x)
if self.model.neck is not None:
x = self.model.neck(x)

# Head
cls_token = x[:, 0]
layer_output = [None, cls_token]
Expand Down Expand Up @@ -140,14 +139,18 @@ def _forward_explain_image_classifier(
scores = pred_results.unbind(0)
labels = logits.argmax(-1, keepdim=True).unbind(0)

return {
outputs = {
"logits": logits,
"feature_vector": feature_vector,
"saliency_map": saliency_map,
"scores": scores,
"labels": labels,
}

if not torch.jit.is_tracing():
outputs["scores"] = scores
outputs["labels"] = labels

return outputs

def get_explain_fn(self) -> Callable:
"""Returns explain function."""
explainer = ViTReciproCAM(
Expand Down Expand Up @@ -352,7 +355,7 @@ def _exporter(self) -> OTXModelExporter:
resize_mode="standard",
pad_value=0,
swap_rgb=False,
via_onnx=True, # NOTE: This should be done via onnx
via_onnx=False,
onnx_export_configuration=None,
output_names=["logits", "feature_vector", "saliency_map"] if self.explain_mode else None,
)
Expand Down Expand Up @@ -588,7 +591,7 @@ def _exporter(self) -> OTXModelExporter:
resize_mode="standard",
pad_value=0,
swap_rgb=False,
via_onnx=True, # NOTE: This should be done via onnx
via_onnx=False,
onnx_export_configuration=None,
output_names=["logits", "feature_vector", "saliency_map"] if self.explain_mode else None,
)
Expand Down Expand Up @@ -763,7 +766,7 @@ def _exporter(self) -> OTXModelExporter:
resize_mode="standard",
pad_value=0,
swap_rgb=False,
via_onnx=True, # NOTE: This should be done via onnx
via_onnx=False,
onnx_export_configuration=None,
output_names=["logits", "feature_vector", "saliency_map"] if self.explain_mode else None,
)
Loading