1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use std::{
    fmt::{self, Display, Formatter},
    io::Write,
    path::{Path, PathBuf},
};
use strum::VariantNames;
use anyhow::{Error, Context};
use tflite::{
    FlatBufferModel, Interpreter, InterpreterBuilder,
    ops::builtin::BuiltinOpResolver,
};

use crate::Format;

#[derive(Debug, Clone, PartialEq, structopt::StructOpt)]
pub struct ModelInfo {
    #[structopt(
        help = "The TensorFlow Lite model to inspect",
        parse(from_os_str)
    )]
    file: PathBuf,
    #[structopt(
        short,
        long,
        help = "The format to print output in",
        default_value = "text",
        possible_values = Format::VARIANTS,
        parse(try_from_str)
    )]
    format: Format,
}

impl ModelInfo {
    pub fn execute(self) -> Result<(), Error> {
        let interpreter =
            load_model(&self.file).context("Unable to load the model")?;

        let info = parse_info(&interpreter);

        match self.format {
            Format::Text => print_info(&info),
            Format::Json => {
                let mut stdout = std::io::stdout();
                serde_json::to_writer_pretty(stdout.lock(), &info)
                    .context("Unable to print to stdout")?;
                writeln!(stdout)?;
            },
        }

        Ok(())
    }
}

fn print_info(info: &ModelDescription) {
    println!("Ops: {}", info.ops);

    println!("Inputs:");
    for input in &info.inputs {
        println!("\t{}", input);
    }

    println!("Outputs:");
    for output in &info.outputs {
        println!("\t{}", output);
    }
}

#[derive(Debug, Clone, PartialEq, serde::Serialize)]
struct ModelDescription {
    inputs: Vec<TensorInfo>,
    outputs: Vec<TensorInfo>,
    ops: usize,
}

#[derive(Debug, Clone, PartialEq, serde::Serialize)]
struct TensorInfo {
    name: String,
    element_kind: String,
    dims: Vec<usize>,
}

impl Display for TensorInfo {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}[", self.name, self.element_kind)?;

        for (i, dim) in self.dims.iter().enumerate() {
            if i > 0 {
                write!(f, ", ")?;
            }

            write!(f, "{}", dim)?;
        }
        write!(f, "]")?;

        Ok(())
    }
}

impl From<tflite::context::TensorInfo> for TensorInfo {
    fn from(t: tflite::context::TensorInfo) -> Self {
        let tflite::context::TensorInfo {
            name,
            element_kind,
            dims,
        } = t;

        TensorInfo {
            name,
            dims,
            element_kind: format!("{:?}", element_kind)
                .trim_start_matches("kTfLite")
                .to_string(),
        }
    }
}

fn parse_info(
    interpreter: &Interpreter<'static, BuiltinOpResolver>,
) -> ModelDescription {
    let inputs = interpreter
        .inputs()
        .iter()
        .map(|ix| interpreter.tensor_info(*ix).unwrap())
        .map(TensorInfo::from)
        .collect();
    let outputs = interpreter
        .outputs()
        .iter()
        .map(|ix| interpreter.tensor_info(*ix).unwrap())
        .map(TensorInfo::from)
        .collect();

    ModelDescription {
        inputs,
        outputs,
        ops: interpreter.nodes_size(),
    }
}

fn load_model(
    filename: &Path,
) -> Result<Interpreter<'static, BuiltinOpResolver>, Error> {
    let raw = std::fs::read(filename).with_context(|| {
        format!("Unable to read \"{}\"", filename.display())
    })?;

    let flat_buffer = FlatBufferModel::build_from_buffer(raw)
        .context("Unable to load the buffer as a TensorFlow Lite model")?;

    let resolver = BuiltinOpResolver::default();

    let interpreter = InterpreterBuilder::new(flat_buffer, resolver)
        .context("Unable to create a model interpreter builder")?
        .build()
        .context("Unable to initialize the model interpreter")?;

    Ok(interpreter)
}