NeuromorphicProcessorProject / snn_toolbox

Toolbox for converting analog to spiking neural networks (ANN to SNN), and running them in a spiking neuron simulator.
MIT License
361 stars 105 forks source link

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices #126

Closed sauravtii closed 2 years ago

sauravtii commented 2 years ago

I am trying out this tutorial (https://www.youtube.com/watch?v=FxTMH9VRsgk) in which he converts the LeNet-5 architecture to SNN and then simulates it using 'INI; simulator. I am following the exact same tutorial, the only difference is, I am using brian2 instead of INI, and it is giving me the following error. Also, I am using the pypi version of SNNtoolbox. I have also provided my config file below.

Link to the github repo mentioned in the tutorial: https://github.com/iCAS-Lab/neuro_comp_class

Can anyone please help me on this ?

The whole output after calling out the main function:

Temporarily setting batch_size to 1 because simulator does not support parallel testing of multiple samples.
Initializing brian2 simulator...

Loading scale factors from disk instead of recalculating.
Loading data set from '.npz' files in /home/saurav/Desktop/icas_spinnaker_snntoolbox/neuro_comp_class/tutorials/snntb/data.

2022-08-12 12:41:29.260092: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-08-12 12:41:31.529721: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1532] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 79132 MB memory:  -> device: 0, name: NVIDIA A100-SXM4-80GB, pci bus id: 0000:01:00.0, compute capability: 8.0
2022-08-12 12:41:31.532770: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1532] Created device /job:localhost/replica:0/task:0/device:GPU:1 with 79132 MB memory:  -> device: 1, name: NVIDIA A100-SXM4-80GB, pci bus id: 0000:47:00.0, compute capability: 8.0
2022-08-12 12:41:31.535699: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1532] Created device /job:localhost/replica:0/task:0/device:GPU:2 with 79132 MB memory:  -> device: 2, name: NVIDIA A100-SXM4-80GB, pci bus id: 0000:81:00.0, compute capability: 8.0
2022-08-12 12:41:31.538725: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1532] Created device /job:localhost/replica:0/task:0/device:GPU:3 with 79132 MB memory:  -> device: 3, name: NVIDIA A100-SXM4-80GB, pci bus id: 0000:c2:00.0, compute capability: 8.0
2022-08-12 12:41:31.539694: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1532] Created device /job:localhost/replica:0/task:0/device:GPU:4 with 2128 MB memory:  -> device: 4, name: NVIDIA DGX Display, pci bus id: 0000:c1:00.0, compute capability: 7.5
Model: "model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_1 (InputLayer)        [(None, 28, 28, 1)]       0         

 conv2d (Conv2D)             (None, 24, 24, 6)         156       

 average_pooling2d (AverageP  (None, 12, 12, 6)        0         
 ooling2D)                                                       

 conv2d_1 (Conv2D)           (None, 8, 8, 16)          2416      

 average_pooling2d_1 (Averag  (None, 4, 4, 16)         0         
 ePooling2D)                                                     

 flatten (Flatten)           (None, 256)               0         

 dense (Dense)               (None, 120)               30840     

 dense_1 (Dense)             (None, 84)                10164     

 dense_2 (Dense)             (None, 10)                850       

=================================================================
Total params: 44,426
Trainable params: 44,426
Non-trainable params: 0
_________________________________________________________________
Evaluating input model on 100 samples...
2022-08-12 12:41:33.194821: I tensorflow/stream_executor/cuda/cuda_dnn.cc:384] Loaded cuDNN version 8100
2022-08-12 12:41:33.798054: W tensorflow/stream_executor/gpu/asm_compiler.cc:111] *** WARNING *** You are using ptxas 10.1.243, which is older than 11.1. ptxas before 11.1 is known to miscompile XLA code, leading to incorrect results or invalid-address errors.

You may not need to update to CUDA 11.1; cherry-picking the ptxas binary is often sufficient.
2022-08-12 12:41:33.799892: W tensorflow/stream_executor/gpu/asm_compiler.cc:230] Falling back to the CUDA driver for PTX compilation; ptxas does not support CC 8.0
2022-08-12 12:41:33.799929: W tensorflow/stream_executor/gpu/asm_compiler.cc:233] Used ptxas at ptxas
2022-08-12 12:41:33.800043: W tensorflow/stream_executor/gpu/redzone_allocator.cc:314] UNIMPLEMENTED: ptxas ptxas too old. Falling back to the driver to compile.
Relying on driver to perform ptx compilation. 
Modify $PATH to customize ptxas location.
This message will be only logged once.
2022-08-12 12:41:34.430378: I tensorflow/stream_executor/cuda/cuda_blas.cc:1786] TensorFloat-32 will be used for the matrix multiplication. This will only be logged once.
Top-1 accuracy: 99.00%
Top-5 accuracy: 100.00%

Parsing input model...
Skipping layer InputLayer.
Parsing layer Conv2D.
Using activation relu.
Parsing layer AveragePooling2D.
Parsing layer Conv2D.
Using activation relu.
Parsing layer AveragePooling2D.
Parsing layer Flatten.
Parsing layer Dense.
Using activation relu.
Parsing layer Dense.
Using activation relu.
Parsing layer Dense.
Using activation softmax.

Building parsed model...

Compiling parsed model...

Model: "model"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 input_1 (InputLayer)        [(1, 28, 28, 1)]          0         

 0Conv2D_24x24x6 (Conv2D)    (1, 24, 24, 6)            156       

 1AveragePooling2D_12x12x6 (  (1, 12, 12, 6)           0         
 AveragePooling2D)                                               

 2Conv2D_8x8x16 (Conv2D)     (1, 8, 8, 16)             2416      

 3AveragePooling2D_4x4x16 (A  (1, 4, 4, 16)            0         
 veragePooling2D)                                                

 4Flatten_256 (Flatten)      (1, 256)                  0         

 5Dense_120 (Dense)          (1, 120)                  30840     

 6Dense_84 (Dense)           (1, 84)                   10164     

 7Dense_10 (Dense)           (1, 10)                   850       

=================================================================
Total params: 44,426
Trainable params: 44,426
Non-trainable params: 0
_________________________________________________________________
Normalizing parameters...
Using scale factor 1.00 for softmax layer.
Plotting distributions of weights and activations before and after normalizing...
Loading activations stored during a previous run.
Loading activations stored during a previous run.
Loading activations stored during a previous run.
Loading activations stored during a previous run.
Loading activations stored during a previous run.

Evaluating parsed model on 100 samples...
Top-1 accuracy: 99.00%
Top-5 accuracy: 99.00%

Building spiking model...
Building layer: 0Conv2D_24x24x6
Transposing kernels.
ERROR      Brian 2 encountered an unexpected error. If you think this is a bug in Brian 2, please report this issue either to the discourse forum at <http://brian.discourse.group/>, or to the issue tracker at <https://github.com/brian-team/brian2/issues>. Please include this file with debug information in your report: /tmp/brian_debug_4pvf2xwq.log  Additionally, you can also include a copy of the script that was run, available at: /tmp/brian_script_iiilrssl.py Thanks! [brian2]
Traceback (most recent call last):
  File "/home/saurav/Desktop/icas_spinnaker_snntoolbox/neuro_comp_class/tutorials/snntb/snntb.py", line 80, in <module>
    main(config_filepath)
  File "/home/saurav/miniconda3/envs/tf/lib/python3.9/site-packages/snntoolbox/bin/run.py", line 31, in main
    run_pipeline(config)
  File "/home/saurav/miniconda3/envs/tf/lib/python3.9/site-packages/snntoolbox/bin/utils.py", line 127, in run_pipeline
    spiking_model.build(parsed_model, **testset)
  File "/home/saurav/miniconda3/envs/tf/lib/python3.9/site-packages/snntoolbox/simulation/utils.py", line 438, in build
    self.setup_layers(batch_shape)
  File "/home/saurav/miniconda3/envs/tf/lib/python3.9/site-packages/snntoolbox/simulation/utils.py", line 785, in setup_layers
    self.build_convolution(layer)
  File "/home/saurav/miniconda3/envs/tf/lib/python3.9/site-packages/snntoolbox/simulation/target_simulators/brian2_target_sim.py", line 178, in build_convolution
    conns, biases = build_convolution(layer, delay, transpose_kernel)
  File "/home/saurav/miniconda3/envs/tf/lib/python3.9/site-packages/snntoolbox/simulation/utils.py", line 1302, in build_convolution
    weights = convert_kernel(weights)
  File "/home/saurav/miniconda3/envs/tf/lib/python3.9/site-packages/snntoolbox/simulation/utils.py", line 1828, in convert_kernel
    return np.copy(kernel[slices])
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

Config file creation:

# Generate Config file
config = configparser.ConfigParser()
config['paths'] = {
    'path_wd': WORKING_DIR,
    'dataset_path': DATASET_DIR,
    'filename_ann': MODEL_NAME,
    'runlabel': MODEL_NAME+'_'+str(NUM_STEPS_PER_SAMPLE)
}
config['tools'] = {
    'evaluate_ann': True,
    'parse': True,
    'normalize': True,
    'simulate': True
}
config['simulation'] = {
    'simulator': 'brian2',
    'duration': NUM_STEPS_PER_SAMPLE,
    'num_to_test': NUM_TEST_SAMPLES,
    'batch_size': BATCH_SIZE,
    'keras_backend': 'tensorflow'
}
config['output'] = {
    'verbose': 0,
    'plot_vars': {
        'input_image',
        'spiketrains',
        'spikerates',
        'spikecounts',
        'operations',
        'normalization_activations',
        'activations',
        'correlation',
        'v_mem',
        'error_t'
    },
    'overwrite': True
}
# Write the configuration file
config_filepath = os.path.join(WORKING_DIR, 'config')
with open(config_filepath, 'w') as configfile:
    config.write(configfile)