def initialize(self, context): self._context = context self._batch_size = context.system_properties.get('batch_size') model_dir = context.system_properties.get('model_dir') print('Loading the model from directory {}'.format(model_dir)) USE_GPU = os.getenv('USE_GPU', None) USE_INF = os.getenv('USE_INF', None) if USE_GPU == '1': self.model = dlr.DLRModel(model_dir, dev_type='gpu', error_log_file=SAGEMAKER_ERROR_LOG_FILE) elif USE_INF == '1': self.model = dlr.DLRModel(model_dir, dev_type='inf', error_log_file=SAGEMAKER_ERROR_LOG_FILE) else: self.model = dlr.DLRModel(model_dir, error_log_file=SAGEMAKER_ERROR_LOG_FILE) # Load user module SAGEMAKER_SUBMIT_DIRECTORY = os.getenv('SAGEMAKER_SUBMIT_DIRECTORY', None) tempdir = tempfile.gettempdir() source_tar = os.path.join(tempdir, 'script.tar.gz') download_s3_resource(SAGEMAKER_SUBMIT_DIRECTORY, source_tar) script_name = None with tarfile.open(source_tar, 'r:*') as tar: for member_info in tar.getmembers(): if member_info.name.endswith('.py'): if script_name is not None: raise RuntimeError('{} contains more than one *.py file'\ .format(source_tar)) print('Importing user module from {}...'.format( member_info.name)) tar.extract(member_info, path=tempdir) script_name = member_info.name if script_name is None: raise RuntimeError('{} contains no *.py file'.format(source_tar)) cur_dir = tempdir script_path = script_name[:-3] if '/' in script_path: file_depth = len(script_path.split('/')) - 1 for i in range(file_depth): cur_dir = os.path.join(cur_dir, script_name[:-3].split('/')[i]) script_path = script_path.split('/')[file_depth] self.user_module = import_user_module(cur_dir, script_path) self.input_names = self.model.get_input_names() self.initialized = True
def thread_job(): device = 'opencl' m = dlr.DLRModel(model_path, device) while True: ret, test_image = cap.read() if (ret == False): continue orig_img, img_data = open_and_norm_image(test_image) input_data = img_data.astype(dtype) m_out = m.run(input_data) out = m_out[0][0] i = 0 print('------------') for det in out: cid = int(det[0]) if cid < 0: continue score = det[1] if score < 0.5: continue i += 1 if (i > 10): break print(i, class_names[cid], det) print('---end---------') if (i > 10): continue global result result = out
def main(): # get argument from parser parser = create_argument_parser() args = parser.parse_args() # set model type model_define = convert_model_define(args.model_type).value # load model data model_root_path = args.model_root_path loader = ModelLoaderFactory.get_loader(model_define, model_root_path) loader.setup() model_path = loader.get_model_path() # create Deep Learning Runtime target = args.target_device m = dlr.DLRModel(model_path, target) # get input data input_files = [args.input_file_path] image_size = model_define["input_size"] transpose_tuple = get_transpose_tuple(model_define) input_tensor = util.get_ndarray_from_imagefiles(input_files, image_size, transpose_tuple) input_data = util.get_input_data(model_define, input_tensor) # run inference res = m.run(input_data) # show inference result and recreate image files util.recreate_images_with_bounding_boxes(input_files, input_tensor, res)
def initialize(self, context): self._context = context self._batch_size = context.system_properties.get('batch_size') model_dir = context.system_properties.get('model_dir') print('Loading the model from directory {}'.format(model_dir)) self.model = dlr.DLRModel(model_dir, error_log_file=SAGEMAKER_ERROR_LOG_FILE) self.initialized = True
def initialize(self, context): manifest = context.manifest model_dir = context.system_properties.get("model_dir") print("Loading the model from directory {}".format(model_dir)) # Load shape info self.shape_info = None for aux_file in glob.glob(os.path.join(model_dir, '*.json')): if os.path.basename(aux_file) == SHAPES_FILE: try: with open(aux_file, 'r') as f: self.shape_info = json.load(f) except Exception as e: raise Exception('Error parsing shape info') if self.shape_info is None: raise Exception( 'Shape info must be given as {}'.format(SHAPES_FILE)) USE_GPU = os.getenv('USE_GPU', None) if USE_GPU == '1': self.model = dlr.DLRModel(model_dir, dev_type='gpu') else: self.model = dlr.DLRModel(model_dir) self.initialized = True
def initialize(self, context): self._context = context self._batch_size = context.system_properties.get('batch_size') model_dir = context.system_properties.get('model_dir') print('Loading the model from directory {}'.format(model_dir)) USE_GPU = os.getenv('USE_GPU', None) if USE_GPU == '1': self.model = dlr.DLRModel(model_dir, dev_type='gpu', error_log_file=SAGEMAKER_ERROR_LOG_FILE) else: self.model = dlr.DLRModel(model_dir, error_log_file=SAGEMAKER_ERROR_LOG_FILE) # Load shape info self.shape_info = None for aux_file in glob.glob(os.path.join(model_dir, '*.json')): if os.path.basename(aux_file) == SHAPES_FILE: try: with open(aux_file, 'r') as f: self.shape_info = json.load(f) except Exception as e: raise Exception('Error parsing shape info') if self.shape_info is None: raise Exception('Shape info must be given as {}'.format(SHAPES_FILE)) self.input_names = self.model.get_input_names() self.initialized = True
def load(self): # load model data loader = ModelLoaderFactory.get_loader(self.__params.model_define, self.__params.model_root_path) loader.setup() model_path = loader.get_model_path() # create Deep Learning Runtime self.__model_loader = loader self.__model = dlr.DLRModel(model_path, self.__params.target_device) # create result creator model_type = self.__model_loader.get_model_detail().model_type self.__result_creator = NeoResultConverterFactory.get_converter( model_type, self.__one_detect_callback, self.__one_image_callback)
def __init__(self, test_image_path, raw_model_params_full_path, raw_model_symbol_full_path, neo_optimized_model_root_dir, short_size=416): """ constructor of sagemaker neo evaluator :param test_image_path: full path of test image with RGB channels :param raw_model_params_full_path: full path of original object detector model's parameters :param raw_model_symbol_full_path: full path of original object detector model's symbol :param neo_optimized_model_root_dir: the directory of Sagemaker Neo optimized model, the .so, .params, .json, manifest, .meta files are stored in this directory, all these files are compiled and generated by Sagemaker Neo :param short_size: resized short size of object detector's input, default value is 416, the longer side will be resized according to image's original width/height ratio """ # load test image self._test_image_path = test_image_path self._test_image_base64 = self.get_base64_encoding( full_path=self._test_image_path) # mxnet model without Sagemaker neo optimization self._raw_model_params_full_path = raw_model_params_full_path self._raw_model_symbol_full_path = raw_model_symbol_full_path # optimized model root directory with Sagemaker neo self._neo_optimized_model_root_dir = neo_optimized_model_root_dir self._short_size = short_size self._height, self._width, self._channels = -1, -1, -1 # load detector model (without Sagemaker neo optimization) self._human_body_detector_without_neo_optimization = gluon.nn.SymbolBlock.imports( symbol_file=self._raw_model_symbol_full_path, input_names=['data'], param_file=self._raw_model_params_full_path, ctx=mx.gpu()) # load Sagemaker neo optimized model self._human_body_detector_with_neo_optimization = dlr.DLRModel( self._neo_optimized_model_root_dir, 'gpu', 0)
input_mean = 127.5 input_std = 127.5 output_frame_rate = 3 # flag for debugging debug = False if not os.path.exists('/tmp/poseEstimator-output'): os.makedirs('/tmp/poseEstimator-output') PhoneHome.disable_feature() print('Loading model...', flush=True) try: model = dlr.DLRModel(MODEL_PATH, 'cpu') except Exception as e: print(e, flush=True) kp_list = [ "nose", "leftEye", "rightEye", "leftEar", "rightEar", "leftShoulder", "rightShoulder", "leftElbow", "rightElbow", "leftWrist", "rightWrist",
from logging import getLogger logger = getLogger(__name__) os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # メッセージに thing_name を入れるために thing_name を環境変数から取得する THING_NAME = os.environ.get('AWS_IOT_THING_NAME') TIMEOUT = 10 ipc_client = connect() INTERVAL = 60 neo_dir = '/app/classifier' classifier_neo = dlr.DLRModel(neo_dir, 'cpu', 0) test_X = np.load('/app/test_X.npy') topic = "inference/result" def signal_handler(signal, frame): logger.info(f"Received {signal}, exiting") sys.exit(0) # Register SIGTERM for shutdown of container signal.signal(signal.SIGTERM, signal_handler) signal.signal(signal.SIGINT, signal_handler)
def initialize(self, context): manifest = context.manifest model_dir = context.system_properties.get("model_dir") print("Loading the model from directory {}".format(model_dir)) self.model = dlr.DLRModel(model_dir) self.initialized = True
INPUT_RESOLUTION = "1280x720" resW, resH = INPUT_RESOLUTION.split('x') imW, imH = int(resW), int(resH) # flag for debugging debug = False if not os.path.exists('/tmp/poseEstimator-output'): os.makedirs('/tmp/poseEstimator-output') PhoneHome.disable_feature() print('Loading model...', flush=True) try: model = dlr.DLRModel(MODEL_PATH, 'gpu', use_default_dlr=True) except Exception as e: print(e, flush=True) height = 257 width = 257 # set stride to 32 based on model size output_stride = 32 floating_model = True input_mean = 127.5 input_std = 127.5 output_frame_rate = 8
mlRootPath = args.mlRootPath imageName = args.imageName prediction_interval_secs = args.interval reshape = (224, 224) score_threshold = 0.3 max_no_of_results = 5 camera = None image_data = None # sample_image = (mlRootPath + "/images/" + imageName).format( # os.path.dirname(os.path.realpath(__file__))) results_directory = mlRootPath + "/inference_log/" # Create the results directory if it does not exist already os.makedirs(results_directory, exist_ok=True) # Initialize example Resnet model dlr_model = dlr.DLRModel(model_path, context) os.system("echo {}".format( "Inference logs can be found under the directory '{}' in the name of the model used. " .format(results_directory))) # Load image based on the format - support jpg,jpeg,png and npy. def load_image(imgName): image_data = None sample_image = (mlRootPath + "/images/" + imgName).format( os.path.dirname(os.path.realpath(__file__))) if imgName.endswith( ".jpg",
def run_model(model_dir, X, y): model = dlr.DLRModel(model_dir) output = np.concatenate( tuple(model.run(X[i:i + 1, :])[0] for i in range(X.shape[0]))).squeeze() return output
import dlr import numpy as np import time model = dlr.DLRModel('compiled', dev_type='gpu') data = np.random.random((1, 3, 608, 608)) y = model.run(data) times = [] for i in range(100): start = time.time() model.run(data) times.append(time.time() - start) print('mean latency', np.mean(times[10:]) * 1000.0)
if score < 0.6: continue box = boxes[0][i] (left, right, top, bottom) = (box[1] * w, box[3] * w, box[0] * h, box[2] * h) p1 = (int(left), int(top)) p2 = (int(right), int(bottom)) cv2.rectangle(frame, p1, p2, (221, 245, 66), 3, 1) cv2.putText(frame, label, (int(left + 10), int(top + 10)), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 0, 0), 2, cv2.LINE_AA) #cv2.imshow(windowName, frame) m = dlr.DLRModel(model_path) cap = cv2.VideoCapture(8) cap.set(cv2.CAP_PROP_FRAME_WIDTH, w) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, h) mbScale = 1024 * 1124 while True: ret, test_img = cap.read() if (ret == False): continue start = datetime.datetime.now() res = getDetectionSSD(test_img, m) end = datetime.datetime.now() save = saveResult(res) process = psutil.Process(os.getpid()) mem = "Memory RSS: {:,} MB".format(
from posenet import decodeMultiplePoses from stream_uploader import init_gg_stream_manager, send_to_gg_stream_manager PhoneHome.disable_feature() pose_model_path = os.environ["POSE_MODEL_PATH"] s3_prefix = "pose-estimator-demo/processed-video-frames/nano/" pose_input_tensor_name = 'sub_2' pose_input_tensor_shape = [1, 257, 257, 3] color_table = [(0, 255, 0), (255, 0, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255), (255, 0, 255)] print('Loading model...', flush=True) try: model = dlr.DLRModel(pose_model_path, 'gpu', use_default_dlr=True) except Exception as e: print(e, flush=True) def gstreamer_pipeline( capture_width=1280, capture_height=720, inference_width=1280, inference_height=720, framerate=24, flip_method=0, ): return ( "nvarguscamerasrc ! " "videoflip method=vertical-flip ! "
model_path = "models/mxnet-ssd-mobilenet-512" class_names = [ "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ] ###################################################################### # Create TVM runtime and do inference # Build TVM runtime device = 'opencl' m = dlr.DLRModel(model_path, device) cap = cv2.VideoCapture(8) cap.set(cv2.CAP_PROP_FRAME_WIDTH, w) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, h) def thread_job(): device = 'opencl' m = dlr.DLRModel(model_path, device) while True: ret, test_image = cap.read() if (ret == False): continue orig_img, img_data = open_and_norm_image(test_image) input_data = img_data.astype(dtype)