Skip to content

Add notebook example for SAMBboxToInstanceMask transform #1134

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add SAM OVMS and Triton server Docker image builders
(<https://github.com/openvinotoolkit/datumaro/pull/1129>)
- Add SAMBboxToInstanceMask transform
(<https://github.com/openvinotoolkit/datumaro/pull/1133>)
(<https://github.com/openvinotoolkit/datumaro/pull/1133>, <https://github.com/openvinotoolkit/datumaro/pull/1134>)

### Enhancements
- Remove xfail marks from the convert integration tests
Expand Down
6 changes: 4 additions & 2 deletions docker/segment-anything/Dockerfile.exporter
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ FROM onnx-exporter-${model_type} AS onnx-exporter-final
ARG model_type

ADD scripts/* scripts/
RUN python scripts/export_onnx_encoder.py --checkpoint ckpt.pth --model-type ${model_type} --output /ws/output/encoder.onnx
RUN python scripts/export_onnx_decoder.py --checkpoint ckpt.pth --model-type ${model_type} --return-single-mask --output /ws/output/decoder.onnx
RUN mkdir -p /ws/output/encoder
RUN python scripts/export_onnx_encoder.py --checkpoint ckpt.pth --model-type ${model_type} --output /ws/output/encoder/model.onnx
RUN mkdir -p /ws/output/decoder
RUN python scripts/export_onnx_decoder.py --checkpoint ckpt.pth --model-type ${model_type} --return-single-mask --output /ws/output/decoder/model.onnx
4 changes: 2 additions & 2 deletions docker/segment-anything/Dockerfile.ovms
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ USER root
RUN mkdir -p /models/sam_encoder/1
RUN mkdir -p /models/sam_decoder/1

COPY --from=onnx-exporter /ws/output/encoder.onnx /models/sam_encoder/1/model.onnx
COPY --from=onnx-exporter /ws/output/decoder.onnx /models/sam_decoder/1/model.onnx
COPY --from=onnx-exporter /ws/output/encoder/* /models/sam_encoder/1/
COPY --from=onnx-exporter /ws/output/decoder/* /models/sam_decoder/1/

ADD ovms/model_config_list.json /models/model_config_list.json

Expand Down
4 changes: 2 additions & 2 deletions docker/segment-anything/Dockerfile.triton
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ADD triton/sam_decoder /models/sam_decoder
RUN mkdir -p /models/sam_encoder/1
RUN mkdir -p /models/sam_decoder/1

COPY --from=onnx-exporter /ws/output/encoder.onnx /models/sam_encoder/1/model.onnx
COPY --from=onnx-exporter /ws/output/decoder.onnx /models/sam_decoder/1/model.onnx
COPY --from=onnx-exporter /ws/output/encoder/* /models/sam_encoder/1/
COPY --from=onnx-exporter /ws/output/decoder/* /models/sam_decoder/1/

ENTRYPOINT [ "tritonserver", "--model-repository=/models" ]
25 changes: 12 additions & 13 deletions docker/segment-anything/scripts/export_onnx_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,15 @@ def forward(self, img: torch.Tensor):

output_names = ["image_embeddings"]

with open(args.output, "wb") as f:
torch.onnx.export(
Encoder(model_type=args.model_type, checkpoint=args.checkpoint),
inputs,
f,
export_params=True,
verbose=False,
opset_version=args.opset,
do_constant_folding=True,
input_names=list(inputs.keys()),
output_names=output_names,
dynamic_axes={"img": [2, 3]},
)
torch.onnx.export(
Encoder(model_type=args.model_type, checkpoint=args.checkpoint),
inputs,
args.output,
export_params=True,
verbose=False,
opset_version=args.opset,
do_constant_folding=True,
input_names=list(inputs.keys()),
output_names=output_names,
dynamic_axes={"img": [2, 3]},
)
10 changes: 10 additions & 0 deletions docs/source/docs/jupyter_notebook_examples/transform.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Please refer `here <https://github.com/openvinotoolkit/datumaro/blob/develop/dat

notebooks/05_transform
notebooks/06_tiling
notebooks/18_bbox_to_instance_mask_using_sam

.. grid:: 1 2 2 2
:gutter: 2
Expand All @@ -34,8 +35,17 @@ Please refer `here <https://github.com/openvinotoolkit/datumaro/blob/develop/dat

This transform is known to be useful for detecting small objects in high-resolution input images [1]_.

.. grid-item-card::

.. button-ref:: notebooks/18_bbox_to_instance_mask_using_sam
:color: primary
:outline:
:expand:

This transform uses Segment Anything Model [2]_ to transform bounding box annotations to instance mask annotations.

References
^^^^^^^^^^

.. [1] F., Ozge Unel, Burak O. Ozkalayci, and Cevahir Cigla. "The power of tiling for small object detection." Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition Workshops. 2019.
.. [2] Kirillov, Alexander, et al. "Segment anything." arXiv preprint arXiv:2304.02643 (2023).
329 changes: 329 additions & 0 deletions notebooks/18_bbox_to_instance_mask_using_sam.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/datumaro/plugins/inference_server_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#
# SPDX-License-Identifier: MIT

from datumaro.plugins.inference_server_plugin.base import InferenceServerType
from datumaro.plugins.inference_server_plugin.base import InferenceServerType, ProtocolType
from datumaro.plugins.inference_server_plugin.ovms import OVMSLauncher
from datumaro.plugins.inference_server_plugin.triton import TritonLauncher

__all__ = ["InferenceServerType", "OVMSLauncher", "TritonLauncher"]
__all__ = ["InferenceServerType", "OVMSLauncher", "TritonLauncher", "ProtocolType"]
2 changes: 2 additions & 0 deletions src/datumaro/plugins/sam_transforms/bbox_to_inst_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from datumaro.util import take_by
from datumaro.util.mask_tools import extract_contours

__all__ = ["SAMBboxToInstanceMask"]


class SAMBboxToInstanceMask(ModelTransform, CliPlugin):
"""Convert bounding boxes to instance mask using Segment Anything Model.
Expand Down