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 208 209 210 211
|
import numpy as np
from numpy.testing import assert_array_equal
from caffe2.python import core, workspace
from caffe2.python.test_util import TestCase
from caffe2.proto import caffe2_pb2
class TestLengthsToShapeOps(TestCase):
def test_lengths_to_shape_ops(self):
workspace.FeedBlob('l', np.array([200, 200, 200], dtype=np.int32))
workspace.RunOperatorOnce(core.CreateOperator(
'LengthsToShape', ['l'], ['s']))
workspace.FeedBlob('res', np.array([3, 200], dtype=np.int32))
assert_array_equal(workspace.FetchBlob('s'), workspace.FetchBlob('res'))
def test_reshape_ops(self):
workspace.FeedBlob('res', np.array([[0, 0, 0, 0]], dtype=np.float32))
workspace.FeedBlob('shape', np.array([1, 4], dtype=np.int32))
workspace.FeedBlob('input', np.zeros((2, 2), dtype=np.float32))
workspace.RunOperatorOnce(core.CreateOperator(
'Reshape', ['input', 'shape'], ['output', 'old_shape']))
assert_array_equal(workspace.FetchBlob('output'),
workspace.FetchBlob('res'))
def test_basic_reshape(self):
_test_reshape_output_and_gradient(old_shape=(4, 2, 1), new_shape=(2, 4))
_test_reshape_output_and_gradient(old_shape=(4, 2, 1), new_shape=(2, 4), arg_shape=False)
def test_missing_dim(self):
_test_reshape_output_and_gradient(old_shape=(4, 2, 1), new_shape=(-1, 8))
_test_reshape_output_and_gradient(old_shape=(4, 2, 1), new_shape=(-1, 8), arg_shape=False)
def test_in_place(self):
_test_reshape_output_and_gradient(old_shape=(4, 2, 1), new_shape=(-1, 8), in_place=True)
_test_reshape_output_and_gradient(old_shape=(4, 2, 1), new_shape=(-1, 8),
in_place=True, arg_shape=False)
def test_zero_dim(self):
_test_reshape_output_and_gradient(old_shape=(4, 2, 1), new_shape=(0, 0, 0),
expected_shape=(4, 2, 1))
_test_reshape_output_and_gradient(old_shape=(4, 2, 1), new_shape=(0, 0, 0),
expected_shape=(4, 2, 1), arg_shape=False)
_test_reshape_output_and_gradient(old_shape=(4, 2, 1), new_shape=(0, 2, 1),
expected_shape=(4, 2, 1))
_test_reshape_output_and_gradient(old_shape=(4, 2, 1), new_shape=(0, 2, 1),
expected_shape=(4, 2, 1), arg_shape=False)
_test_reshape_output_and_gradient(old_shape=(0, 0), new_shape=(0, 0, 0),
expected_shape=(0, 0, 0), arg_shape=False)
def test_zero_dim_and_missing_dim(self):
_test_reshape_output_and_gradient(old_shape=(4, 2, 1), new_shape=(0, -1, 0),
expected_shape=(4, 2, 1))
_test_reshape_output_and_gradient(old_shape=(4, 2, 1), new_shape=(0, -1, 0),
expected_shape=(4, 2, 1), arg_shape=False)
_test_reshape_output_and_gradient(old_shape=(4, 3, 2), new_shape=(-1, 0),
expected_shape=(8, 3))
_test_reshape_output_and_gradient(old_shape=(4, 3, 2), new_shape=(-1, 0),
expected_shape=(8, 3), arg_shape=False)
# empty tensor will just have -1 dim = 0
_test_reshape_output_and_gradient(
old_shape=(2, 0),
new_shape=(-1, 0),
expected_shape=(0, 0),
arg_shape=False
)
def test_backprop(self):
old_shape = (4, 2, 1)
new_shape = (1, 8)
X = np.random.rand(*old_shape).astype(np.float32)
Y = np.random.rand(*new_shape).astype(np.float32)
net = core.Net('net')
net.GivenTensorFill([], 'X', shape=old_shape, values=X.flatten())
net.GivenTensorFill([], 'Y', shape=new_shape, values=Y.flatten())
net.Reshape(['X'], ['X_out', 'old_shape'], shape=new_shape)
net.DotProduct(['X_out', 'Y'], 'Z')
net.AddGradientOperators(['Z'])
workspace.RunNetOnce(net)
Z = workspace.FetchBlob('Z')
X_grad = workspace.FetchBlob('X_grad')
# Check forward computation
np.testing.assert_allclose(
Z.squeeze(), X.reshape(new_shape).dot(Y.T).squeeze(), rtol=1e-5)
# Check the shape of the gradient
np.testing.assert_array_equal(X_grad.shape, X.shape)
# Check the gradient
np.testing.assert_allclose(X_grad, Y.reshape(old_shape), rtol=1e-5)
def test_input_shape_changes(self):
workspace.FeedBlob(
'input_blob',
np.array(np.random.rand(10, 20, 10), dtype=np.float32))
net = core.Net('mynet')
z, _ = net.Reshape('input_blob',
['z_reshape', 'dummy_size'],
shape=(-1, 10))
workspace.CreateNet(net)
workspace.RunNet(net)
workspace.FeedBlob(
'input_blob',
np.array(np.random.rand(10, 40, 10), dtype=np.float32))
workspace.RunNet(net)
def test_nonempty_tensor_gradient(self):
old_shape = [4, 2]
new_shape = [1, 2, -1]
expected_new_shape = [1, 2, 4]
_test_reshape_output_and_gradient(
old_shape=old_shape,
new_shape=new_shape,
expected_shape=expected_new_shape,
expected_gradient=np.ones(shape=old_shape)
)
def test_empty_tensor(self):
old_shape = [4, 0]
new_shape = [1, -1]
expected_new_shape = [1, 0]
_test_reshape_output_and_gradient(
old_shape=old_shape,
new_shape=new_shape,
expected_shape=expected_new_shape,
expected_gradient=np.empty(shape=old_shape)
)
def test_one_dim_empty_tensor_gradient(self):
old_shape = (0,)
new_shape = [1, -1]
expected_new_shape = [1, 0]
_test_reshape_output_and_gradient(
old_shape=old_shape,
new_shape=new_shape,
expected_shape=expected_new_shape,
expected_gradient=np.empty(shape=old_shape)
)
def test_one_dim_and_empty_tensor(self):
old_shape = (0,)
new_shape = [0, -1]
expected_new_shape = [0, 0]
_test_reshape_output_and_gradient(old_shape=old_shape, new_shape=new_shape, expected_shape=expected_new_shape)
def test_scalar_to_tensor(self):
old_shape = ()
new_shape = [1, -1]
expected_new_shape = [1, 1]
_test_reshape_output_and_gradient(old_shape=old_shape, new_shape=new_shape, expected_shape=expected_new_shape)
def _test_reshape_output_and_gradient(
old_shape,
new_shape,
expected_shape=None,
arg_shape=True,
in_place=False,
expected_gradient=None
):
devices = [core.DeviceOption(caffe2_pb2.CPU, 0)]
if workspace.NumGpuDevices() > 0:
devices.append(core.DeviceOption(workspace.GpuDeviceType, 0))
for device_opt in devices:
with core.DeviceScope(device_opt):
if expected_shape is None:
expected_shape = new_shape
net = core.Net('net')
if len(old_shape) == 0:
# scalar, convert to tensor before feeding to blob
X = np.atleast_1d(np.random.rand(*old_shape))
else:
X = np.random.rand(*old_shape).astype(np.float32)
blob_in = 'X'
blob_out = blob_in if in_place else blob_in + '_out'
if arg_shape:
out, _ = net.Reshape([blob_in], [blob_out, 'old_shape'], shape=new_shape)
else:
out, _ = net.Reshape([blob_in, 'new_shape'], [blob_out, 'old_shape'])
workspace.FeedBlob('new_shape', np.asarray(new_shape))
workspace.FeedBlob(blob_in, X)
if expected_gradient is not None:
net.AddGradientOperators([out])
workspace.CreateNet(net)
workspace.RunNetOnce(net)
Y = workspace.FetchBlob(blob_out)
np.testing.assert_allclose(Y, X.reshape(expected_shape))
if expected_gradient is not None:
data_grad = workspace.FetchBlob(blob_in + '_grad')
np.testing.assert_array_equal(data_grad, expected_gradient)
if __name__ == "__main__":
import unittest
unittest.main()
|