pub struct Backbone { /* private fields */ }Expand description
整个骨干。
Implementations§
Source§impl Backbone
impl Backbone
Sourcepub fn load(w: &Weights, p: &str, cfg: &Config) -> Result<Backbone, String>
pub fn load(w: &Weights, p: &str, cfg: &Config) -> Result<Backbone, String>
p 是权重名前缀(PP-OCRv6 里是 model.backbone.encoder)。
Examples found in repository?
examples/backbone_smoke.rs (line 14)
4fn main() {
5 let a: Vec<String> = std::env::args().skip(1).collect();
6 let dir = std::path::Path::new(&a[0]);
7 let (h, w): (usize, usize) = (a[1].parse().unwrap(), a[2].parse().unwrap());
8 let cfg: serde_json::Value =
9 serde_json::from_slice(&std::fs::read(dir.join("config.json")).unwrap()).unwrap();
10 let wts =
11 pagesift_infer::Weights::from_bytes(&std::fs::read(dir.join("model.safetensors")).unwrap())
12 .unwrap();
13 let c = pagesift_ocr::lcnet::Config::from_json(&cfg).unwrap();
14 let b = pagesift_ocr::lcnet::Backbone::load(&wts, "model.backbone.encoder", &c).unwrap();
15 let x = pagesift_infer::Tensor::new(
16 vec![3, h, w],
17 (0..3 * h * w)
18 .map(|i| ((i % 97) as f32 / 97.0) - 0.5)
19 .collect(),
20 );
21 let t = Instant::now();
22 let outs = b.forward(&x);
23 for o in &outs {
24 println!(
25 "{:?} 均值 {:.5}",
26 o.shape,
27 o.data.iter().sum::<f32>() / o.data.len() as f32
28 );
29 }
30 println!("{:.1} ms", t.elapsed().as_secs_f64() * 1e3);
31}Sourcepub fn forward(&self, x: &Tensor) -> Vec<Tensor>
pub fn forward(&self, x: &Tensor) -> Vec<Tensor>
输入 [3, H, W](已归一化),返回 4 段各自的输出(stage1–stage4)。
Examples found in repository?
examples/backbone_smoke.rs (line 22)
4fn main() {
5 let a: Vec<String> = std::env::args().skip(1).collect();
6 let dir = std::path::Path::new(&a[0]);
7 let (h, w): (usize, usize) = (a[1].parse().unwrap(), a[2].parse().unwrap());
8 let cfg: serde_json::Value =
9 serde_json::from_slice(&std::fs::read(dir.join("config.json")).unwrap()).unwrap();
10 let wts =
11 pagesift_infer::Weights::from_bytes(&std::fs::read(dir.join("model.safetensors")).unwrap())
12 .unwrap();
13 let c = pagesift_ocr::lcnet::Config::from_json(&cfg).unwrap();
14 let b = pagesift_ocr::lcnet::Backbone::load(&wts, "model.backbone.encoder", &c).unwrap();
15 let x = pagesift_infer::Tensor::new(
16 vec![3, h, w],
17 (0..3 * h * w)
18 .map(|i| ((i % 97) as f32 / 97.0) - 0.5)
19 .collect(),
20 );
21 let t = Instant::now();
22 let outs = b.forward(&x);
23 for o in &outs {
24 println!(
25 "{:?} 均值 {:.5}",
26 o.shape,
27 o.data.iter().sum::<f32>() / o.data.len() as f32
28 );
29 }
30 println!("{:.1} ms", t.elapsed().as_secs_f64() * 1e3);
31}Sourcepub fn forward_on<E: Exec>(&self, ex: &E, x: &E::T) -> Vec<E::T>
pub fn forward_on<E: Exec>(&self, ex: &E, x: &E::T) -> Vec<E::T>
同 Backbone::forward,在执行器 ex 上算。
Trait Implementations§
Auto Trait Implementations§
impl Forget for Backbone
impl Freeze for Backbone
impl Move for Backbone
impl RefUnwindSafe for Backbone
impl Send for Backbone
impl Sync for Backbone
impl Unpin for Backbone
impl UnsafeUnpin for Backbone
impl UnwindSafe for Backbone
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more