Skip to content

Instantly share code, notes, and snippets.

@srijiths
Last active May 13, 2019 15:56
Show Gist options
  • Save srijiths/94b23ef3b1b3650d819dbfd917d37d28 to your computer and use it in GitHub Desktop.
Save srijiths/94b23ef3b1b3650d819dbfd917d37d28 to your computer and use it in GitHub Desktop.
import redisai as rai
from PIL import Image
import numpy as np
import cv2
from datetime import datetime
def letter_box(numpy_image, height):
shape = numpy_image.shape[:2]
ratio = float(height) / max(shape)
new_shape = (int(round(shape[1] * ratio)), int(round(shape[0] * ratio)))
dw = (height - new_shape[0]) / 2
dh = (height - new_shape[1]) / 2
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
numpy_image = cv2.resize(numpy_image, new_shape, interpolation=cv2.INTER_AREA)
numpy_image = cv2.copyMakeBorder(numpy_image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=(127.5, 127.5, 127.5))
#return numpy_image
return np.expand_dims(numpy_image, axis=0)
def post_process(classes, boxes, shapes):
pad_x = max(shapes[0] - shapes[1], 0) * (new_shape / max(shapes))
pad_y = max(shapes[1] - shapes[0], 0) * (new_shape / max(shapes))
unpad_h = new_shape - pad_y
unpad_w = new_shape - pad_x
for ind, class_val in enumerate(classes):
top, left, bottom, right = boxes[ind]
top = ((top.astype('int32') - pad_y // 2) / unpad_h) * shapes[0]
left = ((left.astype('int32') - pad_x // 2) / unpad_w) * shapes[1]
bottom = ((bottom.astype('int32') - pad_y // 2) / unpad_h) * shapes[0]
right = ((right.astype('int32') - pad_x // 2) / unpad_w) * shapes[1]
yield left, top, right, bottom
if __name__ == "__main__":
con = rai.Client(host='localhost', port=8000)
start_time = datetime.now()
with open('script.txt') as f:
script = f.read()
con.scriptset('script', rai.Device.cpu, script)
with open('yolo.pb', 'rb') as f:
model = f.read()
con.modelset('model', rai.Backend.tf, rai.Device.gpu, model,input=['input_1', 'input_image_shape'],
output=['concat_11', 'concat_12', 'concat_13'])
print('Milliseconds taken to load model set {} '.format((datetime.now() - start_time).microseconds/1000))
start_time = datetime.now()
new_shape = 416
input_shape = rai.Tensor(rai.DType.float, shape=[2], value=[new_shape, new_shape])
con.tensorset('input_shape', input_shape)
for i in range(10000):
image_path = './demo_data/dog.jpg'
pil_image = Image.open(image_path)
numpy_img = np.array(pil_image, dtype='float32')
print('raw image shape and dtype before pre-processing', numpy_img.shape, numpy_img.dtype)
image = letter_box(numpy_img, new_shape)
print('shape and dtype before creating Blobtensor ', image.shape, image.dtype)
image = rai.BlobTensor.from_numpy(image)
print('shape and dtype after creating Blobtensor :', image.shape)
con.tensorset('image', image)
con.scriptrun('script', 'pre_process', input=['image'], output=['normalized_image'])
con.modelrun('model',input=['normalized_image', 'input_shape'],
output=['boxes', 'scores', 'classes'])
boxes = con.tensorget('boxes', as_type=rai.BlobTensor).to_numpy()
classes = con.tensorget('classes', as_type=rai.BlobTensor).to_numpy()
scores = con.tensorget('scores', as_type=rai.BlobTensor).to_numpy()
print('classes :', classes)
print('Milliseconds taken to predict {} instances '.format((datetime.now()-start_time).microseconds/1000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment