def run(): input_api = InputQueue() base_path = os.path.abspath( __file__ + "/../../../test/zoo/resources/serving_quick_start") if not base_path: raise EOFError("You have to set your image path") output_api = OutputQueue() output_api.dequeue() path = os.listdir(base_path) for p in path: if not p.endswith("jpeg"): continue img = cv2.imread(os.path.join(base_path, p)) img = cv2.resize(img, (224, 224)) input_api.enqueue_image(p, img) time.sleep(5) # get all result and dequeue result = output_api.dequeue() for k in result.keys(): output = "image: " + k + ", classification-result:" tmp_dict = json.loads(result[k]) for class_idx in tmp_dict.keys(): output += "class: " + class_idx + "'s prob: " + tmp_dict[class_idx] print(output)
def run(path): input_api = InputQueue() base_path = path if not base_path: raise EOFError("You have to set your image path") output_api = OutputQueue() output_api.dequeue() path = os.listdir(base_path) for p in path: if not p.endswith("jpeg"): continue img = cv2.imread(os.path.join(base_path, p)) img = cv2.resize(img, (224, 224)) data = cv2.imencode(".jpg", img)[1] img_encoded = base64.b64encode(data).decode("utf-8") input_api.enqueue(p, t={"b64": img_encoded}) time.sleep(10) # get all result and dequeue result = output_api.dequeue() for k in result.keys(): output = "image: " + k + ", classification-result:" tmp_list = json.loads(result[k]) for record in range(len(tmp_list)): output += " class: " + str(tmp_list[record][0]) \ + "'s prob: " + str(tmp_list[record][1]) print(output)
def run(path): input_api = InputQueue() base_path = path if not base_path: raise EOFError("You have to set your image path") output_api = OutputQueue() output_api.dequeue() path = [os.path.join(base_path, "cat1.jpeg")] for p in path: if not p.endswith("jpeg"): continue img = cv2.imread(p) img = cv2.resize(img, (224, 224)) data = cv2.imencode(".jpg", img)[1] img_encoded = base64.b64encode(data).decode("utf-8") result = input_api.enqueue("cat", t={"b64": img_encoded}) time.sleep(10) cat_image_prediction = output_api.query("cat") print("cat prediction layer shape: ", cat_image_prediction.shape) class_idx = np.argmax(cat_image_prediction) print("the class index of prediction of cat image result: ", class_idx) # get all result and dequeue result = output_api.dequeue() for k in result.keys(): prediction = output_api.get_ndarray_from_b64(result[k]) # this prediction is the same as the cat_image_prediction above print(k, "prediction layer shape: ", prediction.shape)
def test_input_queue_with_frontend(self): input_api = InputQueue(host="1.1.1.1", port="1111", name="my-test", frontend_url="1.1.1.1:1") input_api2 = InputQueue(frontend_url="1.1.1.1:1") assert input_api.name == "my-test" assert input_api.host == "1.1.1.1" assert input_api.port == "1111" assert input_api.frontend_url == "1.1.1.1:1" assert input_api2.frontend_url == "1.1.1.1:1"
def run(): input_api = InputQueue() output_api = OutputQueue() output_api.dequeue() import numpy as np a = np.array([1, 2]) input_api.enqueue('a', p=a) time.sleep(5) print(output_api.query('a'))
def image_enqueue(redis_args, img_num, proc_num, path, encrypt): print("Entering enqueue") input_api = InputQueue(**redis_args) img = cv2.imread(path) img = cv2.resize(img, (224, 224)) data = cv2.imencode(".jpg", img)[1] img_encoded = base64.b64encode(data).decode("utf-8") if encrypt: img_encoded = encrypt_with_AES_GCM(img_encoded, "secret", "salt") print("Record encoded") img_per_proc = int(img_num / proc_num) procs = [] def push_image(image_num, index, proc_id): print("Entering enqueue", proc_id) for i in range(image_num): input_api.enqueue("my-img-" + str(i + index), t={"b64": img_encoded}) start = time.time() for i in range(proc_num): proc = Process(target=push_image, args=(img_per_proc, i * img_per_proc, i,)) procs.append(proc) proc.start() for p in procs: p.join() end = time.time() print(img_num, "images enqueued") print("total enqueue time:", end - start) fps = img_num / (end - start) print("enqueue fps:", fps) return start
class Predict(ScalarFunction): def __init__(self): super().__init__() self._input_api = None def open(self, function_context: FunctionContext): self._input_api = InputQueue( host="localhost", port="6379", sync=True, frontend_url="http://127.0.0.1:10020") def eval(self, feature_data): with open('/root/inference', 'a') as f: try: feature_samples = [] for feature_element in feature_data.split(' '): feature_samples.append(float(feature_element)) request_instances = { 'instances': [{ 'ids': numpy.array(feature_samples).tolist() }] } # Cluster serving predict sync API response = self._input_api.predict( json.dumps(request_instances)) prediction = ' '.join( response.replace('[', '').replace(']', '').split(',')) f.write('sssssssss' + prediction) f.write('\n') return prediction except Exception: return ''
def test_default_config(self): input_api = InputQueue() output_api = OutputQueue() assert input_api.name == "serving_stream" assert input_api.host == "localhost" assert input_api.port == "6379" assert output_api.name == "serving_stream" assert output_api.host == "localhost" assert output_api.port == "6379"
# # Copyright 2018 Analytics Zoo Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from zoo.serving.client import InputQueue, OutputQueue import numpy as np # input tensor x = np.array([2, 3], dtype=np.float32) input_api = InputQueue() input_api.enqueue_tensor('my_input', input=x) output_api = OutputQueue() result = output_api.query('my_input') print("Result is :" + str(result))
def test_encode(self): input_api = InputQueue() b64 = input_api.data_to_b64(t1=np.array([1, 2]), t2=np.array([3, 4])) byte = base64.b64decode(b64)
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from zoo.serving.client import InputQueue, OutputQueue import os import cv2 import json import time if __name__ == "__main__": input_api = InputQueue() base_path = "../../test/zoo/resources/serving_quick_start" if not base_path: raise EOFError("You have to set your image path") output_api = OutputQueue() output_api.dequeue() path = os.listdir(base_path) for p in path: if not p.endswith("jpeg"): continue img = cv2.imread(os.path.join(base_path, p)) img = cv2.resize(img, (224, 224)) input_api.enqueue_image(p, img)
def open(self, function_context: FunctionContext): self._input_api = InputQueue( host="localhost", port="6379", sync=True, frontend_url="http://127.0.0.1:10020")
def test_input_queue_without_frontend(self): input_api = InputQueue(host="1.1.1.1", port="1111", name="my-test") assert input_api.name == "my-test" assert input_api.host == "1.1.1.1" assert input_api.port == "1111" assert not input_api.frontend_url
help="The path of images you want to validate") parser.add_option("--img_num", type=int, dest="img_num", help="The total number of images you validate") (options, args) = parser.parse_args(sys.argv) val_img_path = options.img_path total_img_num = options.img_num val_txt = os.path.join(val_img_path, "val.txt") path_list = os.listdir(val_img_path) input_api = InputQueue() # push image in queue top1_dict = {} top5_dict = {} # for p in path_list: # if not p.endswith(format_wanted): # continue # img = cv2.imread(os.path.join(val_img_path, p)) # img = cv2.resize(img, (224, 224)) # redis_queue.enqueue_image(p, img) piece_len = int(len(path_list) / 8) s = b = 0 procs = [] for i in range(8): a = s
# # Copyright 2018 Analytics Zoo Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from zoo.serving.client import InputQueue, OutputQueue import numpy as np # input tensor x = np.array([2, 3], dtype=np.float32) input_api = InputQueue() input_api.enqueue('my_input', input=x) output_api = OutputQueue() result = output_api.query('my_input') print("Result is :" + str(result))