Пример #1
0
    def test_debug_info(self):
        engine = BasicEngine(
            test_utils.test_data_path('mobilenet_v1_1.0_224_quant.tflite'))
        # Check model's input format.
        input_tensor_shape = engine.get_input_tensor_shape()
        self.assertListEqual([1, 224, 224, 3], input_tensor_shape.tolist())
        self.assertEqual(224 * 224 * 3, engine.required_input_array_size())

        # Check model's output.
        output_tensors_sizes = engine.get_all_output_tensors_sizes()
        self.assertListEqual([1001], output_tensors_sizes.tolist())
        self.assertEqual(1, engine.get_num_of_output_tensors())
        self.assertEqual(1001, engine.get_output_tensor_size(0))
        self.assertEqual(1001, engine.total_output_array_size())

        # Check SSD model.
        ssd_engine = BasicEngine(
            test_utils.test_data_path(
                'mobilenet_ssd_v1_coco_quant_postprocess.tflite'))
        # Check model's input format.
        input_tensor_shape = ssd_engine.get_input_tensor_shape()
        self.assertListEqual([1, 300, 300, 3], input_tensor_shape.tolist())
        self.assertEqual(300 * 300 * 3, ssd_engine.required_input_array_size())

        # Check model's output.
        output_tensors_sizes = ssd_engine.get_all_output_tensors_sizes()
        self.assertListEqual([80, 20, 20, 1], output_tensors_sizes.tolist())
        self.assertEqual(4, ssd_engine.get_num_of_output_tensors())
        self.assertEqual(80, ssd_engine.get_output_tensor_size(0))
        self.assertEqual(20, ssd_engine.get_output_tensor_size(1))
        self.assertEqual(20, ssd_engine.get_output_tensor_size(2))
        self.assertEqual(1, ssd_engine.get_output_tensor_size(3))
        self.assertEqual(121, ssd_engine.total_output_array_size())
    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)
Пример #3
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)
Пример #4
0
 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])
Пример #5
0
 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)
Пример #6
0
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
Пример #7
0
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')
Пример #8
0
    def test_append_fully_connected_and_softmax_layer_to_model(self):
        in_model_name = (
            'mobilenet_v1_1.0_224_quant_embedding_extractor_edgetpu.tflite')
        in_model_path = test_utils.test_data_path(in_model_name)
        in_engine = BasicEngine(in_model_path)

        # Generate random input tensor.
        np.random.seed(12345)
        input_tensor = np.random.randint(
            0, 255, size=in_engine.get_input_tensor_shape(),
            dtype=np.uint8).flatten()

        # Set up weights, biases and FC output tensor range.
        _, embedding_vector = in_engine.run_inference(input_tensor)
        embedding_vector_dim = embedding_vector.shape[0]
        num_classes = 10
        weights = np.random.randn(embedding_vector_dim,
                                  num_classes).astype(np.float32)
        biases = np.random.randn(num_classes).astype(np.float32)

        fc_output = embedding_vector.dot(weights) + biases
        fc_output_min = float(np.min(fc_output))
        fc_output_max = float(np.max(fc_output))

        try:
            # Create temporary directory, and remove this folder when test
            # finishes. Otherwise, test may fail because of generated files from
            # previous run.
            tmp_dir = tempfile.mkdtemp()
            # Append FC and softmax layers and save model.
            out_model_path = os.path.join(
                tmp_dir, in_model_name[:-7] + '_bp_retrained.tflite')
            AppendFullyConnectedAndSoftmaxLayerToModel(
                in_model_path, out_model_path,
                weights.transpose().flatten(), biases, fc_output_min,
                fc_output_max)
            self.assertTrue(os.path.exists(out_model_path))

            # Run with saved model on same input.
            out_engine = BasicEngine(out_model_path)
            _, result = out_engine.run_inference(input_tensor)
            # Calculate expected result.
            expected = np.exp(fc_output - np.max(fc_output))
            expected = expected / np.sum(expected)
            np.testing.assert_almost_equal(result, expected, decimal=2)
        finally:
            shutil.rmtree(tmp_dir)
Пример #9
0
 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)
Пример #10
0
    def _get_interpreter(self):
        try:
            interpreter = BasicEngine(self._model)

        except RuntimeError:
            raise RuntimeError('TPU not detected')

        return interpreter
Пример #11
0
 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)
Пример #12
0
 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)
Пример #13
0
    def _get_tpu_engine(model):
        try:
            # get runtime for TPU
            model = BasicEngine(model)

        except RuntimeError:
            # TPU has not been detected
            model = None

        return model
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])
Пример #16
0
 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)
Пример #17
0
 def test_edge_tpu_not_exist(self):
   error_message = None
   try:
     _ = BasicEngine(
         test_utils.test_data_path('mobilenet_v1_1.0_224_quant_edgetpu.tflite'),
         'invalid_edgetpu_device_path')
   except RuntimeError as e:
     error_message = str(e)
   self.assertEqual(
       'Path invalid_edgetpu_device_path does not map to an Edge TPU device.',
       error_message)
Пример #18
0
 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 spotter(self, args):
        engine = BasicEngine(args.model_file)

        mic = args.mic if args.mic is None else int(args.mic)
        model.classify_audio(mic,
                             engine,
                             labels_file="config/labels_gc2.raw.txt",
                             commands_file="config/commands_v2_snake.txt",
                             dectection_callback=self._controler.callback,
                             sample_rate_hz=int(args.sample_rate_hz),
                             num_frames_hop=int(args.num_frames_hop))
def serve():
    default_model_dir = '/media/mendel/sem-seg-server/models'
    default_model = 'deeplabv3_mnv2_pascal_quant_edgetpu.tflite'
    default_labels = 'pascal_voc_segmentation_labels.txt'
    parser = argparse.ArgumentParser()
    parser.add_argument('--model', help='.tflite model path',
        default=os.path.join(default_model_dir,default_model))
    parser.add_argument('--labels', help='label file path',
        default=os.path.join(default_model_dir, default_labels))
    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.add_argument('--camera_idx', type=int, help='Index of which video source to use. ', default=1)
    parser.add_argument('--num_detections', type=int, help='Number of detections to return. ', default=3)
    parser.add_argument('--min_area_ratio', type=float, help='segment centroid min area ratio. ', default=0.05)
    parser.set_defaults(keep_aspect_ratio=True)
    args = parser.parse_args()

    print('Loading {} with {} labels.'.format(args.model, args.labels))
    # Initialize engine.
    engine = BasicEngine(args.model)
    labels = load_labels(args.labels)

    # Get native camera resolution.
    cap = cv2.VideoCapture(args.camera_idx)
    camera_res = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    cap.release()

    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
        # Start a thread to recognize and segment objects from camera frames.
        future = executor.submit(recognize_and_segment, args.keep_aspect_ratio,
            args.camera_idx, engine, labels, args.min_area_ratio)

        # Start other threads for the gprc server. 
        server = grpc.server(executor)
        sem_seg_server_pb2_grpc.add_SemanticSegmentationServicer_to_server(
            SemanticSegmentationServicer(args.num_detections, camera_res), server)
        server.add_insecure_port('[::]:50051')
        server.start()

        # Show the value returned by the executor.submit call.
        # This will wait forever unless a runtime error is encountered.
        future.result()

        server.stop(None)
Пример #21
0
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 _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()
Пример #23
0
def main():
    parser = argparse.ArgumentParser()
    model.add_model_flags(parser)
    args = parser.parse_args()
    engine = BasicEngine(args.model_file)
    mic = args.mic if args.mic is None else int(args.mic)
    model.classify_audio(mic,
                         engine,
                         labels_file="config/labels_gc2.raw.txt",
                         result_callback=print_results,
                         sample_rate_hz=int(args.sample_rate_hz),
                         num_frames_hop=int(args.num_frames_hop))
Пример #24
0
 def test_list_edge_tpu_paths(self):
     num_all = len(
         edgetpu_utils.ListEdgeTpuPaths(edgetpu_utils.EDGE_TPU_STATE_NONE))
     unused_engine = BasicEngine(
         test_utils.test_data_path('mobilenet_v1_1.0_224_quant.tflite'))
     num_assigned = len(
         edgetpu_utils.ListEdgeTpuPaths(
             edgetpu_utils.EDGE_TPU_STATE_ASSIGNED))
     self.assertEqual(num_assigned, 1)
     num_available = len(
         edgetpu_utils.ListEdgeTpuPaths(
             edgetpu_utils.EDGE_TPU_STATE_UNASSIGNED))
     self.assertEqual(num_available, num_all - 1)
Пример #25
0
 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)
Пример #26
0
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}')
Пример #27
0
    def test_exhaust_all_edge_tpus(self):
        edge_tpus = edgetpu_utils.ListEdgeTpuPaths(
            edgetpu_utils.EDGE_TPU_STATE_UNASSIGNED)
        # No need to test if there's only one Edge TPU available.
        if len(edge_tpus) <= 1:
            return
        model_path = test_utils.test_data_path(
            'mobilenet_v1_1.0_224_quant_edgetpu.tflite')
        unused_basic_engines = []
        for _ in edge_tpus:
            unused_basic_engines.append(BasicEngine(model_path))

        # Request one more Edge TPU to trigger the exception.
        error_message = None
        expected_message = (
            'Multiple Edge TPUs detected and all have been mapped to at least one '
            'model. If you want to share one Edge TPU with multiple models, '
            'specify `device_path` name.')
        try:
            _ = BasicEngine(model_path)
        except RuntimeError as e:
            error_message = str(e)
        self.assertEqual(expected_message, error_message)
Пример #28
0
    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)
Пример #29
0
 def test_run_inference(self):
     for model in test_utils.get_model_list():
         print('Testing model :', model)
         engine = BasicEngine(test_utils.test_data_path(model))
         input_data = test_utils.generate_random_input(
             1, engine.required_input_array_size())
         latency, ret = engine.run_inference(input_data)
         self.assertEqual(ret.size, engine.total_output_array_size())
         # Check debugging functions.
         self.assertLess(math.fabs(engine.get_inference_time() - latency),
                         0.001)
         raw_output = engine.get_raw_output()
         self.assertEqual(ret.size, raw_output.size)
         for i in range(ret.size):
             if math.isnan(ret[i]) and math.isnan(raw_output[i]):
                 continue
             self.assertLess(math.fabs(ret[i] - raw_output[i]), 0.001)
Пример #30
0
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)