Skip to content

Commit

Permalink
Fix CPU resize with mixed NN/other resampling filters. (#5647)
Browse files Browse the repository at this point in the history
* Fix size computation (handle negative support size)
* Add regression tests

---------

Signed-off-by: Michal Zientkiewicz <michalz@nvidia.com>
  • Loading branch information
mzient committed Sep 27, 2024
1 parent 92ea873 commit 57909cc
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dali/kernels/imgproc/resample/separable_cpu.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019-2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// Copyright (c) 2019-2021, 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -96,7 +96,7 @@ struct ResamplingSetupCPU : SeparableResamplingSetup<_spatial_ndim> {

for (int stage = 0; stage < num_stages; stage++) {
size_t extent = resized_dim_extent(stage);
size_t support = filter_support(stage);
int support = std::max(0, filter_support(stage)); // support() returns -1 for NN interp.
size_t num_coeffs = extent * support;

if (extent > req.indices_size)
Expand Down
46 changes: 46 additions & 0 deletions dali/test/python/operator_2/test_resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,52 @@ def resize_pipe():
assert np.max(np.abs(out - large_data_resized)) < 2


@params(("cpu", 0), ("cpu", 1), ("gpu", 0), ("gpu", 1))
def test_nn_on_one_axis(device, axis):
# Checks whether having NN interpolation in one axis and full resampling in the other works
data = np.array(
[
[0, 0, 0],
[0, 1, 0],
[0, 2, 0],
],
dtype=np.float32,
)

# magnification is NN, minification is triangular
ref = np.array(
[
[0, 0, 0.3333334, 0.3333334, 0, 0],
[0, 0, 1.6666667, 1.6666667, 0, 0],
],
dtype=np.float32,
)

if axis == 1:
data = np.transpose(data, (1, 0))
ref = np.transpose(ref, (1, 0))

# add channel
data = data[..., np.newaxis]
ref = ref[..., np.newaxis]

@pipeline_def(batch_size=1, device_id=0, num_threads=1)
def test_pipe():
src = dali.types.Constant(data, device=device)
return fn.resize(
src,
size=ref.shape[:-1],
min_filter=dali.types.INTERP_LINEAR,
mag_filter=dali.types.INTERP_NN,
antialias=True,
)

pipe = test_pipe()
pipe.build()
(out,) = pipe.run()
check_batch(out, [ref], 1, 1e-5, 1e-5, None, False)


def test_checkerboard_dali_vs_onnx_ref():
improc_data_dir = os.path.join(test_data_root, "db", "imgproc")
ref_dir = os.path.join(improc_data_dir, "ref", "resampling")
Expand Down

0 comments on commit 57909cc

Please sign in to comment.