Skip to content

Instantly share code, notes, and snippets.

@wselfjes
Created July 4, 2021 12:52
Show Gist options
  • Save wselfjes/379a604f67d662b775021390b36519c1 to your computer and use it in GitHub Desktop.
Save wselfjes/379a604f67d662b775021390b36519c1 to your computer and use it in GitHub Desktop.
Multifile input in inferoxy
import uuid
import os
from pathlib import Path
import numpy as np
from PIL import Image
import zmq
def make_package(uid, path_to_images):
result_images = []
for f in os.listdir(path_to_images):
result_images.append(np.asarray(Image.open(path_to_images / f)))
result_tensor = np.stack(result_images)
package = {
"source_id": uid,
"model": "multiple_views_embed",
"inputs": [
{
"data": result_tensor,
"parameters": {},
}
],
}
return package
def main():
context = zmq.Context()
input_socket = context.socket(zmq.PUSH)
input_socket.connect("tcp://localhost:7787")
output_socket = context.socket(zmq.DEALER)
uid = str(uuid.uuid4())
output_socket.setsockopt(zmq.IDENTITY, uid.encode("utf-8"))
output_socket.connect("tcp://localhost:7788")
package = make_package(uid, Path("./images/"))
input_socket.send_pyobj(package)
result = output_socket.recv_pyobj()
print(result)
if __name__ == "__main__":
main()
FROM registry.visionhub.ru/models/base:v5 AS base
# ============ BEGIN User model environment ============
FROM ubuntu:18.04
RUN apt-get update && apt-get install -y libsm6 libxext6 libxrender-dev libglib2.0-0 ffmpeg zlib1g-dev libjpeg-dev
RUN apt-get install -y python3-pip
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 1
RUN update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1
RUN pip install -U pip
RUN pip install opencv-python moviepy
WORKDIR app
COPY . .
# ============ END User model environment ============
# ============ BEGIN Vision Hub API ============
COPY --from=base /model_base/requirements.txt /model_base/requirements.txt
# Environment params for runner
# Install python requirements needed to run Vision Hub API
RUN pip install -r /model_base/requirements.txt
COPY --from=base /model_base /model_base
# Run command for docker container
# PARAMS for `container_runner.py`:
# '--dataset_addr' - 'Address of socket to connect to the dataset queue'
# '--dataset_sync_addr' - 'Address of a socket to synchornize with the dataset queue'
# '--result_addr' - 'Address of a socket to connect to the results queue'
# '--result_sync_addr' - 'Address of a socket to synchornize with the results queue'
# '--TEST_MODE' - '1 if needed to run in test mode, 0 otherwise'
CMD python -u /model_base/container_runner.py --dataset_addr=${dataset_addr} \
--result_addr=${result_addr} \
--test_mode=$TEST_MODE --batch_size=$BATCH_SIZE \
# ============ END Vision Hub API ============
from typing import List, Dict, Union
import numpy as np
from loguru import logger
def predict_batch(
samples: List[Dict[str, Union[np.ndarray, str]]], draw: bool = True
) -> np.ndarray:
results = []
# iterate over batch
for sample in samples:
# read input image parameters
images = sample["image"]
logger.info(f"Images tensor shape: {images.shape}")
prediction = {"embedding": np.random.random((1024,))}
# the must have key
result = {"prediction": prediction, "image": None}
# collect results for batch of samples
results.append(result)
return results
2021-07-04 12:22:25.751 | INFO | shared_modules.parse_config:read_config_with_env:37 - Read config for from /model_base/config.yaml
2021-07-04 12:22:25.751 | DEBUG | shared_modules.parse_config:read_config_with_env:40 - Without environment: {'zmq_sndhwm': 100000, 'zmq_rcvhwm': 100000, 'zmq_sndtimeo': 3600000, 'zmq_rcvtimeo': 3600000}
2021-07-04 12:22:25.752 | DEBUG | shared_modules.parse_config:read_config_with_env:44 - Setted with environment: {'zmq_sndhwm': 100000, 'zmq_rcvhwm': 100000, 'zmq_sndtimeo': 3600000, 'zmq_rcvtimeo': 3600000}
2021-07-04 12:22:25.753 | INFO | runner:__init__:38 - Runner inited
2021-07-04 12:22:32.661 | DEBUG | runner:_process_next_batch:81 - Batch received MinimalBatchObject(uid='ba5db9d9-6fdb-4428-9629-6250a55929c8', requests_info=[RequestInfo(input=(2, 4914, 8775, 3), parameters={})], model=ModelObject(name='multiple_views_embed', address='registry.visionhub.ru/models/multiple_views_embed:v5', stateless=False, batch_size=1, run_on_gpu=False), retries=0, status=<Status.SENT_TO_MODEL: 'SENT_TO_MODEL'>, source_id='zmq:50014a66-10ee-4592-953c-0254abd7dd50', created_at=datetime.datetime(2021, 7, 4, 12, 22, 20, 176725), queued_at=datetime.datetime(2021, 7, 4, 12, 22, 20, 728910), started_at=datetime.datetime(2021, 7, 4, 12, 22, 31, 786155), processed_at=None, done_at=None, sent_at=None, debached_at=None)
2021-07-04 12:22:32.666 | INFO | model:predict_batch:36 - Images tensor shape: (2, 4914, 8775, 3)
2021-07-04 12:22:32.668 | INFO | runner:_process_next_batch:104 - [{'prediction': {'embedding': array([0.47339265, 0.82242602, 0.35913685, ..., 0.61270482, 0.18704969,
0.70998982])}, 'image': None}]
2021-07-04 12:22:32.670 | INFO | runner:_process_next_batch:107 - ResponseBatch(uid='ba5db9d9-6fdb-4428-9629-6250a55929c8', requests_info=[RequestInfo(input=(2, 4914, 8775, 3), parameters={})], model=ModelObject(name='multiple_views_embed', address='registry.visionhub.ru/models/multiple_views_embed:v5', stateless=False, batch_size=1, run_on_gpu=False), retries=0, status=<Status.SENT_TO_MODEL: 'SENT_TO_MODEL'>, source_id=None, created_at=datetime.datetime(2021, 7, 4, 12, 22, 20, 176725), queued_at=datetime.datetime(2021, 7, 4, 12, 22, 20, 728910), started_at=datetime.datetime(2021, 7, 4, 12, 22, 31, 786155), processed_at=None, done_at=None, sent_at=datetime.datetime(2021, 7, 4, 12, 22, 20, 728910), debached_at=None, mini_batches=[MiniResponseBatch(responses_info=[ResponseInfo(output={'embedding': '[0.4733...'}, parameters={}, picture=None)])], error=None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment