Skip to content
Snippets Groups Projects
Commit c2b370e5 authored by Hammouda Elbez's avatar Hammouda Elbez :computer:
Browse files

Added read_activity script

parent d8baf8e2
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
## Classification using SpiNNaker activity and SVM
In this document, we load and preprocess the spikes activity received in the output layer of SpiNNaker, and use an SVM for the classification. Using MNIST dataset
%% Cell type:markdown id: tags:
Imports
%% Cell type:code id: tags:
``` python
import numpy as np
from torchvision import transforms,datasets
from sklearn.svm import LinearSVC
from neo.io import PickleIO
```
%% Cell type:markdown id: tags:
Load MNIST dataset with the preprocessing
%% Cell type:code id: tags:
``` python
transform=transforms.Compose([
transforms.Resize((28,28)),
transforms.Grayscale(),
transforms.ToTensor(),
transforms.Normalize((0,),(1,))])
dataPath='~/datasets'
mnistTrain=datasets.MNIST(dataPath,train=True,download=True,transform=transform)
mnistTest=datasets.MNIST(dataPath,train=False,download=True,transform=transform)
```
%% Cell type:markdown id: tags:
Load SpiNNaker activity data
%% Cell type:code id: tags:
``` python
Train60k = PickleIO(filename="[Trainset_Activity]")
```
%% Cell type:code id: tags:
``` python
Test10k = PickleIO(filename="[Testset_Activity]")
```
%% Cell type:markdown id: tags:
Read the blocks from the neo file
%% Cell type:code id: tags:
``` python
Train60k_block = Train60k.read_block()
```
%% Cell type:code id: tags:
``` python
Test10k_block = Test10k.read_block()
```
%% Cell type:markdown id: tags:
Define network information
%% Cell type:code id: tags:
``` python
input_nbr = 60000
input_nbr_testset = 10000
neuron_nbr = len(Train60k_block.segments[0].spiketrains)
presentation_time = 29
```
%% Cell type:code id: tags:
``` python
def neoToNpArray(block, input_nbr, neuron_nbr, presentation_time):
"""Convert Neo object to Numpy array
Args:
block (neo): neo object
input_nbr (int): number of inputs
neuron_nbr (int): number of neurons
presentation_time (int): presentation time for each input (silence period included)
Returns:
activityArray (numpy array): numpy array of the network activity
"""
activityArray = np.zeros((input_nbr, presentation_time, neuron_nbr))
for neuron_id, spikesPerNeuron in enumerate(block.segments[0].spiketrains):
for spike in spikesPerNeuron:
spike_timestamp = float(spike)
in_nbr = int(spike_timestamp / presentation_time)
pre_time = int(spike_timestamp % presentation_time)
activityArray[in_nbr][pre_time][neuron_id] = 1
return activityArray
```
%% Cell type:markdown id: tags:
Show the time of the latest spike
%% Cell type:code id: tags:
``` python
max = - np.Infinity
for spikes in Train60k_block.segments[0].spiketrains:
if len(np.array(spikes)) > 0:
s = np.max(np.array(spikes))
if max < s:
max = s
print("last spike time: ",max)
```
%% Cell type:markdown id: tags:
Convert Neo object to Numpy array
%% Cell type:code id: tags:
``` python
Train60k_array= neoToNpArray(Train60k_block,input_nbr,neuron_nbr,presentation_time)
```
%% Cell type:markdown id: tags:
Print the shape to confirm the content
%% Cell type:code id: tags:
``` python
Train60k_array.shape
```
%% Cell type:code id: tags:
``` python
def optimizeNpy(npyArray, presentation_time):
"""Optimize the Numpy array by keeping only the first spike
Args:
npyArray (numpy array): numpy array of the network activity
presentation_time (int): presentation time for each input (silence period included)
Returns:
npyArray_reduced (numpy array): reduced numpy array
"""
npyArray_reduced = np.zeros((npyArray.shape[0],presentation_time,npyArray.shape[2]))
activityArray = np.zeros((presentation_time,npyArray.shape[2]))
# for each input
for i,inp in enumerate(npyArray):
# for each timestamp before 19
for j,s in enumerate(inp[0:presentation_time]):
# for each neuron
for k,n in enumerate(s):
if(activityArray[k] == 0 and n == 1):
npyArray_reduced[i,j,k] = 1
activityArray[k] = 1
activityArray = np.zeros((presentation_time,npyArray.shape[2]))
return npyArray_reduced
```
%% Cell type:markdown id: tags:
Reduce the content of numpy array by reducing the spikes activity and keeping only first spikes per neuron
%% Cell type:code id: tags:
``` python
Train60k_array_reduced = optimizeNpy(Train60k_array,19)
```
%% Cell type:markdown id: tags:
Check the shape of the new created numpy array
%% Cell type:code id: tags:
``` python
Train60k_array_reduced.shape
```
%% Cell type:code id: tags:
``` python
def optimizeSteps(npyArray, input_nbr, neuron_nbr, presentation_time):
"""Code the spikes in a numerical format suitable for SVM training
Args:
npyArray (numpy array): numpy array of the network activity
input_nbr (int): number of inputs
neuron_nbr (int): number of neurons
presentation_time (int): time of presentation (ms)
Returns:
activityArray (numpy array): numpy array with the proper spikes coding
"""
activityArray = np.zeros((input_nbr,neuron_nbr))
totalStep = presentation_time
for i,inp in enumerate(npyArray):
# for each timestamp
for j,s in enumerate(inp):
# for each neuron
for k,n in enumerate(s):
if(activityArray[i,k] == 0 and n == 1):
activityArray[i,k] = ((totalStep - j) / totalStep)
return activityArray
```
%% Cell type:markdown id: tags:
Apply the spike encoding for numerical representation before using SVM
%% Cell type:code id: tags:
``` python
Train60k_array_reduced_encoded = optimizeSteps(Train60k_array_reduced, 60000, 1600, presentation_time)
```
%% Cell type:markdown id: tags:
Check the shape of the new created array
%% Cell type:code id: tags:
``` python
Train60k_array_reduced_encoded.shape
```
%% Cell type:markdown id: tags:
Convert Neo object to Numpy array for testset
%% Cell type:code id: tags:
``` python
Test10k_array = neoToNpArray(Test10k_block,input_nbr_testset,neuron_nbr,presentation_time)
```
%% Cell type:markdown id: tags:
Check the shape of the new created array
%% Cell type:code id: tags:
``` python
Test10k_array.shape
```
%% Cell type:markdown id: tags:
### SVM
%% Cell type:markdown id: tags:
Prepare the data for SVM
%% Cell type:code id: tags:
``` python
labelsArray = np.array(mnistTrain.targets)
activityArraySVM = Train60k_array_reduced_encoded.reshape((60000,-1))
```
%% Cell type:markdown id: tags:
Train the SVM and show the score
%% Cell type:code id: tags:
``` python
clf_spike = LinearSVC(max_iter=10000)
clf_spike.fit(activityArraySVM, labelsArray)
score = clf_spike.score(activityArraySVM, labelsArray)
print("score: ",round(score * 100,2), "%")
```
%% Cell type:markdown id: tags:
Test the network accuracy using testset
%% Cell type:code id: tags:
``` python
Test10k_array_reduced = optimizeNpy(Test10k_array,19)
```
%% Cell type:code id: tags:
``` python
Test10k_array_reduced_encoded = optimizeSteps(Test10k_array_reduced, 10000, 1600, presentation_time)
```
%% Cell type:code id: tags:
``` python
labelsArray_test = np.array(mnistTest.targets)
activityArraySVM_test = Test10k_array_reduced_encoded.reshape((10000,-1))
```
%% Cell type:code id: tags:
``` python
activityArraySVM_test.shape
```
%% Cell type:code id: tags:
``` python
predict_spike = clf_spike.predict(activityArraySVM_test)
error_spike = labelsArray_test[labelsArray_test != predict_spike]
error_spike[:] = 1
print("Acc:", (1 - error_spike.sum()/len(predict_spike))*100,"%")
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment