def test_conv_onchip(Simulator, plt): """Tests a fully on-chip conv connection. """ from nengo._vendor.npconv2d.conv2d import conv2d kernel = np.array([[-1, 2, -1], [-1, 2, -1], [-1, 2, -1]], dtype=float) kernel /= kernel.max() image = np.array([[1, 2, 1, 2, 0], [2, 3, 2, 1, 1], [1, 2, 1, 2, 3], [2, 3, 2, 1, 1], [1, 2, 1, 2, 0]], dtype=float) image /= image.max() input_scale = 119. bias = input_scale * image.ravel() neuron_type = nengo.SpikingRectifiedLinear() y_ref = LoihiSpikingRectifiedLinear().rates(image.ravel(), input_scale, 0) y_ref = conv2d(y_ref.reshape(1, 5, 5, 1), kernel.reshape(3, 3, 1, 1), pad='VALID') y_ref = LoihiSpikingRectifiedLinear().rates(y_ref.ravel(), 1., 0.).reshape(3, 3) with nengo.Network() as net: a = nengo.Ensemble(bias.size, 1, neuron_type=neuron_type, gain=nengo.dists.Choice([0]), bias=bias) transform = nengo_transforms.Convolution(n_filters=1, input_shape=(5, 5, 1), init=kernel.reshape( 3, 3, 1, 1)) b = nengo.Ensemble(transform.output_shape.size, 1, neuron_type=neuron_type, gain=nengo.dists.Choice([1]), bias=nengo.dists.Choice([0])) nengo.Connection(a.neurons, b.neurons, transform=transform) bp = nengo.Probe(b.neurons, synapse=nengo.Alpha(0.02)) with Simulator(net) as sim: sim.run(0.3) y_ref = y_ref / input_scale y = sim.data[bp][-1].reshape(3, -1) / input_scale plt.subplot(121) plt.imshow(y_ref) plt.colorbar() plt.subplot(122) plt.imshow(y) plt.colorbar() assert np.allclose(y, y_ref, atol=0.02, rtol=0.1)
def step_conv(): Y[...] += conv2d.conv2d(X, W, pad=pad, stride=stride)[0]
def test_convolution(dimensions, padding, channels_last, fixed_kernel, Simulator, allclose, rng, seed): input_d = 4 input_channels = 2 output_channels = 5 kernel_d = 3 kernel_size = (kernel_d, ) if dimensions == 1 else (kernel_d, kernel_d) output_d = input_d - kernel_d // 2 * 2 if padding == "valid" else input_d input_shape = (input_d, input_channels) kernel_shape = (kernel_d, input_channels, output_channels) output_shape = (output_d, output_channels) if dimensions == 2: input_shape = (input_d, ) + input_shape kernel_shape = (kernel_d, ) + kernel_shape output_shape = (output_d, ) + output_shape if not channels_last: input_shape = tuple(np.roll(input_shape, 1)) output_shape = tuple(np.roll(output_shape, 1)) with nengo.Network(seed=seed) as net: x = rng.randn(*input_shape) w = rng.randn( *kernel_shape) if fixed_kernel else nengo.dists.Uniform(-0.1, 0.1) a = nengo.Node(np.ravel(x)) b = nengo.Node(size_in=np.prod(output_shape)) conn = nengo.Connection( a, b, synapse=None, transform=nengo.Convolution( output_channels, input_shape, init=w, padding=padding, kernel_size=kernel_size, strides=(1, ) if dimensions == 1 else (1, 1), channels_last=channels_last, ), ) p = nengo.Probe(b) # check error handling bad_in = nengo.Node([0]) bad_out = nengo.Node(size_in=5) with pytest.raises(ValidationError): nengo.Connection(bad_in, b, transform=conn.transform) with pytest.raises(ValidationError): nengo.Connection(a, bad_out, transform=conn.transform) assert conn.transform.output_shape.shape == output_shape assert conn.transform.kernel_shape == kernel_shape with Simulator(net) as sim: sim.step() weights = sim.data[conn].weights if not channels_last: x = np.moveaxis(x, 0, -1) if dimensions == 1: x = x[:, None, :] weights = weights[:, None, :, :] truth = conv2d.conv2d(x[None, ...], weights, pad=padding.upper())[0] if not channels_last: truth = np.moveaxis(truth, -1, 0) assert allclose(sim.data[p][0], np.ravel(truth))
def test_conv_preslice(Simulator, plt): from nengo._vendor.npconv2d.conv2d import conv2d kernel = np.array([[-1, 2, -1], [-1, 2, -1], [-1, 2, -1]], dtype=float) kernel /= kernel.max() image = np.array([[1, 2, 1, 2, 0], [2, 3, 2, 1, 1], [1, 2, 1, 2, 3], [2, 3, 2, 1, 1], [1, 2, 1, 2, 0]], dtype=float) image /= image.max() image2 = np.column_stack([c * x for c in image.T for x in (1, -1)]) input_gain = 149. neuron_type = nengo.SpikingRectifiedLinear() y_ref = LoihiSpikingRectifiedLinear().rates(image.ravel(), input_gain, 0) y_ref = conv2d(y_ref.reshape(1, 5, 5, 1), kernel.reshape(3, 3, 1, 1), pad='VALID') y_ref = LoihiSpikingRectifiedLinear().rates(y_ref.ravel(), 1., 0.).reshape(3, 3) with nengo.Network() as net: u = nengo.Node(image2.ravel()) a = nengo.Ensemble(50, 1, neuron_type=neuron_type, gain=nengo.dists.Choice([input_gain]), bias=nengo.dists.Choice([0])) transform = nengo_transforms.Convolution(n_filters=1, input_shape=(5, 5, 1), init=kernel.reshape( 3, 3, 1, 1)) b = nengo.Ensemble(transform.output_shape.size, 1, neuron_type=neuron_type, gain=nengo.dists.Choice([1]), bias=nengo.dists.Choice([0])) nengo.Connection(u, a.neurons, synapse=None) nengo.Connection(a.neurons[::2], b.neurons, transform=transform) bp = nengo.Probe(b.neurons, synapse=nengo.Alpha(0.02)) hw_opts = dict(snip_max_spikes_per_step=100) with Simulator(net, hardware_options=hw_opts) as sim: sim.run(0.3) y_ref = y_ref / input_gain y = sim.data[bp][-1].reshape(3, -1) / input_gain plt.subplot(121) plt.imshow(y_ref) plt.colorbar() plt.subplot(122) plt.imshow(y) plt.colorbar() assert np.allclose(y, y_ref, atol=0.02, rtol=0.1)
def test_convolution( dimensions, padding, channels_last, fixed_kernel, Simulator, rng, seed): input_d = 4 input_channels = 2 output_channels = 5 kernel_d = 3 kernel_size = (kernel_d,) if dimensions == 1 else (kernel_d, kernel_d) output_d = input_d - kernel_d // 2 * 2 if padding == "valid" else input_d input_shape = (input_d, input_channels) kernel_shape = (kernel_d, input_channels, output_channels) output_shape = (output_d, output_channels) if dimensions == 2: input_shape = (input_d,) + input_shape kernel_shape = (kernel_d,) + kernel_shape output_shape = (output_d,) + output_shape if not channels_last: input_shape = tuple(np.roll(input_shape, 1)) output_shape = tuple(np.roll(output_shape, 1)) with nengo.Network(seed=seed) as net: x = rng.randn(*input_shape) w = (rng.randn(*kernel_shape) if fixed_kernel else nengo.dists.Uniform(-0.1, 0.1)) a = nengo.Node(np.ravel(x)) b = nengo.Node(size_in=np.prod(output_shape)) conn = nengo.Connection( a, b, synapse=None, transform=nengo.Convolution( output_channels, input_shape, init=w, padding=padding, kernel_size=kernel_size, strides=(1,) if dimensions == 1 else (1, 1), channels_last=channels_last)) p = nengo.Probe(b) # check error handling bad_in = nengo.Node([0]) bad_out = nengo.Node(size_in=5) with pytest.raises(ValidationError): nengo.Connection(bad_in, b, transform=conn.transform) with pytest.raises(ValidationError): nengo.Connection(a, bad_out, transform=conn.transform) assert conn.transform.output_shape.shape == output_shape assert conn.transform.kernel_shape == kernel_shape with Simulator(net) as sim: sim.step() weights = sim.data[conn].weights if not channels_last: x = np.moveaxis(x, 0, -1) if dimensions == 1: x = x[:, None, :] weights = weights[:, None, :, :] truth = conv2d.conv2d(x[None, ...], weights, pad=padding.upper())[0] if not channels_last: truth = np.moveaxis(truth, -1, 0) assert np.allclose(sim.data[p][0], np.ravel(truth))