Skip to content

Commit

Permalink
Make roi_align_rotated_op_test not rely on 1.12.0 numpy.rot90 (pytorc…
Browse files Browse the repository at this point in the history
…h#9267)

Summary:
Breaking this out of pytorch#8338

Use a local version of `np.rot90` with an `axes` argument, since we don't have NumPy 1.12.0 in all of the test environments. Caffe2 conda2-ubuntu16.04, for example, fails. Generally, it seems better to not require a NumPy bump just for this test.

cc mingzhe09088
Pull Request resolved: pytorch#9267

Reviewed By: mingzhe09088

Differential Revision: D8767819

Pulled By: orionr

fbshipit-source-id: c51a6295d58366eba06e4e55e3f1ffaa8af96975
  • Loading branch information
orionr authored and facebook-github-bot committed Jul 9, 2018
1 parent 768a0e3 commit 936f47f
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion caffe2/python/operator_test/roi_align_rotated_op_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,59 @@ def test_simple_rotations(
sampling_ratio=0,
)

def roialign_rot90(m, k=1, axes=(0,1)):
axes = tuple(axes)
if len(axes) != 2:
raise ValueError("len(axes) must be 2.")

m = np.asanyarray(m)

if axes[0] == axes[1] or np.absolute(axes[0] - axes[1]) == m.ndim:
raise ValueError("Axes must be different.")

if (axes[0] >= m.ndim or axes[0] < -m.ndim or
axes[1] >= m.ndim or axes[1] < -m.ndim):
raise ValueError(
"Axes={} out of range for array of ndim={}.".format(axes, m.ndim))

k %= 4

if k == 0:
return m[:]
if k == 2:
return roialign_flip(roialign_flip(m, axes[0]), axes[1])

axes_list = np.arange(0, m.ndim)
(axes_list[axes[0]], axes_list[axes[1]]) = (axes_list[axes[1]],
axes_list[axes[0]])

if k == 1:
return np.transpose(roialign_flip(m,axes[1]), axes_list)
else:
# k == 3
return roialign_flip(np.transpose(m, axes_list), axes[1])

def roialign_flip(m, axis):
if not hasattr(m, 'ndim'):
m = np.asarray(m)
indexer = [slice(None)] * m.ndim
try:
indexer[axis] = slice(None, None, -1)
except IndexError:
raise ValueError("axis=%i is invalid for the %i-dimensional input array"
% (axis, m.ndim))
return m[tuple(indexer)]

def roialign_ref(X, R):
# `angle` denotes counter-clockwise rotation. Rotate the input
# feature map in the opposite (clockwise) direction and perform
# standard RoIAlign. We assume all RoIs have the same angle.
#
# Also note that we need to have our own version of np.rot90,
# since axes isn't an argument until 1.12.0 and doesn't exist
# on all tested platforms.
norm_angle = (angle + 360) % 360
X_ref = np.rot90(X, k=-norm_angle / 90, axes=(2, 3))
X_ref = roialign_rot90(X, k=-norm_angle / 90, axes=(2, 3))

# Rotate RoIs clockwise wrt the center of the input feature
# map to make them horizontal and convert from
Expand Down

0 comments on commit 936f47f

Please sign in to comment.