|
| 1 | +import json |
| 2 | +import os |
| 3 | +import random |
| 4 | +import sys |
| 5 | +import time |
| 6 | +from multiprocessing import Process |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +from skimage.io import imread |
| 10 | +from skimage.transform import resize |
| 11 | + |
| 12 | +try: |
| 13 | + sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../deps/readies")) |
| 14 | + import paella |
| 15 | +except: |
| 16 | + pass |
| 17 | + |
| 18 | +TEST_TF = os.environ.get("TEST_TF") != "0" and os.environ.get("WITH_TF") != "0" |
| 19 | +TEST_TFLITE = os.environ.get("TEST_TFLITE") != "0" and os.environ.get("WITH_TFLITE") != "0" |
| 20 | +TEST_PT = os.environ.get("TEST_PT") != "0" and os.environ.get("WITH_PT") != "0" |
| 21 | +TEST_ONNX = os.environ.get("TEST_ONNX") != "0" and os.environ.get("WITH_ORT") != "0" |
| 22 | +DEVICE = os.environ.get('DEVICE', 'CPU').upper().encode('utf-8', 'ignore').decode('utf-8') |
| 23 | +VALGRIND = os.environ.get("VALGRIND") == "1" |
| 24 | +print(f"Running tests on {DEVICE}\n") |
| 25 | + |
| 26 | + |
| 27 | +def ensureSlaveSynced(con, env, timeout_ms=5000): |
| 28 | + if env.useSlaves: |
| 29 | + # When WAIT returns, all the previous write commands |
| 30 | + # sent in the context of the current connection are |
| 31 | + # guaranteed to be received by the number of replicas returned by WAIT. |
| 32 | + wait_reply = con.execute_command('WAIT', '1', timeout_ms) |
| 33 | + number_replicas = 0 |
| 34 | + try: |
| 35 | + number_replicas = int(wait_reply) |
| 36 | + # does not contain anything convertible to int |
| 37 | + except ValueError as verr: |
| 38 | + pass |
| 39 | + # Exception occurred while converting to int |
| 40 | + except Exception as ex: |
| 41 | + pass |
| 42 | + env.assertTrue(number_replicas >= 1) |
| 43 | + |
| 44 | + |
| 45 | +# Ensures command is sent and forced disconnect |
| 46 | +# after without waiting for the reply to be parsed |
| 47 | +# Usefull for checking behaviour of commands |
| 48 | +# that are run with background threads |
| 49 | +def send_and_disconnect(cmd, red): |
| 50 | + pool = red.connection_pool |
| 51 | + con = pool.get_connection(cmd[0]) |
| 52 | + ret = con.send_command(*cmd) |
| 53 | + con.disconnect() |
| 54 | + return ret |
| 55 | + |
| 56 | + |
| 57 | +def check_cuda(): |
| 58 | + return os.system('which nvcc') |
| 59 | + |
| 60 | + |
| 61 | +def info_to_dict(info): |
| 62 | + info = [el.decode('utf-8') if type(el) is bytes else el for el in info] |
| 63 | + return dict(zip(info[::2], info[1::2])) |
| 64 | + |
| 65 | + |
| 66 | +def load_mobilenet_test_data(): |
| 67 | + test_data_path = os.path.join(os.path.dirname(__file__), 'test_data') |
| 68 | + labels_filename = os.path.join(test_data_path, 'imagenet_class_index.json') |
| 69 | + image_filename = os.path.join(test_data_path, 'panda.jpg') |
| 70 | + model_filename = os.path.join(test_data_path, 'mobilenet_v2_1.4_224_frozen.pb') |
| 71 | + |
| 72 | + with open(model_filename, 'rb') as f: |
| 73 | + model_pb = f.read() |
| 74 | + |
| 75 | + with open(labels_filename, 'r') as f: |
| 76 | + labels = json.load(f) |
| 77 | + |
| 78 | + img_height, img_width = 224, 224 |
| 79 | + |
| 80 | + img = imread(image_filename) |
| 81 | + img = resize(img, (img_height, img_width), mode='constant', anti_aliasing=True) |
| 82 | + img = img.astype(np.float32) |
| 83 | + |
| 84 | + return model_pb, labels, img |
| 85 | + |
| 86 | + |
| 87 | +def run_mobilenet(con, img, input_var, output_var): |
| 88 | + time.sleep(0.5 * random.randint(0, 10)) |
| 89 | + con.execute_command('AI.TENSORSET', 'input', |
| 90 | + 'FLOAT', 1, img.shape[1], img.shape[0], img.shape[2], |
| 91 | + 'BLOB', img.tobytes()) |
| 92 | + |
| 93 | + con.execute_command('AI.MODELRUN', 'mobilenet', |
| 94 | + 'INPUTS', 'input', 'OUTPUTS', 'output') |
| 95 | + |
| 96 | + |
| 97 | +def run_test_multiproc(env, n_procs, fn, args=tuple()): |
| 98 | + procs = [] |
| 99 | + |
| 100 | + def tmpfn(): |
| 101 | + con = env.getConnection() |
| 102 | + fn(con, *args) |
| 103 | + return 1 |
| 104 | + |
| 105 | + for _ in range(n_procs): |
| 106 | + p = Process(target=tmpfn) |
| 107 | + p.start() |
| 108 | + procs.append(p) |
| 109 | + |
| 110 | + [p.join() for p in procs] |
0 commit comments