Esempio n. 1
0
def run_example():
  runtime.__full_version__

  current_path = Path(__file__).absolute().parent
  path = current_path.joinpath("../assets/quantized_models/MNISTnet_uint8_quant_without_softmax.tflite")
 
  # Load a model and compile the model
  sess = session.create(str(path))
 
  # Print the model summary
  sess.print_summary()
  
  # Print the first input tensor shape and dimensions
  input_meta = sess.inputs()[0]
  print(input_meta)
  
  # Generate the random input tensor according to the input shape
  input = np.random.randint(-128, 127, input_meta.shape(), dtype=np.int8)
  
  # Run the inference
  outputs = sess.run(input)
  
  print("== Output ==")
  print(outputs)
  print(outputs[0].numpy())
Esempio n. 2
0
def classify(image_path):
    from furiosa.runtime import session
    from helper import load_labels

    image = Image.open(image_path)
    current_path = Path(__file__).absolute().parent
    path = current_path \
        .joinpath("../assets/quantized_models/imagenet_224x224_mobilenet_v1_uint8_quantization-aware-trained_dm_1.0_without_softmax.tflite")

    print(f"Loading and compiling the model {path}")
    with session.create(str(path)) as sess:
        print(f"Model has been compiled successfully")

        print("Model input and output:")
        print(sess.print_summary())

        _, height, width, channel = sess.input(0).shape()
        image = image.convert('RGB').resize((width, height))

        data = np.zeros((width, height, channel), np.uint8)
        data[:width, :height, :channel] = image
        data = np.reshape(data, (1, width, height, channel))

        start_time = time.time()
        outputs = sess.run(data)
        print('Prediction elapsed {:.2f} secs'.format(time.time() -
                                                      start_time))

        classified = np.squeeze(outputs[0].numpy())
        imagenet_labels = load_labels(
            current_path.joinpath('../assets/labels/ImageNetLabels.txt'))
        Class = collections.namedtuple('Class', ['id', 'score'])
        objects = []
        for idx, n in np.ndenumerate(classified):
            objects.append(Class(idx[0], n))

        objects.sort(key=lambda x: x[1], reverse=True)
        print("[Top 5 scores:]")
        for object in objects[:5]:
            print("{}: {}".format(imagenet_labels[object.id], object.score))
Esempio n. 3
0
def random_input_inference(model_path, num_inf):
    from furiosa.runtime import session

    print(f"Loading and compiling the model {model_path}")
    with session.create(str(model_path)) as sess:
        print(f"Model has been compiled successfully")

        print("Model input and output:")
        print(sess.print_summary())

        for idx in range(num_inf):
            print(f'Iteration {idx}...')

            inputs = []
            for session_input in sess.inputs():
                if session_input.dtype() == DataType.UINT8:
                    inputs.append(
                        np.random.randint(0,
                                          255,
                                          session_input.shape(),
                                          dtype=np.uint8))
                elif session_input.dtype() == DataType.INT8:
                    inputs.append(
                        np.random.randint(-128,
                                          127,
                                          session_input.shape(),
                                          dtype=np.int8))
                elif session_input.dtype() == DataType.FLOAT32:
                    inputs.append(
                        np.random.random(session_input.shape()).astype(
                            np.float32))
                else:
                    raise Exception(
                        f"Unsupported DataType({session_input.dtype()}) of input."
                    )
            sess.run(inputs)
Esempio n. 4
0
 def __init__(self, model_path):
     self.nux_sess = session.create(model=model_path)
     super().__init__(model_path)
Esempio n. 5
0
 def __init__(self, model_path):
     self.session = session.create(model=model_path)