def gen(): engine = BasicEngine(model) labels = read_label_file(label) cap = get_cap() # a = engine.get_num_of_output_tensors() # b = engine.total_output_array_size() # c = engine.get_output_tensor_size(0) # d = engine.required_input_array_size() # print(a, b, c, d) while True: _, frame = cap.read() input_val = cv2.resize(frame, (432, 368)) input_val = input_val.flatten() ans = engine.RunInference(input_val) heat_map = ans[1].reshape([54, 46, 57]) prop = heat_map[1, :, :] prop = np.multiply(prop, 255) # prop = cv2.resize(prop, (460, 640)) _, buffer = cv2.imencode(".jpg", prop) yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + io.BytesIO(buffer).read() + b'\r\n')
def test_device_path(self): all_edgetpu_paths = edgetpu_utils.ListEdgeTpuPaths( edgetpu_utils.EDGE_TPU_STATE_NONE) engine = BasicEngine( test_utils.test_data_path('mobilenet_v1_1.0_224_quant.tflite'), all_edgetpu_paths[0]) self.assertEqual(engine.device_path(), all_edgetpu_paths[0])
def main(): parser = argparse.ArgumentParser() parser.add_argument( '--model', help='Path of the segmentation model.', required=True) parser.add_argument( '--input', help='File path of the input image.', required=True) parser.add_argument('--output', help='File path of the output image.') parser.add_argument( '--keep_aspect_ratio', dest='keep_aspect_ratio', action='store_true', help=( 'keep the image aspect ratio when down-sampling the image by adding ' 'black pixel padding (zeros) on bottom or right. ' 'By default the image is resized and reshaped without cropping. This ' 'option should be the same as what is applied on input images during ' 'model training. Otherwise the accuracy may be affected and the ' 'bounding box of detection result may be stretched.')) parser.set_defaults(keep_aspect_ratio=False) args = parser.parse_args() if not args.output: output_name = 'semantic_segmentation_result.jpg' else: output_name = args.output # Initialize engine. engine = BasicEngine(args.model) _, height, width, _ = engine.get_input_tensor_shape() # Open image. img = Image.open(args.input) if args.keep_aspect_ratio: resized_img, ratio = image_processing.resampling_with_original_ratio( img, (width, height), Image.NEAREST) else: resized_img = img.resize((width, height)) ratio = (1., 1.) input_tensor = np.asarray(resized_img).flatten() _, raw_result = engine.run_inference(input_tensor) result = np.reshape(raw_result, (height, width)) new_width, new_height = int(width * ratio[0]), int(height * ratio[1]) # If keep_aspect_ratio, we need to remove the padding area. result = result[:new_height, :new_width] vis_result = label_to_color_image(result.astype(int)).astype(np.uint8) vis_result = Image.fromarray(vis_result) vis_img = resized_img.crop((0, 0, new_width, new_height)) # Concat resized input image and processed segmentation results. concated_image = Image.new('RGB', (new_width*2, new_height)) concated_image.paste(vis_img, (0, 0)) concated_image.paste(vis_result, (width, 0)) concated_image.save(output_name) print('Please check ', output_name)
def _run_benchmark_for_model(model_name): """Runs benchmark for given model with a random input. Args: model_name: string, file name of the model. Returns: float, average inference time. """ iterations = 200 if ('edgetpu' in model_name) else 20 print('Benchmark for [', model_name, ']') print('model path = ', test_utils.test_data_path(model_name)) engine = BasicEngine(test_utils.test_data_path(model_name)) print('Shape of input tensor : ', engine.get_input_tensor_shape()) # Prepare a random generated input. input_size = engine.required_input_array_size() random_input = test_utils.generate_random_input(1, input_size) # Convert it to a numpy.array. input_data = np.array(random_input, dtype=np.uint8) benchmark_time = timeit.timeit( lambda: engine.run_inference(input_data), number=iterations) # Time consumed for each iteration (milliseconds). time_per_inference = (benchmark_time / iterations) * 1000 print(time_per_inference, 'ms (iterations = ', iterations, ')') return time_per_inference
def start(self): self.engine = BasicEngine(self.model_file) self.input_shape = self.engine.get_input_tensor_shape()[1:3] self.subscribe() if self.enable_visualization: self.timer = rospy.Timer(rospy.Duration(self.duration), self.visualize_cb)
def __init__(self): super(EdgeTPUSemanticSegmenter, self).__init__() rospack = rospkg.RosPack() pkg_path = rospack.get_path('coral_usb') self.bridge = CvBridge() self.classifier_name = rospy.get_param('~classifier_name', rospy.get_name()) model_file = os.path.join( pkg_path, './models/deeplabv3_mnv2_pascal_quant_edgetpu.tflite') model_file = rospy.get_param('~model_file', model_file) label_file = rospy.get_param('~label_file', None) self.engine = BasicEngine(model_file) self.input_shape = self.engine.get_input_tensor_shape()[1:3] if label_file is None: self.label_names = [ 'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor' ] self.label_ids = list(range(len(self.label_names))) else: self.label_ids, self.label_names = self._load_labels(label_file) self.pub_label = self.advertise('~output/label', Image, queue_size=1) self.pub_image = self.advertise('~output/image', Image, queue_size=1)
def __init__(self, model_path, mirror=False): """Creates a PoseEngine with given model. Args: model_path: String, path to TF-Lite Flatbuffer file. mirror: Flip keypoints horizontally Raises: ValueError: An error occurred when model output is invalid. """ BasicEngine.__init__(self, model_path) self._mirror = mirror self._input_tensor_shape = self.get_input_tensor_shape() if (self._input_tensor_shape.size != 4 or self._input_tensor_shape[3] != 3 or self._input_tensor_shape[0] != 1): raise ValueError( ('Image model should have input shape [1, height, width, 3]!' ' This model has {}.'.format(self._input_tensor_shape))) _, self.image_height, self.image_width, self.image_depth = \ self.get_input_tensor_shape() # The API returns all the output tensors flattened and concatenated. We # have to figure out the boundaries from the tensor shapes & sizes. offset = 0 self._output_offsets = [0] for size in self.get_all_output_tensors_sizes(): offset += size self._output_offsets.append(int(offset))
def test_negative_tensor_index(self): engine = BasicEngine( test_utils.test_data_path('mobilenet_v1_1.0_224_quant.tflite')) error_message = None try: engine.get_output_tensor_size(-1) except RuntimeError as e: error_message = str(e) self.assertEqual('tensor_index must >= 0!', error_message)
def test_tensor_index_exceed(self): engine = BasicEngine( test_utils.test_data_path('mobilenet_v1_1.0_224_quant.tflite')) error_message = None try: engine.get_output_tensor_size(100) except RuntimeError as e: error_message = str(e) self.assertEqual('tensor_index doesn\'t exist!', error_message)
def _GetOutputNumberClasses(model_path): """Gets the number of output classes. Args: model_path: string, path of the model. Returns: int, number of the output classes. """ tmp = BasicEngine(model_path) assert tmp.get_num_of_output_tensors() == 1 return tmp.total_output_array_size()
def _GetRequiredShape(model_path): """Gets image shape required by model. Args: model_path: string, path of the model. Returns: (width, height). """ tmp = BasicEngine(model_path) input_tensor = tmp.get_input_tensor_shape() return (input_tensor[2], input_tensor[1])
def test_run_inference_implicit_size_different_types(self): engine = BasicEngine( test_utils.test_data_path('mobilenet_v1_1.0_224_quant.tflite')) input_size = engine.required_input_array_size() input_data = test_utils.generate_random_input(1, input_size) self._test_inference_with_different_input_types(engine, input_data) input_data = test_utils.generate_random_input(1, input_size + 1) self._test_inference_with_different_input_types(engine, input_data) input_data = test_utils.generate_random_input(1, input_size + 64) self._test_inference_with_different_input_types(engine, input_data)
def getRequiredInputShape(self): """ Get the required input shape for the model. """ basic_engine = BasicEngine(self._model_path) input_tensor_shape = basic_engine.get_input_tensor_shape() if (input_tensor_shape.size != 4 or input_tensor_shape[3] != 3 or input_tensor_shape[0] != 1): raise RuntimeError( 'Invalid input tensor shape! Expected: [1, height, width, 3]') return (input_tensor_shape[2], input_tensor_shape[1])
def _get_input_tensor_shape(model_path): """Gets input tensor shape of given model. Args: model_path: string, path of the model. Returns: List of integers. """ tmp = BasicEngine(model_path) shape = tmp.get_input_tensor_shape() return shape.copy()
def _get_shape(model): """Gets images shape required by model. Args: model: string, file name of the input model. Returns: (width, height) """ basic_engine = BasicEngine(test_utils.test_data_path('imprinting', model)) _, height, width, _ = basic_engine.get_input_tensor_shape() return (width, height)
def __init__(self, model_path, mirror=False): BasicEngine.__init__(self, model_path) self._mirror = mirror self._input_tensor_shape = self.get_input_tensor_shape() _, self.image_height, self.image_width, self.image_depth = self.get_input_tensor_shape( ) offset = 0 self._output_offsets = [0] for size in self.get_all_output_tensors_sizes(): offset += size self._output_offsets.append(offset)
def coralQueue(inqueue, addr): context = zmq.Context() socket = context.socket(zmq.PAIR) socket.connect('tcp://%s' % addr) engine = BasicEngine(MODELPATH) fps = FPS() while True: obj = inqueue.get() if obj is None: socket.send_pyobj(None) # BLOCK HERE break # start_time = time.time() img, content = obj input_tensor = np.asarray(img).flatten() _, raw_result = engine.RunInference(input_tensor) bbox_result = [] num_candidates = raw_result[tensor_start_index[3]] for i in range(int(round(num_candidates))): score = raw_result[tensor_start_index[2] + i] if score > threshold: label_id = int(round(raw_result[tensor_start_index[1] + i])) if label_id in target_labels: y1 = max(0.0, raw_result[tensor_start_index[0] + 4 * i]) x1 = max(0.0, raw_result[tensor_start_index[0] + 4 * i + 1]) y2 = min(1.0, raw_result[tensor_start_index[0] + 4 * i + 2]) x2 = min(1.0, raw_result[tensor_start_index[0] + 4 * i + 3]) # This is ratio. bbox_result.append([x1, y1, x2, y2, score]) bbox_result.sort(key=lambda x: -x[4]) # end_time = time.time() # SDLogger.debug(f'Preprocess + Inference costs: {end_time-start_time:.3f}') try: pass socket.send_pyobj((content, bbox_result[:top_k]), flags=zmq.NOBLOCK) except Exception as e: SDLogger.error('Error when sending the detection result: %s' % e) SDLogger.info(f'Detection FPS: {fps():.2f}')
def test_inference_with_bad_input_size(self): engine = BasicEngine( test_utils.test_data_path('mobilenet_v1_1.0_224_quant.tflite')) expected_size = engine.required_input_array_size() input_data = test_utils.generate_random_input(1, expected_size - 1) error_message = None try: engine.run_inference(input_data, expected_size - 1) except AssertionError as e: error_message = str(e) self.assertEqual( 'Wrong input size={}, expected={}.'.format(expected_size - 1, expected_size), error_message)
def __init__(self, model_path): """Creates a BasicEngine with given model. Args: model_path: String, path to TF-Lite Flatbuffer file. Raises: ValueError: An error occurred when the output format of model is invalid. """ BasicEngine.__init__(self, model_path) output_tensors_sizes = self.get_all_output_tensors_sizes() if output_tensors_sizes.size != 1: raise ValueError( ('Classification model should have 1 output tensor only!' 'This model has {}.'.format(output_tensors_sizes.size)))
def __init__(self, model_path): """Creates a EmbeddingEngine with given model and labels. Args: model_path: String, path to TF-Lite Flatbuffer file. Raises: ValueError: An error occurred when model output is invalid. """ BasicEngine.__init__(self, model_path) output_tensors_sizes = self.get_all_output_tensors_sizes() if output_tensors_sizes.size != 1: raise ValueError( ('Dectection model should have only 1 output tensor!' 'This model has {}.'.format(output_tensors_sizes.size)))
def test_use_all_edge_tpu(self): available_tpus = edgetpu_utils.ListEdgeTpuPaths( edgetpu_utils.EDGE_TPU_STATE_UNASSIGNED) recorded_tpus = [] engine_list = [] for _ in available_tpus: engine = BasicEngine( test_utils.test_data_path('mobilenet_v1_1.0_224_quant.tflite')) recorded_tpus.append(engine.device_path()) engine_list.append(engine) remaining_tpus = edgetpu_utils.ListEdgeTpuPaths( edgetpu_utils.EDGE_TPU_STATE_UNASSIGNED) self.assertEqual(0, len(remaining_tpus)) self.assertTupleEqual(tuple(recorded_tpus), available_tpus)
class HeadPoseEstimator: def __init__(self, model_head_pose): # Init Head Pose Estimator self.devices = edgetpu_utils.ListEdgeTpuPaths(edgetpu_utils.EDGE_TPU_STATE_UNASSIGNED) self.engine = BasicEngine(model_path=model_head_pose, device_path=self.devices[0]) self.model_height = self.engine.get_input_tensor_shape()[1] self.model_width = self.engine.get_input_tensor_shape()[2]
def _get_interpreter(self): try: interpreter = BasicEngine(self._model) except RuntimeError: raise RuntimeError('TPU not detected') return interpreter
def run_benchmark(model): """Returns average inference time in ms on specified model on random input.""" print('Benchmark for [%s]' % model) print('model path = %s' % test_utils.test_data_path(model)) engine = BasicEngine(test_utils.test_data_path(model)) print('input tensor shape = %s' % engine.get_input_tensor_shape()) iterations = 200 if 'edgetpu' in model else 20 input_size = engine.required_input_array_size() random_input = test_utils.generate_random_input(1, input_size) input_data = np.array(random_input, dtype=np.uint8) result = 1000 * timeit.timeit(lambda: engine.run_inference(input_data), number=iterations) / iterations print('%.2f ms (iterations = %d)' % (result, iterations)) return result
def main(): log.basicConfig(format=" [ %(levelname)s] %(message)s", level=log.INFO, stream=sys.stdout) args = build_argparser().parse_args() #log.info("Loading the network files model") voice = sound_decode(args.input) infer_engine = BasicEngine(args.model) latency, results = infer_engine.RunInference(voice) print(latency) print(infer_engine.get_inference_time()) print(results)
def test_invalid_model_path(self): error_message = None try: _ = BasicEngine('invalid_model_path.tflite') except RuntimeError as e: error_message = str(e) self.assertEqual('Could not open \'invalid_model_path.tflite\'.', error_message)
def __init__(self, model_path, mirror=False): """Creates a PoseEngine with given model. Args: model_path: String, path to TF-Lite Flatbuffer file. mirror: Flip keypoints horizontally Raises: ValueError: An error occurred when model output is invalid. """ BasicEngine.__init__(self, model_path) self._mirror = mirror self._input_tensor_shape = self.get_input_tensor_shape() if (self._input_tensor_shape.size != 4 or self._input_tensor_shape[3] != 3 or self._input_tensor_shape[0] != 1): raise ValueError( ('Image model should have input shape [1, height, width, 3]!' ' This model has {}.'.format(self._input_tensor_shape))) _, self.image_height, self.image_width, self.image_depth = self.get_input_tensor_shape( ) # The API returns all the output tensors flattened and concatenated. We # have to figure out the boundaries from the tensor shapes & sizes. offset = 0 self._output_offsets = [0] for size in self.get_all_output_tensors_sizes(): offset += int(size) self._output_offsets.append(offset) # Auto-detect stride size def calcStride(h, w, L): return int( (2 * h * w) / (math.sqrt(h**2 + 4 * h * L * w - 2 * h * w + w**2) - h - w)) heatmap_size = self.get_output_tensor_size(4) print("Heatmap size: ", heatmap_size) self.stride = calcStride(self.image_height, self.image_width, heatmap_size) self.heatmap_size = (self.image_width // self.stride + 1, self.image_height // self.stride + 1) print("Stride Guess: ", self.stride, self.heatmap_size)
def __init__(self, save_file): self.engine = BasicEngine(save_file) self.board_size = go.N self.output_policy_size = self.board_size**2 + 1 input_tensor_shape = self.engine.get_input_tensor_shape() expected_input_shape = [1, self.board_size, self.board_size, 17] if not np.array_equal(input_tensor_shape, expected_input_shape): raise RuntimeError( 'Invalid input tensor shape {}. Expected: {}'.format( input_tensor_shape, expected_input_shape)) output_tensors_sizes = self.engine.get_all_output_tensors_sizes() expected_output_tensor_sizes = [self.output_policy_size, 1] if not np.array_equal(output_tensors_sizes, expected_output_tensor_sizes): raise RuntimeError( 'Invalid output tensor sizes {}. Expected: {}'.format( output_tensors_sizes, expected_output_tensor_sizes))
def __init__(self, model_path, keep_classes=False): """ Args: model_path (str): Path to the model you want to retrain. This model must be a ``.tflite`` file output by the ``join_tflite_models`` tool. For more information about how to create a compatible model, read `Retrain an image classification model on-device <https://coral.ai/docs/edgetpu/retrain-classification-ondevice/>`_. keep_classes (bool): If True, keep the existing classes from the pre-trained model (and use training to add additional classes). If False, drop the existing classes and train the model to include new classes only. """ self._engine = ImprintingEnginePythonWrapper.CreateFromFile( model_path, keep_classes) self._num_classes = 0 if keep_classes: tmp = BasicEngine(model_path) assert tmp.get_num_of_output_tensors() == 1 self._num_classes = tmp.total_output_array_size()
def deep_inferencer(results, frameBuffer, model, device): deep_engine = None deep_engine = BasicEngine(model, device) print("Loaded Graphs!!! (Deeplab)") while True: if frameBuffer.empty(): continue # Run inference. color_image = frameBuffer.get() prepimg_deep = color_image[:, :, ::-1].copy() prepimg_deep = prepimg_deep.flatten() tinf = time.perf_counter() latency, result_deep = deep_engine.RunInference(prepimg_deep) print(time.perf_counter() - tinf, "sec (Deeplab)") results.put(result_deep)
def __init__(self, model_path): """Creates a DetectionEngine with given model. Args: model_path: String, path to TF-Lite Flatbuffer file. Raises: ValueError: An error occurred when model output is invalid. """ BasicEngine.__init__(self, model_path) output_tensors_sizes = self.get_all_output_tensors_sizes() if output_tensors_sizes.size != 4: raise ValueError( ('Dectection model should have 4 output tensors!' 'This model has {}.'.format(output_tensors_sizes.size))) self._tensor_start_index = [0] offset = 0 for i in range(3): offset = offset + output_tensors_sizes[i] self._tensor_start_index.append(offset)