def test_channelshape_str(): assert (repr(ChannelShape( (1, 2, 3))) == "ChannelShape(shape=(1, 2, 3), channels_last=True)") assert (repr(ChannelShape((1, 2, 3), channels_last=False)) == "ChannelShape(shape=(1, 2, 3), channels_last=False)") # `str` always has channels last assert str(ChannelShape((1, 2, 3))) == "(1, 2, ch=3)" assert str(ChannelShape((1, 2, 3), channels_last=False)) == "(ch=1, 2, 3)"
def split_transform(transform, in_slice=None, out_slice=None): a_slice = slice(None) b_slice = slice(None) if isinstance(transform, Convolution): if in_slice is not None: assert in_slice.channel_slice_only() a_slice = in_slice.channel_slice if out_slice is not None: assert out_slice.channel_slice_only() b_slice = out_slice.channel_slice assert isinstance(transform.init, np.ndarray), "doesn't work with distributions" kernel = transform.init[:, :, a_slice, b_slice] rows, cols = transform.input_shape.spatial_shape nc = kernel.shape[2] input_shape = ChannelShape( (rows, cols, nc) if transform.channels_last else (nc, rows, cols), channels_last=transform.channels_last, ) return Convolution( kernel.shape[3], input_shape, strides=transform.strides, channels_last=transform.channels_last, padding=transform.padding, kernel_size=transform.kernel_size, init=kernel, ) else: if in_slice is not None: assert in_slice.channel_slice_only() a_slice = in_slice.channel_slice if out_slice is not None: assert out_slice.channel_slice_only() b_slice = out_slice.channel_slice return transform[b_slice, a_slice]
def test_transforms(): check_init_args(Dense, ["shape", "init"]) # No check_repr because dense matrices are usually too big assert repr(Dense((1, 2), init=[[1, 1]])) == "Dense(shape=(1, 2))" check_init_args( Convolution, [ "n_filters", "input_shape", "kernel_size", "strides", "padding", "channels_last", "init", ], ) check_repr(Convolution(n_filters=3, input_shape=(1, 2, 3))) check_repr( Convolution(n_filters=3, input_shape=(1, 2, 3), kernel_size=(3, 2))) check_repr( Convolution(n_filters=3, input_shape=(1, 2, 3), channels_last=False)) assert (repr(Convolution( n_filters=3, input_shape=(1, 2, 3))) == "Convolution(n_filters=3, input_shape=(1, 2, 3))") assert (repr( Convolution(n_filters=3, input_shape=(1, 2, 3), kernel_size=(3, 2)) ) == "Convolution(n_filters=3, input_shape=(1, 2, 3), kernel_size=(3, 2))") assert ( repr( Convolution(n_filters=3, input_shape=(1, 2, 3), channels_last=False)) == "Convolution(n_filters=3, input_shape=(1, 2, 3), channels_last=False)") check_init_args(Sparse, ["shape", "indices", "init"]) # No check_repr because sparse matrices are usually too big assert repr(Sparse((1, 1), indices=[[1, 1], [1, 1]])) == "Sparse(shape=(1, 1))" assert (repr(Sparse((1, 1), indices=[[1, 1], [1, 1], [1, 1]], init=2)) == "Sparse(shape=(1, 1))") check_init_args(SparseMatrix, ["indices", "data", "shape"]) check_repr( SparseMatrix(indices=[[1, 2], [3, 4]], data=[5, 6], shape=(7, 8))) assert repr(SparseMatrix(((1, 2), (3, 4)), (5, 6), (7, 8))).replace( ", dtype=int64", "") == ("SparseMatrix(indices=array([[1, 2],\n [3, 4]]), " "data=array([5, 6]), shape=(7, 8))") check_init_args(ChannelShape, ["shape", "channels_last"]) check_repr(ChannelShape(shape=(1, 2, 3), channels_last=True)) assert (repr(ChannelShape( (1, 2, 3))) == "ChannelShape(shape=(1, 2, 3), channels_last=True)") assert (repr(ChannelShape((1, 2, 3), channels_last=False)) == "ChannelShape(shape=(1, 2, 3), channels_last=False)") # __str__ always has channels last assert str(ChannelShape((1, 2, 3))) == "(1, 2, ch=3)" assert str(ChannelShape((1, 2, 3), channels_last=False)) == "(ch=1, 2, 3)" check_init_args(NoTransform, ["size_in"]) check_repr(NoTransform(size_in=1)) for dimensions in range(2): assert repr( NoTransform(dimensions)) == "NoTransform(size_in=%d)" % dimensions
def test_convtransposeinc_2d(channels_last, strides, kernel_size, padding, rng, allclose, plt): """Test ConvTransposeInc by ensuring it is the transpose of ConvInc. Since convolution is a linear operator, it can be expressed as a matrix ``A``. We can therefore state that ``C.dot(A.dot(x)) == (A.T.dot(C.T)).T.dot(x)``, for an arbitrary vector ``x`` and arbitrary matrix ``C``. This test asserts this identity, and thereby tests the ``ConvTransposeInc`` operator against the ``ConvInc`` operator. """ spatial_shape = (16, 17) in_channels = 32 out_channels = 64 x_shape = ChannelShape.from_space_and_channels(spatial_shape, in_channels, channels_last=channels_last) conv = Convolution( out_channels, x_shape, kernel_size=kernel_size, strides=strides, padding=padding, channels_last=channels_last, ) nk = 10 # number of vectors we test ConvTransposeInc with C = rng.randn(nk, conv.output_shape.size) # compute ``conv_output = C.dot(A.dot(x))``, where ``A`` is the convolution operator x = Signal(rng.randn(*x_shape.shape)) w = Signal( rng.randn(kernel_size[0], kernel_size[1], in_channels, out_channels)) y = Signal(np.zeros(conv.output_shape.shape)) signals = {sig: np.array(sig.initial_value) for sig in (x, w, y)} step_conv = ConvInc(w, x, y, conv).make_step(signals, None, None) step_conv() x_flat = signals[x].ravel() y_flat = signals[y].ravel() conv_output = C.dot(y_flat) # compute ``conv_transpose_output = (A.T.dot(C.T)).T.dot(x)``, where ``A.T`` is the # transpose convolution operator (applied to one column of ``C.T`` at a time) conv_transpose = ConvolutionTranspose( in_channels, conv.output_shape, output_shape=x_shape, kernel_size=kernel_size, strides=strides, padding=padding, channels_last=channels_last, ) xt = Signal(np.zeros(conv_transpose.input_shape.shape)) wt = Signal(np.transpose(w.initial_value, (0, 1, 3, 2))) yt = Signal(np.zeros(conv_transpose.output_shape.shape)) signals_tr = {sig: np.array(sig.initial_value) for sig in (xt, wt, yt)} step_trans = ConvTransposeInc(wt, xt, yt, conv_transpose).make_step( signals_tr, None, None) AtCt = [] for k in range(nk): signals_tr[xt][:] = C[k].reshape(conv_transpose.input_shape.shape) signals_tr[yt][:] = 0 step_trans() AtCt.append(signals_tr[yt].copy().ravel()) AtCt = np.array(AtCt) conv_transpose_output = AtCt.dot(x_flat) assert conv_transpose_output.shape == conv_output.shape success = allclose(conv_transpose_output, conv_output) if success: plt.saveas = None else: debug_convtransposeinc_2d(w, wt, x, xt, y, yt, conv, conv_transpose, plt) assert success