pub struct DehazeNet { /* private fields */ }Implementations§
Source§impl DehazeNet
impl DehazeNet
Sourcepub fn new(vb: VarBuilder<'_>) -> Result<Self>
pub fn new(vb: VarBuilder<'_>) -> Result<Self>
Examples found in repository?
examples/custom.rs (line 17)
3fn main() {
4 let device = candle_core::Device::cuda_if_available(0).unwrap();
5
6 let base_dir = env!("CARGO_MANIFEST_DIR");
7 let weight_path = format!("{base_dir}/dehazer.safetensors");
8 let vb = unsafe {
9 VarBuilder::from_mmaped_safetensors(
10 &[&weight_path],
11 candle_core::DType::F32,
12 &device,
13 )
14 .unwrap()
15 };
16
17 let model = DehazeNet::new(vb).unwrap();
18
19 // println!("{model:?}");
20
21 let img = image::open(format!("{base_dir}/testdata/test2.png")).unwrap();
22
23 let raw = img.to_rgb8().into_vec();
24 let data = Tensor::from_vec(
25 raw,
26 (img.height() as usize, img.width() as usize, 3),
27 &device,
28 )
29 .unwrap()
30 .to_dtype(candle_core::DType::F32)
31 .unwrap()
32 .broadcast_div(&Tensor::new(255f32, &device).unwrap())
33 .unwrap()
34 .permute((2, 0, 1))
35 .unwrap()
36 .unsqueeze(0)
37 .unwrap();
38
39 println!("{data:?}");
40
41 let out = model.forward(&data).unwrap();
42
43 // 处理输出张量
44 let out = out.squeeze(0).unwrap(); // 移除批次维度 [c, h, w]
45
46 let (_, height, width) = out.dims3().unwrap();
47
48
49 let image_data: Vec<u8> = out
50 .permute((1, 2, 0))
51 .unwrap() // [H, W, C] 符合图像布局
52 .flatten_all()
53 .unwrap()
54 .to_vec1::<f32>()
55 .unwrap()
56 .iter()
57 .map(|&v| (v.clamp(0.0, 1.0) * 255.0) as u8)
58 .collect();
59
60 // 保存图像
61 let img_out =
62 image::RgbImage::from_raw(width as u32, height as u32, image_data).expect("创建图像失败");
63
64 img_out.save("dehazed_output.jpg").expect("保存图像失败");
65 println!("去雾结果已保存为 dehazed_output.jpg");
66}Sourcepub fn with_device(device: &Device) -> Result<Self>
pub fn with_device(device: &Device) -> Result<Self>
Examples found in repository?
examples/base.rs (line 9)
5fn main() {
6 let device = candle_core::Device::cuda_if_available(0).unwrap();
7 let base_dir = env!("CARGO_MANIFEST_DIR");
8
9 let model = DehazeNet::with_device(&device).unwrap();
10
11 let img = image::open(format!("{base_dir}/testdata/test2.png")).unwrap();
12
13 let raw = img.to_rgb8().into_vec();
14 let data = Tensor::from_vec(
15 raw,
16 (img.height() as usize, img.width() as usize, 3),
17 &device,
18 )
19 .unwrap()
20 .to_dtype(candle_core::DType::F32)
21 .unwrap()
22 .broadcast_div(&Tensor::new(255f32, &device).unwrap())
23 .unwrap()
24 .permute((2, 0, 1))
25 .unwrap()
26 .unsqueeze(0)
27 .unwrap();
28
29 println!("{data:?}");
30
31 let out = model.forward(&data).unwrap();
32
33 // 处理输出张量
34 let out = out.squeeze(0).unwrap(); // 移除批次维度 [c, h, w]
35
36 let (_, height, width) = out.dims3().unwrap();
37
38 let image_data: Vec<u8> = out
39 .permute((1, 2, 0))
40 .unwrap() // [H, W, C] 符合图像布局
41 .flatten_all()
42 .unwrap()
43 .to_vec1::<f32>()
44 .unwrap()
45 .iter()
46 .map(|&v| (v.clamp(0.0, 1.0) * 255.0) as u8)
47 .collect();
48
49 // 保存图像
50 let img_out =
51 image::RgbImage::from_raw(width as u32, height as u32, image_data).expect("创建图像失败");
52
53 img_out.save("result/dehazed_output.jpg").expect("保存图像失败");
54 println!("去雾结果已保存为 result/dehazed_output.jpg");
55}Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for DehazeNet
impl !UnwindSafe for DehazeNet
impl Freeze for DehazeNet
impl Send for DehazeNet
impl Sync for DehazeNet
impl Unpin for DehazeNet
impl UnsafeUnpin for DehazeNet
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
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more