Beispiel #1
0
def train(model_path, data_dir, output_dir):
  """Trains a softmax regression model given data and embedding extractor.
  Args:
    model_path: string, path to embedding extractor.
    data_dir: string, directory that contains training data.
    output_dir: string, directory to save retrained tflite model and label map.
  """
  t0 = time.perf_counter()
  image_paths, labels, label_map = get_image_paths(data_dir)
  train_and_val_dataset, test_dataset = shuffle_and_split(image_paths, labels)
  # Initializes interpreter and allocates tensors here to avoid repeatedly
  # initialization which is time consuming.
  interpreter = make_interpreter(model_path, device=':0')
  interpreter.allocate_tensors()
  print('Extract embeddings for data_train')
  train_and_val_dataset['data_train'] = extract_embeddings(
      train_and_val_dataset['data_train'], interpreter)
  print('Extract embeddings for data_val')
  train_and_val_dataset['data_val'] = extract_embeddings(
      train_and_val_dataset['data_val'], interpreter)
  t1 = time.perf_counter()
  print('Data preprocessing takes %.2f seconds' % (t1 - t0))

  # Construct model and start training
  weight_scale = 5e-2
  reg = 0.0
  feature_dim = train_and_val_dataset['data_train'].shape[1]
  num_classes = np.max(train_and_val_dataset['labels_train']) + 1
  model = SoftmaxRegression(
      feature_dim, num_classes, weight_scale=weight_scale, reg=reg)

  learning_rate = 1e-2
  batch_size = 100
  num_iter = 500
  model.train_with_sgd(
      train_and_val_dataset, num_iter, learning_rate, batch_size=batch_size)
  t2 = time.perf_counter()
  print('Training takes %.2f seconds' % (t2 - t1))

  # Append learned weights to input model and save as tflite format.
  out_model_path = os.path.join(output_dir, 'retrained_model_edgetpu.tflite')
  with open(out_model_path, 'wb') as f:
    f.write(model.serialize_model(model_path))
  print('Model %s saved.' % out_model_path)
  label_map_path = os.path.join(output_dir, 'label_map.txt')
  save_label_map(label_map, label_map_path)
  print('Label map %s saved.' % label_map_path)
  t3 = time.perf_counter()
  print('Saving retrained model and label map takes %.2f seconds' % (t3 - t2))

  retrained_interpreter = make_interpreter(out_model_path, device=':0')
  retrained_interpreter.allocate_tensors()
  test_embeddings = extract_embeddings(test_dataset['data_test'],
                                       retrained_interpreter)
  saved_model_acc = np.mean(
      np.argmax(test_embeddings, axis=1) == test_dataset['labels_test'])
  print('Saved tflite model test accuracy: %.2f%%' % (saved_model_acc * 100))
  t4 = time.perf_counter()
  print('Checking test accuracy takes %.2f seconds' % (t4 - t3))
Beispiel #2
0
def object_frame(inputQueue, outputQueue):
    # interpreter = tf.lite.Interpreter(model_path=TFLITE_PATH+'/model.tflite')
    if not tpu:
        interpreter = tflite.Interpreter(model_path=TFLITE_PATH +
                                         '/model.tflite')
    else:
        if not cust:
            interpreter = make_interpreter(TFLITE_PATH+\
             '/mobilenet_ssd_v2_face_quant_postprocess_edgetpu.tflite')
        if cust:
            interpreter = make_interpreter(TFLITE_PATH+\
             '/detect_edgetpu.tflite')
    interpreter.allocate_tensors()
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    # keep looping
    while True:
        data_out = []
        # check to see if there is a frame in our input queue
        if not inputQueue.empty():
            # grab the frame from the input queue
            img = inputQueue.get()

            if not tpu:
                input_data = np.expand_dims(img, axis=0)
                input_data = input_data / 127.5 - 1
                input_data = np.asarray(input_data, dtype=np.float32)
                interpreter.set_tensor(input_details[0]['index'], input_data)
                interpreter.invoke()
            else:
                common.set_input(interpreter, img)
                interpreter.invoke()
                scale = (1, 1)
                objects = detect.get_objects(interpreter, confThreshold, scale)

            if not tpu:
                boxes = interpreter.get_tensor(output_details[0]['index'])[0]
                classe = interpreter.get_tensor(output_details[1]['index'])[0]
                score = interpreter.get_tensor(output_details[2]['index'])[0]
                data_out = [boxes, classe, score]
            else:
                if objects:
                    for obj in objects:
                        box = obj.bbox
                        # print('bbox:',obj.bbox)
                        xmin = int(box[0])
                        ymin = int(box[1])
                        xmax = int(box[2])
                        ymax = int(box[3])
                        data_out = [[[ymin, xmin, ymax, xmax]], obj.id,
                                    obj.score]

            # print('data_out:',data_out )

            outputQueue.put(data_out)
Beispiel #3
0
def classify_image(model_file, image_file, image_quantization=None):
    """Runs image classification and returns result with the highest score.

  Args:
    model_file: string, model file name.
    image_file: string, image file name.
    image_quantization: (scale: float, zero_point: float), assumed image
      quantization parameters.

  Returns:
    Classification result with the highest score as (index, score) tuple.
  """
    interpreter = make_interpreter(test_data_path(model_file))
    interpreter.allocate_tensors()
    image = test_image(image_file, common.input_size(interpreter))

    input_type = common.input_details(interpreter, 'dtype')
    if np.issubdtype(input_type, np.floating):
        # This preprocessing is specific to MobileNet V1 with floating point input.
        image = (input_type(image) - 127.5) / 127.5

    if np.issubdtype(input_type, np.integer) and image_quantization:
        image = rescale_image(
            image, image_quantization,
            common.input_details(interpreter, 'quantization'), input_type)

    common.set_input(interpreter, image)
    interpreter.invoke()
    return classify.get_classes(interpreter)[0]
Beispiel #4
0
    def __init__(self, config):
        self.labels = self.read_labels(config.label_path)
        LOGGER.debug(f"Available devices: {list_edge_tpus()}")
        LOGGER.debug(f"Loading interpreter with device {config.device}")

        if config.device == "cpu":
            self.interpreter = tflite.Interpreter(
                model_path="/detectors/models/edgetpu/cpu_model.tflite", )
        else:
            self.interpreter = make_interpreter(
                config.model_path,
                device=config.device,
            )

        self.interpreter.allocate_tensors()

        self.tensor_input_details = self.interpreter.get_input_details()
        self.tensor_output_details = self.interpreter.get_output_details()

        if config.model_width and config.model_height:
            self._model_width = config.model_width
            self._model_height = config.model_height
        else:
            self._model_width = self.tensor_input_details[0]["shape"][1]
            self._model_height = self.tensor_input_details[0]["shape"][2]
Beispiel #5
0
    def __init__(self, model_path, device):
        self.__interpreter = edgetpu.make_interpreter(os.path.join(
            model_path, 'edgetpu.tflite'),
                                                      device=device)
        self.__interpreter.allocate_tensors()

        self.__model_shape = common.input_size(self.__interpreter)
Beispiel #6
0
    def __init__(
            self,
            sample_im,
            model_dir='/mounted_folder/models',
            model_name='ssdlite_mobiledet_coco_qat_postprocess_edgetpu.tflite',
            img_size=416,
            conf_thres=0.5,
            classes_ids=[80],
            max_instances_per_class=5):
        # ssdlite_mobiledet_coco_qat_postprocess_edgetpu.tflite   |   ssd_mobilenet_v2_coco_quant_postprocess_edgetpu.tflite
        self.img_size = img_size
        self.conf_thres = conf_thres
        self.classes_ids = classes_ids
        # if isinstance(max_instances_per_class, int):
        #     self.max_instances_per_class = [max_instances_per_class]*len(classes_ids)
        # elif len(max_instances_per_class)== len(classes_ids):
        #     self.max_instances_per_class = max_instances_per_class
        # else:
        #     raise NameError('Inconsistent max instances per class and classes ids')
        self.classes_ids = classes_ids

        # Initialize the TF interpreter
        model_file_path_and_name = os.path.join(model_dir, model_name)
        self.interpreter = edgetpu.make_interpreter(model_file_path_and_name)
        self.interpreter.allocate_tensors()
        self.size = common.input_size(self.interpreter)
Beispiel #7
0
    def __init__(self,
                 appDuration=50,
                 cameraResolution=(304, 304),
                 useVideoPort=True,
                 btDisconMode=False,
                 serialPort='/dev/rfcomm0',
                 mailboxName='abc',
                 minObjectScore=0.35):
        self.cameraResolution = cameraResolution
        self.useVideoPort = useVideoPort
        self.btDisconMode = btDisconMode
        self.serialPort = serialPort
        self.mailboxName = mailboxName
        self.appDuration = appDuration  #seconds to run
        self.minObjectScore = minObjectScore

        modelFile = 'ssd_mobilenet_v2_coco_quant_postprocess_edgetpu.tflite'
        objectLabelsFile = 'coco_labels.txt'
        print("Reading Model: ", modelFile)

        # Initialize the TF interpreter
        self.interpreter = edgetpu.make_interpreter(modelFile)
        self.interpreter.allocate_tensors()

        print("Reading object labels: ", objectLabelsFile)
        self.labels = self.readLabelFile(objectLabelsFile)
        print("Minimal object score: ", self.minObjectScore)
Beispiel #8
0
def detect_person(image_input):

    from pycoral.adapters import common
    from pycoral.adapters import detect
    from pycoral.utils.dataset import read_label_file
    from pycoral.utils.edgetpu import make_interpreter
    label_path = os.path.join(BASE_DIR, 'coral_files', 'coco_labels.txt')
    model_path = os.path.join(
        BASE_DIR, 'coral_files',
        'ssd_mobilenet_v2_coco_quant_postprocess_edgetpu.tflite')
    print(model_path)
    image = Image.fromarray(image_input)
    print(image)

    labels = read_label_file(label_path)
    print("labels", labels)
    interpreter = make_interpreter(model_path)
    print("INterpreter made")
    interpreter.allocate_tensors()
    print("Tensor allocated")
    _, scale = common.set_resized_input(
        interpreter, image.size,
        lambda size: image.resize(size, Image.ANTIALIAS))
    print("Before invoke")
    interpreter.invoke()
    objs = detect.get_objects(interpreter, 0.4, scale)
    print(objs)
    for obj in objs:
        print(labels.get(obj.id, obj.id))
        print('  id:    ', obj.id)
        print('  score: ', obj.score)
        print('  bbox:  ', obj.bbox)

    return False
Beispiel #9
0
    def thread_job(model_name, input_filename, num_inferences, task_type,
                   device):
        """Runs classification or detection job on one Python thread."""
        tid = threading.get_ident()
        logging.info('Thread: %d, # inferences: %d, model: %s', tid,
                     num_inferences, model_name)

        interpreter = make_interpreter(test_utils.test_data_path(model_name),
                                       device)
        interpreter.allocate_tensors()
        with test_utils.test_image(input_filename) as img:
            if task_type == 'classification':
                resize_image = img.resize(common.input_size(interpreter),
                                          Image.NEAREST)
                common.set_input(interpreter, resize_image)
            elif task_type == 'detection':
                common.set_resized_input(
                    interpreter, img.size,
                    lambda size: img.resize(size, Image.NEAREST))
            else:
                raise ValueError(
                    'task_type should be classification or detection, but is given %s'
                    % task_type)
            for _ in range(num_inferences):
                interpreter.invoke()
                if task_type == 'classification':
                    classify.get_classes(interpreter)
                else:
                    detect.get_objects(interpreter)
        logging.info('Thread: %d, model: %s done', tid, model_name)
Beispiel #10
0
 def detection_task(num_inferences):
   tid = threading.get_ident()
   print('Thread: %d, %d inferences for detection task' %
         (tid, num_inferences))
   model_name = 'ssd_mobilenet_v1_coco_quant_postprocess_edgetpu.tflite'
   interpreter = make_interpreter(
       test_utils.test_data_path(model_name), device=':1')
   interpreter.allocate_tensors()
   print('Thread: %d, using device 1' % tid)
   with test_utils.test_image('cat.bmp') as img:
     for _ in range(num_inferences):
       _, scale = common.set_resized_input(
           interpreter,
           img.size,
           lambda size, image=img: image.resize(size, Image.ANTIALIAS))
       interpreter.invoke()
       ret = detect.get_objects(
           interpreter, score_threshold=0.7, image_scale=scale)
       self.assertEqual(len(ret), 1)
       self.assertEqual(ret[0].id, 16)  # cat
       expected_bbox = detect.BBox(
           xmin=int(0.1 * img.size[0]),
           ymin=int(0.1 * img.size[1]),
           xmax=int(0.7 * img.size[0]),
           ymax=int(1.0 * img.size[1]))
       self.assertGreaterEqual(
           detect.BBox.iou(expected_bbox, ret[0].bbox), 0.85)
   print('Thread: %d, done detection task' % tid)
Beispiel #11
0
    def __init__(self, config, bus, verbose=False):
        super().__init__(config, bus)
        bus.register('localized_artf', 'debug_rgbd', 'dropped')

        self.verbose = verbose

        model_path = config.get('model_path', 'subt/models/system/edgetpu.0/model_edgetpu.tflite')
        self.interpreter = edgetpu.make_interpreter(model_path, device=config.get('device'))
        self.interpreter.allocate_tensors()

        self.thresholds = config.get('thresholds', {
            'backpack': 0.84,
            'survivor': 0.95,
            'phone': 1000,  # Disabled.
            'rope': 0.85,
            'helmet': 0.95,
            'fire_extinguisher': 0.85,
            'drill': 0.9,
            'vent': 0.95,
            'cube': 1000  # Disabled.
            })
        self.categories = dict(enumerate(self.thresholds.keys()))
        self.min_threshold = min(self.thresholds.values())

        self.min_depth = config.get('min_depth', 0.1)
        self.max_depth = config.get('max_depth', 10.0)
        self.min_valid_depth_pixels = config.get('min_valid_depth_pixels', 4)

        self.camera_params = config['camera']

        self.batch_size = config.get('batch_size', 1)  # how many images process in one step

        self.input_size = coral_common.input_size(self.interpreter)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--model',
                        help='File path of Tflite model.',
                        required=True)
    parser.add_argument('--labels',
                        help='File path of label file.',
                        required=True)
    parser.add_argument('--picamera',
                        action='store_true',
                        help="Use PiCamera for image capture",
                        default=False)
    parser.add_argument('-t',
                        '--threshold',
                        type=float,
                        default=0.5,
                        help='Classification score threshold')
    args = parser.parse_args()

    print('Loading {} with {} labels.'.format(args.model, args.labels))
    labels = read_label_file(args.labels) if args.labels else {}
    interpreter = make_interpreter(args.model)
    interpreter.allocate_tensors()

    # Initialize video stream
    vs = VideoStream(usePiCamera=args.picamera, resolution=(640, 480)).start()
    time.sleep(1)

    fps = FPS().start()

    while True:
        try:
            # Read frame from video
            screenshot = vs.read()
            image = Image.fromarray(screenshot)
            _, scale = common.set_resized_input(
                interpreter, image.size,
                lambda size: image.resize(size, Image.ANTIALIAS))
            interpreter.invoke()
            objs = detect.get_objects(interpreter, args.threshold, scale)

            draw_objects(image, objs, labels)

            if (cv2.waitKey(5) & 0xFF == ord('q')):
                fps.stop()
                break

            fps.update()
        except KeyboardInterrupt:
            fps.stop()
            break

    print("Elapsed time: " + str(fps.elapsed()))
    print("Approx FPS: :" + str(fps.fps()))

    cv2.destroyAllWindows()
    vs.stop()
    time.sleep(2)
Beispiel #13
0
def detect_func():

    ## parser = argparse.ArgumentParser(
    ## formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    ## parser.add_argument('-m', '--model', required=True,
    ##                     help='File path of .tflite file')
    # parser.add_argument('-i', '--input', required=True,
    #                   help='File path of image to process')
    # parser.add_argument('-l', '--labels', help='File path of labels file')
    # parser.add_argument('-t', '--threshold', type=float, default=0.4,
    #                    help='Score threshold for detected objects')
    # parser.add_argument('-o', '--output',
    #                    help='File path for the result image with annotations')
    # parser.add_argument('-c', '--count', type=int, default=5,
    #                     help='Number of times to run inference')
    #  args = parser.parse_args()

    labels = read_label_file('test_data/coco_labels.txt')
    interpreter = make_interpreter(
        'test_data/ssd_mobilenet_v2_coco_quant_postprocess.tflite')
    interpreter.allocate_tensors()

    image = Image.open('pic.jpg')
    _, scale = common.set_resized_input(
        interpreter, image.size,
        lambda size: image.resize(size, Image.ANTIALIAS))

    print('----INFERENCE TIME----')
    print('Note: The first inference is slow because it includes',
          'loading the model into Edge TPU memory.')
    for _ in range(5):
        start = time.perf_counter()
        interpreter.invoke()
        inference_time = time.perf_counter() - start
        objs = detect.get_objects(interpreter, 0.4, scale)
        print('%.2f ms' % (inference_time * 1000))

    print('-------RESULTS--------')
    if not objs:
        print('No objects detected')

    people_flag = 0
    for obj in objs:
        if obj.id == 0:
            print('people detected!')
            people_flag = 1
        print(labels.get(obj.id, obj.id))
        print('  id:    ', obj.id)
        print('  score: ', obj.score)
        print('  bbox:  ', obj.bbox)

        #  if args.output:
        #    image = image.convert('RGB')
        #    draw_objects(ImageDraw.Draw(image), objs, labels)
        #   image.save(args.output)
        #    image.show()

        return people_flag
Beispiel #14
0
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-m',
                        '--model',
                        required=True,
                        help='File path of .tflite file.')
    parser.add_argument('-i',
                        '--input',
                        required=True,
                        help='Image to be classified.')
    parser.add_argument('-l', '--labels', help='File path of labels file.')
    parser.add_argument('-k',
                        '--top_k',
                        type=int,
                        default=1,
                        help='Max number of classification results')
    parser.add_argument('-t',
                        '--threshold',
                        type=float,
                        default=0.0,
                        help='Classification score threshold')
    parser.add_argument('-c',
                        '--count',
                        type=int,
                        default=5,
                        help='Number of times to run inference')
    args = parser.parse_args()

    labels = read_label_file(args.labels) if args.labels else {}

    interpreter = make_interpreter(*args.model.split('@'))
    interpreter.allocate_tensors()

    _, height, width = interpreter.get_input_details()[0]['shape']
    size = [height, width]
    image = Image.open(args.input).resize(size, Image.ANTIALIAS)
    common.set_input(interpreter, image)

    trigger = GPIO("/dev/gpiochip2", 13, "out")  # pin 37

    print('----INFERENCE TIME----')
    print('Note: The first inference on Edge TPU is slow because it includes',
          'loading the model into Edge TPU memory.')
    #for _ in range(args.count):
    while (1):
        start = time.perf_counter()
        trigger.write(True)
        time.sleep(0.0005)
        trigger.write(False)
        #interpreter.invoke()
        inference_time = time.perf_counter() - start
        #classes = classify.get_classes(interpreter, args.top_k, args.threshold)
        print('%.1fms' % (inference_time * 1000))

    print('-------RESULTS--------')
    for c in classes:
        print('%s: %.5f' % (labels.get(c.id, c.id), c.score))
Beispiel #15
0
def main():
    default_model_dir = 'models'
    default_detection_model = 'facedetector_quant_postprocess_edgetpu.tflite'
    default_classification_model = 'classifier_quant_edgetpu.tflite'
    default_labels = 'labels.txt'
    parser = argparse.ArgumentParser()

    parser.add_argument('--detection_model',
                        help='.tflite detection model path',
                        default=os.path.join(default_model_dir,
                                             default_detection_model))
    parser.add_argument('--classification_model',
                        help='.tflite classification model path',
                        default=os.path.join(default_model_dir,
                                             default_classification_model))
    parser.add_argument('--image',
                        help='Path of the image.',
                        default="test_images/test_image_9_faces.jpg")
    parser.add_argument('--threshold',
                        type=float,
                        default=0.3,
                        help='classifier score threshold')
    args = parser.parse_args()

    print('Loading {}'.format(args.detection_model))
    detection_interpreter = make_interpreter(args.detection_model, device=':0')
    detection_interpreter.allocate_tensors()

    print('Loading {}'.format(args.classification_model))
    classification_interpreter = make_interpreter(args.classification_model,
                                                  device=':0')
    classification_interpreter.allocate_tensors()

    print('Loading {}'.format(args.image))
    image = load_image(args.image)

    boxes, predictions = detect_and_classify_faces(detection_interpreter,
                                                   classification_interpreter,
                                                   image, args.threshold)

    final_image = append_boxes_to_img(image, boxes, predictions)

    cv2.imshow('image', final_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Beispiel #16
0
 def test_invoke_with_dma_buffer_model_not_ready(self):
     interpreter = edgetpu.make_interpreter(self._default_test_model_path())
     input_size = 224 * 224 * 3
     # Note: Exception is triggered because interpreter.allocate_tensors() is not
     # called.
     with self.assertRaisesRegex(
             RuntimeError, 'Invoke called on model that is not ready.'):
         edgetpu.invoke_with_dmabuffer(interpreter._native_handle(), 0,
                                       input_size)
Beispiel #17
0
 def test_run_inference_smaller_input_size(self):
     interpreter = edgetpu.make_interpreter(self._default_test_model_path())
     interpreter.allocate_tensors()
     input_size = required_input_array_size(interpreter)
     input_data = test_utils.generate_random_input(1, input_size - 1)
     with self.assertRaisesRegex(ValueError,
                                 'input size=150527, expected=150528'):
         self._run_inference_with_different_input_types(
             interpreter, input_data)
Beispiel #18
0
 def __init__(self):
     self.face_model = os.path.join(
         os.path.dirname(__file__),
         'models/mobilenet_ssd_v2_face_quant_postprocess_edgetpu.tflite')
     self.max_faces = 10
     self.threshold = FACE_DETECTOR_THRESHOLD
     self.interpreter = make_interpreter(self.face_model)
     self.interpreter.allocate_tensors()
     self.inference_size = input_size(self.interpreter)
    def __init__(self, model, device=':0', keep_aspect_ratio=True):

        self.img_sub = rospy.Subscriber("~input", ImageMsg, self.callback)
        self.interpreter = make_interpreter(model, device=device)
        self.interpreter.allocate_tensors()
        self.model_input_width, self.model_input_height = common.input_size(
            self.interpreter)
        self.keep_aspect_ratio = keep_aspect_ratio
        self.bridge = CvBridge()
Beispiel #20
0
 def test_invoke_with_mem_buffer_model_not_ready(self):
     interpreter = edgetpu.make_interpreter(self._default_test_model_path())
     input_size = 224 * 224 * 3
     np_input = np.zeros(input_size, dtype=np.uint8)
     # Note: Exception is triggered because interpreter.allocate_tensors() is not
     # called.
     with self.assertRaisesRegex(
             RuntimeError, 'Invoke called on model that is not ready.'):
         edgetpu.invoke_with_membuffer(interpreter._native_handle(),
                                       np_input.ctypes.data, input_size)
Beispiel #21
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()
    interpreter_a = make_interpreter(classification_model, device=':0')
    interpreter_a.allocate_tensors()
    interpreter_b = make_interpreter(detection_model, device=':0')
    interpreter_b.allocate_tensors()

    with open_image(image_name) as image:
        size_a = common.input_size(interpreter_a)
        common.set_input(interpreter_a, image.resize(size_a, Image.NEAREST))
        _, scale_b = common.set_resized_input(
            interpreter_b, image.size,
            lambda size: image.resize(size, Image.NEAREST))

    num_iterations = (num_inferences + batch_size - 1) // batch_size
    for _ in range(num_iterations):
        for _ in range(batch_size):
            interpreter_a.invoke()
            classify.get_classes(interpreter_a, top_k=1)
        for _ in range(batch_size):
            interpreter_b.invoke()
            detect.get_objects(interpreter_b,
                               score_threshold=0.,
                               image_scale=scale_b)
    return time.perf_counter() - start_time
Beispiel #22
0
    def classification_job(classification_model, image_name, num_inferences):
        """Runs classification job."""
        interpreter = make_interpreter(classification_model, device=':0')
        interpreter.allocate_tensors()
        size = common.input_size(interpreter)
        with open_image(image_name) as image:
            common.set_input(interpreter, image.resize(size, Image.NEAREST))

        for _ in range(num_inferences):
            interpreter.invoke()
            classify.get_classes(interpreter, top_k=1)
Beispiel #23
0
    def __init__(self):
        super().__init__("CoralWorker")

        self.interpreter = make_interpreter(
            os.path.join(
                ExopticonWorker.get_data_dir(),
                "ssd_mobilenet_v2_coco_quant_postprocess_edgetpu.tflite"))
        self.interpreter.allocate_tensors()
        input_size = common.input_size(self.interpreter)
        self.labels = read_label_file(
            os.path.join(ExopticonWorker.get_data_dir(), "coco_labels.txt"))
def _get_ref_result(ref_model, input_tensors):
    interpreter = make_interpreter(test_utils.test_data_path(ref_model))
    interpreter.allocate_tensors()
    input_details = interpreter.get_input_details()
    assert len(input_details) == 1
    output_details = interpreter.get_output_details()
    assert len(output_details) == 1

    interpreter.tensor(input_details[0]['index'])()[0][:, :] = input_tensors[0]
    interpreter.invoke()
    return np.array(interpreter.tensor(output_details[0]['index'])())
Beispiel #25
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--model',
                        required=True,
                        help='Path of the segmentation model.')
    parser.add_argument('--input',
                        required=True,
                        help='File path of the input image.')
    parser.add_argument('--output',
                        default='semantic_segmentation_result.jpg',
                        help='File path of the output image.')
    parser.add_argument(
        '--keep_aspect_ratio',
        action='store_true',
        default=False,
        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.'))
    args = parser.parse_args()

    interpreter = make_interpreter(args.model, device=':0')
    interpreter.allocate_tensors()
    width, height = common.input_size(interpreter)

    img = Image.open(args.input)
    if args.keep_aspect_ratio:
        resized_img, _ = common.set_resized_input(
            interpreter, img.size,
            lambda size: img.resize(size, Image.ANTIALIAS))
    else:
        resized_img = img.resize((width, height), Image.ANTIALIAS)
        common.set_input(interpreter, resized_img)

    interpreter.invoke()

    result = segment.get_output(interpreter)
    if len(result.shape) == 3:
        result = np.argmax(result, axis=-1)

    # If keep_aspect_ratio, we need to remove the padding area.
    new_width, new_height = resized_img.size
    result = result[:new_height, :new_width]
    mask_img = Image.fromarray(label_to_color_image(result).astype(np.uint8))

    # Concat resized input image and processed segmentation results.
    output_img = Image.new('RGB', (2 * new_width, new_height))
    output_img.paste(resized_img, (0, 0))
    output_img.paste(mask_img, (width, 0))
    output_img.save(args.output)
    print('Done. Results saved at', args.output)
def _make_runner(model_paths, devices):
  """Constructs PipelinedModelRunner given model paths and devices."""
  print('Using devices: ', devices)
  print('Using models: ', model_paths)

  if len(model_paths) != len(devices):
    raise ValueError('# of devices and # of model_paths should match')

  interpreters = [make_interpreter(m, d) for m, d in zip(model_paths, devices)]
  for interpreter in interpreters:
    interpreter.allocate_tensors()
  return pipeline.PipelinedModelRunner(interpreters)
Beispiel #27
0
def main():
    default_model_dir = '../all_models'
    default_model = 'mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite'
    default_labels = 'coco_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(
        '--top_k',
        type=int,
        default=3,
        help='number of categories with highest score to display')
    parser.add_argument('--camera_idx',
                        type=int,
                        help='Index of which video source to use. ',
                        default=0)
    parser.add_argument('--threshold',
                        type=float,
                        default=0.1,
                        help='classifier score threshold')
    args = parser.parse_args()

    print('Loading {} with {} labels.'.format(args.model, args.labels))
    interpreter = make_interpreter(args.model)
    interpreter.allocate_tensors()
    labels = read_label_file(args.labels)
    inference_size = input_size(interpreter)

    cap = cv2.VideoCapture(args.camera_idx)

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        cv2_im = frame

        cv2_im_rgb = cv2.cvtColor(cv2_im, cv2.COLOR_BGR2RGB)
        cv2_im_rgb = cv2.resize(cv2_im_rgb, inference_size)
        run_inference(interpreter, cv2_im_rgb.tobytes())
        objs = get_objects(interpreter, args.threshold)[:args.top_k]
        cv2_im = append_objs_to_img(cv2_im, inference_size, objs, labels)

        cv2.imshow('frame', cv2_im)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
def run_two_models_one_tpu(classification_model, detection_model, image_name,
                           num_inferences, batch_size):
    start_time = time.perf_counter()
    interpreter_a = make_interpreter(classification_model, device=':0')
    interpreter_a.allocate_tensors()
    interpreter_b = make_interpreter(detection_model, device=':0')
    interpreter_b.allocate_tensors()

    identification = []
    classification = []

    with open_image(image_name) as image:
        size_a = common.input_size(interpreter_a)
        common.set_input(interpreter_a, image.resize(size_a, Image.NEAREST))
        _, scale_b = common.set_resized_input(
            interpreter_b, image.size,
            lambda size: image.resize(size, Image.NEAREST))

    num_iterations = (num_inferences + batch_size - 1) // batch_size
    for _ in tqdm(range(num_iterations)):
        for _ in range(batch_size):
            identification_start_time = time.perf_counter()
            interpreter_b.invoke()
            detect.get_objects(interpreter_b, score_threshold=0.,
                               image_scale=scale_b)
            identification.append(time.perf_counter() -
                                  identification_start_time)
        for _ in range(batch_size):
            classification_start_time = time.perf_counter()
            interpreter_a.invoke()
            result1 = classify.get_classes(interpreter_a, top_k=4)
            interpreter_a.invoke()
            result2 = classify.get_classes(interpreter_a, top_k=4)
            interpreter_a.invoke()
            result3 = classify.get_classes(interpreter_a, top_k=4)

            classification.append(time.perf_counter() -
                                  classification_start_time)
    total_time = time.perf_counter() - start_time
    return total_time, identification, classification
Beispiel #29
0
def main():
  parser = argparse.ArgumentParser(
      formatter_class=argparse.ArgumentDefaultsHelpFormatter)
  parser.add_argument('-m', '--model', required=True,
                      help='File path of .tflite file.')
  parser.add_argument('-i', '--input',
                      help='Image to be classified.')
  parser.add_argument('-l', '--labels',
                      help='File path of labels file.')
  parser.add_argument('-k', '--top_k', type=int, default=1,
                      help='Max number of classification results')
  parser.add_argument('-t', '--threshold', type=float, default=0.0,
                      help='Classification score threshold')
  parser.add_argument('-c', '--count', type=int, default=5,
                      help='Number of times to run inference')
  args = parser.parse_args()

  labels = read_label_file(args.labels) if args.labels else {}

  interpreter = make_interpreter(*args.model.split('@'))
  interpreter.allocate_tensors()

  _, height, width = interpreter.get_input_details()[0]['shape']
  size = [height, width]

  trigger = GPIO("/dev/gpiochip2", 13, "out")  # pin 37

  print('----INFERENCE TIME----')
  print('Note: The first inference on Edge TPU is slow because it includes',
        'loading the model into Edge TPU memory.')
  #for i in range(1,351):
  while 1:
    #input_image_name = "./testSample/img_"+ str(i) + ".jpg"
    #input_image_name = "./testSample/img_1.jpg"
    #image = Image.open(input_image_name).resize(size, Image.ANTIALIAS)
    arr = numpy.random.randint(0,255,(28,28), dtype='uint8')
    image = Image.fromarray(arr, 'L').resize(size, Image.ANTIALIAS)
    common.set_input(interpreter, image)

    start = time.perf_counter()
    trigger.write(True)
    interpreter.invoke()
    trigger.write(False)
    inference_time = time.perf_counter() - start
    print('%.6fms' % (inference_time * 1000))
    
    classes = classify.get_classes(interpreter, args.top_k, args.threshold)

    print('RESULTS for image ', 1)
    for c in classes:
      print('%s: %.6f' % (labels.get(c.id, c.id), c.score))
    def _train_and_test(self, model_path, train_points, test_points,
                        keep_classes):
        # Train.
        engine = ImprintingEngine(model_path, keep_classes)

        extractor = make_interpreter(engine.serialize_extractor_model(),
                                     device=':0')
        extractor.allocate_tensors()

        for point in train_points:
            for image in point.images:
                with test_utils.test_image('imprinting', image) as img:
                    set_input(extractor, img)
                    extractor.invoke()
                    embedding = classify.get_scores(extractor)
                    self.assertEqual(len(embedding), engine.embedding_dim)
                    engine.train(embedding, point.class_id)

        # Test.
        trained_model = engine.serialize_model()
        classifier = make_interpreter(trained_model, device=':0')
        classifier.allocate_tensors()

        self.assertEqual(len(classifier.get_output_details()), 1)

        if not keep_classes:
            self.assertEqual(len(train_points),
                             classify.num_classes(classifier))

        for point in test_points:
            with test_utils.test_image('imprinting', point.image) as img:
                set_input(classifier, img)
                classifier.invoke()
                top = classify.get_classes(classifier, top_k=1)[0]
                self.assertEqual(top.id, point.class_id)
                self.assertGreater(top.score, point.score)

        return trained_model