1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#![cfg(feature = "neural_network")]
use ndarray::{Array4, s};
use rustyml::neural_network::layer::TrainingParameters;
use rustyml::neural_network::layer::activation_layer::linear::Linear;
use rustyml::neural_network::layer::activation_layer::relu::ReLU;
use rustyml::neural_network::layer::convolution_layer::PaddingType;
use rustyml::neural_network::layer::convolution_layer::depthwise_conv_2d::DepthwiseConv2D;
use rustyml::neural_network::loss_function::mean_squared_error::MeanSquaredError;
use rustyml::neural_network::neural_network_trait::Layer;
use rustyml::neural_network::optimizer::sgd::SGD;
use rustyml::neural_network::sequential::Sequential;
#[test]
fn test_depthwise_conv2d_creation() {
// Create DepthwiseConv2D layer
let layer = DepthwiseConv2D::new(
3, // filters
(3, 3), // kernel_size
(1, 1), // strides
PaddingType::Valid, // padding
ReLU::new(), // activation
)
.unwrap();
// Verify layer type
assert_eq!(layer.layer_type(), "DepthwiseConv2D");
// Verify parameter count (weights + bias)
// Weight shape: [3, 1, 3, 3] = 27, bias: [3] = 3
assert_eq!(layer.param_count(), TrainingParameters::Trainable(30));
// Output shape should be "Unknown" before initialization
assert_eq!(layer.output_shape(), "Unknown");
}
#[test]
fn test_depthwise_conv2d_forward() {
// Create test input data: [batch_size, channels, height, width]
let batch_size = 2;
let channels = 3;
let height = 6;
let width = 6;
let mut input_data = Array4::zeros((batch_size, channels, height, width));
// Set different values for each channel to verify that depthwise convolution processes channels independently
for b in 0..batch_size {
for c in 0..channels {
for h in 0..height {
for w in 0..width {
input_data[[b, c, h, w]] = (b + c * 10 + h + w) as f32;
}
}
}
}
let input = input_data.into_dyn();
// Create and initialize DepthwiseConv2D layer
let mut layer = DepthwiseConv2D::new(
channels, // filters must equal input_channels
(3, 3), // kernel_size
(1, 1), // strides
PaddingType::Valid, // padding
Linear::new(), // no activation
)
.unwrap();
layer.initialize_weights(channels);
// Forward propagation
let output = layer.forward(&input).unwrap();
// Verify output shape
// Input: [2, 3, 6, 6], kernel (3,3), stride (1,1), valid padding
// Output should be: [2, 3, 4, 4]
assert_eq!(output.shape(), &[2, 3, 4, 4]);
// Verify output shape string
assert_eq!(layer.output_shape(), "(2, 3, 4, 4)");
}
#[test]
fn test_depthwise_conv2d_sequential_model() {
// Create Sequential model
let mut model = Sequential::new();
// Create and initialize DepthwiseConv2D layer
let mut depthwise_layer = DepthwiseConv2D::new(
3, // filters
(2, 2), // kernel_size
(1, 1), // strides
PaddingType::Valid, // padding
ReLU::new(), // activation
)
.unwrap();
depthwise_layer.initialize_weights(3);
// Add layer and compile model
model
.add(depthwise_layer)
.compile(SGD::new(0.01).unwrap(), MeanSquaredError::new());
// Create test input data: [batch_size, channels, height, width]
let batch_size = 1;
let input_channels = 3;
let height = 4;
let width = 4;
let mut input_data = Array4::zeros((batch_size, input_channels, height, width));
// Set different values for each channel
for c in 0..input_channels {
for h in 0..height {
for w in 0..width {
input_data[[0, c, h, w]] = (c * 10 + h * width + w) as f32;
}
}
}
let input = input_data.into_dyn();
// Display model structure
model.summary();
// Forward propagation
let output = model.predict(&input).unwrap();
// Verify output shape
// Input: [1, 3, 4, 4], kernel (2,2), stride (1,1), valid padding
// Output should be: [1, 3, 3, 3]
assert_eq!(output.shape(), &[1, 3, 3, 3]);
// Since ReLU activation is used, all output values should be non-negative
for value in output.iter() {
assert!(*value >= 0.0);
}
}
#[test]
fn test_depthwise_conv2d_same_padding() {
// Test Same padding
let mut model = Sequential::new();
let mut depthwise_layer = DepthwiseConv2D::new(
2, // filters
(3, 3), // kernel_size
(1, 1), // strides
PaddingType::Same, // padding
Linear::new(), // no activation
)
.unwrap();
depthwise_layer.initialize_weights(2);
model
.add(depthwise_layer)
.compile(SGD::new(0.01).unwrap(), MeanSquaredError::new());
// Create input data
let input_data =
Array4::from_shape_fn((1, 2, 5, 5), |(_, c, h, w)| (c * 100 + h * 10 + w) as f32)
.into_dyn();
// Forward propagation
let output = model.predict(&input_data).unwrap();
// With Same padding, output shape should match input shape (except for channel count)
assert_eq!(output.shape(), &[1, 2, 5, 5]);
}
#[test]
fn test_depthwise_conv2d_different_strides() {
// Test different strides
let mut model = Sequential::new();
let mut depthwise_layer = DepthwiseConv2D::new(
2, // filters
(3, 3), // kernel_size
(2, 2), // strides - larger strides
PaddingType::Valid, // padding
Linear::new(), // no activation
)
.unwrap();
depthwise_layer.initialize_weights(2);
model
.add(depthwise_layer)
.compile(SGD::new(0.01).unwrap(), MeanSquaredError::new());
// Create input data
let input_data =
Array4::from_shape_fn((1, 2, 8, 8), |(_, c, h, w)| (c * 100 + h * 10 + w) as f32)
.into_dyn();
// Forward propagation
let output = model.predict(&input_data).unwrap();
// Verify output shape
// Input: [1, 2, 8, 8], kernel (3,3), stride (2,2), valid padding
// Output should be: [1, 2, 3, 3] ((8-3)/2 + 1 = 3)
assert_eq!(output.shape(), &[1, 2, 3, 3]);
}
#[test]
fn test_depthwise_conv2d_training() {
// Test training process
let mut model = Sequential::new();
let mut depthwise_layer = DepthwiseConv2D::new(
2, // filters
(3, 3), // kernel_size
(1, 1), // strides
PaddingType::Valid, // padding
ReLU::new(), // activation
)
.unwrap();
depthwise_layer.initialize_weights(2);
model
.add(depthwise_layer)
.compile(SGD::new(0.1).unwrap(), MeanSquaredError::new());
// Create training data
let input_data = Array4::from_shape_fn((4, 2, 5, 5), |(b, c, h, w)| {
(b + c * 10 + h + w) as f32 * 0.1
})
.into_dyn();
// Create target data (output shape should be [4, 2, 3, 3])
let target_data = Array4::ones((4, 2, 3, 3)).into_dyn();
// Train the model
let result = model.fit(&input_data, &target_data, 5);
assert!(result.is_ok());
// Verify predictions
let predictions = model.predict(&input_data).unwrap();
assert_eq!(predictions.shape(), &[4, 2, 3, 3]);
}
#[test]
fn test_depthwise_conv2d_backward() {
// Test backward propagation
let input_channels = 2;
let mut layer = DepthwiseConv2D::new(
input_channels, // filters
(2, 2), // kernel_size
(1, 1), // strides
PaddingType::Valid, // padding
Linear::new(), // no activation
)
.unwrap();
layer.initialize_weights(input_channels);
// Create simple input data
let input_data =
Array4::from_shape_fn((1, 2, 3, 3), |(_, c, h, w)| (c * 10 + h * 3 + w) as f32).into_dyn();
// Forward propagation
let output = layer.forward(&input_data).unwrap();
assert_eq!(output.shape(), &[1, 2, 2, 2]);
// Create gradient output
let grad_output = Array4::ones((1, 2, 2, 2)).into_dyn();
// Backward propagation
let grad_input = layer.backward(&grad_output).unwrap();
// Verify gradient input shape
assert_eq!(grad_input.shape(), input_data.shape());
}
#[test]
fn test_depthwise_conv2d_channel_independence() {
// Verify that depthwise convolution indeed processes each channel independently
let mut layer = DepthwiseConv2D::new(
2, // filters
(2, 2), // kernel_size
(1, 1), // strides
PaddingType::Valid, // padding
Linear::new(), // no activation
)
.unwrap();
layer.initialize_weights(2);
// Create special input: first channel all 1s, second channel all 2s
let mut input_data = Array4::zeros((1, 2, 3, 3));
// Set first channel to 1
for h in 0..3 {
for w in 0..3 {
input_data[[0, 0, h, w]] = 1.0;
}
}
// Set second channel to 2
for h in 0..3 {
for w in 0..3 {
input_data[[0, 1, h, w]] = 2.0;
}
}
let input = input_data.into_dyn();
// Forward propagation
let output = layer.forward(&input).unwrap();
// Output shape should be [1, 2, 2, 2]
assert_eq!(output.shape(), &[1, 2, 2, 2]);
// Since depthwise convolution processes channels independently,
// the output values of the second channel should be proportional to those of the first channel
// (assuming same weights and biases)
}
#[test]
fn test_depthwise_conv2d_edge_cases() {
// Test edge cases
// 1. Minimum convolution kernel size (1x1)
let mut layer_1x1 = DepthwiseConv2D::new(
1, // filters
(1, 1), // kernel_size
(1, 1), // strides
PaddingType::Valid, // padding
Linear::new(), // no activation
)
.unwrap();
layer_1x1.initialize_weights(1);
let input_1x1 = Array4::ones((1, 1, 2, 2)).into_dyn();
let output_1x1 = layer_1x1.forward(&input_1x1).unwrap();
assert_eq!(output_1x1.shape(), &[1, 1, 2, 2]);
// 2. Large strides resulting in small output
let mut layer_large_stride = DepthwiseConv2D::new(
1, // filters
(2, 2), // kernel_size
(3, 3), // large strides
PaddingType::Valid, // padding
Linear::new(), // no activation
)
.unwrap();
layer_large_stride.initialize_weights(1);
let input_large = Array4::ones((1, 1, 5, 5)).into_dyn();
let output_large = layer_large_stride.forward(&input_large).unwrap();
assert_eq!(output_large.shape(), &[1, 1, 2, 2]);
}
#[test]
fn test_depthwise_conv2d_multiple_batches() {
// Test multiple batch data
let mut model = Sequential::new();
let mut depthwise_layer = DepthwiseConv2D::new(
3, // filters
(2, 2), // kernel_size
(1, 1), // strides
PaddingType::Valid, // padding
Linear::new(), // no activation
)
.unwrap();
depthwise_layer.initialize_weights(3);
model
.add(depthwise_layer)
.compile(SGD::new(0.01).unwrap(), MeanSquaredError::new());
// Create multi-batch input data
let batch_size = 5;
let input_data = Array4::from_shape_fn((batch_size, 3, 4, 4), |(b, c, h, w)| {
(b * 1000 + c * 100 + h * 10 + w) as f32 * 0.01
})
.into_dyn();
// Forward propagation
let output = model.predict(&input_data).unwrap();
// Verify output shape
assert_eq!(output.shape(), &[batch_size, 3, 3, 3]);
// Verify that outputs of different batches are indeed different
let batch0_output = output.slice(s![0, 0, 0, 0]).into_scalar();
let batch1_output = output.slice(s![1, 0, 0, 0]).into_scalar();
assert_ne!(batch0_output, batch1_output);
}