NeuromorphicProcessorProject / snntoolbox_applications

Collection of Spiking Neural Network applications for SNN Toolbox.
MIT License
30 stars 14 forks source link

Shape issue converting ann to snn #4

Closed mantier closed 3 years ago

mantier commented 3 years ago

Hi,

I am having trouble to convert a very simple model for Loihi. I do not see how it is different from your mnist example and I do not know how to tackle this. Could you please help?

The model is

def createKerasNetworkModel():
    inputs = tf.keras.Input(
        shape=(390,),
        name="inputs_layer",
        )

    char_0_layer = tf.keras.layers.Dense(
        units=256,
        activation=tf.nn.relu,
        name="char_0_layer",
    )
    char_0 = char_0_layer(inputs)

    char_1_layer = tf.keras.layers.Dense(
        units=256,
        activation=tf.nn.relu,
        name="char_1_layer",
    )
    char_1 = char_1_layer(char_0)

    outputs_layer = tf.keras.layers.Dense(
        units=29,
        activation='softmax',
        name="outputs_layer",
    )
    outputs = outputs_layer(char_1)

    model = tf.keras.Model(inputs=inputs, outputs=outputs)
    model.summary()
    return {
        "model": model,
        "layers": {
            "inputs": inputs,
            "char_0": char_0,
            "char_1": char_1,
            "outputs": outputs,
        },
    }

Training with keras works without any problem. However, converting it returns a shape error. I must admit, the configuration is still a black-box for me. So here is the configuration I copied from a tutorial and the function with which I start the conversion:


def gen_config_file(working_dir, config_filepath, dataset_filepath, model_filepath,
                    reset_mode='soft', max_num_cores_per_chip=128,
                    num_test_samples=1, duration=512):
    """Generates a SNN-TB config file to convert a pre-trained ANN into an SNN.

    :returns Path to generated SNN-TB config file.
    """

    import os

    # Define configuration for ANN to SNN conversion
    import configparser
    config = configparser.ConfigParser()

    config['paths'] = {
        # Path where log get stored and where ANN is expected
        'path_wd': working_dir,
        'dataset_path': dataset_filepath,                   # Path to data set
        'filename_ann': model_filepath,                    # Name of model .h5 file in path_wd
        # Name of directory where results of a single run get stored
        'runlabel': "_".join([model_filepath, reset_mode])
    }

    config['tools'] = {
        'evaluate_ann': True,  # Test ANN on dataset before conversion
        'parse': True,        # Parses input model
        'normalize': False    # Not needed for Loihi backend -> Do we need that?
    }

    config['simulation'] = {
        'simulator': 'loihi',            # Selects Loihi as backend of SNN-TB
        'duration': duration,            # Number of time steps to run each sample
        'num_to_test': num_test_samples,  # How many samples to run
        # Batch size 1 is the only supported value for Loihi backend
        'batch_size': 1,
        'keras_backend': 'tensorflow'}

    config['restrictions'] = {
        # Currently supported layers
        'spiking_layers': {'Dense', 'Conv2D', 'MaxPooling2D', 'AveragePooling2D', 'DepthwiseConv2D'}
    }

    config['loihi'] = {
        'validate_partitions': False,  # Validates correctness of compiled layers
        'save_output': False,          # Plots compiler information
        'use_reset_snip': True,        # Using snip accelerates reset between samples
        # Estimate overflow of dendritic accumulator.
        'do_overflow_estimate': True,
        # Tune thresholds to optimal dynamic range.
        'normalize_thresholds': True,
        # Scaling factor between max input and threshold
        'desired_threshold_to_input_ratio': 2**3 if reset_mode == 'hard' else 1,
        # Arguments passed to NxTF layer
        'compartment_kwargs': {'biasExp': 6, 'vThMant': 512},
        # Arguments passed to NxTF layer
        'connection_kwargs': {'numWeightBits': 8, 'weightExponent': 0, 'numBiasBits': 12},
        # reset_mode can be 'soft'/'hard' for reset-by-subtraction (more accurate) or reset-to-zero
        'reset_mode': reset_mode,
        # Limites the maximum number of usale cores per chip
        'num_cores_per_chip': max_num_cores_per_chip,
        # Saturation level of ReLU function used in ANN
        'saturation': 6,
        # Create histrams of weights/biases of converted SNN
        'plot_histograms': False,
    }

    # Generate config file
    #config_filepath = os.path.join(working_dir, 'config')
    with open(config_filepath, 'w') as configfile:
        config.write(configfile)

    return config_filepath

def convert_model(config_file_path):
    """Converts ANN into SNN given configuration in <config_file_path>."""

    from snntoolbox.bin.run import main

    main(config_file_path)

Here's the complete output on the console, including training. Any idea what the reason is?

gerrit@CMTCL0X48258510:/c/0_localStorage/NMC/src/neuromorphic-computing/kws_SNN-TB_Loihi$ /usr/bin/python3 /c/0_localStorage/NMC/src/neuromorphic-computing/kws_SNN-TB_Loihi/main.py
2020-12-20 12:31:51.133724: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2020-12-20 12:31:51.133866: E tensorflow/stream_executor/cuda/cuda_driver.cc:313] failed call to cuInit: UNKNOWN ERROR (303)
2020-12-20 12:31:51.133921: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (CMTCL0X48258510): /proc/driver/nvidia/version does not exist
2020-12-20 12:31:51.134424: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-12-20 12:31:51.148448: I tensorflow/core/platform/profile_utils/cpu_utils.cc:102] CPU Frequency: 2712000000 Hz
2020-12-20 12:31:51.151613: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7f4e00000b20 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-12-20 12:31:51.151673: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
inputs_layer (InputLayer)    [(None, 390)]             0         
_________________________________________________________________
char_0_layer (Dense)         (None, 256)               100096    
_________________________________________________________________
char_1_layer (Dense)         (None, 256)               65792     
_________________________________________________________________
outputs_layer (Dense)        (None, 29)                7453      
=================================================================
Total params: 173,341
Trainable params: 173,341
Non-trainable params: 0
_________________________________________________________________
3750/3750 [==============================] - 8s 2ms/step - loss: 2.7631 - accuracy: 0.6617 - val_loss: 2.7505 - val_accuracy: 0.6744
Initializing loihi simulator...

Loading data set from '.npz' files in /c/0_localStorage/NMC/src/neuromorphic-computing/data.

Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
inputs_layer (InputLayer)    [(None, 390)]             0         
_________________________________________________________________
char_0_layer (Dense)         (None, 256)               100096    
_________________________________________________________________
char_1_layer (Dense)         (None, 256)               65792     
_________________________________________________________________
outputs_layer (Dense)        (None, 29)                7453      
=================================================================
Total params: 173,341
Trainable params: 173,341
Non-trainable params: 0
_________________________________________________________________
Evaluating input model on 100 samples...
Top-1 accuracy: 91.00%
Top-5 accuracy: 100.00%

Parsing input model...
Skipping layer InputLayer.
Parsing layer Dense.
Using activation relu.
Parsing layer Dense.
Using activation relu.
Parsing layer Dense.
Using activation relu.

Building parsed model...

Compiling parsed model...

Model: "model_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
inputs_layer (InputLayer)    [(1, 390)]                0         
_________________________________________________________________
0Dense_256 (Dense)           (1, 256)                  100096    
_________________________________________________________________
1Dense_256 (Dense)           (1, 256)                  65792     
_________________________________________________________________
2Dense_29 (Dense)            (1, 29)                   7453      
=================================================================
Total params: 173,341
Trainable params: 173,341
Non-trainable params: 0
_________________________________________________________________
Evaluating parsed model on 100 samples...
Traceback (most recent call last):
  File "/c/0_localStorage/NMC/src/neuromorphic-computing/kws_SNN-TB_Loihi/main.py", line 71, in <module>
    ANN_to_SNN.convert_model(config_filepath)
  File "/c/0_localStorage/NMC/src/neuromorphic-computing/kws_SNN-TB_Loihi/ANN_to_SNN.py", line 89, in convert_model
    main(config_file_path)
  File "/home/gerrit/.local/lib/python3.6/site-packages/snntoolbox/bin/run.py", line 31, in main
    run_pipeline(config)
  File "/home/gerrit/.local/lib/python3.6/site-packages/snntoolbox/bin/utils.py", line 102, in run_pipeline
    num_to_test, **testset)
  File "/home/gerrit/.local/lib/python3.6/site-packages/snntoolbox/parsing/utils.py", line 860, in evaluate
    verbose=0)
  File "/home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1081, in evaluate
    tmp_logs = test_function(iterator)
  File "/home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 580, in __call__
    result = self._call(*args, **kwds)
  File "/home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 627, in _call
    self._initialize(args, kwds, add_initializers_to=initializers)
  File "/home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 506, in _initialize
    *args, **kwds))
  File "/home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 2446, in _get_concrete_function_internal_garbage_collected
    graph_function, _, _ = self._maybe_define_function(args, kwargs)
  File "/home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 2777, in _maybe_define_function
    graph_function = self._create_graph_function(args, kwargs)
  File "/home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/eager/function.py", line 2667, in _create_graph_function
    capture_by_value=self._capture_by_value),
  File "/home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/framework/func_graph.py", line 981, in func_graph_from_py_func
    func_outputs = python_func(*func_args, **func_kwargs)
  File "/home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py", line 441, in wrapped_fn
    return weak_wrapped_fn().__wrapped__(*args, **kwds)
  File "/home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/framework/func_graph.py", line 968, in wrapper
    raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

    /home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:941 test_function  *
        outputs = self.distribute_strategy.run(
    /home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:951 run  **
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
        return fn(*args, **kwargs)
    /home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:912 test_step  **
        y, y_pred, sample_weight, regularization_losses=self.losses)
    /home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/compile_utils.py:205 __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    /home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/keras/losses.py:143 __call__
        losses = self.call(y_true, y_pred)
    /home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/keras/losses.py:246 call
        return self.fn(y_true, y_pred, **self._fn_kwargs)
    /home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/keras/losses.py:1527 categorical_crossentropy
        return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)
    /home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/keras/backend.py:4561 categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)
    /home/gerrit/.local/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py:1117 assert_is_compatible_with
        raise ValueError("Shapes %s and %s are incompatible" % (self, other))

    ValueError: Shapes (1, 1) and (1, 29) are incompatible
mantier commented 3 years ago

Sorry, I just realized I opened the issue in the wrong repo. I have moved it here: https://github.com/NeuromorphicProcessorProject/snn_toolbox/issues/79