-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support gzip & zstd compression (#599)
* feat: support gzip & zstd compression Signed-off-by: Keming <[email protected]> * fix args help Signed-off-by: Keming <[email protected]> * add compression example Signed-off-by: Keming <[email protected]> --------- Signed-off-by: Keming <[email protected]>
- Loading branch information
Showing
14 changed files
with
323 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "mosec" | ||
version = "0.8.9" | ||
version = "0.9.0" | ||
authors = ["Keming <[email protected]>", "Zichen <[email protected]>"] | ||
edition = "2021" | ||
license = "Apache-2.0" | ||
|
@@ -25,3 +25,5 @@ serde = "1.0" | |
serde_json = "1.0" | ||
utoipa = "5" | ||
utoipa-swagger-ui = { version = "8", features = ["axum"] } | ||
tower = "0.5.1" | ||
tower-http = {version = "0.6.1", features = ["compression-zstd", "decompression-zstd", "compression-gzip", "decompression-gzip"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Compression | ||
|
||
This example demonstrates how to use the `--compression` feature for segmentation tasks. We use the example from the [Segment Anything Model 2](https://github.com/facebookresearch/sam2/blob/main/notebooks/image_predictor_example.ipynb). The request includes an image and its low resolution mask, the response is the final mask. Since there are lots of duplicate values in the mask, we can use `gzip` or `zstd` to compress it. | ||
|
||
## Server | ||
|
||
```shell | ||
python examples/segment/server.py --compression | ||
``` | ||
|
||
<details> | ||
<summary>segment.py</summary> | ||
|
||
```{include} ../../../examples/segment/server.py | ||
:code: python | ||
``` | ||
|
||
</details> | ||
|
||
## Client | ||
|
||
```shell | ||
python examples/segment/client.py | ||
``` | ||
|
||
<details> | ||
<summary>segment.py</summary> | ||
|
||
```{include} ../../../examples/segment/client.py | ||
:code: python | ||
``` | ||
|
||
</details> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Copyright 2023 MOSEC Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import gzip | ||
from http import HTTPStatus | ||
from io import BytesIO | ||
|
||
import httpx | ||
import msgpack # type: ignore | ||
import numbin | ||
import numpy as np | ||
from PIL import Image # type: ignore | ||
|
||
truck_image = Image.open( | ||
BytesIO( | ||
httpx.get( | ||
"https://raw.githubusercontent.com/facebookresearch/sam2/main/notebooks/images/truck.jpg" | ||
).content | ||
) | ||
) | ||
array = np.array(truck_image.convert("RGB")) | ||
# assume we have obtains the low resolution mask from the previous step | ||
mask = np.zeros((256, 256)) | ||
|
||
resp = httpx.post( | ||
"http://127.0.0.1:8000/inference", | ||
content=gzip.compress( | ||
msgpack.packb( # type: ignore | ||
{ | ||
"image": numbin.dumps(array), | ||
"mask": numbin.dumps(mask), | ||
"labels": [1, 1], | ||
"point_coords": [[500, 375], [1125, 625]], | ||
} | ||
) | ||
), | ||
headers={"Accept-Encoding": "gzip", "Content-Encoding": "gzip"}, | ||
) | ||
assert resp.status_code == HTTPStatus.OK, resp.status_code | ||
res = numbin.loads(msgpack.loads(resp.content)) | ||
assert res.shape == array.shape[:2], f"expect {array.shape[:2]}, got {res.shape}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Copyright 2023 MOSEC Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# refer to https://github.com/facebookresearch/sam2/blob/main/notebooks/image_predictor_example.ipynb | ||
|
||
import numbin | ||
import torch # type: ignore | ||
from sam2.sam2_image_predictor import SAM2ImagePredictor # type: ignore | ||
|
||
from mosec import Server, Worker, get_logger | ||
from mosec.mixin import MsgpackMixin | ||
|
||
logger = get_logger() | ||
MIN_TF32_MAJOR = 8 | ||
|
||
|
||
class SegmentAnything(MsgpackMixin, Worker): | ||
def __init__(self): | ||
# select the device for computation | ||
if torch.cuda.is_available(): | ||
device = torch.device("cuda") | ||
elif torch.backends.mps.is_available(): | ||
device = torch.device("mps") | ||
else: | ||
device = torch.device("cpu") | ||
logger.info("using device: %s", device) | ||
|
||
self.predictor = SAM2ImagePredictor.from_pretrained( | ||
"facebook/sam2-hiera-large", device=device | ||
) | ||
|
||
if device.type == "cuda": | ||
# use bfloat16 | ||
torch.autocast("cuda", dtype=torch.bfloat16).__enter__() | ||
# turn on tf32 for Ampere GPUs (https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices) | ||
if torch.cuda.get_device_properties(0).major >= MIN_TF32_MAJOR: | ||
torch.backends.cuda.matmul.allow_tf32 = True | ||
torch.backends.cudnn.allow_tf32 = True | ||
|
||
def forward(self, data: dict) -> bytes: | ||
with torch.inference_mode(): | ||
self.predictor.set_image(numbin.loads(data["image"])) | ||
masks, _, _ = self.predictor.predict( | ||
point_coords=data["point_coords"], | ||
point_labels=data["labels"], | ||
mask_input=numbin.loads(data["mask"])[None, :, :], | ||
multimask_output=False, | ||
) | ||
return numbin.dumps(masks[0]) | ||
|
||
|
||
if __name__ == "__main__": | ||
server = Server() | ||
server.append_worker(SegmentAnything, num=1, max_batch_size=1) | ||
server.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ ruff>=0.7 | |
pre-commit>=2.15.0 | ||
httpx[http2]==0.27.2 | ||
httpx-sse==0.4.0 | ||
zstandard~=0.23 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.