Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagating model type information (input tensors) to rust? #16

Open
winding-lines opened this issue Sep 8, 2019 · 3 comments
Open

Comments

@winding-lines
Copy link
Contributor

@boncheolgu,

My code fails with the following error:

Input tensor  Ok(TensorInfo { name: "normalized_input_image_tensor", element_kind: kTfLiteFloat32, dims: [1, 300, 300, 3] })
Error: ErrorMessage { msg: "Invalid type reference of `kTfLiteUInt8` to the original type `kTfLiteFloat32`" }

This is the relevant code, this is lifted from your example.

pub fn do_it(model_file: &Path, input: &[u8]) -> Result<(), Error> {
    let model = FlatBufferModel::build_from_file(model_file)?;
    let resolver = BuiltinOpResolver::default();

    let builder = InterpreterBuilder::new(&model, &resolver)?;
    let mut interpreter = builder.build()?;

    interpreter.allocate_tensors()?;
    let input_indexes = interpreter.inputs().to_vec();
    eprintln!(
        "Input tensor  {:?}",
        interpreter.tensor_info(input_indexes[0])
    );
    let buffer = interpreter.tensor_data_mut(input_indexes[0])?;
    buffer.copy_from_slice(input);

    println!("=== Pre-invoke Interpreter State ===");
    interpreter.print_state();

    interpreter.invoke()?;

    println!("\n\n=== Post-invoke Interpreter State ===");
    interpreter.print_state();
    Ok(())
}

The error makes a lot of sense and I will play with the API to make fix it. Have you done any thinking on how we could propagate this information to the rust layer?

One option would be to look at the model and generate code for the API. I am not sure if this is something you thought about and discarded or if it's something you have on the roadmap. Would love to collaborate on it, if you are interested.

@winding-lines winding-lines changed the title Propagating type information to rust? Propagating model type information (input tensors) to rust? Sep 8, 2019
@boncheolgu
Copy link
Owner

Currently, tensor's type can be obtained by TensorInfo::element_kind. I didn't understand what you meant by "generate code for the API". Could you explain it more?

@winding-lines
Copy link
Contributor Author

I am thinking of a macro that would take a model (can be a stand-in for a family of compatible models) and would generate a typed rust interface for it.

I am not sure if this would be useful, what do you think?

@boncheolgu
Copy link
Owner

I still don't get it what you suggested. In order to generate APIs, models should be given at compile time, but it is not always possible. e.g. FlatBufferModel is created from a file. However, typed interfaces are always welcome. I'll appreciate it if you try that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants