Last active
February 13, 2024 13:43
-
-
Save hhsecond/dd3b7d50ceaef7a7630853b0e8dd7e65 to your computer and use it in GitHub Desktop.
This script shows how to use RedisAI to store and serve multiple models. It uses redisai-py==0.9.0 and ml2rt as external dependencies
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
############################################################# | |
# Script is using input resources from redisai-example repo # | |
# https://github.com/RedisAI/redisai-examples # | |
# # | |
############################################################# | |
from pathlib import Path | |
import multiprocessing | |
import redisai | |
import ml2rt | |
import numpy as np | |
import json | |
import string | |
con = redisai.Client() | |
# Setting up first model | |
modelpath1 = Path('redisai-examples/models/pytorch/charrnn/charrnn_pipeline.pt') | |
model1 = ml2rt.load_model(modelpath1) | |
con.modelset('model1', 'torch', 'cpu', model1) | |
hidden = np.zeros((2, 1, 300), dtype=np.float32) | |
con.tensorset('hidden', hidden) | |
# Setting up second model and script | |
modelpath2 = Path('redisai-examples/models/pytorch/imagenet/resnet50.pt') | |
scriptpath2 = 'redisai-examples/models/pytorch/imagenet/data_processing_script.txt' | |
class_idx = json.load(open("redisai-examples/data/imagenet_classes.json")) | |
model2 = ml2rt.load_model(modelpath2) | |
script2 = ml2rt.load_script(scriptpath2) | |
con.modelset('model2', 'torch', 'cpu', model2) | |
con.scriptset('script2', 'cpu', script2) | |
def first_model(): | |
con.tensorset('prime', [5], dtype='int64', shape=(1,)) | |
con.modelrun('model1', ['prime', 'hidden'], ['out']) | |
out1 = con.tensorget('out') | |
all_characters = string.printable | |
outstr = ''.join([all_characters[i] for i in out1]) | |
print('==========================================') | |
print(f"Done first model, Output string: {outstr}") | |
print('==========================================') | |
def second_model_with_script(): | |
image = np.random.random((224, 224, 3)).astype(np.uint8) | |
con.tensorset('image', image) | |
con.scriptrun('script2', 'pre_process_3ch', 'image', 'temp1') | |
con.modelrun('model2', 'temp1', 'temp2') | |
con.scriptrun('script2', 'post_process', 'temp2', 'out') | |
out2 = con.tensorget('out') | |
print('==========================================') | |
print(f"Done second model. Output class is: {class_idx[str(out2[0])]}") | |
print('==========================================') | |
if __name__ == '__main__': | |
p1 = multiprocessing.Process(target=first_model) | |
p2 = multiprocessing.Process(target=second_model_with_script) | |
p1.start() | |
p2.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment