def main():
  parser = argparse.ArgumentParser()
  parser.add_argument(
    '--model',
    help='TensorFlow Liteモデルのファイルパス', required=True)
  parser.add_argument(
    '--label', help='ラベルファイルのファイルパス', required=True)
  parser.add_argument(
    '--npz', help='mnist.npzのファイルパス', required=True)
  parser.add_argument(
    '--print', help='結果を標準出力へ出力するか否か',
    type=bool,default=False, required=False)

  args = parser.parse_args()

  # ラベルの読み込み
  labels = ReadLabelFile(args.label)
  # モデルを読込、エンジンを初期化する
  engine = ClassificationEngine(args.model)
  
  count = 0
  # mnist.npzを読込読み込んだ分、画像分類を実行する
  for input_tensor in LoadData(args.npz):
    results = engine.ClassifyWithInputTensor(
      input_tensor=input_tensor.flatten(), threshold=0.1, top_k=3)
    # 出力オプションが真だった場合は結果を出力する
    if(args.print):
      print('-----------{0:6}----------'.format(count))

      for result in results:
        print(labels[result[0]], end="")
        print('  Score : ', result[1])
    count += 1
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--model',
                        help='File path of Tflite model.',
                        required=True)
    parser.add_argument('--label',
                        help='File path of label file.',
                        required=True)
    args = parser.parse_args()

    labels = dataset_utils.ReadLabelFile(args.label)
    engine = ClassificationEngine(args.model)

    with picamera.PiCamera() as camera:
        camera.resolution = (640, 480)
        camera.framerate = 30
        _, height, width, _ = engine.get_input_tensor_shape()
        camera.start_preview()
        try:
            stream = io.BytesIO()
            for _ in camera.capture_continuous(stream,
                                               format='rgb',
                                               use_video_port=True,
                                               resize=(width, height)):
                stream.truncate()
                stream.seek(0)
                input_tensor = np.frombuffer(stream.getvalue(), dtype=np.uint8)
                start_ms = time.time()
                results = engine.ClassifyWithInputTensor(input_tensor, top_k=1)
                elapsed_ms = time.time() - start_ms
                if results:
                    camera.annotate_text = '%s %.2f\n%.2fms' % (labels[
                        results[0][0]], results[0][1], elapsed_ms * 1000.0)
        finally:
            camera.stop_preview()
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--model',
                        help='File path of Tflite model.',
                        required=True)
    parser.add_argument('--label',
                        help='File path of label file.',
                        required=True)
    parser.add_argument('--image',
                        help='File path of the image to be recognized.',
                        required=True)
    args = parser.parse_args()

    # Prepare labels.
    labels = ReadLabelFile(args.label)
    # Initialize engine.
    engine = ClassificationEngine(args.model)

    # Read input image and convert to tensor
    img = Image.open(args.image)
    img = img.convert('L')
    img = ImageOps.invert(img)
    input_tensor_shape = engine.get_input_tensor_shape()
    if (input_tensor_shape.size != 4 or input_tensor_shape[3] != 1
            or input_tensor_shape[0] != 1):
        raise RuntimeError(
            'Invalid input tensor shape! Expected: [1, height, width, 3]')
    _, height, width, _ = input_tensor_shape
    img = img.resize((width, height), Image.NEAREST)
    input_tensor = numpy.asarray(img).flatten()

    # Run inference.
    for result in engine.ClassifyWithInputTensor(input_tensor=input_tensor,
                                                 threshold=0.1,
                                                 top_k=3):
        print('---------------------------')
        print(labels[result[0]])
        print('Score : ', result[1])

    # for time measurement
    for i in range(5000):
        for result in engine.ClassifyWithInputTensor(input_tensor=input_tensor,
                                                     threshold=0.1,
                                                     top_k=3):
            pass
Exemple #4
0
  def classification_job(classification_model, image_name, num_inferences):
    """Runs classification job."""
    engine = ClassificationEngine(classification_model)
    with open_image(image_name) as image:
      tensor = get_input_tensor(engine, image)

    # Using `ClassifyWithInputTensor` to exclude image down-scale cost.
    for _ in range(num_inferences):
      engine.ClassifyWithInputTensor(tensor, top_k=1)
def main():
    fashion_mnist = keras.datasets.fashion_mnist
    (train_images, train_labels), (test_images,
                                   test_labels) = fashion_mnist.load_data()

    engine = ClassificationEngine("model_edgetpu.tflite")
    for result in engine.ClassifyWithInputTensor(numpy.asarray(
            test_images[0]).flatten(),
                                                 top_k=3):
        print("------------------")
        print(test_labels[result[0]])
        print("Score: ", result[1])
def classify(path_list_, same_list_, THREAD):
    engine = ClassificationEngine(FaceNet_weight)
    pred = bool()
    correct = 0
    for same_index, pair in enumerate(path_list_):
        picture1_embs = []
        picture2_embs = []

        for k, img in enumerate(pair):
            img = Image.open(img)
            img = np.asarray(img).flatten()
            result = engine.ClassifyWithInputTensor(img,
                                                    top_k=200,
                                                    threshold=-0.5)
            result.sort(key=takeSecond)

            if k == 1:
                for i in range(0, len(result)):
                    picture1_embs.append(result[i][1])
            else:
                for i in range(0, len(result)):
                    picture2_embs.append(result[i][1])

        picture1_embs = np.array(picture1_embs)
        picture2_embs = np.array(picture2_embs)

        diff = np.mean(np.square(picture1_embs - picture2_embs))

        if diff < THREAD:
            pred = True
        else:
            pred = False

        if pred == same_list_[same_index]:
            correct += 1

    accuracy = correct / len(path_list_)

    return accuracy
Exemple #7
0
class ObjectClassification(object):
    def __init__(self, model, labels, threshold, camera_res):
        self.threshold = threshold
        self.engine = ClassificationEngine(model)
        self.labels = self.read_labels(labels)
        self.camera_res = camera_res

    def read_labels(self, file_path):
        with open(file_path, 'r') as f:
            lines = f.readlines()
        labels = {}
        for line in lines:
            pair = line.strip().split(maxsplit=1)
            labels[int(pair[0])] = pair[1].strip()
        return labels

    def pre_process(self, frame):
        frame_expanded = np.expand_dims(frame.get(), axis=0)
        return frame_expanded.flatten()

    def post_process(self, objects):
        processed_objects = []
        for label_id, score in objects:
            processed_objects.append({
                "label": str(self.labels[label_id]),
                "confidence": float(score)
            })
        return processed_objects

    def return_objects(self, frame):
        tensor = self.pre_process(frame)

        detected_objects = self.engine.ClassifyWithInputTensor(
            tensor, threshold=self.threshold, top_k=3)

        objects = self.post_process(detected_objects)
        # LOGGER.info(self.engine.get_inference_time())
        return objects
Exemple #8
0
def run_two_models_one_tpu(classification_model, detection_model, image_name,
                           num_inferences, batch_size):
  """Runs two models ALTERNATIVELY using one Edge TPU.

  It runs classification model `batch_size` times and then switch to run
  detection model `batch_size` time until each model is run `num_inferences`
  times.

  Args:
    classification_model: string, path to classification model
    detection_model: string, path to detection model.
    image_name: string, path to input image.
    num_inferences: int, number of inferences to run for each model.
    batch_size: int, indicates how many inferences to run one model before
      switching to the other one.

  Returns:
    double, wall time it takes to finish the job.
  """
  start_time = time.perf_counter()
  engine_a = ClassificationEngine(classification_model)
  # `engine_b` shares the same Edge TPU as `engine_a`
  engine_b = DetectionEngine(detection_model, engine_a.device_path())
  with open_image(image_name) as image:
    # Resized image for `engine_a`, `engine_b`.
    tensor_a = get_input_tensor(engine_a, image)
    tensor_b = get_input_tensor(engine_b, image)

  num_iterations = (num_inferences + batch_size - 1) // batch_size
  for _ in range(num_iterations):
    # Using `ClassifyWithInputTensor` and `DetectWithInputTensor` on purpose to
    # exclude image down-scale cost.
    for _ in range(batch_size):
      engine_a.ClassifyWithInputTensor(tensor_a, top_k=1)
    for _ in range(batch_size):
      engine_b.DetectWithInputTensor(tensor_b, top_k=1)
  return time.perf_counter() - start_time