-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
178 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# downloader stage is used to obtain the model code from provate repo + model weights from Zenodo | ||
FROM ubuntu AS downloader | ||
WORKDIR /work | ||
RUN apt-get update && apt-get install --no-install-recommends -y ca-certificates wget unzip | ||
|
||
RUN mkdir /app | ||
# downloading pretrained weights from Zenodo | ||
RUN wget https://zenodo.org/record/6663662/files/head_swin_bnneck.zip -O /app/head_swin_bnneck.zip | ||
RUN unzip /app/head_swin_bnneck.zip -d /app/head_swin_bnneck | ||
RUN rm /app/head_swin_bnneck.zip | ||
# last.ckpt is the same as model.ckpt, so deleting it to save image space | ||
RUN rm /app/head_swin_bnneck/last.ckpt | ||
|
||
FROM python:3.9-slim AS FINAL | ||
|
||
# installing openCV dependencies | ||
RUN apt-get update && apt-get install -y --no-install-recommends ffmpeg libsm6 libxext6 && rm -rf /var/lib/apt/lists/* | ||
|
||
WORKDIR /app | ||
COPY requirements.txt /requirements.txt | ||
|
||
# --extra-index-url https://download.pytorch.org/whl/cpu avoids CUDA installation | ||
RUN python -m pip install --upgrade pip && pip install --extra-index-url https://download.pytorch.org/whl/cpu -r /requirements.txt | ||
COPY --from=downloader /app . | ||
|
||
ENV KAFKA_URL=kafka:9092 | ||
ENV INPUT_QUEUE=kashtanka_calvin_zhirui_yolov5_output | ||
ENV OUTPUT_QUEUE=kashtanka_calvin_zhirui_embeddings_output | ||
CMD python3 serve.py | ||
COPY code . | ||
|
||
FROM FINAL as TESTS | ||
COPY example /app/example | ||
RUN python -m unittest discover -v | ||
|
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 was deleted.
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import os | ||
import infer | ||
|
||
import kafkajobs | ||
from infer import process_job | ||
|
||
kafkaUrl = os.environ['KAFKA_URL'] | ||
inputQueueName = os.environ['INPUT_QUEUE'] | ||
outputQueueName = os.environ['OUTPUT_QUEUE'] | ||
|
||
appName = "zhiru-calvin-head-swin-bnneck-feature-extractor" | ||
|
||
worker = kafkajobs.jobqueue.JobQueueWorker(appName, kafkaBootstrapUrl=kafkaUrl, topicName=inputQueueName, appName=appName) | ||
resultQueue = kafkajobs.jobqueue.JobQueueProducer(kafkaUrl, outputQueueName, appName) | ||
|
||
|
||
device = 'cpu' | ||
model,config = infer.load_pretrained_model("./head_swin_bnneck", device) | ||
preproc_transform = infer.get_infer_transform(config.image_size) | ||
print("model loaded") | ||
|
||
def work(): | ||
print("Service started. Pooling for a job") | ||
while True: | ||
job = worker.GetNextJob(5000) | ||
uid = job["uid"] | ||
|
||
print("Got job {0}".format(uid)) | ||
|
||
out_job = process_job(model,preproc_transform, job) | ||
|
||
resultQueue.Enqueue(uid, out_job) | ||
worker.Commit() | ||
print("{0}: Job processed successfully, results are submited to kafka".format(uid)) | ||
|
||
work() |
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,43 @@ | ||
|
||
import unittest | ||
import json | ||
|
||
import infer | ||
|
||
device = 'cpu' | ||
|
||
model,config = infer.load_pretrained_model("./head_swin_bnneck", device) | ||
#print("Config") | ||
#print(config) | ||
preproc_transform = infer.get_infer_transform(config.image_size) | ||
|
||
class EmbeddingsTest(unittest.TestCase): | ||
def test_embedding_produced(self): | ||
input_shapshot_path = "./example/input_snapshot.json" | ||
input_snapshot = json.load(open(input_shapshot_path)) | ||
|
||
input_image = input_snapshot["yolo5_output"][0]["head"] | ||
|
||
embedding = infer.get_embedding_for_json(model, preproc_transform, input_image) | ||
|
||
print(embedding) | ||
assert embedding.shape[0] == 1 | ||
assert embedding.shape[1] == 1024 | ||
|
||
def test_process_job(self): | ||
input_shapshot_path = "./example/input_snapshot.json" | ||
job = json.load(open(input_shapshot_path)) | ||
|
||
expected_output_path = "./example/expected_output_snapshot.json" | ||
expected_output = json.load(open(expected_output_path)) | ||
|
||
output_job = infer.process_job(model, preproc_transform, job) | ||
|
||
assert json.dumps(expected_output) == json.dumps(output_job) | ||
assert "yolo5_output" not in output_job | ||
|
||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() | ||
print("Self test success") |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.