Skip to main content

DehazeNet

Struct DehazeNet 

Source
pub struct DehazeNet { /* private fields */ }

Implementations§

Source§

impl DehazeNet

Source

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}
Source

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§

Source§

impl Debug for DehazeNet

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Module for DehazeNet

Source§

fn forward(&self, xs: &Tensor) -> Result<Tensor>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<M> ModuleT for M
where M: Module,

Source§

fn forward_t(&self, xs: &Tensor, _train: bool) -> Result<Tensor, Error>

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more