From 6bbbc77f9ce14ea172305c627ff8e3da7c82501c Mon Sep 17 00:00:00 2001 From: omerferhatt Date: Fri, 3 May 2024 00:59:47 +0300 Subject: [PATCH 1/4] ViT-B classifier added with first 30 labels --- .../vit-b/nuclio/function-gpu.yaml | 88 +++++++++++++++++++ .../omerferhatt/vit-b/nuclio/function.yaml | 84 ++++++++++++++++++ .../pytorch/omerferhatt/vit-b/nuclio/main.py | 44 ++++++++++ .../omerferhatt/vit-b/nuclio/model_handler.py | 27 ++++++ 4 files changed, 243 insertions(+) create mode 100644 serverless/pytorch/omerferhatt/vit-b/nuclio/function-gpu.yaml create mode 100644 serverless/pytorch/omerferhatt/vit-b/nuclio/function.yaml create mode 100644 serverless/pytorch/omerferhatt/vit-b/nuclio/main.py create mode 100644 serverless/pytorch/omerferhatt/vit-b/nuclio/model_handler.py diff --git a/serverless/pytorch/omerferhatt/vit-b/nuclio/function-gpu.yaml b/serverless/pytorch/omerferhatt/vit-b/nuclio/function-gpu.yaml new file mode 100644 index 000000000000..66b2850c521d --- /dev/null +++ b/serverless/pytorch/omerferhatt/vit-b/nuclio/function-gpu.yaml @@ -0,0 +1,88 @@ +metadata: + name: pth-omerferhatt-vitb + namespace: cvat + annotations: + name: ViT-B + type: detector + framework: pytorch + spec: | + [ + { "id": 0, "name": "tench", "type": "tag" }, + { "id": 1, "name": "goldfish", "type": "tag" }, + { "id": 2, "name": "great white shark", "type": "tag" }, + { "id": 3, "name": "tiger shark", "type": "tag" }, + { "id": 4, "name": "hammerhead shark", "type": "tag" }, + { "id": 5, "name": "electric ray", "type": "tag" }, + { "id": 6, "name": "stingray", "type": "tag" }, + { "id": 7, "name": "cock", "type": "tag" }, + { "id": 8, "name": "hen", "type": "tag" }, + { "id": 9, "name": "ostrich", "type": "tag" }, + { "id": 10, "name": "brambling", "type": "tag" }, + { "id": 11, "name": "goldfinch", "type": "tag" }, + { "id": 12, "name": "house finch", "type": "tag" }, + { "id": 13, "name": "junco", "type": "tag" }, + { "id": 14, "name": "indigo bunting", "type": "tag" }, + { "id": 15, "name": "robin", "type": "tag" }, + { "id": 16, "name": "bulbul", "type": "tag" }, + { "id": 17, "name": "jay", "type": "tag" }, + { "id": 18, "name": "magpie", "type": "tag" }, + { "id": 19, "name": "chickadee", "type": "tag" }, + { "id": 20, "name": "water ouzel", "type": "tag" }, + { "id": 21, "name": "kite", "type": "tag" }, + { "id": 22, "name": "bald eagle", "type": "tag" }, + { "id": 23, "name": "vulture", "type": "tag" }, + { "id": 24, "name": "great grey owl", "type": "tag" }, + { "id": 25, "name": "European fire salamander", "type": "tag" }, + { "id": 26, "name": "common newt", "type": "tag" }, + { "id": 27, "name": "eft", "type": "tag" }, + { "id": 28, "name": "spotted salamander", "type": "tag" }, + { "id": 29, "name": "axolotl", "type": "tag" } + ] +spec: + description: ImageNet-1k pretrained ViT-B + runtime: 'python:3.8' + handler: main:handler + eventTimeout: 30s + build: + image: cvat.pth.omerferhatt.vitb:latest-gpu + baseImage: nvidia/cuda:12.1.0-runtime-ubuntu22.04 + + directives: + preCopy: + - kind: RUN + value: |- + apt update \ + && apt install -y --no-install-recommends \ + wget \ + git \ + ca-certificates \ + python-is-python3 \ + python3 \ + python3-pip \ + && rm -rf /var/lib/apt/lists/* + - kind: WORKDIR + value: /opt/nuclio + - kind: RUN + value: pip install pillow pyyaml + - kind: RUN + value: |- + pip install torch torchvision + + triggers: + myHttpTrigger: + maxWorkers: 1 + kind: 'http' + workerAvailabilityTimeoutMilliseconds: 10000 + attributes: + maxRequestBodySize: 33554432 # 32MB + + resources: + limits: + nvidia.com/gpu: 1 + + platform: + attributes: + restartPolicy: + name: always + maximumRetryCount: 3 + mountMode: volume diff --git a/serverless/pytorch/omerferhatt/vit-b/nuclio/function.yaml b/serverless/pytorch/omerferhatt/vit-b/nuclio/function.yaml new file mode 100644 index 000000000000..125e73538238 --- /dev/null +++ b/serverless/pytorch/omerferhatt/vit-b/nuclio/function.yaml @@ -0,0 +1,84 @@ +metadata: + name: pth-omerferhatt-vitb + namespace: cvat + annotations: + name: ViT-H + type: detector + framework: pytorch + spec: | + [ + { "id": 0, "name": "tench", "type": "tag" }, + { "id": 1, "name": "goldfish", "type": "tag" }, + { "id": 2, "name": "great white shark", "type": "tag" }, + { "id": 3, "name": "tiger shark", "type": "tag" }, + { "id": 4, "name": "hammerhead shark", "type": "tag" }, + { "id": 5, "name": "electric ray", "type": "tag" }, + { "id": 6, "name": "stingray", "type": "tag" }, + { "id": 7, "name": "cock", "type": "tag" }, + { "id": 8, "name": "hen", "type": "tag" }, + { "id": 9, "name": "ostrich", "type": "tag" }, + { "id": 10, "name": "brambling", "type": "tag" }, + { "id": 11, "name": "goldfinch", "type": "tag" }, + { "id": 12, "name": "house finch", "type": "tag" }, + { "id": 13, "name": "junco", "type": "tag" }, + { "id": 14, "name": "indigo bunting", "type": "tag" }, + { "id": 15, "name": "robin", "type": "tag" }, + { "id": 16, "name": "bulbul", "type": "tag" }, + { "id": 17, "name": "jay", "type": "tag" }, + { "id": 18, "name": "magpie", "type": "tag" }, + { "id": 19, "name": "chickadee", "type": "tag" }, + { "id": 20, "name": "water ouzel", "type": "tag" }, + { "id": 21, "name": "kite", "type": "tag" }, + { "id": 22, "name": "bald eagle", "type": "tag" }, + { "id": 23, "name": "vulture", "type": "tag" }, + { "id": 24, "name": "great grey owl", "type": "tag" }, + { "id": 25, "name": "European fire salamander", "type": "tag" }, + { "id": 26, "name": "common newt", "type": "tag" }, + { "id": 27, "name": "eft", "type": "tag" }, + { "id": 28, "name": "spotted salamander", "type": "tag" }, + { "id": 29, "name": "axolotl", "type": "tag" } + ] +spec: + description: ImageNet-1k pretrained ViT-B + runtime: 'python:3.8' + handler: main:handler + eventTimeout: 30s + build: + image: cvat.pth.omerferhatt.vitb + baseImage: ubuntu:22.04 + + directives: + preCopy: + - kind: RUN + value: |- + apt update \ + && apt install -y --no-install-recommends \ + wget \ + git \ + ca-certificates \ + python-is-python3 \ + python3 \ + python3-pip \ + && rm -rf /var/lib/apt/lists/* + - kind: WORKDIR + value: /opt/nuclio + - kind: RUN + value: pip install pillow pyyaml + - kind: RUN + value: |- + pip install torch torchvision --extra-index-url https://download.pytorch.org/whl/cpu + + triggers: + myHttpTrigger: + maxWorkers: 2 + kind: 'http' + workerAvailabilityTimeoutMilliseconds: 10000 + attributes: + maxRequestBodySize: 33554432 # 32MB + + platform: + attributes: + restartPolicy: + name: always + maximumRetryCount: 3 + mountMode: volume diff --git a/serverless/pytorch/omerferhatt/vit-b/nuclio/main.py b/serverless/pytorch/omerferhatt/vit-b/nuclio/main.py new file mode 100644 index 000000000000..4d21e0663bec --- /dev/null +++ b/serverless/pytorch/omerferhatt/vit-b/nuclio/main.py @@ -0,0 +1,44 @@ +import base64 +import io +import json +import yaml + +from model_handler import ModelHandler +from PIL import Image + + +def init_context(context): + context.logger.info("Init context... 0%") + with open("/opt/nuclio/function.yaml", "rb") as function_file: + config_yaml = yaml.load(function_file, Loader=yaml.FullLoader) + + model = ModelHandler() + context.user_data.labels = [ + label["name"] for label in eval(config_yaml["metadata"]["annotations"]["spec"]) + ] + context.user_data.yaml = config_yaml + context.user_data.model = model + context.logger.info("Init context...100%") + +def handler(context, event): + context.logger.info("Run ViT-H model") + data = event.body + buf = io.BytesIO(base64.b64decode(data["image"])) + + image = Image.open(buf).convert("RGB") + class_id, _, score = context.user_data.model.infer(image) + + results = [{ + "confidence": str(score), + "label": context.user_data.labels[class_id], + "type": "tag", + "objectType": "tag", + }] + context.logger.info(f"Results: {results}") + + return context.Response( + body=json.dumps(results), + headers={}, + content_type="application/json", + status_code=200, + ) \ No newline at end of file diff --git a/serverless/pytorch/omerferhatt/vit-b/nuclio/model_handler.py b/serverless/pytorch/omerferhatt/vit-b/nuclio/model_handler.py new file mode 100644 index 000000000000..62be49b97f48 --- /dev/null +++ b/serverless/pytorch/omerferhatt/vit-b/nuclio/model_handler.py @@ -0,0 +1,27 @@ +from typing import Tuple + +import torch +import torchvision as tv +from PIL import Image + + +class ModelHandler: + weights = tv.models.ViT_B_16_Weights.DEFAULT + preprocess = weights.transforms() + + def __init__(self) -> None: + self.model = tv.models.vit_b_16(weights=self.weights) + self.model.eval() + self.device = torch.device("cpu") + if torch.cuda.is_available(): + self.device = torch.device("cuda") + self.model = self.model.to(self.device) + + def infer(self, image: Image) -> Tuple[int, str, float]: + with torch.inference_mode(): + batch = self.preprocess(image).unsqueeze(0).to(self.device) + prediction = self.model(batch).squeeze(0).softmax(0) + class_id = prediction.argmax().item() + score = prediction[class_id].item() + category_name = self.weights.meta["categories"][class_id] + return (class_id, category_name, score) \ No newline at end of file From 766ac2cd597336c939907765f97db89e0a1bddab Mon Sep 17 00:00:00 2001 From: omerferhatt Date: Fri, 3 May 2024 01:01:49 +0300 Subject: [PATCH 2/4] Typo fixes --- serverless/pytorch/omerferhatt/vit-b/nuclio/function.yaml | 2 +- serverless/pytorch/omerferhatt/vit-b/nuclio/main.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/serverless/pytorch/omerferhatt/vit-b/nuclio/function.yaml b/serverless/pytorch/omerferhatt/vit-b/nuclio/function.yaml index 125e73538238..5ff3df14788f 100644 --- a/serverless/pytorch/omerferhatt/vit-b/nuclio/function.yaml +++ b/serverless/pytorch/omerferhatt/vit-b/nuclio/function.yaml @@ -2,7 +2,7 @@ metadata: name: pth-omerferhatt-vitb namespace: cvat annotations: - name: ViT-H + name: ViT-B type: detector framework: pytorch spec: | diff --git a/serverless/pytorch/omerferhatt/vit-b/nuclio/main.py b/serverless/pytorch/omerferhatt/vit-b/nuclio/main.py index 4d21e0663bec..a4b7e5ffe22a 100644 --- a/serverless/pytorch/omerferhatt/vit-b/nuclio/main.py +++ b/serverless/pytorch/omerferhatt/vit-b/nuclio/main.py @@ -21,7 +21,7 @@ def init_context(context): context.logger.info("Init context...100%") def handler(context, event): - context.logger.info("Run ViT-H model") + context.logger.info("Run ViT-B model") data = event.body buf = io.BytesIO(base64.b64decode(data["image"])) From 3c9d0f6c2a702d58244dc3dd9ce286adcb1b1d15 Mon Sep 17 00:00:00 2001 From: omerferhatt Date: Fri, 3 May 2024 01:02:26 +0300 Subject: [PATCH 3/4] EOF added --- serverless/pytorch/omerferhatt/vit-b/nuclio/main.py | 2 +- serverless/pytorch/omerferhatt/vit-b/nuclio/model_handler.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/serverless/pytorch/omerferhatt/vit-b/nuclio/main.py b/serverless/pytorch/omerferhatt/vit-b/nuclio/main.py index a4b7e5ffe22a..ceae26c7d7b4 100644 --- a/serverless/pytorch/omerferhatt/vit-b/nuclio/main.py +++ b/serverless/pytorch/omerferhatt/vit-b/nuclio/main.py @@ -41,4 +41,4 @@ def handler(context, event): headers={}, content_type="application/json", status_code=200, - ) \ No newline at end of file + ) diff --git a/serverless/pytorch/omerferhatt/vit-b/nuclio/model_handler.py b/serverless/pytorch/omerferhatt/vit-b/nuclio/model_handler.py index 62be49b97f48..a9ceccb8b27b 100644 --- a/serverless/pytorch/omerferhatt/vit-b/nuclio/model_handler.py +++ b/serverless/pytorch/omerferhatt/vit-b/nuclio/model_handler.py @@ -24,4 +24,4 @@ def infer(self, image: Image) -> Tuple[int, str, float]: class_id = prediction.argmax().item() score = prediction[class_id].item() category_name = self.weights.meta["categories"][class_id] - return (class_id, category_name, score) \ No newline at end of file + return (class_id, category_name, score) From 5db441ab7c4aca44c4bbc725aeda971bf22b46c6 Mon Sep 17 00:00:00 2001 From: Omer Sarioglu Date: Fri, 3 May 2024 13:42:51 +0300 Subject: [PATCH 4/4] Update serverless/pytorch/omerferhatt/vit-b/nuclio/main.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- serverless/pytorch/omerferhatt/vit-b/nuclio/main.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/serverless/pytorch/omerferhatt/vit-b/nuclio/main.py b/serverless/pytorch/omerferhatt/vit-b/nuclio/main.py index ceae26c7d7b4..a94349944c40 100644 --- a/serverless/pytorch/omerferhatt/vit-b/nuclio/main.py +++ b/serverless/pytorch/omerferhatt/vit-b/nuclio/main.py @@ -23,9 +23,8 @@ def init_context(context): def handler(context, event): context.logger.info("Run ViT-B model") data = event.body - buf = io.BytesIO(base64.b64decode(data["image"])) + image = Image.open(io.BytesIO(base64.b64decode(data["image"]))).convert("RGB") - image = Image.open(buf).convert("RGB") class_id, _, score = context.user_data.model.infer(image) results = [{