Files
wehub-resource-sync 5cbd3f29e3
Fuzz / Run fuzz harnesses (${{ github.event_name == 'schedule' && 'nightly' || 'smoke' }}) (push) Has been cancelled
Create Releases / call-mac (push) Has been cancelled
Create Releases / call-linux (push) Has been cancelled
Create Releases / call-sdist (push) Has been cancelled
Create Releases / call-win (push) Has been cancelled
Create Releases / call-pyodide (push) Has been cancelled
Windows_No_Exception_CI / build (x64, 3.10) (push) Has been cancelled
Check URLs / build (push) Has been cancelled
Create Releases / Attest CI build artifacts (push) Has been cancelled
Create Releases / Check for Publish release build to pypi (push) Has been cancelled
Create Releases / Check for Publish preview build to test.pypi-weekly (push) Has been cancelled
Create Releases / Publish preview build to test.pypi-weekly (push) Has been cancelled
Create Releases / Check for Publish release build to test.pypi (rc-candidates) (push) Has been cancelled
Create Releases / Publish release build to test.pypi (push) Has been cancelled
Create Releases / Check for Publish preview build to pypi-weekly (push) Has been cancelled
Create Releases / Publish preview build to pypi-weekly (push) Has been cancelled
Create Releases / Publish release build to pypi (push) Has been cancelled
Create Releases / test source distribution (push) Has been cancelled
clang-tidy / clang-tidy (push) Has been cancelled
Lint / Validate SBOM (push) Has been cancelled
Lint / Enforce style (push) Has been cancelled
CI / Test windows-2022, 3.14, External, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, Internal, debug=1, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=1, onnx_ml=1, autogen=1 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=0, onnx_ml=0, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
Pixi CI / Install and lint (ubuntu-24.04-arm) (push) Has been cancelled
Pixi CI / Install and lint (windows-2022) (push) Has been cancelled
Pixi CI / Xcode generator build (push) Has been cancelled
Pixi CI / Install and test (macos-latest, default) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-24.04-arm, default) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-latest, default) (push) Has been cancelled
Pixi CI / Install and test (windows-2022, default) (push) Has been cancelled
Pixi CI / Install and test (macos-latest, oldies) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-24.04-arm, oldies) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-latest, oldies) (push) Has been cancelled
Pixi CI / Install and test (windows-2022, oldies) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Copilot Setup Steps / copilot-setup-steps (push) Has been cancelled
Generate and publish ONNX docs / build (push) Has been cancelled
Generate and publish ONNX docs / deploy (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:41:19 +08:00

91 lines
3.4 KiB
Python

# Copyright (c) ONNX Project Contributors
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
import numpy as np
from onnx.reference.op_run import OpRun
def construct_original_grid(data_size, align_corners):
is_2d = len(data_size) == 2
size_zeros = np.zeros(data_size)
original_grid = [np.ones(data_size)]
for dim, dim_size in enumerate(data_size):
if align_corners == 1:
step = 2.0 / (dim_size - 1)
start = -1
stop = 1 + 0.0001
a = np.arange(start, stop, step)
else:
step = 2.0 / dim_size
start = -1 + step / 2
stop = 1
a = np.arange(start, stop, step)
if dim == 0:
if is_2d:
y = np.reshape(a, (dim_size, 1)) + size_zeros
original_grid = [y, *original_grid]
else:
z = np.reshape(a, (dim_size, 1, 1)) + size_zeros
original_grid = [z, *original_grid]
elif dim == 1:
if is_2d:
x = np.reshape(a, (1, dim_size)) + size_zeros
original_grid = [x, *original_grid]
else:
y = np.reshape(a, (1, dim_size, 1)) + size_zeros
original_grid = [y, *original_grid]
else:
x = np.reshape(a, (1, dim_size)) + size_zeros
original_grid = [x, *original_grid]
return np.stack(original_grid, axis=2 if is_2d else 3)
def apply_affine_transform(theta_n, original_grid_homo):
# theta_n: (N, 2, 3) for 2D, (N, 3, 4) for 3D
# original_grid_homo: (H, W, 3) for 2D, (D, H, W, 4) for 3D
assert theta_n.ndim == 3, (
"theta_n shall have shape of (N, 2, 3) for 2D, (N, 3, 4) for 3D"
)
if original_grid_homo.ndim == 3:
N, dim_2d, dim_homo = theta_n.shape
assert dim_2d == 2 and dim_homo == 3
H, W, dim_homo = original_grid_homo.shape
assert dim_homo == 3
# reshape to [H * W, dim_homo] and then transpose to [dim_homo, H * W]
original_grid_transposed = np.transpose(
np.reshape(original_grid_homo, (H * W, dim_homo))
)
grid_n = np.matmul(
theta_n, original_grid_transposed
) # shape (N, dim_2d, H * W)
# transpose to (N, H * W, dim_2d) and then reshape to (N, H, W, dim_2d)
grid = np.reshape(np.transpose(grid_n, (0, 2, 1)), (N, H, W, dim_2d))
return grid.astype(np.float32)
assert original_grid_homo.ndim == 4
N, dim_3d, dim_homo = theta_n.shape
assert dim_3d == 3 and dim_homo == 4
D, H, W, dim_homo = original_grid_homo.shape
assert dim_homo == 4
# reshape to [D * H * W, dim_homo] and then transpose to [dim_homo, D * H * W]
original_grid_transposed = np.transpose(
np.reshape(original_grid_homo, (D * H * W, dim_homo))
)
grid_n = np.matmul(
theta_n, original_grid_transposed
) # shape (N, dim_3d, D * H * W)
# transpose to (N, D * H * W, dim_3d) and then reshape to (N, D, H, W, dim_3d)
grid = np.reshape(np.transpose(grid_n, (0, 2, 1)), (N, D, H, W, dim_3d))
return grid.astype(np.float32)
class AffineGrid(OpRun):
def _run(self, theta, size, align_corners=None):
align_corners = align_corners or self.align_corners
_, _, *data_size = size
original_grid = construct_original_grid(data_size, align_corners)
grid = apply_affine_transform(theta, original_grid)
return (grid,)