Struct ConvNeXt

Source
pub struct ConvNeXt<F: Float + Debug + ScalarOperand + Send + Sync> {
    pub stem: Sequential<F>,
    pub stages: Vec<ConvNeXtStage<F>>,
    pub head: Option<Sequential<F>>,
    pub config: ConvNeXtConfig,
}
Expand description

ConvNeXt model

Fields§

§stem: Sequential<F>

Stem layer (initial convolution)

§stages: Vec<ConvNeXtStage<F>>

Main stages of the network

§head: Option<Sequential<F>>

Classification head (if include_top is true)

§config: ConvNeXtConfig

Model configuration

Implementations§

Source§

impl<F: Float + Debug + ScalarOperand + Send + Sync> ConvNeXt<F>

Source

pub fn new(config: ConvNeXtConfig) -> Result<Self>

Create a new ConvNeXt model

Examples found in repository?
examples/convnext_example.rs (line 58)
8fn main() -> Result<()> {
9    println!("ConvNeXt Example");
10    println!("----------------");
11
12    // Create a random input tensor (batch_size=1, channels=3, height=224, width=224)
13    let input_shape = [1, 3, 224, 224];
14    let mut input = Array::<f32, _>::zeros(input_shape).into_dyn();
15
16    // Fill with random values between 0 and 1
17    for elem in input.iter_mut() {
18        *elem = rand::random::<f32>();
19    }
20
21    // Create ConvNeXt-Tiny with default configuration
22    println!("\nConvNeXt-Tiny:");
23    let convnext_tiny = ConvNeXt::convnext_tiny(1000, true)?;
24    let output_tiny = convnext_tiny.forward(&input)?;
25    println!("Output shape: {:?}", output_tiny.shape());
26
27    // Create ConvNeXt-Small with default configuration
28    println!("\nConvNeXt-Small:");
29    let convnext_small = ConvNeXt::convnext_small(1000, true)?;
30    let output_small = convnext_small.forward(&input)?;
31    println!("Output shape: {:?}", output_small.shape());
32
33    // Create ConvNeXt-Base with default configuration
34    println!("\nConvNeXt-Base:");
35    let convnext_base = ConvNeXt::convnext_base(1000, true)?;
36    let output_base = convnext_base.forward(&input)?;
37    println!("Output shape: {:?}", output_base.shape());
38
39    // Create ConvNeXt-Large with default configuration
40    println!("\nConvNeXt-Large:");
41    let convnext_large = ConvNeXt::convnext_large(1000, true)?;
42    let output_large = convnext_large.forward(&input)?;
43    println!("Output shape: {:?}", output_large.shape());
44
45    // Custom ConvNeXt with specific configuration
46    println!("\nCustom ConvNeXt:");
47    let custom_config = ConvNeXtConfig {
48        variant: ConvNeXtVariant::Tiny,
49        input_channels: 3,
50        depths: vec![3, 3, 9, 3],
51        dims: vec![96, 192, 384, 768],
52        num_classes: 10,
53        dropout_rate: Some(0.2),
54        layer_scale_init_value: 1e-6,
55        include_top: true,
56    };
57
58    let custom_convnext = ConvNeXt::new(custom_config)?;
59    let output_custom = custom_convnext.forward(&input)?;
60    println!("Output shape: {:?}", output_custom.shape());
61
62    // Example of inference
63    println!("\nInference example with ConvNeXt-Tiny:");
64    let inference_input = Array::<f32, _>::zeros(input_shape).into_dyn();
65    let inference_output = convnext_tiny.forward(&inference_input)?;
66
67    // Get top prediction (normally you'd have class labels)
68    let mut max_val = f32::MIN;
69    let mut max_idx = 0;
70
71    for (i, &val) in inference_output.iter().enumerate() {
72        if val > max_val {
73            max_val = val;
74            max_idx = i;
75        }
76    }
77
78    println!(
79        "Predicted class: {} with confidence: {:.4}",
80        max_idx, max_val
81    );
82
83    Ok(())
84}
Source

pub fn convnext_tiny(num_classes: usize, include_top: bool) -> Result<Self>

Create a ConvNeXt-Tiny model

Examples found in repository?
examples/convnext_example.rs (line 23)
8fn main() -> Result<()> {
9    println!("ConvNeXt Example");
10    println!("----------------");
11
12    // Create a random input tensor (batch_size=1, channels=3, height=224, width=224)
13    let input_shape = [1, 3, 224, 224];
14    let mut input = Array::<f32, _>::zeros(input_shape).into_dyn();
15
16    // Fill with random values between 0 and 1
17    for elem in input.iter_mut() {
18        *elem = rand::random::<f32>();
19    }
20
21    // Create ConvNeXt-Tiny with default configuration
22    println!("\nConvNeXt-Tiny:");
23    let convnext_tiny = ConvNeXt::convnext_tiny(1000, true)?;
24    let output_tiny = convnext_tiny.forward(&input)?;
25    println!("Output shape: {:?}", output_tiny.shape());
26
27    // Create ConvNeXt-Small with default configuration
28    println!("\nConvNeXt-Small:");
29    let convnext_small = ConvNeXt::convnext_small(1000, true)?;
30    let output_small = convnext_small.forward(&input)?;
31    println!("Output shape: {:?}", output_small.shape());
32
33    // Create ConvNeXt-Base with default configuration
34    println!("\nConvNeXt-Base:");
35    let convnext_base = ConvNeXt::convnext_base(1000, true)?;
36    let output_base = convnext_base.forward(&input)?;
37    println!("Output shape: {:?}", output_base.shape());
38
39    // Create ConvNeXt-Large with default configuration
40    println!("\nConvNeXt-Large:");
41    let convnext_large = ConvNeXt::convnext_large(1000, true)?;
42    let output_large = convnext_large.forward(&input)?;
43    println!("Output shape: {:?}", output_large.shape());
44
45    // Custom ConvNeXt with specific configuration
46    println!("\nCustom ConvNeXt:");
47    let custom_config = ConvNeXtConfig {
48        variant: ConvNeXtVariant::Tiny,
49        input_channels: 3,
50        depths: vec![3, 3, 9, 3],
51        dims: vec![96, 192, 384, 768],
52        num_classes: 10,
53        dropout_rate: Some(0.2),
54        layer_scale_init_value: 1e-6,
55        include_top: true,
56    };
57
58    let custom_convnext = ConvNeXt::new(custom_config)?;
59    let output_custom = custom_convnext.forward(&input)?;
60    println!("Output shape: {:?}", output_custom.shape());
61
62    // Example of inference
63    println!("\nInference example with ConvNeXt-Tiny:");
64    let inference_input = Array::<f32, _>::zeros(input_shape).into_dyn();
65    let inference_output = convnext_tiny.forward(&inference_input)?;
66
67    // Get top prediction (normally you'd have class labels)
68    let mut max_val = f32::MIN;
69    let mut max_idx = 0;
70
71    for (i, &val) in inference_output.iter().enumerate() {
72        if val > max_val {
73            max_val = val;
74            max_idx = i;
75        }
76    }
77
78    println!(
79        "Predicted class: {} with confidence: {:.4}",
80        max_idx, max_val
81    );
82
83    Ok(())
84}
Source

pub fn convnext_small(num_classes: usize, include_top: bool) -> Result<Self>

Create a ConvNeXt-Small model

Examples found in repository?
examples/convnext_example.rs (line 29)
8fn main() -> Result<()> {
9    println!("ConvNeXt Example");
10    println!("----------------");
11
12    // Create a random input tensor (batch_size=1, channels=3, height=224, width=224)
13    let input_shape = [1, 3, 224, 224];
14    let mut input = Array::<f32, _>::zeros(input_shape).into_dyn();
15
16    // Fill with random values between 0 and 1
17    for elem in input.iter_mut() {
18        *elem = rand::random::<f32>();
19    }
20
21    // Create ConvNeXt-Tiny with default configuration
22    println!("\nConvNeXt-Tiny:");
23    let convnext_tiny = ConvNeXt::convnext_tiny(1000, true)?;
24    let output_tiny = convnext_tiny.forward(&input)?;
25    println!("Output shape: {:?}", output_tiny.shape());
26
27    // Create ConvNeXt-Small with default configuration
28    println!("\nConvNeXt-Small:");
29    let convnext_small = ConvNeXt::convnext_small(1000, true)?;
30    let output_small = convnext_small.forward(&input)?;
31    println!("Output shape: {:?}", output_small.shape());
32
33    // Create ConvNeXt-Base with default configuration
34    println!("\nConvNeXt-Base:");
35    let convnext_base = ConvNeXt::convnext_base(1000, true)?;
36    let output_base = convnext_base.forward(&input)?;
37    println!("Output shape: {:?}", output_base.shape());
38
39    // Create ConvNeXt-Large with default configuration
40    println!("\nConvNeXt-Large:");
41    let convnext_large = ConvNeXt::convnext_large(1000, true)?;
42    let output_large = convnext_large.forward(&input)?;
43    println!("Output shape: {:?}", output_large.shape());
44
45    // Custom ConvNeXt with specific configuration
46    println!("\nCustom ConvNeXt:");
47    let custom_config = ConvNeXtConfig {
48        variant: ConvNeXtVariant::Tiny,
49        input_channels: 3,
50        depths: vec![3, 3, 9, 3],
51        dims: vec![96, 192, 384, 768],
52        num_classes: 10,
53        dropout_rate: Some(0.2),
54        layer_scale_init_value: 1e-6,
55        include_top: true,
56    };
57
58    let custom_convnext = ConvNeXt::new(custom_config)?;
59    let output_custom = custom_convnext.forward(&input)?;
60    println!("Output shape: {:?}", output_custom.shape());
61
62    // Example of inference
63    println!("\nInference example with ConvNeXt-Tiny:");
64    let inference_input = Array::<f32, _>::zeros(input_shape).into_dyn();
65    let inference_output = convnext_tiny.forward(&inference_input)?;
66
67    // Get top prediction (normally you'd have class labels)
68    let mut max_val = f32::MIN;
69    let mut max_idx = 0;
70
71    for (i, &val) in inference_output.iter().enumerate() {
72        if val > max_val {
73            max_val = val;
74            max_idx = i;
75        }
76    }
77
78    println!(
79        "Predicted class: {} with confidence: {:.4}",
80        max_idx, max_val
81    );
82
83    Ok(())
84}
Source

pub fn convnext_base(num_classes: usize, include_top: bool) -> Result<Self>

Create a ConvNeXt-Base model

Examples found in repository?
examples/convnext_example.rs (line 35)
8fn main() -> Result<()> {
9    println!("ConvNeXt Example");
10    println!("----------------");
11
12    // Create a random input tensor (batch_size=1, channels=3, height=224, width=224)
13    let input_shape = [1, 3, 224, 224];
14    let mut input = Array::<f32, _>::zeros(input_shape).into_dyn();
15
16    // Fill with random values between 0 and 1
17    for elem in input.iter_mut() {
18        *elem = rand::random::<f32>();
19    }
20
21    // Create ConvNeXt-Tiny with default configuration
22    println!("\nConvNeXt-Tiny:");
23    let convnext_tiny = ConvNeXt::convnext_tiny(1000, true)?;
24    let output_tiny = convnext_tiny.forward(&input)?;
25    println!("Output shape: {:?}", output_tiny.shape());
26
27    // Create ConvNeXt-Small with default configuration
28    println!("\nConvNeXt-Small:");
29    let convnext_small = ConvNeXt::convnext_small(1000, true)?;
30    let output_small = convnext_small.forward(&input)?;
31    println!("Output shape: {:?}", output_small.shape());
32
33    // Create ConvNeXt-Base with default configuration
34    println!("\nConvNeXt-Base:");
35    let convnext_base = ConvNeXt::convnext_base(1000, true)?;
36    let output_base = convnext_base.forward(&input)?;
37    println!("Output shape: {:?}", output_base.shape());
38
39    // Create ConvNeXt-Large with default configuration
40    println!("\nConvNeXt-Large:");
41    let convnext_large = ConvNeXt::convnext_large(1000, true)?;
42    let output_large = convnext_large.forward(&input)?;
43    println!("Output shape: {:?}", output_large.shape());
44
45    // Custom ConvNeXt with specific configuration
46    println!("\nCustom ConvNeXt:");
47    let custom_config = ConvNeXtConfig {
48        variant: ConvNeXtVariant::Tiny,
49        input_channels: 3,
50        depths: vec![3, 3, 9, 3],
51        dims: vec![96, 192, 384, 768],
52        num_classes: 10,
53        dropout_rate: Some(0.2),
54        layer_scale_init_value: 1e-6,
55        include_top: true,
56    };
57
58    let custom_convnext = ConvNeXt::new(custom_config)?;
59    let output_custom = custom_convnext.forward(&input)?;
60    println!("Output shape: {:?}", output_custom.shape());
61
62    // Example of inference
63    println!("\nInference example with ConvNeXt-Tiny:");
64    let inference_input = Array::<f32, _>::zeros(input_shape).into_dyn();
65    let inference_output = convnext_tiny.forward(&inference_input)?;
66
67    // Get top prediction (normally you'd have class labels)
68    let mut max_val = f32::MIN;
69    let mut max_idx = 0;
70
71    for (i, &val) in inference_output.iter().enumerate() {
72        if val > max_val {
73            max_val = val;
74            max_idx = i;
75        }
76    }
77
78    println!(
79        "Predicted class: {} with confidence: {:.4}",
80        max_idx, max_val
81    );
82
83    Ok(())
84}
Source

pub fn convnext_large(num_classes: usize, include_top: bool) -> Result<Self>

Create a ConvNeXt-Large model

Examples found in repository?
examples/convnext_example.rs (line 41)
8fn main() -> Result<()> {
9    println!("ConvNeXt Example");
10    println!("----------------");
11
12    // Create a random input tensor (batch_size=1, channels=3, height=224, width=224)
13    let input_shape = [1, 3, 224, 224];
14    let mut input = Array::<f32, _>::zeros(input_shape).into_dyn();
15
16    // Fill with random values between 0 and 1
17    for elem in input.iter_mut() {
18        *elem = rand::random::<f32>();
19    }
20
21    // Create ConvNeXt-Tiny with default configuration
22    println!("\nConvNeXt-Tiny:");
23    let convnext_tiny = ConvNeXt::convnext_tiny(1000, true)?;
24    let output_tiny = convnext_tiny.forward(&input)?;
25    println!("Output shape: {:?}", output_tiny.shape());
26
27    // Create ConvNeXt-Small with default configuration
28    println!("\nConvNeXt-Small:");
29    let convnext_small = ConvNeXt::convnext_small(1000, true)?;
30    let output_small = convnext_small.forward(&input)?;
31    println!("Output shape: {:?}", output_small.shape());
32
33    // Create ConvNeXt-Base with default configuration
34    println!("\nConvNeXt-Base:");
35    let convnext_base = ConvNeXt::convnext_base(1000, true)?;
36    let output_base = convnext_base.forward(&input)?;
37    println!("Output shape: {:?}", output_base.shape());
38
39    // Create ConvNeXt-Large with default configuration
40    println!("\nConvNeXt-Large:");
41    let convnext_large = ConvNeXt::convnext_large(1000, true)?;
42    let output_large = convnext_large.forward(&input)?;
43    println!("Output shape: {:?}", output_large.shape());
44
45    // Custom ConvNeXt with specific configuration
46    println!("\nCustom ConvNeXt:");
47    let custom_config = ConvNeXtConfig {
48        variant: ConvNeXtVariant::Tiny,
49        input_channels: 3,
50        depths: vec![3, 3, 9, 3],
51        dims: vec![96, 192, 384, 768],
52        num_classes: 10,
53        dropout_rate: Some(0.2),
54        layer_scale_init_value: 1e-6,
55        include_top: true,
56    };
57
58    let custom_convnext = ConvNeXt::new(custom_config)?;
59    let output_custom = custom_convnext.forward(&input)?;
60    println!("Output shape: {:?}", output_custom.shape());
61
62    // Example of inference
63    println!("\nInference example with ConvNeXt-Tiny:");
64    let inference_input = Array::<f32, _>::zeros(input_shape).into_dyn();
65    let inference_output = convnext_tiny.forward(&inference_input)?;
66
67    // Get top prediction (normally you'd have class labels)
68    let mut max_val = f32::MIN;
69    let mut max_idx = 0;
70
71    for (i, &val) in inference_output.iter().enumerate() {
72        if val > max_val {
73            max_val = val;
74            max_idx = i;
75        }
76    }
77
78    println!(
79        "Predicted class: {} with confidence: {:.4}",
80        max_idx, max_val
81    );
82
83    Ok(())
84}
Source

pub fn convnext_xlarge(num_classes: usize, include_top: bool) -> Result<Self>

Create a ConvNeXt-XLarge model

Trait Implementations§

Source§

impl<F: Clone + Float + Debug + ScalarOperand + Send + Sync> Clone for ConvNeXt<F>

Source§

fn clone(&self) -> ConvNeXt<F>

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<F: Debug + Float + Debug + ScalarOperand + Send + Sync> Debug for ConvNeXt<F>

Source§

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

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

impl<F: Float + Debug + ScalarOperand + Send + Sync> Layer<F> for ConvNeXt<F>

Source§

fn as_any(&self) -> &dyn Any

Get the layer as a dyn Any for downcasting Read more
Source§

fn as_any_mut(&mut self) -> &mut dyn Any

Get the layer as a mutable dyn Any for downcasting Read more
Source§

fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>>

Forward pass of the layer Read more
Source§

fn backward( &self, _input: &Array<F, IxDyn>, _grad_output: &Array<F, IxDyn>, ) -> Result<Array<F, IxDyn>>

Backward pass of the layer to compute gradients Read more
Source§

fn update(&mut self, _learning_rate: F) -> Result<()>

Update the layer parameters with the given gradients Read more
Source§

fn params(&self) -> Vec<Array<F, IxDyn>>

Get the parameters of the layer Read more
Source§

fn set_training(&mut self, training: bool)

Set the layer to training mode (true) or evaluation mode (false) Read more
Source§

fn is_training(&self) -> bool

Get the current training mode Read more
Source§

fn gradients(&self) -> Vec<Array<F, IxDyn>>

Get the gradients of the layer parameters Read more
Source§

fn set_gradients(&mut self, _gradients: &[Array<F, IxDyn>]) -> Result<()>

Set the gradients of the layer parameters Read more
Source§

fn set_params(&mut self, _params: &[Array<F, IxDyn>]) -> Result<()>

Set the parameters of the layer Read more
Source§

fn layer_type(&self) -> &str

Get the type of the layer (e.g., “Dense”, “Conv2D”) Read more
Source§

fn parameter_count(&self) -> usize

Get the number of trainable parameters in this layer Read more
Source§

fn layer_description(&self) -> String

Get a detailed description of this layer Read more

Auto Trait Implementations§

§

impl<F> Freeze for ConvNeXt<F>

§

impl<F> !RefUnwindSafe for ConvNeXt<F>

§

impl<F> Send for ConvNeXt<F>

§

impl<F> Sync for ConvNeXt<F>

§

impl<F> Unpin for ConvNeXt<F>
where F: Unpin,

§

impl<F> !UnwindSafe for ConvNeXt<F>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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