Exemple #1
0
# Populations
In = Population(10, Input)
Out = Population(10, Output)
smallOut = Population(5, Output)


# Filters
vertical_filter = np.array(
    [ -1.0, 0.0, 1.0]
)

# Full connection
proj = SharedProjection(
    pre = In, 
    post = Out, 
    target = 'ws',
).convolve( weights = vertical_filter, method='filter', padding='border')

proj = SharedProjection(
    pre = In, 
    post = smallOut, 
    target = 'ws',
).convolve( weights = vertical_filter, method='filter', padding='border')

# Compile
compile()

# Set input
In[5:].r = 1.0
Exemple #2
0
from ANNarchy import *
from ANNarchy.extensions.image import *
from ANNarchy.extensions.weightsharing import SharedProjection

Linear = Neuron(equations="r=sum(exc): min=0.0")

# Create the population
pop = ImagePopulation(geometry=(480, 640, 3))
pooled = Population(geometry=(48, 64, 3), neuron=Linear)
filtered = Population(geometry=(48, 64, 3), neuron=Linear)

# Max pooling over the input image
pool_proj = SharedProjection(pre=pop,
                             post=pooled,
                             target='exc',
                             operation='mean').pooling()

# Blue Filter
blue_filter = [[[[2.0, -1.0, -1.0]]], [[[-1.0, 2.0, -1.0]]],
               [[[-1.0, -1.0, 2.0]]]]
blue_proj = SharedProjection(pre=pooled, post=filtered,
                             target='exc').convolve(weights=blue_filter,
                                                    method='filter',
                                                    multiple=True,
                                                    padding='border')

# Compile
compile()

# Set the image
pop.set_image('test.jpg')
Exemple #3
0
# Dummy neurons
Input = Neuron(parameters="r = 0.0")
Output = Neuron(equations="r = sum(ws)")

# Populations
In = Population((10, 10), Input)
Out = Population((5, 10), Output)

# Filters
vertical_filter = np.array([-1.0, 0.0, 1.0])

# Full connection
proj = SharedProjection(pre=In, post=Out,
                        target='ws').convolve(weights=vertical_filter,
                                              keep_last_dimension=True,
                                              method='filter',
                                              padding='border')

# Compile
compile()

# Set input
for i in range(10):
    In[i:, i].r = 1.0

# Simulate()
simulate(1.0)

# Plot
from pylab import *
Exemple #4
0
# `ANNarchy.extensions.image` depends on the Python bindings of OpenCV, they must be installed before running the script.
# We first create an `ImagePopulation` that will load images:
image = ImagePopulation(geometry=(480, 640, 3))


# Its geometry specifies the size of the images that can be loaded, here 640x480 RGB images. Note the geometry must be of the form (height, width, channels), where channels is 1 for grayscale images and 3 for color images.
# The next step is to reduce the size of the image, what can be done by using the `SharedProjection` class of the `weightsharing` extension. 
# We define a dummy artificial neuron, whose firing rate `r` will simply be the sum of excitatory connections /ensured to be positive, but this should always be the case). We then create a smaller population `pooled` with this neuron type, and connect it to the `ImagePopulation` using mean-pooling:

LinearNeuron = Neuron(equations="r=sum(exc): min=0.0")

# Subsampling population
pooled = Population(geometry=(48, 64, 3), neuron = LinearNeuron)

# Mean-pooling projection
pool_proj = SharedProjection(pre=image, post=pooled, target='exc', operation='mean')
pool_proj.pooling()


# The `pooled` population reduces the size of the image by a factor ten (defined by the size of the population) by averaging the pixels values over 10x10 regions (`operation` is set to `'mean'`, but one could use `'max'` or `'min'`). The `pooling()` connector creates the "fake" connection pattern (as no weights are involved).
# Let's apply now a 3x3 box filter on each channel of the pooled population:

# Smoothing population
smoothed = Population(geometry=(48, 64, 3), neuron = LinearNeuron)

# Box filter projection
box_filter = np.ones((3, 3, 1))/9.
smooth_proj = SharedProjection(pre=pooled, post=smoothed, target='exc')
smooth_proj.convolve(weights=box_filter, method='filter')

Exemple #5
0
# `ANNarchy.extensions.image` depends on the Python bindings of OpenCV, they must be installed before running the script.
# We first create an `ImagePopulation` that will load images:
image = ImagePopulation(geometry=(480, 640, 3))

# Its geometry specifies the size of the images that can be loaded, here 640x480 RGB images. Note the geometry must be of the form (height, width, channels), where channels is 1 for grayscale images and 3 for color images.
# The next step is to reduce the size of the image, what can be done by using the `SharedProjection` class of the `weightsharing` extension.
# We define a dummy artificial neuron, whose firing rate `r` will simply be the sum of excitatory connections /ensured to be positive, but this should always be the case). We then create a smaller population `pooled` with this neuron type, and connect it to the `ImagePopulation` using mean-pooling:

LinearNeuron = Neuron(equations="r=sum(exc): min=0.0")

# Subsampling population
pooled = Population(geometry=(48, 64, 3), neuron=LinearNeuron)

# Mean-pooling projection
pool_proj = SharedProjection(pre=image,
                             post=pooled,
                             target='exc',
                             operation='mean')
pool_proj.pooling()

# The `pooled` population reduces the size of the image by a factor ten (defined by the size of the population) by averaging the pixels values over 10x10 regions (`operation` is set to `'mean'`, but one could use `'max'` or `'min'`). The `pooling()` connector creates the "fake" connection pattern (as no weights are involved).
# Let's apply now a 3x3 box filter on each channel of the pooled population:

# Smoothing population
smoothed = Population(geometry=(48, 64, 3), neuron=LinearNeuron)

# Box filter projection
box_filter = np.ones((3, 3, 1)) / 9.
smooth_proj = SharedProjection(pre=pooled, post=smoothed, target='exc')
smooth_proj.convolve(weights=box_filter, method='filter')

# To perform a convolution operation on the population (or more precisely a cross-correlation), we call the `convolve()` connector method of the `SharedProjection`. It requires to define a kernel (`weights`) that will be convolved over the input population. Here we use a simple box filter, but any filter can be used.
Exemple #6
0
# Population getting the video stream
width = 640
height = 480
video = VideoPopulation(geometry=(height, width, 3))

# Define a normalizedred filter with dimensions 10*10*3
extent = 10
red_filter = [[[2.0 / extent**2, -1.0 / extent**2, -1.0 / extent**2]
               for j in range(extent)] for i in range(extent)]

# Create a population of DNF neurons downscaling the image with a factor 10
dnf = Population(geometry=(height / extent, width / extent), neuron=DNF)

# Create the convolution usinf the red filter
ff = SharedProjection(pre=video, post=dnf,
                      target='exc').convolve(weights=red_filter,
                                             method='filter')

# Create difference of Gaussians lateral connections for denoising/competition
lat = Projection(pre=dnf, post=dnf, target='inh').connect_dog(amp_pos=0.15,
                                                              sigma_pos=0.05,
                                                              amp_neg=0.1,
                                                              sigma_neg=0.5,
                                                              limit=0.1)

# Compile
compile()

# Start the camera
video.start_camera(0)
Exemple #7
0
    [
        [ [-1.0]*depth, [-1.0]*depth, [-1.0]*depth],
        [ [ 0.0]*depth, [ 0.0]*depth, [ 0.0]*depth],
        [ [ 1.0]*depth, [ 1.0]*depth, [ 1.0]*depth]
    ],
    [
        [ [ 1.0]*depth, [ 1.0]*depth, [ 1.0]*depth],
        [ [ 0.0]*depth, [ 0.0]*depth, [ 0.0]*depth],
        [ [-1.0]*depth, [-1.0]*depth, [-1.0]*depth]
    ]
  ]

# Full connection
proj = SharedProjection(
    pre = In, 
    post = Out, 
    target = 'ws',
).convolve( weights = bank_filter, multiple=True, method='filter', padding='border')

pool = SharedProjection(
    pre = Out, 
    post = Pool, 
    target = 'ws',
    operation='max'
).pooling(extent=(1,1,4))

# Compile
compile()

# Set input
In[30:70, 30:70, :].r = 1.0
Exemple #8
0
from ANNarchy import *
from ANNarchy.extensions.weightsharing import SharedProjection

# Dummy neurons
Input = Neuron(parameters="r = 0.0")
Output = Neuron(equations="r = sum(ws)")

# Populations
In = Population((10, 10, 2), Input)
Out = Population((2, 2, 2), Output)

# Max-pooling
proj = SharedProjection(pre=In, post=Out, target='ws',
                        operation='max').pooling()

# Compile
compile()

# Set input
In[2, 2, 0].r = 1.0
In[7, 7, 1].r = 1.0

# Simulate()
simulate(1.0)

# Plot
from pylab import *
subplot(2, 2, 1)
imshow(In.r[:, :, 0], cmap=cm.gray, interpolation='nearest')
subplot(2, 2, 2)
imshow(Out.r[:, :, 0], cmap=cm.gray, interpolation='nearest')