/
final_blaze_block.rs
207 lines (184 loc) · 5.09 KB
/
final_blaze_block.rs
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
// Reference implementation:
// https://github.com/hollance/BlazeFace-PyTorch/blob/master/blazeface.py
use candle_core::{Module, Result, Tensor};
use candle_nn::{Conv2d, Conv2dConfig, VarBuilder};
/// Final BlazeBlock.
pub(crate) struct FinalBlazeBlock {
conv0: Conv2d,
conv1: Conv2d,
}
impl FinalBlazeBlock {
pub(crate) fn load(
channels: usize,
variables: &VarBuilder,
weight_0_name: &str,
bias_0_name: &str,
weight_1_name: &str,
bias_1_name: &str,
) -> Result<Self> {
let weight_0 = variables.get_with_hints(
(channels, 1, 3, 3),
weight_0_name,
candle_nn::init::ZERO,
)?;
let bias_0 = variables.get_with_hints(
channels,
bias_0_name,
candle_nn::init::ZERO,
)?;
let weight_1 = variables.get_with_hints(
(channels, channels, 1, 1),
weight_1_name,
candle_nn::init::ZERO,
)?;
let bias_1 = variables.get_with_hints(
channels,
bias_1_name,
candle_nn::init::ZERO,
)?;
Self::new(
channels, weight_0, bias_0, weight_1, bias_1,
)
}
fn new(
channels: usize,
weight_0: Tensor,
bias_0: Tensor,
weight_1: Tensor,
bias_1: Tensor,
) -> Result<Self> {
Ok(Self {
conv0: Conv2d::new(
weight_0,
Some(bias_0),
Conv2dConfig {
stride: 2,
groups: channels,
..Default::default()
},
),
conv1: Conv2d::new(
weight_1,
Some(bias_1),
Conv2dConfig {
..Default::default()
},
),
})
}
}
impl Module for FinalBlazeBlock {
fn forward(
&self,
input: &Tensor,
) -> Result<Tensor> {
let h = input
.pad_with_zeros(2, 0, 2)? // height padding
.pad_with_zeros(3, 0, 2)?; // width padding
let x = self.conv0.forward(&h)?;
let x = self.conv1.forward(&x)?;
x.relu()
}
}
#[cfg(test)]
mod tests {
use super::*;
use candle_core::{safetensors, DType, Device, Tensor};
#[test]
fn test_final_blaze_block() {
// Set up the device
let device = Device::Cpu;
// Set up the configuration
let batch_size = 1;
let channels = 96;
let width = 64;
let height = 64;
// Set up the convolution parameters
let weight_0 =
Tensor::rand(0., 1., (channels, 1, 3, 3), &device).unwrap();
let bias_0 = Tensor::rand(0., 1., channels, &device).unwrap();
let weight_1 = Tensor::rand(
0.,
1.,
(channels, channels, 1, 1),
&device,
)
.unwrap();
let bias_1 = Tensor::rand(0., 1., channels, &device).unwrap();
// Instantiate the FinalBlazeBlock
let block = FinalBlazeBlock::new(
channels, weight_0, bias_0, weight_1, bias_1,
)
.unwrap();
// Set up the input Tensor
let input = Tensor::rand(
0.,
1.,
(batch_size, channels, height, width),
&device,
)
.unwrap(); // (1, 96, 64, 64)
// Call forward method and get the output
let output = block.forward(&input).unwrap(); // (1, 96, 32, 32)
assert_eq!(
output.dims(),
&[
batch_size,
channels,
height / 2,
width / 2
]
);
}
#[test]
fn test_load() {
// Set up the device
let device = Device::Cpu;
let dtype = DType::F16;
// Set up the configuration
let batch_size = 1;
let width = 64;
let height = 64;
// Load the variables
let safetensors = safetensors::load(
"src/blaze_face/data/blazefaceback.safetensors",
&device,
)
.unwrap();
let variables =
candle_nn::VarBuilder::from_tensors(safetensors, dtype, &device);
// Instantiate the FinalBlazeBlock
let single_block = FinalBlazeBlock::load(
96,
&variables,
"final.convs.0.weight",
"final.convs.0.bias",
"final.convs.1.weight",
"final.convs.1.bias",
)
.unwrap();
// Set up the input Tensor
let input = Tensor::rand(
0.,
1.,
(batch_size, 96, height, width),
&device,
)
.unwrap()
.to_dtype(dtype)
.unwrap(); // (1, 96, 64, 64)
// Call forward method and get the output
let output = single_block
.forward(&input)
.unwrap(); // (1, 96, 32, 32)
assert_eq!(
output.dims(),
&[
batch_size,
96,
height / 2,
width / 2
]
);
}
}