def get_model_wrapper(): from model import model class MyWrapper(model.ModelWrapper): def __init__(self, input): super(MyWrapper, self).__init__() self.input = input self.lag = None self.sourceCallback = False self.sinkCallback = False def SourceCallback(self, buffer): print("requesting input...") buffer.copy_from(self.input.astype(np.float)) self.sourceCallback = True def LagNotification(self, lag): print("Predict is lagging by {}".format(lag)) self.lag = lag def SinkCallback(self, buffer): print("output callback happening...") self.sinkCallback = True from model import model print("Input size={}".format(model.get_default_input_shape().Size())) input = np.ones((model.get_default_input_shape().Size())) return MyWrapper(input)
def test_python(model_path): target_dir = os.path.join(os.path.dirname(model_path), "tutorial_python") if os.path.isdir(target_dir): rmtree(target_dir) os.makedirs(target_dir) # invoke "wrap.py" helper to create a compilable C++ project wrap_model(model_path, target_dir, "python") # compile the project using cmake. model_dir = os.path.join(target_dir, "model") make_project(model_dir) # did it actually build? binary_dir = os.path.join(model_dir, "build") if os.name == 'nt': binary = os.path.join(binary_dir, "release", "_model.pyd") else: binary = os.path.join(binary_dir, "_model.so") if not os.path.isfile(binary): print("### wrap_test failed, binary '{}' was not produced".format(os.path.basename(binary))) return 1 # execute the compiled python module and check the output sys.path += [ target_dir ] from model import model print("Input size={}".format(model.get_default_input_shape().Size())) input = np.zeros((model.get_default_input_shape().Size())) output = model.predict(input) result = ", ".join([str(x) for x in list(output)]) if result != "0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0": print("### FAILED wrap_test python module did not return the expected results, got: {}".format(result)) return 1 else: print("### PASSED wrap_test: test_python") # make sure we don't leak. before = 0 after = 0 output = None gc.collect() before = len(gc.get_objects()) output = model.predict(input) output = None gc.collect() after = len(gc.get_objects()) if before != after: print("### FAILED wrap_test python detected a memory leak in our predict call, before# {}, after# {}".format(before, after)) return 1 return 0
def test_python(model_path, target_dir): target_dir = os.path.join(os.path.dirname(model_path), target_dir) if os.path.isdir(target_dir): rmtree(target_dir) os.makedirs(target_dir) # invoke "wrap.py" helper to create a compilable C++ project wrap_model(model_path, target_dir, "python") # compile the project using cmake. model_dir = os.path.join(target_dir, "model") make_project(model_dir) # did it actually build? binary_dir = os.path.join(model_dir, "build") if os.name == 'nt': binary = os.path.join(binary_dir, "release", "_model.pyd") else: binary = os.path.join(binary_dir, "_model.so") if not os.path.isfile(binary): print("### wrap_test failed, binary '{}' was not produced".format(os.path.basename(binary))) return 1 # execute the compiled python module and check the output sys.path += [ target_dir ] from model import model class MyWrapper(model.ModelWrapper): def __init__(self, input): super(MyWrapper, self).__init__() self.input = input self.lag = None def SourceCallback(self, buffer): print("requesting input...") buffer.copy_from(self.input.astype(np.float)) def LagNotification(self, lag): print("Predict is lagging by {}".format(lag)) self.lag = lag def SinkCallback(self, buffer): print("output callback happening...") print("Input size={}".format(model.get_default_input_shape().Size())) input = np.ones((model.get_default_input_shape().Size())) wrapper = MyWrapper(input) output = wrapper.Predict() time.sleep(1) # this should cause the lag notification. output = wrapper.Predict() result = ", ".join([str(x) for x in list(output)]) print("Prediction={}".format(result)) if result != "1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0": print("### FAILED wrap_test python module did not return the expected results, got: {}".format(result)) return 1 else: print("### PASSED wrap_test: test_python") # make sure lag notification was called. if wrapper.lag is None: print("### FAILED lag notification callback never happened") return 1 # make sure we don't leak. before = 0 after = 0 output = None gc.collect() before = len(gc.get_objects()) output = wrapper.Predict() output = None gc.collect() after = len(gc.get_objects()) if before != after: print("### FAILED wrap_test python detected a memory leak in our predict call, before# {}, after# {}".format(before, after)) return 1 return 0
def test_python(model_path, target_dir): target_dir = os.path.join(os.path.dirname(model_path), target_dir) if os.path.isdir(target_dir): rmtree(target_dir) os.makedirs(target_dir) # invoke "wrap.py" helper to create a compilable C++ project wrap_model(model_path, target_dir, "python") # compile the project using cmake. model_dir = os.path.join(target_dir, "model") make_project(model_dir) # did it actually build? binary_dir = os.path.join(model_dir, "build") if os.name == 'nt': binary = os.path.join(binary_dir, "release", "_model.pyd") else: binary = os.path.join(binary_dir, "_model.so") if not os.path.isfile(binary): print("### wrap_test failed, binary '{}' was not produced".format( os.path.basename(binary))) return 1 # execute the compiled python module and check the output sys.path += [target_dir] from model import model print("Input size={}".format(model.get_default_input_shape().Size())) wrapper = get_model_wrapper() output = wrapper.Predict() time.sleep(1) # this should cause the lag notification. output = wrapper.Predict() result = ", ".join([str(x) for x in list(output)]) print("Prediction={}".format(result)) if result != "1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0": print( "### FAILED wrap_test python module did not return the expected results, got: {}" .format(result)) return 1 else: print("### PASSED wrap_test: test_python") # make sure callbacks happened if not wrapper.sinkCallback: print("### FAILED SinkCallback never happened") return 1 # make sure callbacks happened if not wrapper.sourceCallback: print("### FAILED SourceCallback never happened") return 1 # make sure lag notification was called. if wrapper.lag is None: print("### FAILED lag notification callback never happened") return 1 # make sure we can call the helper functions. try: print("GetInputShape={}".format(wrapper.GetInputShape(0))) print("GetInputSize={}".format(wrapper.GetInputSize(0))) print("GetOutputShape={}".format(wrapper.GetOutputShape(0))) print("GetOutputSize={}".format(wrapper.GetOutputSize(0))) print("Reset={}".format(wrapper.Reset())) print("GetMetadata={}".format(wrapper.GetMetadata("Id"))) except: print("### FAILED wrap_test python failed to call helper methods") return 1 memory_test(simple_test) return 0