Пример #1
0
    def load(self, path, shape_dict=None):
        # pylint: disable=C0415
        import tflite.Model as model

        with open(path, "rb") as tf_graph:
            content = tf_graph.read()

        # tflite.Model.Model is tflite.Model in 1.14 and 2.1.0
        try:
            tflite_model = model.Model.GetRootAsModel(content, 0)
        except AttributeError:
            tflite_model = model.GetRootAsModel(content, 0)

        try:
            version = tflite_model.Version()
            logger.debug("tflite version %s", version)
        except Exception:
            raise TVMCException("input file not tflite")

        if version != 3:
            raise TVMCException("input file not tflite version 3")

        logger.debug("tflite_input_type")
        input_shapes, dtype_dict = TFLiteFrontend._input_type(tflite_model)
        if shape_dict is not None:
            input_shapes.update(shape_dict)

        logger.debug(
            "parse TFLite model and convert into Relay computation graph")
        mod, params = relay.frontend.from_tflite(tflite_model,
                                                 shape_dict=input_shapes,
                                                 dtype_dict=dtype_dict)
        return mod, params
Пример #2
0
from tflite import Model
import numpy as np
from collections import OrderedDict
from facial_lm_model import FacialLM_Model
from utils import GetKeysDict
import torch

data = open("model_weights/face_landmark.tflite", "rb").read()
model = Model.GetRootAsModel(data, 0)

tflite_graph = model.Subgraphs(0)
tflite_graph.Name()

# Tensor name to index mapping
tflite_tensor_dict = {}
for i in range(tflite_graph.TensorsLength()):
    tflite_tensor_dict[tflite_graph.Tensors(i).Name().decode("utf8")] = i


def get_weights(tensor_name):
    index = tflite_tensor_dict[tensor_name]
    tensor = tflite_graph.Tensors(index)

    buffer = tensor.Buffer()
    shape = [tensor.Shape(i) for i in range(tensor.ShapeLength())]

    weights = model.Buffers(buffer).DataAsNumpy()
    weights = weights.view(dtype=np.float32)
    weights = weights.reshape(shape)
    return weights
Пример #3
0
import tflite
from tflite import Model
import pdb


buf = open('weights/yolov3_quant.tflite', 'rb').read()
model = Model.GetRootAsModel(buf, 0)
subgraph = model.Subgraphs(0)
# print(dir(subgraph))
print('subgraph.OperatorsLength()', subgraph.OperatorsLength())

n_ops = subgraph.OperatorsLength() 
print('n_ops', n_ops)
i_op = 0
# op = subgraph.Operators(i_op)
# print("Inputs", op.Inputs(0), op.Inputs(1))
# print(op.BuiltinOptionsType())
# assert(op.BuiltinOptionsType() == tflite.BuiltinOptions.ReshapeOptions)
for i_op in range(n_ops):
    op = subgraph.Operators(i_op)
    if op.BuiltinOptionsType() == tflite.BuiltinOptions.ReshapeOptions:
        print(i_op)
        i_out = op.Outputs(0)
        out = subgraph.Tensors(i_out) 
        print('out', out.Name())
        op_opt = op.BuiltinOptions()
        opt = tflite.ReshapeOptions()
        opt.Init(op_opt.Bytes, op_opt.Pos)
        print(opt.NewShapeAsNumpy())

# print(op.BuiltinOptions())