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>
impl<F: Float + Debug + ScalarOperand + Send + Sync> ConvNeXt<F>
Sourcepub fn new(config: ConvNeXtConfig) -> Result<Self>
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}
Sourcepub fn convnext_tiny(num_classes: usize, include_top: bool) -> Result<Self>
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}
Sourcepub fn convnext_small(num_classes: usize, include_top: bool) -> Result<Self>
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}
Sourcepub fn convnext_base(num_classes: usize, include_top: bool) -> Result<Self>
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}
Sourcepub fn convnext_large(num_classes: usize, include_top: bool) -> Result<Self>
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}
Sourcepub fn convnext_xlarge(num_classes: usize, include_top: bool) -> Result<Self>
pub fn convnext_xlarge(num_classes: usize, include_top: bool) -> Result<Self>
Create a ConvNeXt-XLarge model
Trait Implementations§
Source§impl<F: Float + Debug + ScalarOperand + Send + Sync> Layer<F> for ConvNeXt<F>
impl<F: Float + Debug + ScalarOperand + Send + Sync> Layer<F> for ConvNeXt<F>
Source§fn as_any_mut(&mut self) -> &mut dyn Any
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>>
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>>
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<()>
fn update(&mut self, _learning_rate: F) -> Result<()>
Update the layer parameters with the given gradients Read more
Source§fn set_training(&mut self, training: bool)
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
fn is_training(&self) -> bool
Get the current training mode Read more
Source§fn gradients(&self) -> Vec<Array<F, IxDyn>> ⓘ
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<()>
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<()>
fn set_params(&mut self, _params: &[Array<F, IxDyn>]) -> Result<()>
Set the parameters of the layer Read more
Source§fn layer_type(&self) -> &str
fn layer_type(&self) -> &str
Get the type of the layer (e.g., “Dense”, “Conv2D”) Read more
Source§fn parameter_count(&self) -> usize
fn parameter_count(&self) -> usize
Get the number of trainable parameters in this layer Read more
Source§fn layer_description(&self) -> String
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> 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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