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
use std::sync::Arc;

use anyhow::Result;
use ndarray::Ix2;
use ort::{
  environment::Environment, tensor::InputTensor, GraphOptimizationLevel, Session, SessionBuilder,
};

use crate::typedef::Arr;

pub struct ClipSession(Session);

impl ClipSession {
  pub fn new(dir: &str, env: &Arc<Environment>, onnx: &str) -> Result<Self> {
    Ok(Self(
      SessionBuilder::new(env)?
        .with_optimization_level(GraphOptimizationLevel::Level3)?
        .with_inter_threads(num_cpus::get() as i16)?
        .with_model_from_file(format!("{}/{}.onnx", dir, onnx))?,
    ))
  }

  pub fn run(&self, li: impl AsRef<[InputTensor]>) -> anyhow::Result<Arr> {
    let out = self.0.run(li.as_ref())?;
    Ok(
      out[0]
        .try_extract::<f32>()?
        .view()
        .to_owned()
        .into_dimensionality::<Ix2>()?,
    )
  }
}