pub struct EfficientNet<F: Float + Debug + ScalarOperand + Send + Sync> { /* private fields */ }
Expand description
EfficientNet implementation
Implementations§
Source§impl<F: Float + Debug + ScalarOperand + Send + Sync> EfficientNet<F>
impl<F: Float + Debug + ScalarOperand + Send + Sync> EfficientNet<F>
Sourcepub fn new(config: EfficientNetConfig) -> Result<Self>
pub fn new(config: EfficientNetConfig) -> Result<Self>
Create a new EfficientNet model
Examples found in repository?
examples/efficientnet_example.rs (line 64)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6 println!("EfficientNet Example");
7
8 // Create EfficientNet-B0 model for image classification
9 let input_channels = 3; // RGB images
10 let num_classes = 1000; // ImageNet classes
11
12 println!(
13 "Creating EfficientNet-B0 model with {} input channels and {} output classes",
14 input_channels, num_classes
15 );
16
17 // Create model
18 let model = EfficientNet::<f32>::efficientnet_b0(input_channels, num_classes)?;
19
20 // Create dummy input (batch_size=1, channels=3, height=224, width=224)
21 let input = Array::from_shape_fn(IxDyn(&[1, input_channels, 224, 224]), |_| {
22 rand::random::<f32>()
23 });
24
25 println!("Input shape: {:?}", input.shape());
26
27 // Forward pass
28 let output = model.forward(&input)?;
29
30 println!("Output shape: {:?}", output.shape());
31 println!("Output contains logits for {} classes", output.shape()[1]);
32
33 // Create EfficientNet-B3 model (larger model)
34 println!("\nCreating EfficientNet-B3 model...");
35
36 let model_b3 = EfficientNet::<f32>::efficientnet_b3(input_channels, num_classes)?;
37
38 // Create dummy input with higher resolution for B3 (300x300)
39 let input_b3 = Array::from_shape_fn(IxDyn(&[1, input_channels, 300, 300]), |_| {
40 rand::random::<f32>()
41 });
42
43 println!("Input shape for B3: {:?}", input_b3.shape());
44
45 // Forward pass
46 let output_b3 = model_b3.forward(&input_b3)?;
47
48 println!("Output shape for B3: {:?}", output_b3.shape());
49
50 // Create a custom EfficientNet model for smaller images
51 println!("\nCreating a custom EfficientNet model for smaller images...");
52
53 // Create simplified config with fewer stages
54 let mut custom_config = EfficientNetConfig::efficientnet_b0(input_channels, 10); // 10 classes
55
56 // Simplify by keeping only first 4 stages
57 custom_config.stages.truncate(4);
58
59 // Scale down the model
60 custom_config.width_coefficient = 0.5;
61 custom_config.depth_coefficient = 0.5;
62 custom_config.resolution = 32; // For CIFAR-10 size images
63
64 let custom_model = EfficientNet::<f32>::new(custom_config)?;
65
66 // Create dummy input for small images (32x32)
67 let small_input = Array::from_shape_fn(IxDyn(&[1, input_channels, 32, 32]), |_| {
68 rand::random::<f32>()
69 });
70
71 println!("Custom input shape: {:?}", small_input.shape());
72
73 // Forward pass
74 let custom_output = custom_model.forward(&small_input)?;
75
76 println!("Custom output shape: {:?}", custom_output.shape());
77 println!(
78 "Custom model produces logits for {} classes",
79 custom_output.shape()[1]
80 );
81
82 println!("\nEfficientNet example completed successfully!");
83
84 Ok(())
85}
Sourcepub fn efficientnet_b0(
input_channels: usize,
num_classes: usize,
) -> Result<Self>
pub fn efficientnet_b0( input_channels: usize, num_classes: usize, ) -> Result<Self>
Create EfficientNet-B0 model
Examples found in repository?
examples/efficientnet_example.rs (line 18)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6 println!("EfficientNet Example");
7
8 // Create EfficientNet-B0 model for image classification
9 let input_channels = 3; // RGB images
10 let num_classes = 1000; // ImageNet classes
11
12 println!(
13 "Creating EfficientNet-B0 model with {} input channels and {} output classes",
14 input_channels, num_classes
15 );
16
17 // Create model
18 let model = EfficientNet::<f32>::efficientnet_b0(input_channels, num_classes)?;
19
20 // Create dummy input (batch_size=1, channels=3, height=224, width=224)
21 let input = Array::from_shape_fn(IxDyn(&[1, input_channels, 224, 224]), |_| {
22 rand::random::<f32>()
23 });
24
25 println!("Input shape: {:?}", input.shape());
26
27 // Forward pass
28 let output = model.forward(&input)?;
29
30 println!("Output shape: {:?}", output.shape());
31 println!("Output contains logits for {} classes", output.shape()[1]);
32
33 // Create EfficientNet-B3 model (larger model)
34 println!("\nCreating EfficientNet-B3 model...");
35
36 let model_b3 = EfficientNet::<f32>::efficientnet_b3(input_channels, num_classes)?;
37
38 // Create dummy input with higher resolution for B3 (300x300)
39 let input_b3 = Array::from_shape_fn(IxDyn(&[1, input_channels, 300, 300]), |_| {
40 rand::random::<f32>()
41 });
42
43 println!("Input shape for B3: {:?}", input_b3.shape());
44
45 // Forward pass
46 let output_b3 = model_b3.forward(&input_b3)?;
47
48 println!("Output shape for B3: {:?}", output_b3.shape());
49
50 // Create a custom EfficientNet model for smaller images
51 println!("\nCreating a custom EfficientNet model for smaller images...");
52
53 // Create simplified config with fewer stages
54 let mut custom_config = EfficientNetConfig::efficientnet_b0(input_channels, 10); // 10 classes
55
56 // Simplify by keeping only first 4 stages
57 custom_config.stages.truncate(4);
58
59 // Scale down the model
60 custom_config.width_coefficient = 0.5;
61 custom_config.depth_coefficient = 0.5;
62 custom_config.resolution = 32; // For CIFAR-10 size images
63
64 let custom_model = EfficientNet::<f32>::new(custom_config)?;
65
66 // Create dummy input for small images (32x32)
67 let small_input = Array::from_shape_fn(IxDyn(&[1, input_channels, 32, 32]), |_| {
68 rand::random::<f32>()
69 });
70
71 println!("Custom input shape: {:?}", small_input.shape());
72
73 // Forward pass
74 let custom_output = custom_model.forward(&small_input)?;
75
76 println!("Custom output shape: {:?}", custom_output.shape());
77 println!(
78 "Custom model produces logits for {} classes",
79 custom_output.shape()[1]
80 );
81
82 println!("\nEfficientNet example completed successfully!");
83
84 Ok(())
85}
Sourcepub fn efficientnet_b1(
input_channels: usize,
num_classes: usize,
) -> Result<Self>
pub fn efficientnet_b1( input_channels: usize, num_classes: usize, ) -> Result<Self>
Create EfficientNet-B1 model
Sourcepub fn efficientnet_b2(
input_channels: usize,
num_classes: usize,
) -> Result<Self>
pub fn efficientnet_b2( input_channels: usize, num_classes: usize, ) -> Result<Self>
Create EfficientNet-B2 model
Sourcepub fn efficientnet_b3(
input_channels: usize,
num_classes: usize,
) -> Result<Self>
pub fn efficientnet_b3( input_channels: usize, num_classes: usize, ) -> Result<Self>
Create EfficientNet-B3 model
Examples found in repository?
examples/efficientnet_example.rs (line 36)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6 println!("EfficientNet Example");
7
8 // Create EfficientNet-B0 model for image classification
9 let input_channels = 3; // RGB images
10 let num_classes = 1000; // ImageNet classes
11
12 println!(
13 "Creating EfficientNet-B0 model with {} input channels and {} output classes",
14 input_channels, num_classes
15 );
16
17 // Create model
18 let model = EfficientNet::<f32>::efficientnet_b0(input_channels, num_classes)?;
19
20 // Create dummy input (batch_size=1, channels=3, height=224, width=224)
21 let input = Array::from_shape_fn(IxDyn(&[1, input_channels, 224, 224]), |_| {
22 rand::random::<f32>()
23 });
24
25 println!("Input shape: {:?}", input.shape());
26
27 // Forward pass
28 let output = model.forward(&input)?;
29
30 println!("Output shape: {:?}", output.shape());
31 println!("Output contains logits for {} classes", output.shape()[1]);
32
33 // Create EfficientNet-B3 model (larger model)
34 println!("\nCreating EfficientNet-B3 model...");
35
36 let model_b3 = EfficientNet::<f32>::efficientnet_b3(input_channels, num_classes)?;
37
38 // Create dummy input with higher resolution for B3 (300x300)
39 let input_b3 = Array::from_shape_fn(IxDyn(&[1, input_channels, 300, 300]), |_| {
40 rand::random::<f32>()
41 });
42
43 println!("Input shape for B3: {:?}", input_b3.shape());
44
45 // Forward pass
46 let output_b3 = model_b3.forward(&input_b3)?;
47
48 println!("Output shape for B3: {:?}", output_b3.shape());
49
50 // Create a custom EfficientNet model for smaller images
51 println!("\nCreating a custom EfficientNet model for smaller images...");
52
53 // Create simplified config with fewer stages
54 let mut custom_config = EfficientNetConfig::efficientnet_b0(input_channels, 10); // 10 classes
55
56 // Simplify by keeping only first 4 stages
57 custom_config.stages.truncate(4);
58
59 // Scale down the model
60 custom_config.width_coefficient = 0.5;
61 custom_config.depth_coefficient = 0.5;
62 custom_config.resolution = 32; // For CIFAR-10 size images
63
64 let custom_model = EfficientNet::<f32>::new(custom_config)?;
65
66 // Create dummy input for small images (32x32)
67 let small_input = Array::from_shape_fn(IxDyn(&[1, input_channels, 32, 32]), |_| {
68 rand::random::<f32>()
69 });
70
71 println!("Custom input shape: {:?}", small_input.shape());
72
73 // Forward pass
74 let custom_output = custom_model.forward(&small_input)?;
75
76 println!("Custom output shape: {:?}", custom_output.shape());
77 println!(
78 "Custom model produces logits for {} classes",
79 custom_output.shape()[1]
80 );
81
82 println!("\nEfficientNet example completed successfully!");
83
84 Ok(())
85}
Sourcepub fn efficientnet_b4(
input_channels: usize,
num_classes: usize,
) -> Result<Self>
pub fn efficientnet_b4( input_channels: usize, num_classes: usize, ) -> Result<Self>
Create EfficientNet-B4 model
Sourcepub fn efficientnet_b5(
input_channels: usize,
num_classes: usize,
) -> Result<Self>
pub fn efficientnet_b5( input_channels: usize, num_classes: usize, ) -> Result<Self>
Create EfficientNet-B5 model
Sourcepub fn efficientnet_b6(
input_channels: usize,
num_classes: usize,
) -> Result<Self>
pub fn efficientnet_b6( input_channels: usize, num_classes: usize, ) -> Result<Self>
Create EfficientNet-B6 model
Sourcepub fn efficientnet_b7(
input_channels: usize,
num_classes: usize,
) -> Result<Self>
pub fn efficientnet_b7( input_channels: usize, num_classes: usize, ) -> Result<Self>
Create EfficientNet-B7 model
Trait Implementations§
Source§impl<F: Float + Debug + ScalarOperand + Send + Sync> Layer<F> for EfficientNet<F>
impl<F: Float + Debug + ScalarOperand + Send + Sync> Layer<F> for EfficientNet<F>
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 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 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 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 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 EfficientNet<F>
impl<F> !RefUnwindSafe for EfficientNet<F>
impl<F> Send for EfficientNet<F>
impl<F> Sync for EfficientNet<F>
impl<F> Unpin for EfficientNet<F>where
F: Unpin,
impl<F> !UnwindSafe for EfficientNet<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> 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