示例#1
0
def test_tile(plt, rng, color):
    n = 200
    angles = rng.uniform(0, np.pi, size=n)
    colors = rng.uniform(size=(n, 3))

    genimg = lambda k: wave_image(
        (30, 31), angles[k], color=colors[k] if color else None)
    images = np.array([genimg(k) for k in range(n)])

    ne_plt.tile(images, grid=True, gridcolor=(1, 1, 0))
示例#2
0
def test_tile(plt, rng, color):
    n = 200
    angles = rng.uniform(0, np.pi, size=n)
    colors = rng.uniform(size=(n, 3))

    genimg = lambda k: wave_image(
        (30, 31), angles[k], color=colors[k] if color else None)
    images = np.array([genimg(k) for k in range(n)])

    ne_plt.tile(images, grid=True, gridcolor=(1, 1, 0))
示例#3
0
def show(images):
    """
    Render a given numpy.uint8 2D array of pixel data.
    """
    from matplotlib import pyplot
    import matplotlib as mpl
    from nengo_extras.matplotlib import tile
    image_array = np.stack(images)
    show_params = {'interpolation': 'nearest'}
    tile(image_array, rows=4, cols=5, **show_params)
    #ax.xaxis.set_ticks_position('top')
    #ax.yaxis.set_ticks_position('left')
    pyplot.show()
示例#4
0
文件: make_figures.py 项目: hunse/phd
def make_gabors():
    from nengo.dists import Uniform
    from nengo_extras.matplotlib import tile
    from nengo_extras.vision import Gabor
    from hunse_thesis.dists import LogUniform

    rng = np.random.RandomState(3)

    # r, c = 10, 20
    r, c = 9, 12

    # gabor = Gabor()
    # gabor = Gabor(freq=Uniform(0.5, 1.5))
    gabor = Gabor(freq=LogUniform(np.log(0.5), np.log(1.5)))

    gabors = gabor.generate(r * c, (32, 32), rng=rng)

    tile(gabors, rows=r, cols=c)

    plt.savefig('gabors.pdf')
#sigma_x=Tile(np.sort(np.repeat(np.linspace(0.2, 1.0,  n_sigma), n_exc//n_sigma))),
#sigma_y=Tile(np.sort(np.repeat(np.linspace(0.2, 1.0, n_sigma), n_exc//n_sigma))),
input_encoders = Gabor(sigma_x=Choice([0.5]),
                       sigma_y=Choice([0.5]),
                       theta=Tile(np.sort(np.repeat(np.linspace(-np.pi,np.pi,n_gabor), n_exc//n_gabor))),
                       freq=Tile(np.sort(np.repeat(np.linspace(0.2,2.0,n_gabor), n_exc//n_gabor))),
                       phase=Tile(np.sort(np.repeat(np.linspace(-np.pi,np.pi,n_gabor), n_exc//n_gabor))),
                       ).generate(n_exc, gabor_size, rng=rng)
input_encoders = SequentialMask((n_x, n_y)).populate(input_encoders, rng=rng, flatten=True)

#input_encoders = rng.normal(size=(n_exc, n_x * n_y))
#input_encoders = rng.normal(size=(n_exc, 5, 5))
#input_encoders = SequentialMask((n_x, n_y)).populate(input_encoders, rng=rng, random_positions=True, flatten=True)

print(f'input_encoders.shape = {input_encoders.shape}')
tile(input_encoders.reshape((-1, n_x, n_y)), rows=10, cols=n_gabor, grid=True)
plt.show()

model_dict = build_network(params, dimensions=input_dimensions, inputs=input_data,
                           input_encoders=input_encoders, direct_input=False,
                           presentation_time=presentation_time, pause_time=pause_time,
                           coords=coords_dict,
                           t_learn_exc=t_train, t_learn_inh=t_train,
                           sample_weights_every=t_end // 5.0)

network = model_dict['network']

plt.imshow(network.srf_network.weights_initial_E.T, aspect='auto', interpolation='nearest')
plt.colorbar(label='Synaptic weight')
plt.show()
示例#6
0
def test_load_mnist(plt):
    (trainX, _), (_, _) = load_mnist()
    tile(trainX.reshape(-1, 28, 28))
示例#7
0
def test_conv_split(Simulator, rng, plt, allclose):
    channels_last = False

    # load data
    with open(os.path.join(test_dir, 'mnist10.pkl'), 'rb') as f:
        test10 = pickle.load(f)

    input_shape = nengo_transforms.ChannelShape((1, 28, 28),
                                                channels_last=channels_last)

    n_filters = 8
    kernel_size = (7, 7)
    kernel = Gabor(freq=Uniform(0.5, 1)).generate(n_filters,
                                                  kernel_size,
                                                  rng=rng)
    kernel = kernel[None, :, :, :]  # single channel
    kernel = np.transpose(kernel, (2, 3, 0, 1))
    strides = (2, 2)

    seed = 3  # fix seed to do the same computation for both channel positions

    with nengo.Network(seed=seed) as net:
        nengo_loihi.add_params(net)

        a = nengo.Node(test10[0][0].ravel())

        # --- make population to turn image into spikes
        nc = 1
        in_kernel = np.array([1.]).reshape((1, 1, 1, nc))
        transform = nengo_transforms.Convolution(1,
                                                 input_shape,
                                                 kernel_size=(1, 1),
                                                 init=in_kernel,
                                                 channels_last=channels_last)
        b = nengo.Ensemble(transform.output_shape.size,
                           1,
                           neuron_type=nengo.SpikingRectifiedLinear(),
                           max_rates=nengo.dists.Choice([50]),
                           intercepts=nengo.dists.Choice([0]))
        net.config[b].on_chip = False
        nengo.Connection(a, b.neurons, transform=transform)
        in_shape = transform.output_shape

        transform = nengo_transforms.Convolution(n_filters,
                                                 in_shape,
                                                 kernel_size=kernel_size,
                                                 strides=strides,
                                                 init=kernel,
                                                 channels_last=channels_last)
        out_shape = transform.output_shape
        split_slices = conv.split_channels(out_shape,
                                           max_size=1024,
                                           max_channels=4)

        # --- make convolution population, split across ensembles
        cc = []
        cp = []
        out_shapes = []
        xslice = conv.ImageSlice(in_shape)
        for yslice in split_slices:
            transform_xy = conv.split_transform(transform, xslice, yslice)
            out_shapes.append(transform_xy.output_shape)
            c = nengo.Ensemble(transform_xy.output_shape.size,
                               1,
                               neuron_type=nengo.LIF(),
                               max_rates=nengo.dists.Choice([15]),
                               intercepts=nengo.dists.Choice([0]))
            nengo.Connection(b.neurons, c.neurons, transform=transform_xy)
            cc.append(c)
            cp.append(nengo.Probe(c.neurons))

    simtime = 0.3

    with nengo.Simulator(net, optimize=False) as sim_nengo:
        sim_nengo.run(simtime)

    hw_opts = dict(snip_max_spikes_per_step=100)
    with Simulator(net, seed=seed, hardware_options=hw_opts) as sim_loihi:
        sim_loihi.run(simtime)

    nengo_out = []
    loihi_out = []
    for p, out_shape_i in zip(cp, out_shapes):
        nengo_out.append(
            (sim_nengo.data[p] > 0).sum(axis=0).reshape(out_shape_i.shape))
        loihi_out.append(
            (sim_loihi.data[p] > 0).sum(axis=0).reshape(out_shape_i.shape))

    if channels_last:
        nengo_out = np.concatenate(nengo_out, axis=2)
        loihi_out = np.concatenate(loihi_out, axis=2)

        # put channels first to display them separately
        nengo_out = np.transpose(nengo_out, (2, 0, 1))
        loihi_out = np.transpose(loihi_out, (2, 0, 1))
    else:
        nengo_out = np.concatenate(nengo_out, axis=0)
        loihi_out = np.concatenate(loihi_out, axis=0)

    out_max = np.maximum(nengo_out.max(), loihi_out.max())

    # --- plot results
    rows = 2
    cols = 3

    ax = plt.subplot(rows, cols, 1)
    imshow(test10[0][0].reshape((28, 28)), vmin=0, vmax=1, ax=ax)

    ax = plt.subplot(rows, cols, 2)
    tile(np.transpose(kernel[0], (2, 0, 1)), cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 3)
    plt.hist(nengo_out.ravel(), bins=31)
    plt.hist(loihi_out.ravel(), bins=31)

    ax = plt.subplot(rows, cols, 4)
    tile(nengo_out, vmin=0, vmax=out_max, cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 6)
    tile(loihi_out, vmin=0, vmax=out_max, cols=8, ax=ax)

    assert allclose(loihi_out, nengo_out, atol=0.05 * out_max, rtol=0.15)
示例#8
0
def test_conv_connection(channels, channels_last, Simulator, seed, rng, plt,
                         allclose):
    if channels_last:
        plt.saveas = None
        pytest.xfail("Blocked by CxBase cannot be > 256 bug")

    # load data
    with open(os.path.join(test_dir, 'mnist10.pkl'), 'rb') as f:
        test10 = pickle.load(f)

    test_x = test10[0][0].reshape(28, 28)
    test_x = 1.999 * test_x - 0.999  # range (-1, 1)
    input_shape = nengo_transforms.ChannelShape(
        (test_x.shape + (channels, )) if channels_last else
        ((channels, ) + test_x.shape),
        channels_last=channels_last)

    filters = Gabor(freq=Uniform(0.5, 1)).generate(8, (7, 7), rng=rng)
    filters = filters[None, :, :, :]  # single channel
    filters = np.transpose(filters, (2, 3, 0, 1))
    strides = (2, 2)
    tau_rc = 0.02
    tau_ref = 0.002
    tau_s = 0.005
    dt = 0.001

    neuron_type = LoihiLIF(tau_rc=tau_rc, tau_ref=tau_ref)

    pres_time = 0.1

    with nengo.Network(seed=seed) as model:
        nengo_loihi.add_params(model)

        u = nengo.Node(test_x.ravel(), label="u")

        a = nengo.Ensemble(input_shape.size,
                           1,
                           neuron_type=LoihiSpikingRectifiedLinear(),
                           max_rates=nengo.dists.Choice([40 / channels]),
                           intercepts=nengo.dists.Choice([0]),
                           label='a')
        model.config[a].on_chip = False

        if channels == 1:
            nengo.Connection(u, a.neurons, transform=1, synapse=None)
        elif channels == 2:
            # encode image into spikes using two channels (on/off)
            if input_shape.channels_last:
                nengo.Connection(u, a.neurons[0::2], transform=1, synapse=None)
                nengo.Connection(u,
                                 a.neurons[1::2],
                                 transform=-1,
                                 synapse=None)
            else:
                k = input_shape.spatial_shape[0] * input_shape.spatial_shape[1]
                nengo.Connection(u, a.neurons[:k], transform=1, synapse=None)
                nengo.Connection(u, a.neurons[k:], transform=-1, synapse=None)

            filters = np.concatenate([filters, -filters], axis=2)
        else:
            raise ValueError("Test not configured for more than two channels")

        conv2d_transform = nengo_transforms.Convolution(
            8,
            input_shape,
            strides=strides,
            kernel_size=(7, 7),
            channels_last=channels_last,
            init=filters)

        output_shape = conv2d_transform.output_shape

        gain, bias = neuron_type.gain_bias(max_rates=100, intercepts=0)
        gain = gain * 0.01  # account for `a` max_rates
        b = nengo.Ensemble(output_shape.size,
                           1,
                           neuron_type=neuron_type,
                           gain=nengo.dists.Choice([gain[0]]),
                           bias=nengo.dists.Choice([bias[0]]),
                           label='b')
        nengo.Connection(a.neurons,
                         b.neurons,
                         synapse=tau_s,
                         transform=conv2d_transform)

        bp = nengo.Probe(b.neurons)

    with nengo.Simulator(model, dt=dt, optimize=False) as sim:
        sim.run(pres_time)
    ref_out = sim.data[bp].mean(axis=0).reshape(output_shape.shape)

    # Currently, non-gpu TensorFlow does not support channels first in conv
    use_nengo_dl = HAS_DL and channels_last
    ndl_out = np.zeros_like(ref_out)
    if use_nengo_dl:
        with nengo_dl.Simulator(model, dt=dt) as sim_dl:
            sim_dl.run(pres_time)
        ndl_out = sim_dl.data[bp].mean(axis=0).reshape(output_shape.shape)

    with nengo_loihi.Simulator(model, dt=dt, target='simreal') as sim_real:
        sim_real.run(pres_time)
    real_out = sim_real.data[bp].mean(axis=0).reshape(output_shape.shape)

    with Simulator(model, dt=dt) as sim_loihi:
        if "loihi" in sim_loihi.sims:
            sim_loihi.sims["loihi"].snip_max_spikes_per_step = 800
        sim_loihi.run(pres_time)
    sim_out = sim_loihi.data[bp].mean(axis=0).reshape(output_shape.shape)

    if not output_shape.channels_last:
        ref_out = np.transpose(ref_out, (1, 2, 0))
        ndl_out = np.transpose(ndl_out, (1, 2, 0))
        real_out = np.transpose(real_out, (1, 2, 0))
        sim_out = np.transpose(sim_out, (1, 2, 0))

    out_max = max(ref_out.max(), sim_out.max())

    # --- plot results
    rows = 2
    cols = 3

    ax = plt.subplot(rows, cols, 1)
    imshow(test_x, vmin=0, vmax=1, ax=ax)

    ax = plt.subplot(rows, cols, 2)
    tile(np.transpose(filters[0], (2, 0, 1)), cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 3)
    plt.hist(ref_out.ravel(), bins=31)
    plt.hist(sim_out.ravel(), bins=31)

    ax = plt.subplot(rows, cols, 4)
    tile(np.transpose(ref_out, (2, 0, 1)), vmin=0, vmax=out_max, cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 5)
    tile(np.transpose(ndl_out, (2, 0, 1)), vmin=0, vmax=out_max, cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 6)
    tile(np.transpose(sim_out, (2, 0, 1)), vmin=0, vmax=out_max, cols=8, ax=ax)

    if use_nengo_dl:
        assert allclose(ndl_out, ref_out, atol=1e-5, rtol=1e-5)
    assert allclose(real_out, ref_out, atol=1, rtol=1e-3)
    assert allclose(sim_out, ref_out, atol=10, rtol=1e-3)
示例#9
0
def test_pop_tiny(pop_type, out_channels_last, request, plt, seed, rng,
                  allclose):
    nc = 2

    tau_rc = 0.02
    tau_ref = 0.001
    tau_s = 0.0
    dt = 0.001

    neuron_bias = 1.

    pres_time = 0.4

    sti, stj = 1, 1

    if nc == 1:
        filters = np.array([[-0.5, 2., -0.25], [-0.75, 2., -1.0],
                            [-0.5, 3., -0.5], [-1.0, 6.,
                                               -0.25]]).reshape(1, 4, 1, 3)
        filters = np.transpose(filters, (0, 2, 3, 1))

        test_x = np.array([[1, 5, 1], [2, 1, 2]])
        test_x = test_x[:, :, None]
    elif nc == 2:
        filters = np.array([[[-0.5, 2., -0.2], [-0.7, 2., -1.0],
                             [-0.5, 3., -0.5], [-1.0, 6., -0.2]],
                            [[-1.0, 2., -1.0], [-0.5, 2., -0.5],
                             [-0.8, 3., -0.2], [-1.0, 4.,
                                                -0.2]]]).reshape(2, 4, 1, 3)
        filters = np.transpose(filters, (0, 2, 3, 1))

        test_x = np.array([[[1, 5, 1], [2, 1, 2]], [[0, 3, 1], [4, 2, 1]]])
        test_x = np.transpose(test_x, (1, 2, 0))

    test_x = test_x / (test_x.max() + 0.001)

    # --- compute nengo_loihi outputs
    inp_biases = test_x
    inp_shape = ImageShape.from_shape(inp_biases.shape, channels_last=True)
    ni, nj, nk = inp_shape.shape(channels_last=True)
    nc, si, sj, nf = filters.shape
    nij = ni * nj
    nyi = 1 + (ni - si) // sti
    nyj = 1 + (nj - sj) // stj
    out_size = nyi * nyj * nf
    assert out_size <= 1024

    model = loihi_cx.CxModel()

    # input group
    inp = loihi_cx.CxGroup(ni * nj * nk, label='inp')
    assert inp.n <= 1024
    inp.configure_relu()
    inp.bias[:] = inp_biases.ravel()

    inp_ax = loihi_cx.CxAxons(nij, label='inp_ax')
    inp_ax.set_axon_map(inp_shape.pixel_idxs(), inp_shape.channel_idxs())
    inp.add_axons(inp_ax)

    model.add_group(inp)

    # conv group
    neurons = loihi_cx.CxGroup(out_size, label='neurons')
    assert neurons.n <= 1024
    neurons.configure_lif(tau_rc=tau_rc, tau_ref=tau_ref, dt=dt)
    neurons.configure_filter(tau_s, dt=dt)
    neurons.bias[:] = neuron_bias

    synapses = loihi_cx.CxSynapses(inp_shape.n_pixels, label='synapses')
    conv2d_transform = Conv2D.from_kernel(
        filters,
        inp_shape,
        strides=(sti, stj),
        output_channels_last=out_channels_last)
    weights, indices, axon_to_weight_map, cx_bases = conv2d_loihi_weights(
        conv2d_transform)
    synapses.set_population_weights(weights,
                                    indices,
                                    axon_to_weight_map,
                                    cx_bases,
                                    pop_type=pop_type)
    neurons.add_synapses(synapses)

    out_probe = loihi_cx.CxProbe(target=neurons, key='s')
    neurons.add_probe(out_probe)

    inp_ax.target = synapses
    model.add_group(neurons)

    # simulation
    model.discretize()

    n_steps = int(pres_time / dt)
    target = request.config.getoption("--target")
    if target == 'loihi':
        with LoihiSimulator(model, use_snips=False, seed=seed) as sim:
            sim.run_steps(n_steps)
            sim_out = sim.get_probe_output(out_probe)
    else:
        with CxSimulator(model, seed=seed) as sim:
            sim.run_steps(n_steps)
            sim_out = sim.get_probe_output(out_probe)

    sim_out = np.sum(sim_out, axis=0) * (dt / pres_time)
    if out_channels_last:
        sim_out.shape = (nyi, nyj, nf)
        sim_out = np.transpose(sim_out, (2, 0, 1))
    else:
        sim_out.shape = (nf, nyi, nyj)

    out_max = sim_out.max()

    # --- plot results
    rows = 1
    cols = 2

    ax = plt.subplot(rows, cols, 1)
    plt.hist(sim_out.ravel(), bins=11)

    ax = plt.subplot(rows, cols, 2)
    tile(sim_out, vmin=0, vmax=out_max, grid=True, ax=ax)

    print("sim_out:\n%r" % (sim_out[:, :, 0], ))

    # ref_out determined by emulator running code known to work
    if nc == 1:
        ref_out = np.array([[0.06, 0.02], [0.055, 0.], [0.0825, 0.0225],
                            [0.125, 0.04]])
    elif nc == 2:
        ref_out = np.array([[0.0975, 0.02], [0.0825, 0.02], [0.125, 0.055],
                            [0.2475, 0.0825]])
    assert allclose(sim_out[:, :, 0], ref_out, rtol=0, atol=1e-7)
示例#10
0
def test_conv2d_weights(request, plt, seed, rng, allclose):
    pop_type = 32
    out_channels_last = False

    # load data
    with open(os.path.join(test_dir, 'mnist10.pkl'), 'rb') as f:
        test10 = pickle.load(f)

    test_x = test10[0][0].reshape(28, 28)
    test_x = test_x[3:24, 3:24]
    test_x = 1.999 * test_x - 0.999

    filters = Gabor(freq=Uniform(0.5, 1)).generate(8, (7, 7), rng=rng)
    sti, stj = 2, 2
    tau_rc = 0.02
    tau_ref = 0.002
    tau_s = 0.005
    dt = 0.001

    encode_type = nengo.SpikingRectifiedLinear()
    encode_gain = 1. / dt
    encode_bias = 0.
    neuron_type = nengo.LIF(tau_rc=tau_rc, tau_ref=tau_ref)
    neuron_gain = 1.
    neuron_bias = 1.

    pres_time = 0.2

    # --- compute ideal outputs
    def conv_pm(x, kernel):
        y0 = scipy.signal.correlate2d(x[0], kernel, mode='valid')[::sti, ::stj]
        y1 = scipy.signal.correlate2d(x[1], kernel, mode='valid')[::sti, ::stj]
        return [y0, -y1]

    ref_out = np.array([test_x, -test_x])
    ref_out = loihi_rates(encode_type, ref_out, encode_gain, encode_bias, dt)
    ref_out = ref_out / encode_gain
    ref_out = np.array([conv_pm(ref_out, kernel) for kernel in filters])
    ref_out = ref_out.sum(axis=1)  # sum positive and negative parts
    ref_out = loihi_rates(neuron_type, ref_out, neuron_gain, neuron_bias, dt)

    # --- compute nengo_loihi outputs
    inp_biases = np.array([test_x, -test_x])
    inp_shape = ImageShape.from_shape(inp_biases.shape, channels_last=False)

    kernel = np.array([filters, -filters])  # two channels, pos and neg
    kernel = np.transpose(kernel, (0, 2, 3, 1))
    conv2d_transform = Conv2D.from_kernel(
        kernel,
        inp_shape,
        strides=(sti, stj),
        output_channels_last=out_channels_last)

    ni, nj, nk = inp_shape.shape(channels_last=True)
    out_size = ref_out.size
    nf, nyi, nyj = ref_out.shape
    assert out_size <= 1024

    model = loihi_cx.CxModel()

    # input group
    inp = loihi_cx.CxGroup(inp_shape.size, label='inp')
    assert inp.n <= 1024
    inp.configure_relu()
    inp.bias[:] = inp_biases.ravel()

    inp_ax = loihi_cx.CxAxons(inp_shape.n_pixels, label='inp_ax')
    inp_ax.set_axon_map(inp_shape.pixel_idxs(), inp_shape.channel_idxs())
    inp.add_axons(inp_ax)

    model.add_group(inp)

    # conv group
    neurons = loihi_cx.CxGroup(out_size, label='neurons')
    assert neurons.n <= 1024
    neurons.configure_lif(tau_rc=tau_rc, tau_ref=tau_ref, dt=dt)
    neurons.configure_filter(tau_s, dt=dt)
    neurons.bias[:] = neuron_bias

    synapses = loihi_cx.CxSynapses(inp_shape.n_pixels, label='synapses')
    weights, indices, axon_to_weight_map, cx_bases = conv2d_loihi_weights(
        conv2d_transform)
    synapses.set_population_weights(weights,
                                    indices,
                                    axon_to_weight_map,
                                    cx_bases,
                                    pop_type=pop_type)

    neurons.add_synapses(synapses)

    out_probe = loihi_cx.CxProbe(target=neurons, key='s')
    neurons.add_probe(out_probe)

    inp_ax.target = synapses
    model.add_group(neurons)

    # simulation
    model.discretize()

    n_steps = int(pres_time / dt)
    target = request.config.getoption("--target")
    if target == 'loihi':
        with LoihiSimulator(model, use_snips=False, seed=seed) as sim:
            sim.run_steps(n_steps)
            sim_out = sim.get_probe_output(out_probe)
    else:
        with CxSimulator(model, seed=seed) as sim:
            sim.run_steps(n_steps)
            sim_out = sim.get_probe_output(out_probe)

    sim_out = np.sum(sim_out, axis=0) / pres_time
    if out_channels_last:
        sim_out.shape = (nyi, nyj, nf)
        sim_out = np.transpose(sim_out, (2, 0, 1))
    else:
        sim_out.shape = (nf, nyi, nyj)

    out_max = max(ref_out.max(), sim_out.max())

    # --- plot results
    rows = 2
    cols = 2

    ax = plt.subplot(rows, cols, 1)
    tile(filters, cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 2)
    tile(ref_out, vmin=0, vmax=out_max, cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 3)
    plt.hist(ref_out.ravel(), bins=31)
    plt.hist(sim_out.ravel(), bins=31)

    ax = plt.subplot(rows, cols, 4)
    # tile(sim_out, vmin=0, vmax=1, cols=8, ax=ax)
    tile(sim_out, vmin=0, vmax=out_max, cols=8, ax=ax)

    assert allclose(sim_out, ref_out, atol=10, rtol=1e-3)
示例#11
0
def test_load_svhn(plt):
    (trainX, _), (_, _) = load_svhn()
    trainX = trainX.reshape((-1, 3, 32, 32))
    trainX = np.transpose(trainX, (0, 2, 3, 1))
    tile(trainX, ax=plt.gca())
示例#12
0
def test_load_mnist(plt):
    trainX = load_mnist()[0][0]
    tile(trainX.reshape((-1, 28, 28)), ax=plt.gca())
示例#13
0
def test_load_ilsvrc2012(plt):
    testX, _, _, _ = load_ilsvrc2012(n_files=1)
    testX = testX.reshape((-1, 3, 256, 256))
    testX = np.transpose(testX, (0, 2, 3, 1))
    tile(testX, ax=plt.gca())
示例#14
0
def test_load_cifar100(plt):
    trainX = load_cifar100()[0][0]
    trainX = trainX.reshape((-1, 3, 32, 32))
    trainX = np.transpose(trainX, (0, 2, 3, 1))
    tile(trainX, ax=plt.gca())
示例#15
0
文件: datasets.py 项目: hunse/phd
import matplotlib.pyplot as plt
import numpy as np

import nengo_extras.data
from nengo_extras.matplotlib import tile

plt.figure(figsize=(6.4, 6.4), dpi=200)

plt.subplot(511)
(mnist, _), _ = nengo_extras.data.load_mnist()
tile(mnist.reshape(-1, 28, 28), rows=3, cols=20)
plt.title('MNIST')

plt.subplot(512)
(svhn, _), _ = nengo_extras.data.load_svhn()
tile(np.transpose(svhn.reshape(-1, 3, 32, 32), (0, 2, 3, 1)), rows=3, cols=20)
plt.title('SVHN')

plt.subplot(513)
(cifar10, _), _ = nengo_extras.data.load_cifar10(n_train=1, n_test=0)
tile(np.transpose(cifar10.reshape(-1, 3, 32, 32), (0, 2, 3, 1)), rows=3, cols=20)
plt.title('CIFAR-10')

plt.subplot(514)
(cifar100, _), _ = nengo_extras.data.load_cifar100()
tile(np.transpose(cifar100.reshape(-1, 3, 32, 32), (0, 2, 3, 1)), rows=3, cols=20)
plt.title('CIFAR-100')

plt.subplot(515)
ilsvrc2012, _, _, _ = nengo_extras.data.load_ilsvrc2012(n_files=1)
tile(np.transpose(ilsvrc2012.reshape(-1, 3, 256, 256), (0, 2, 3, 1)), rows=2, cols=14)
示例#16
0
import nengo_extras.data
from nengo_extras.matplotlib import tile

images, labels, data_mean, label_names = nengo_extras.data.load_ilsvrc2012()

# images, labels, data_mean, label_names = (
#     nengo_extras.data.load_ilsvrc2012(n_files=1))

for s in label_names:
    print(s)

plt.figure()
# show = 'butcher'
show_names = ['butcher', 'restaurant', 'planetarium', 'church', 'library']

for i, show_name in enumerate(show_names):
    match = [s.startswith(show_name) for s in label_names]
    index = match.index(True) if True in match else None
    mask = (labels == index) if index is not None else None
    count = mask.sum() if mask is not None else None
    if count is not None:
        print("Found %d matches for %r: showing index %d, %d examples" %
              (sum(match), show_name, index, count))

        plt.subplot(len(show_names), 1, i + 1)
        tile(np.transpose(images[mask].reshape(-1, 3, 256, 256), (0, 2, 3, 1)),
             rows=3,
             cols=5)

plt.show()
示例#17
0
def test_conv2d_weights(channels_last, hw_opts, request, plt, seed, rng,
                        allclose):
    def loihi_rates_n(neuron_type, x, gain, bias, dt):
        """Compute Loihi rates on higher dimensional inputs"""
        y = x.reshape(-1, x.shape[-1])
        gain = np.asarray(gain)
        bias = np.asarray(bias)
        if gain.ndim == 0:
            gain = gain * np.ones(x.shape[-1])
        if bias.ndim == 0:
            bias = bias * np.ones(x.shape[-1])
        rates = loihi_rates(neuron_type, y, gain, bias, dt)
        return rates.reshape(*x.shape)

    if channels_last:
        plt.saveas = None
        pytest.xfail("Blocked by CxBase cannot be > 256 bug")

    target = request.config.getoption("--target")
    if target != 'loihi' and len(hw_opts) > 0:
        pytest.skip("Hardware options only available on hardware")

    pop_type = 32

    # load data
    with open(os.path.join(test_dir, 'mnist10.pkl'), 'rb') as f:
        test10 = pickle.load(f)

    test_x = test10[0][0].reshape(28, 28)
    test_x = test_x[3:24, 3:24]
    test_x = 1.999 * test_x - 0.999

    filters = Gabor(freq=Uniform(0.5, 1)).generate(8, (7, 7), rng=rng)
    sti, stj = 2, 2
    tau_rc = 0.02
    tau_ref = 0.002
    tau_s = 0.005
    dt = 0.001

    encode_type = nengo.SpikingRectifiedLinear()
    encode_gain = 1. / dt
    encode_bias = 0.
    neuron_type = nengo.LIF(tau_rc=tau_rc, tau_ref=tau_ref)
    neuron_gain = 1.
    neuron_bias = 1.

    pres_time = 0.2

    # --- compute ideal outputs
    def conv_pm(x, kernel):
        y0 = scipy.signal.correlate2d(x[0], kernel, mode='valid')[::sti, ::stj]
        y1 = scipy.signal.correlate2d(x[1], kernel, mode='valid')[::sti, ::stj]
        return [y0, -y1]

    ref_out = np.array([test_x, -test_x])
    ref_out = loihi_rates_n(encode_type, ref_out, encode_gain, encode_bias, dt)
    ref_out = ref_out / encode_gain
    ref_out = np.array([conv_pm(ref_out, kernel) for kernel in filters])
    ref_out = ref_out.sum(axis=1)  # sum positive and negative parts
    ref_out = loihi_rates_n(neuron_type, ref_out, neuron_gain, neuron_bias, dt)

    # --- compute nengo_loihi outputs
    inp_biases = np.stack([test_x, -test_x], axis=-1 if channels_last else 0)
    inp_shape = nengo_transforms.ChannelShape(inp_biases.shape,
                                              channels_last=channels_last)

    kernel = np.array([filters, -filters])  # two channels, pos and neg
    kernel = np.transpose(kernel, (2, 3, 0, 1))
    conv2d_transform = nengo_transforms.Convolution(
        8,
        inp_shape,
        strides=(sti, stj),
        channels_last=channels_last,
        kernel_size=(7, 7),
        init=kernel)

    out_size = ref_out.size
    nf, nyi, nyj = ref_out.shape
    assert out_size <= 1024

    model = Model()

    # input block
    inp = LoihiBlock(inp_shape.size, label='inp')
    assert inp.n_neurons <= 1024
    inp.compartment.configure_relu()
    inp.compartment.bias[:] = inp_biases.ravel()

    inp_ax = Axon(np.prod(inp_shape.spatial_shape), label='inp_ax')
    inp_ax.set_compartment_axon_map(target_axons=conv.pixel_idxs(inp_shape),
                                    atoms=conv.channel_idxs(inp_shape))
    inp.add_axon(inp_ax)

    model.add_block(inp)

    # conv block
    neurons = LoihiBlock(out_size, label='neurons')
    assert neurons.n_neurons <= 1024
    neurons.compartment.configure_lif(tau_rc=tau_rc, tau_ref=tau_ref, dt=dt)
    neurons.compartment.configure_filter(tau_s, dt=dt)
    neurons.compartment.bias[:] = neuron_bias

    synapse = Synapse(np.prod(inp_shape.spatial_shape), label='synapse')
    weights, indices, axon_to_weight_map, bases = conv.conv2d_loihi_weights(
        conv2d_transform)
    synapse.set_population_weights(weights,
                                   indices,
                                   axon_to_weight_map,
                                   bases,
                                   pop_type=pop_type)

    neurons.add_synapse(synapse)

    out_probe = Probe(target=neurons, key='spiked')
    neurons.add_probe(out_probe)

    inp_ax.target = synapse
    model.add_block(neurons)

    # simulation
    discretize_model(model)

    n_steps = int(pres_time / dt)
    if target == 'loihi':
        with HardwareInterface(model, use_snips=False, seed=seed,
                               **hw_opts) as sim:
            sim.run_steps(n_steps)
            sim_out = sim.get_probe_output(out_probe)
    else:
        with EmulatorInterface(model, seed=seed) as sim:
            sim.run_steps(n_steps)
            sim_out = sim.get_probe_output(out_probe)

    sim_out = np.sum(sim_out, axis=0) / pres_time
    if channels_last:
        sim_out.shape = (nyi, nyj, nf)
        sim_out = np.transpose(sim_out, (2, 0, 1))
    else:
        sim_out.shape = (nf, nyi, nyj)

    out_max = max(ref_out.max(), sim_out.max())

    # --- plot results
    rows = 2
    cols = 2

    ax = plt.subplot(rows, cols, 1)
    tile(filters, cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 2)
    tile(ref_out, vmin=0, vmax=out_max, cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 3)
    plt.hist(ref_out.ravel(), bins=31)
    plt.hist(sim_out.ravel(), bins=31)

    ax = plt.subplot(rows, cols, 4)
    # tile(sim_out, vmin=0, vmax=1, cols=8, ax=ax)
    tile(sim_out, vmin=0, vmax=out_max, cols=8, ax=ax)

    assert allclose(sim_out, ref_out, atol=10, rtol=1e-3)
示例#18
0
def test_conv_connection(channels, Simulator, seed, rng, plt, allclose):
    # channels_last = True
    channels_last = False
    if channels > 1:
        pytest.xfail("Cannot send population spikes to chip")

    # load data
    with open(os.path.join(test_dir, 'mnist10.pkl'), 'rb') as f:
        test10 = pickle.load(f)

    test_x = test10[0][0].reshape(28, 28)
    test_x = 1.999 * test_x - 0.999  # range (-1, 1)
    test_x = test_x[:, :, None]  # single channel
    input_shape = ImageShape(test_x.shape[0],
                             test_x.shape[1],
                             channels,
                             channels_last=channels_last)

    filters = Gabor(freq=Uniform(0.5, 1)).generate(8, (7, 7), rng=rng)
    filters = filters[None, :, :, :]  # single channel
    filters = np.transpose(filters, (0, 2, 3, 1))  # filters last
    strides = (2, 2)
    tau_rc = 0.02
    tau_ref = 0.002
    tau_s = 0.005
    dt = 0.001

    neuron_type = LoihiLIF(tau_rc=tau_rc, tau_ref=tau_ref)

    pres_time = 0.1

    with nengo.Network(seed=seed) as model:
        nengo_loihi.add_params(model)

        u = nengo.Node(nengo.processes.PresentInput([test_x.ravel()],
                                                    pres_time),
                       label='u')

        a = nengo.Ensemble(input_shape.size,
                           1,
                           neuron_type=LoihiSpikingRectifiedLinear(),
                           max_rates=nengo.dists.Choice([40 / channels]),
                           intercepts=nengo.dists.Choice([0]),
                           label='a')
        model.config[a].on_chip = False

        if channels == 1:
            nengo.Connection(u, a.neurons, transform=1, synapse=None)
        elif channels == 2:
            # encode image into spikes using two channels (on/off)
            if input_shape.channels_last:
                nengo.Connection(u, a.neurons[0::2], transform=1, synapse=None)
                nengo.Connection(u,
                                 a.neurons[1::2],
                                 transform=-1,
                                 synapse=None)
            else:
                k = input_shape.rows * input_shape.cols
                nengo.Connection(u, a.neurons[:k], transform=1, synapse=None)
                nengo.Connection(u, a.neurons[k:], transform=-1, synapse=None)

            filters = np.vstack([filters, -filters])
        else:
            raise ValueError("Test not configured for more than two channels")

        conv2d_transform = Conv2D.from_kernel(filters,
                                              input_shape,
                                              strides=strides)
        output_shape = conv2d_transform.output_shape

        gain, bias = neuron_type.gain_bias(max_rates=100, intercepts=0)
        gain = gain * 0.01  # account for `a` max_rates
        b = nengo.Ensemble(output_shape.size,
                           1,
                           neuron_type=neuron_type,
                           gain=nengo.dists.Choice([gain[0]]),
                           bias=nengo.dists.Choice([bias[0]]),
                           label='b')
        nengo.Connection(a.neurons,
                         b.neurons,
                         synapse=tau_s,
                         transform=conv2d_transform)

        bp = nengo.Probe(b.neurons)

    with nengo.Simulator(model, dt=dt, optimize=False) as sim:
        sim.run(pres_time)
    ref_out = sim.data[bp].mean(axis=0).reshape(output_shape.shape())

    # Currently, default TensorFlow does not support channels first in conv
    use_nengo_dl = nengo_dl is not None and channels_last
    ndl_out = np.zeros_like(ref_out)
    if use_nengo_dl:
        with nengo_dl.Simulator(model, dt=dt) as sim:
            sim.run(pres_time)
        ndl_out = sim.data[bp].mean(axis=0).reshape(output_shape.shape())

    with nengo_loihi.Simulator(model, dt=dt, target='simreal') as sim:
        sim.run(pres_time)
    real_out = sim.data[bp].mean(axis=0).reshape(output_shape.shape())

    with Simulator(model, dt=dt) as sim:
        sim.run(pres_time)
    sim_out = sim.data[bp].mean(axis=0).reshape(output_shape.shape())

    if not output_shape.channels_last:
        ref_out = np.transpose(ref_out, (1, 2, 0))
        ndl_out = np.transpose(ndl_out, (1, 2, 0))
        real_out = np.transpose(real_out, (1, 2, 0))
        sim_out = np.transpose(sim_out, (1, 2, 0))

    out_max = max(ref_out.max(), sim_out.max())

    # --- plot results
    rows = 2
    cols = 3

    ax = plt.subplot(rows, cols, 1)
    imshow(test_x, vmin=0, vmax=1, ax=ax)

    ax = plt.subplot(rows, cols, 2)
    tile(np.transpose(filters[0], (2, 0, 1)), cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 3)
    plt.hist(ref_out.ravel(), bins=31)
    plt.hist(sim_out.ravel(), bins=31)

    ax = plt.subplot(rows, cols, 4)
    tile(np.transpose(ref_out, (2, 0, 1)), vmin=0, vmax=out_max, cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 5)
    tile(np.transpose(ndl_out, (2, 0, 1)), vmin=0, vmax=out_max, cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 6)
    tile(np.transpose(sim_out, (2, 0, 1)), vmin=0, vmax=out_max, cols=8, ax=ax)

    if use_nengo_dl:
        assert allclose(ndl_out, ref_out, atol=1e-5, rtol=1e-5)
    assert allclose(real_out, ref_out, atol=1, rtol=1e-3)
    assert allclose(sim_out, ref_out, atol=10, rtol=1e-3)
示例#19
0
def test_pop_tiny(pop_type, channels_last, nc, request, plt, seed, allclose):
    tau_rc = 0.02
    tau_ref = 0.001
    tau_s = 0.0
    dt = 0.001

    neuron_bias = 1.

    pres_time = 0.4

    sti, stj = 1, 1

    if nc == 1:
        filters = np.array([[-0.5, 2., -0.25], [-0.75, 2., -1.0],
                            [-0.5, 3., -0.5], [-1.0, 6.,
                                               -0.25]]).reshape(1, 4, 1, 3)

        inp_biases = np.array([[1, 5, 1], [2, 1, 2]])
        inp_biases = inp_biases[:, :, None]
    elif nc == 2:
        filters = np.array([[[-0.5, 2., -0.2], [-0.7, 2., -1.0],
                             [-0.5, 3., -0.5], [-1.0, 6., -0.2]],
                            [[-1.0, 2., -1.0], [-0.5, 2., -0.5],
                             [-0.8, 3., -0.2], [-1.0, 4.,
                                                -0.2]]]).reshape(2, 4, 1, 3)

        inp_biases = np.array([[[1, 5, 1], [2, 1, 2]], [[0, 3, 1], [4, 2, 1]]])
        inp_biases = np.transpose(inp_biases, (1, 2, 0))

    # rearrange to (kernel_rows, kernel_cols, in_channels, out_channels)
    filters = np.transpose(filters, (2, 3, 0, 1))

    inp_biases = inp_biases / (inp_biases.max() + 0.001)

    # --- compute nengo_loihi outputs
    ni, nj, nk = inp_biases.shape
    si, sj, nc, nf = filters.shape
    nij = ni * nj
    nyi = 1 + (ni - si) // sti
    nyj = 1 + (nj - sj) // stj
    out_size = nyi * nyj * nf
    assert out_size <= 1024

    model = Model()

    # input block
    inp = LoihiBlock(ni * nj * nk, label='inp')
    assert inp.n_neurons <= 1024
    inp.compartment.configure_relu()
    inp.compartment.bias[:] = inp_biases.ravel()

    inp_ax = Axon(nij, label='inp_ax')

    # we always compute the pixel/channel idxs with channels_last=True
    # (not sure why?), and then set it to the correct value afterwards
    inp_shape = nengo_transforms.ChannelShape((ni, nj, nk), channels_last=True)
    inp_ax.set_compartment_axon_map(target_axons=conv.pixel_idxs(inp_shape),
                                    atoms=conv.channel_idxs(inp_shape))
    inp_shape.shape = (ni, nj, nk) if channels_last else (nk, ni, nj)
    inp_shape.channels_last = channels_last

    inp.add_axon(inp_ax)

    model.add_block(inp)

    # conv block
    neurons = LoihiBlock(out_size, label='neurons')
    assert neurons.n_neurons <= 1024
    neurons.compartment.configure_lif(tau_rc=tau_rc, tau_ref=tau_ref, dt=dt)
    neurons.compartment.configure_filter(tau_s, dt=dt)
    neurons.compartment.bias[:] = neuron_bias

    synapse = Synapse(np.prod(inp_shape.spatial_shape), label='synapse')
    conv2d_transform = nengo_transforms.Convolution(
        nf,
        inp_shape,
        strides=(sti, stj),
        channels_last=channels_last,
        init=filters,
        kernel_size=(1, 3))
    weights, indices, axon_to_weight_map, bases = conv.conv2d_loihi_weights(
        conv2d_transform)
    synapse.set_population_weights(weights,
                                   indices,
                                   axon_to_weight_map,
                                   bases,
                                   pop_type=pop_type)
    neurons.add_synapse(synapse)

    out_probe = Probe(target=neurons, key='spiked')
    neurons.add_probe(out_probe)

    inp_ax.target = synapse
    model.add_block(neurons)

    # simulation
    discretize_model(model)

    n_steps = int(pres_time / dt)
    target = request.config.getoption("--target")
    if target == 'loihi':
        with HardwareInterface(model, use_snips=False, seed=seed) as sim:
            sim.run_steps(n_steps)
            sim_out = sim.get_probe_output(out_probe)
    else:
        with EmulatorInterface(model, seed=seed) as sim:
            sim.run_steps(n_steps)
            sim_out = sim.get_probe_output(out_probe)

    sim_out = np.sum(sim_out, axis=0) * (dt / pres_time)
    if channels_last:
        sim_out.shape = (nyi, nyj, nf)
        sim_out = np.transpose(sim_out, (2, 0, 1))
    else:
        sim_out.shape = (nf, nyi, nyj)

    out_max = sim_out.max()

    # --- plot results
    rows = 1
    cols = 2

    ax = plt.subplot(rows, cols, 1)
    plt.hist(sim_out.ravel(), bins=11)

    ax = plt.subplot(rows, cols, 2)
    tile(sim_out, vmin=0, vmax=out_max, grid=True, ax=ax)

    # ref_out determined by emulator running code known to work
    if nc == 1:
        ref_out = np.array([[0.06, 0.02], [0.055, 0.], [0.0825, 0.0225],
                            [0.125, 0.04]])
    elif nc == 2:
        ref_out = np.array([[0.0975, 0.02], [0.0825, 0.02], [0.125, 0.055],
                            [0.2475, 0.0825]])
    assert allclose(sim_out[:, :, 0], ref_out, rtol=0, atol=1e-7)
示例#20
0
def test_conv_connection(channels, channels_last, Simulator, seed, rng, plt,
                         allclose):
    # load data
    with open(os.path.join(test_dir, "mnist10.pkl"), "rb") as f:
        test10 = pickle.load(f)

    test_x = test10[0][0].reshape((28, 28))
    test_x = 1.999 * test_x - 0.999  # range (-1, 1)
    input_shape = make_channel_shape(test_x.shape, channels, channels_last)

    filters = Gabor(freq=Uniform(0.5, 1)).generate(8, (7, 7), rng=rng)
    filters = filters[None, :, :, :]  # single channel
    filters = np.transpose(filters, (2, 3, 0, 1))
    strides = (2, 2)
    tau_rc = 0.02
    tau_ref = 0.002
    tau_s = 0.005

    neuron_type = LoihiLIF(tau_rc=tau_rc, tau_ref=tau_ref)

    pres_time = 0.1

    with nengo.Network(seed=seed) as model:
        nengo_loihi.add_params(model)

        u = nengo.Node(test_x.ravel(), label="u")

        a = nengo.Ensemble(
            input_shape.size,
            1,
            neuron_type=LoihiSpikingRectifiedLinear(),
            max_rates=nengo.dists.Choice([40 / channels]),
            intercepts=nengo.dists.Choice([0]),
            label="a",
        )
        model.config[a].on_chip = False

        if channels == 1:
            nengo.Connection(u, a.neurons, transform=1, synapse=None)
        elif channels == 2:
            # encode image into spikes using two channels (on/off)
            if input_shape.channels_last:
                nengo.Connection(u, a.neurons[0::2], transform=1, synapse=None)
                nengo.Connection(u,
                                 a.neurons[1::2],
                                 transform=-1,
                                 synapse=None)
            else:
                k = input_shape.spatial_shape[0] * input_shape.spatial_shape[1]
                nengo.Connection(u, a.neurons[:k], transform=1, synapse=None)
                nengo.Connection(u, a.neurons[k:], transform=-1, synapse=None)

            filters = np.concatenate([filters, -filters], axis=2)
        else:
            raise ValueError("Test not configured for more than two channels")

        conv2d_transform = nengo_transforms.Convolution(
            8,
            input_shape,
            strides=strides,
            kernel_size=(7, 7),
            channels_last=channels_last,
            init=filters,
        )

        output_shape = conv2d_transform.output_shape

        gain, bias = neuron_type.gain_bias(max_rates=100, intercepts=0)
        gain = gain * 0.01  # account for `a` max_rates
        b = nengo.Ensemble(
            output_shape.size,
            1,
            neuron_type=neuron_type,
            gain=nengo.dists.Choice([gain[0]]),
            bias=nengo.dists.Choice([bias[0]]),
            label="b",
        )
        nengo.Connection(a.neurons,
                         b.neurons,
                         synapse=tau_s,
                         transform=conv2d_transform)

        bp = nengo.Probe(b.neurons)

    with nengo.Simulator(model, optimize=False) as sim_nengo:
        sim_nengo.run(pres_time)
    ref_out = sim_nengo.data[bp].mean(axis=0).reshape(output_shape.shape)

    with Simulator(model, target="simreal") as sim_emu:
        sim_emu.run(pres_time)
    emu_out = sim_emu.data[bp].mean(axis=0).reshape(output_shape.shape)

    with Simulator(model, hardware_options={"snip_max_spikes_per_step":
                                            800}) as sim_loihi:
        sim_loihi.run(pres_time)
    sim_out = sim_loihi.data[bp].mean(axis=0).reshape(output_shape.shape)

    if not output_shape.channels_last:
        ref_out = np.transpose(ref_out, (1, 2, 0))
        emu_out = np.transpose(emu_out, (1, 2, 0))
        sim_out = np.transpose(sim_out, (1, 2, 0))

    out_max = max(ref_out.max(), emu_out.max(), sim_out.max())

    # --- plot results
    rows = 2
    cols = 3

    ax = plt.subplot(rows, cols, 1)
    imshow(test_x, vmin=0, vmax=1, ax=ax)

    ax = plt.subplot(rows, cols, 2)
    tile(np.transpose(filters[0], (2, 0, 1)), cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 3)
    plt.hist(ref_out.ravel(), bins=31)
    plt.hist(sim_out.ravel(), bins=31)

    ax = plt.subplot(rows, cols, 4)
    tile(np.transpose(ref_out, (2, 0, 1)), vmin=0, vmax=out_max, cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 5)
    tile(np.transpose(emu_out, (2, 0, 1)), vmin=0, vmax=out_max, cols=8, ax=ax)

    ax = plt.subplot(rows, cols, 6)
    tile(np.transpose(sim_out, (2, 0, 1)), vmin=0, vmax=out_max, cols=8, ax=ax)

    assert allclose(emu_out, ref_out, atol=10, rtol=1e-3)
    assert allclose(sim_out, ref_out, atol=10, rtol=1e-3)
示例#21
0

for learner in learners:
    plot_batches(learner.batch_costs, label=type(learner).__name__)
plt.ylabel('train cost')
plt.legend(loc='best')

plt.subplot(212)


def plot_epochs(x, label=None):
    epoch_inds = len(trainX) * np.arange(1, len(x) + 1)
    plt.plot(epoch_inds, x, label=label)


for learner in learners:
    plot_epochs(learner.test_errors, label=type(learner).__name__)
plt.xlabel('example')
plt.ylabel('test error')
plt.legend(loc='best')

# plot encoders
plt.figure(2)
rows, cols = len(learners), 1
for i, learner in enumerate(learners):
    encoders = learner.network.weights[0].T.reshape(-1, 28, 28)
    plt.subplot(rows, cols, i + 1)
    nplt.tile(encoders, grid=True)

plt.show()
示例#22
0
def test_conv_deepnet(
    channels_last,
    pop_type,
    precompute,
    Simulator,
    request,
    rng,
    seed,
    plt,
    allclose,
):
    """Run a convolutional network with two layers on the chip.

    Checks that network with block splitting on the target matches one without
    on the emulator.
    """
    # TODO: This case fails in NxSDK 0.9.0 but will be fixed in the next version.
    # Remove this check once the next version is released.
    if pop_type == 32:
        pytest.skip("Pop32 multichip test requires latest NxSDK")

    def set_partition(partition):
        os.environ["PARTITION"] = partition

    request.addfinalizer(lambda: set_partition(""))
    # multichip pop_type = 16 works only on nahuku32 board currently
    if pop_type == 16:
        set_partition("nahuku32")

    def conv_layer(x,
                   input_shape,
                   array_init=None,
                   label=None,
                   conn_args=None,
                   **conv_args):
        conn_args = {} if conn_args is None else conn_args

        if array_init is not None:
            assert all(a not in conv_args
                       for a in ("init", "kernel_size", "n_filters"))
            assert array_init.ndim == 4
            conv_args["init"] = array_init
            conv_args["kernel_size"] = array_init.shape[:2]
            assert array_init.shape[2] == input_shape.n_channels
            conv_args["n_filters"] = array_init.shape[3]

        conv = nengo.Convolution(input_shape=input_shape, **conv_args)

        # add an ensemble to implement the activation function
        layer = nengo.Ensemble(conv.output_shape.size, 1, label=label)

        # connect up the input object to the new layer
        conn = nengo.Connection(x, layer.neurons, transform=conv)

        return layer, conv, conn

    channels = 1
    n_filters0 = 1
    n_filters1 = 4
    n_filters2 = 4

    # load data
    with open(os.path.join(test_dir, "mnist10.pkl"), "rb") as f:
        test10 = pickle.load(f)

    test_x = test10[0][0].reshape(28, 28)  # range (0, 1)
    input_shape = make_channel_shape(test_x.shape, channels, channels_last)

    filters0 = np.ones((1, 1, channels, n_filters0))

    # use Gabor filters for first layer
    filters1 = Gabor(freq=Uniform(0.5, 1),
                     sigma_x=Choice([0.9]),
                     sigma_y=Choice([0.9])).generate(n_filters1, (7, 7),
                                                     rng=rng)
    assert n_filters0 == 1
    filters1 = filters1[None, :, :, :]  # single channel
    filters1 = np.transpose(filters1,
                            (2, 3, 0, 1))  # rows, cols, in_chan, out_chan

    # use random combinations of first-layer channels in 1x1 convolution
    filters2 = rng.uniform(-0.2, 1,
                           size=(n_filters1, n_filters2)).clip(0, None)
    filters2 *= 2 / filters2.sum(axis=0,
                                 keepdims=True)  # each filter sums to 2
    filters2 = filters2[None, None, :, :]  # rows, cols, in_chan, out_chan

    tau_s = 0.001
    max_rate = 100
    amp = 1 / max_rate
    f_split = 2 if pop_type == 32 else 4

    # use Loihi neuron type so Nengo sim mimics Loihi neuron effects
    neuron_type = LoihiSpikingRectifiedLinear(amplitude=amp)

    pres_time = 0.2

    with nengo.Network(seed=seed) as net:
        nengo_loihi.add_params(net)

        net.config[nengo.Ensemble].neuron_type = neuron_type
        net.config[nengo.Ensemble].max_rates = Choice([max_rate])
        net.config[nengo.Ensemble].intercepts = Choice([0])
        net.config[nengo.Connection].synapse = tau_s

        u = nengo.Node(test_x.ravel(), label="u")

        layer0, conv0, conn0 = conv_layer(
            u,
            input_shape=input_shape,
            array_init=filters0,
            strides=(1, 1),
            channels_last=channels_last,
            label="layer0",
            conn_args=dict(synapse=None),
        )
        net.config[layer0].on_chip = False

        layer1, conv1, conn1 = conv_layer(
            layer0.neurons,
            input_shape=conv0.output_shape,
            array_init=filters1,
            strides=(2, 2),
            channels_last=channels_last,
            label="layer1",
        )
        net.config[layer1].block_shape = nengo_loihi.BlockShape(
            make_shape((4, 4), f_split, channels_last), conv1)
        net.config[conn1].pop_type = pop_type

        layer2, conv2, conn2 = conv_layer(
            layer1.neurons,
            input_shape=conv1.output_shape,
            array_init=filters2,
            strides=(1, 1),
            channels_last=channels_last,
            label="layer2",
        )
        net.config[layer2].block_shape = nengo_loihi.BlockShape(
            make_shape((4, 4), f_split, channels_last), conv2)
        net.config[conn2].pop_type = pop_type

        output_p = nengo.Probe(layer2.neurons)
        output_shape = conv2.output_shape

    with nengo.Simulator(net, optimize=False) as sim_nengo:
        sim_nengo.run(pres_time)
        ref_out = (sim_nengo.data[output_p] > 0).sum(axis=0).reshape(
            output_shape.shape)

    with Simulator(net, target="sim") as sim_emu:
        sim_emu.run(pres_time)
        emu_out = (sim_emu.data[output_p] > 0).sum(axis=0).reshape(
            output_shape.shape)

    # TODO: Remove the if condition when configurable timeout parameter
    # is available in nxsdk
    if (pop_type == 32 or
            os.popen("sinfo -h --partition=nahuku32").read().find("idle") > 0):
        with Simulator(
                net,
                precompute=precompute,
                hardware_options={
                    "allocator": RoundRobin(),
                    "snip_max_spikes_per_step": 800,
                },
        ) as sim_loihi:
            sim_loihi.run(pres_time)
            sim_out = ((sim_loihi.data[output_p] > 0).sum(axis=0).reshape(
                output_shape.shape))
    elif nengo_loihi.version.dev is None:
        pytest.fail(
            "Pop16 multichip test failed since Nahuku32 is unavailable")
    else:
        pytest.skip(
            "Pop16 multichip test skipped since Nahuku32 is unavailable")

    out_max = ref_out.max()
    ref_out = ref_out / out_max
    emu_out = emu_out / out_max
    sim_out = sim_out / out_max

    if channels_last:
        # channels first, to display channels in separate plots
        ref_out = np.transpose(ref_out, (2, 0, 1))
        emu_out = np.transpose(emu_out, (2, 0, 1))
        sim_out = np.transpose(sim_out, (2, 0, 1))

    # --- plot results
    rows = 2
    cols = 3

    ax = plt.subplot(rows, cols, 1)
    imshow(test_x, vmin=0, vmax=1, ax=ax)

    ax = plt.subplot(rows, cols, 2)
    tile(np.transpose(filters1, (2, 3, 0, 1))[0],
         rows=2,
         cols=2,
         grid=True,
         ax=ax)

    ax = plt.subplot(rows, cols, 3)
    plt.hist((ref_out.ravel(), emu_out.ravel(), sim_out.ravel()), bins=21)

    ax = plt.subplot(rows, cols, 4)
    tile(ref_out, rows=2, cols=2, grid=True, ax=ax)

    ax = plt.subplot(rows, cols, 5)
    tile(emu_out, rows=2, cols=2, grid=True, ax=ax)

    ax = plt.subplot(rows, cols, 6)
    tile(sim_out, rows=2, cols=2, grid=True, ax=ax)

    assert allclose(sim_out, ref_out, atol=0.15, rtol=1e-3)
    assert allclose(sim_out, emu_out, atol=1e-3, rtol=1e-3)
示例#23
0
import matplotlib.pyplot as plt
import numpy as np

from nengo.dists import Uniform
from nengo_extras.matplotlib import tile
from nengo_extras.vision import Gabor
from hunse_thesis.dists import LogUniform

rng = np.random.RandomState(3)

# r, c = 10, 20
r, c = 9, 12

# gabor = Gabor()
# gabor = Gabor(freq=Uniform(0.5, 1.5))
gabor = Gabor(freq=LogUniform(np.log(0.5), np.log(1.5)))

gabors = gabor.generate(r * c, (32, 32), rng=rng)

tile(gabors, rows=r, cols=c)

plt.savefig('gabors.pdf')
plt.show()
示例#24
0
def test_load_cifar100(plt):
    (trainX, _), (_, _) = load_cifar100()
    trainX = trainX.reshape(-1, 3, 32, 32)
    trainX = np.transpose(trainX, (0, 2, 3, 1))
    tile(trainX)