-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Autodiff/training support for Nearest Interpolation (#1414)
Add training support for nearest interpolation --------- Co-authored-by: yurzhang <[email protected]> Co-authored-by: Dilshod Tadjibaev <[email protected]>
- Loading branch information
1 parent
0601dc7
commit 0c92c8c
Showing
14 changed files
with
469 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#[burn_tensor_testgen::testgen(ad_nearest_interpolate)] | ||
mod tests { | ||
use super::*; | ||
use burn_tensor::module::interpolate; | ||
use burn_tensor::ops::{InterpolateMode, InterpolateOptions}; | ||
use burn_tensor::{Data, Shape, Tensor}; | ||
|
||
#[test] | ||
fn test_upsample_interpolation() { | ||
let test = InterpolateTestCase { | ||
batch_size: 2, | ||
channels: 1, | ||
height: 7, | ||
width: 5, | ||
height_out: 8, | ||
width_out: 7, | ||
}; | ||
|
||
test.assert_output(TestTensor::from([ | ||
[[ | ||
[4., 2., 4., 2., 2.], | ||
[2., 1., 2., 1., 1.], | ||
[2., 1., 2., 1., 1.], | ||
[2., 1., 2., 1., 1.], | ||
[2., 1., 2., 1., 1.], | ||
[2., 1., 2., 1., 1.], | ||
[2., 1., 2., 1., 1.], | ||
]], | ||
[[ | ||
[4., 2., 4., 2., 2.], | ||
[2., 1., 2., 1., 1.], | ||
[2., 1., 2., 1., 1.], | ||
[2., 1., 2., 1., 1.], | ||
[2., 1., 2., 1., 1.], | ||
[2., 1., 2., 1., 1.], | ||
[2., 1., 2., 1., 1.], | ||
]], | ||
])); | ||
} | ||
|
||
#[test] | ||
fn test_downsample_interpolation() { | ||
let test = InterpolateTestCase { | ||
batch_size: 1, | ||
channels: 1, | ||
height: 8, | ||
width: 8, | ||
height_out: 4, | ||
width_out: 6, | ||
}; | ||
|
||
test.assert_output(TestTensor::from([[[ | ||
[1., 1., 1., 0., 1., 1., 1., 0.], | ||
[0., 0., 0., 0., 0., 0., 0., 0.], | ||
[1., 1., 1., 0., 1., 1., 1., 0.], | ||
[0., 0., 0., 0., 0., 0., 0., 0.], | ||
[1., 1., 1., 0., 1., 1., 1., 0.], | ||
[0., 0., 0., 0., 0., 0., 0., 0.], | ||
[1., 1., 1., 0., 1., 1., 1., 0.], | ||
[0., 0., 0., 0., 0., 0., 0., 0.], | ||
]]])); | ||
} | ||
|
||
struct InterpolateTestCase { | ||
batch_size: usize, | ||
channels: usize, | ||
height: usize, | ||
width: usize, | ||
height_out: usize, | ||
width_out: usize, | ||
} | ||
|
||
impl InterpolateTestCase { | ||
fn assert_output(self, x_grad: TestTensor<4>) { | ||
let shape_x = Shape::new([self.batch_size, self.channels, self.height, self.width]); | ||
let device = Default::default(); | ||
let x = TestAutodiffTensor::from_data( | ||
TestTensorInt::arange(0..shape_x.num_elements() as i64, &x_grad.device()) | ||
.reshape(shape_x) | ||
.into_data() | ||
.convert(), | ||
&device, | ||
) | ||
.require_grad(); | ||
|
||
let output = interpolate( | ||
x.clone(), | ||
[self.height_out, self.width_out], | ||
InterpolateOptions::new(InterpolateMode::Nearest), | ||
); | ||
|
||
let grads = output.backward(); | ||
let x_grad_actual = x.grad(&grads).unwrap(); | ||
|
||
x_grad | ||
.to_data() | ||
.assert_approx_eq(&x_grad_actual.into_data(), 3); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.