-
Notifications
You must be signed in to change notification settings - Fork 793
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Aaron Pham <[email protected]>
- Loading branch information
Showing
11 changed files
with
143 additions
and
12 deletions.
There are no files selected for viewing
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,23 @@ | ||
# MNIST classifier | ||
|
||
This project demonstrates a simple CNN for MNIST classifier served with BentoML. | ||
|
||
### Instruction | ||
|
||
Run training scripts: | ||
|
||
```bash | ||
bazel run :train -- --num-epochs 2 | ||
``` | ||
|
||
Serve with either gRPC or HTTP: | ||
|
||
```bash | ||
bentoml serve-grpc --production --enable-reflection | ||
``` | ||
|
||
Run the test suite: | ||
|
||
```bash | ||
pytest tests | ||
``` |
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 |
---|---|---|
|
@@ -6,4 +6,5 @@ labels: | |
include: | ||
- "*.py" | ||
python: | ||
lock_packages: true | ||
requirements_txt: ./requirements.txt |
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,9 +1,11 @@ | ||
optax>=0.1.3 | ||
flax>=0.6.1 | ||
jax>=0.3.23 | ||
jaxlib>=0.3.22 | ||
jax[cpu] | ||
tensorflow;platform_system!="Darwin" | ||
tensorflow-macos;platform_system=="Darwin" | ||
tensorflow-datasets | ||
cattrs>=22.1.0 | ||
attrs>=22.1.0 | ||
Pillow | ||
pytest | ||
pytest-asyncio |
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,74 @@ | ||
from __future__ import annotations | ||
|
||
import io | ||
from typing import TYPE_CHECKING | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
import bentoml | ||
from bentoml.testing.grpc import create_channel | ||
from bentoml.testing.grpc import async_client_call | ||
|
||
if TYPE_CHECKING: | ||
import jax.numpy as jnp | ||
|
||
|
||
@pytest.fixture() | ||
def img(): | ||
import PIL.Image | ||
|
||
images = {} | ||
digits = list(range(10)) | ||
for digit in digits: | ||
img_path = f"samples/{digit}.png" | ||
with open(img_path, "rb") as f: | ||
img_bytes = f.read() | ||
arr = np.array(PIL.Image.open(io.BytesIO(img_bytes))) | ||
images[digit] = { | ||
"bytes": img_bytes, | ||
"array": arr, | ||
} | ||
|
||
return images | ||
|
||
|
||
@pytest.fixture(name="client") | ||
@pytest.mark.asyncio | ||
async def fixture_client(host: str): | ||
return await bentoml.client.from_url(host) | ||
|
||
|
||
# TODO: update with bentoml.client once | ||
# gRPC client is implemented. | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_image_grpc( | ||
host: str, img: dict[int, dict[str, bytes | jnp.ndarray]], enable_grpc: bool | ||
): | ||
if not enable_grpc: | ||
pytest.skip("Skipping gRPC test when testing on HTTP.") | ||
async with create_channel(host) as channel: | ||
for digit, d in img.items(): | ||
img_bytes = d["bytes"] | ||
await async_client_call( | ||
"predict", | ||
channel=channel, | ||
data={"serialized_bytes": img_bytes}, | ||
assert_data=lambda resp: resp.ndarray.int32_values == [digit], | ||
) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_image_http( | ||
client: bentoml.client.Client, | ||
img: dict[int, dict[str, bytes | jnp.ndarray]], | ||
enable_grpc: bool, | ||
): | ||
if enable_grpc: | ||
pytest.skip("Skipping HTTP test when testing on gRPC.") | ||
for digit, d in img.items(): | ||
img_bytes = d["bytes"] | ||
expected = f"{digit}".encode() | ||
assert await client.predict(img_bytes) == expected |
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
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 @@ | ||
from __future__ import annotations |