Closed sauravtii closed 1 year ago
Can you replace the last activation layer by,
and see if it helps?
It didn't, facing the same error again.
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
Cell In [12], line 7
2 from spkeras.spkeras.models import cnn_to_snn
4 #Current normalisation using cnn_to_snn
5 ##Default: signed_bit=0, amp_factor=100, method=1, epsilon = 0.001
----> 7 snn_model = cnn_to_snn(signed_bit=0)(cnn_model,x_train)
File /opt/ml_team_data/saurav/new/saurav/vgg16/SpKeras/spkeras/spkeras/models.py:29, in cnn_to_snn.__call__(self, mdl, x_train)
27 self.use_bias = use_bias
28 self.get_config()
---> 29 self.model = self.convert(mdl,x_train,
30 thresholding = self.thresholding,
31 scaling_factor = self.scaling_factor,
32 method = self.method,
33 timesteps=self.timesteps)
35 return self
File /opt/ml_team_data/saurav/new/saurav/vgg16/SpKeras/spkeras/spkeras/models.py:169, in cnn_to_snn.convert(self, mdl, x_train, thresholding, scaling_factor, method, timesteps)
167 new_mdl['config']['layers'] = layers
168 new_mdl = json.dumps(new_mdl)
--> 169 new_model = model_from_json(new_mdl,
170 custom_objects={'SpikeActivation':SpikeActivation})
171 input_shape = model.layers[0].input_shape
172 #new_model.build(input_shape)
173 #new_model = keras.Model(inputs=inputs, outputs=outputs)
File ~/.local/lib/python3.8/site-packages/keras/saving/model_config.py:109, in model_from_json(json_string, custom_objects)
86 """Parses a JSON model configuration string and returns a model instance.
87
88 Usage:
(...)
103 A Keras model instance (uncompiled).
104 """
105 from keras.layers import (
106 deserialize_from_json,
107 )
--> 109 return deserialize_from_json(json_string, custom_objects=custom_objects)
File ~/.local/lib/python3.8/site-packages/keras/layers/serialization.py:272, in deserialize_from_json(json_string, custom_objects)
266 populate_deserializable_objects()
267 config = json_utils.decode_and_deserialize(
268 json_string,
269 module_objects=LOCAL.ALL_OBJECTS,
270 custom_objects=custom_objects,
271 )
--> 272 return deserialize(config, custom_objects)
File ~/.local/lib/python3.8/site-packages/keras/layers/serialization.py:249, in deserialize(config, custom_objects)
212 """Instantiates a layer from a config dictionary.
213
214 Args:
(...)
246 ```
247 """
248 populate_deserializable_objects()
--> 249 return generic_utils.deserialize_keras_object(
250 config,
251 module_objects=LOCAL.ALL_OBJECTS,
252 custom_objects=custom_objects,
253 printable_module_name="layer",
254 )
File ~/.local/lib/python3.8/site-packages/keras/utils/generic_utils.py:734, in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
731 custom_objects = custom_objects or {}
733 if "custom_objects" in arg_spec.args:
--> 734 deserialized_obj = cls.from_config(
735 cls_config,
736 custom_objects=dict(
737 list(_GLOBAL_CUSTOM_OBJECTS.items())
738 + list(_THREAD_LOCAL_CUSTOM_OBJECTS.__dict__.items())
739 + list(custom_objects.items())
740 ),
741 )
742 else:
743 with CustomObjectScope(custom_objects):
File ~/.local/lib/python3.8/site-packages/keras/engine/training.py:3034, in Model.from_config(cls, config, custom_objects)
3027 functional_model_keys = [
3028 "name",
3029 "layers",
3030 "input_layers",
3031 "output_layers",
3032 ]
3033 if all(key in config for key in functional_model_keys):
-> 3034 inputs, outputs, layers = functional.reconstruct_from_config(
3035 config, custom_objects
3036 )
3037 model = cls(
3038 inputs=inputs, outputs=outputs, name=config.get("name")
3039 )
3040 functional.connect_ancillary_layers(model, layers)
File ~/.local/lib/python3.8/site-packages/keras/engine/functional.py:1510, in reconstruct_from_config(config, custom_objects, created_layers)
1508 for layer_data in tf.nest.flatten(output_layers):
1509 layer_name, node_index, tensor_index = layer_data.as_list()
-> 1510 assert layer_name in created_layers
1511 layer = created_layers[layer_name]
1512 node_index = get_node_index(layer, node_index)
AssertionError:
Model architecture:
input_shape = (32, 32, 3)
input_layer = Input(input_shape)
layer = Conv2D(filters=4,
kernel_size=(1, 1),
strides=(1, 1),
padding="same")(input_layer)
layer=Activation('relu')(layer)
# 1
layer = Conv2D(filters=64,
kernel_size=(3, 3),
strides=(1, 1),
padding="same")(input_layer)
layer=Activation('relu')(layer)
# 2
layer = Conv2D(filters=64,
kernel_size=(3, 3),
strides=(1, 1),
padding="same")(layer)
layer=Activation('relu')(layer)
# Using Conv layer as a pooling layer
layer = Conv2D(filters=64,
kernel_size=(3, 3),
strides=(2, 2),
padding="same")(layer)
layer=Activation('relu')(layer)
# 3
layer = Conv2D(filters=128,
kernel_size=(3, 3),
strides=(1, 1),
padding="same")(layer)
layer=Activation('relu')(layer)
# 4
layer = Conv2D(filters=128,
kernel_size=(3, 3),
strides=(1, 1),
padding="same")(layer)
layer=Activation('relu')(layer)
# Using Conv layer as a pooling layer
layer = Conv2D(filters=128,
kernel_size=(3, 3),
strides=(2, 2),
padding="same")(layer)
layer=Activation('relu')(layer)
# 5
layer = Conv2D(filters=256,
kernel_size=(3, 3),
strides=(1, 1),
padding="same")(layer)
layer=Activation('relu')(layer)
# 6
layer = Conv2D(filters=256,
kernel_size=(3, 3),
strides=(1, 1),
padding="same")(layer)
layer=Activation('relu')(layer)
# 7
layer = Conv2D(filters=256,
kernel_size=(3, 3),
strides=(1, 1),
padding="same")(layer)
layer=Activation('relu')(layer)
# Using Conv layer as a pooling layer
layer = Conv2D(filters=256,
kernel_size=(3, 3),
strides=(2, 2),
padding="same")(layer)
layer=Activation('relu')(layer)
# 8
layer = Conv2D(filters=512,
kernel_size=(3, 3),
strides=(1, 1),
padding="same")(layer)
layer=Activation('relu')(layer)
# 9
layer = Conv2D(filters=512,
kernel_size=(3, 3),
strides=(1, 1),
padding="same")(layer)
layer=Activation('relu')(layer)
# 10
layer = Conv2D(filters=512,
kernel_size=(3, 3),
strides=(1, 1),
padding="same")(layer)
layer=Activation('relu')(layer)
# Using Conv layer as a pooling layer
layer = Conv2D(filters=512,
kernel_size=(3, 3),
strides=(2, 2),
padding="same")(layer)
layer=Activation('relu')(layer)
# 11
layer = Conv2D(filters=512,
kernel_size=(3, 3),
strides=(1, 1),
padding="same")(layer)
layer=Activation('relu')(layer)
# 12
layer = Conv2D(filters=512,
kernel_size=(3, 3),
strides=(1, 1),
padding="same")(layer)
layer=Activation('relu')(layer)
# 13
layer = Conv2D(filters=512,
kernel_size=(3, 3),
strides=(1, 1),
padding="same")(layer)
layer=Activation('relu')(layer)
# Using Conv layer as a pooling layer
layer = Conv2D(filters=512,
kernel_size=(3, 3),
strides=(2, 2),
padding="same")(layer)
layer=Activation('relu')(layer)
layer = Flatten()(layer)
layer = Dense(units=512)(layer)
layer=Activation('relu')(layer)
layer = Dense(units=10)(layer)
layer=Activation('Softmax')(layer)
model = Model(input_layer, layer)
What's the tensorflow version you using?
2.10.0
You may need downgrade the version.
SpKeras was originally tested in tensorflow 2.3.1. I also tested it with version below 2.5 and it works fine.
I did downgrade the tensorflow version to 2.3.1 but facing the same issue:
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
Cell In [16], line 7
2 from spkeras.spkeras.models import cnn_to_snn
4 #Current normalisation using cnn_to_snn
5 ##Default: signed_bit=0, amp_factor=100, method=1, epsilon = 0.001
----> 7 snn_model = cnn_to_snn(signed_bit=0)(cnn_model,x_train)
File /opt/ml_team_data/saurav/new/saurav/vgg16/SpKeras/spkeras/spkeras/models.py:29, in cnn_to_snn.__call__(self, mdl, x_train)
27 self.use_bias = use_bias
28 self.get_config()
---> 29 self.model = self.convert(mdl,x_train,
30 thresholding = self.thresholding,
31 scaling_factor = self.scaling_factor,
32 method = self.method,
33 timesteps=self.timesteps)
35 return self
File /opt/ml_team_data/saurav/new/saurav/vgg16/SpKeras/spkeras/spkeras/models.py:169, in cnn_to_snn.convert(self, mdl, x_train, thresholding, scaling_factor, method, timesteps)
167 new_mdl['config']['layers'] = layers
168 new_mdl = json.dumps(new_mdl)
--> 169 new_model = model_from_json(new_mdl,
170 custom_objects={'SpikeActivation':SpikeActivation})
171 input_shape = model.layers[0].input_shape
172 #new_model.build(input_shape)
173 #new_model = keras.Model(inputs=inputs, outputs=outputs)
File ~/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/model_config.py:122, in model_from_json(json_string, custom_objects)
120 config = json.loads(json_string)
121 from tensorflow.python.keras.layers import deserialize # pylint: disable=g-import-not-at-top
--> 122 return deserialize(config, custom_objects=custom_objects)
File ~/.local/lib/python3.8/site-packages/tensorflow/python/keras/layers/serialization.py:171, in deserialize(config, custom_objects)
160 """Instantiates a layer from a config dictionary.
161
162 Arguments:
(...)
168 Layer instance (may be Model, Sequential, Network, Layer...)
169 """
170 populate_deserializable_objects()
--> 171 return generic_utils.deserialize_keras_object(
172 config,
173 module_objects=LOCAL.ALL_OBJECTS,
174 custom_objects=custom_objects,
175 printable_module_name='layer')
File ~/.local/lib/python3.8/site-packages/tensorflow/python/keras/utils/generic_utils.py:354, in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
351 custom_objects = custom_objects or {}
353 if 'custom_objects' in arg_spec.args:
--> 354 return cls.from_config(
355 cls_config,
356 custom_objects=dict(
357 list(_GLOBAL_CUSTOM_OBJECTS.items()) +
358 list(custom_objects.items())))
359 with CustomObjectScope(custom_objects):
360 return cls.from_config(cls_config)
File ~/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py:616, in Functional.from_config(cls, config, custom_objects)
600 @classmethod
601 def from_config(cls, config, custom_objects=None):
602 """Instantiates a Model from its config (output of `get_config()`).
603
604 Arguments:
(...)
614 ValueError: In case of improperly formatted config dict.
615 """
--> 616 input_tensors, output_tensors, created_layers = reconstruct_from_config(
617 config, custom_objects)
618 model = cls(inputs=input_tensors, outputs=output_tensors,
619 name=config.get('name'))
620 connect_ancillary_layers(model, created_layers)
File ~/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py:1233, in reconstruct_from_config(config, custom_objects, created_layers)
1231 for layer_data in nest.flatten(output_layers):
1232 layer_name, node_index, tensor_index = layer_data.as_list()
-> 1233 assert layer_name in created_layers
1234 layer = created_layers[layer_name]
1235 node_index = get_node_index(layer, node_index)
AssertionError:
It could be caused by the version problem, but I cannot reproduce your error.
The package works fine to me, when I followed the setup, tensorflow 2.3.1 and python 3.7.6.
I have a python version greater that 3.7.6. But I don't think that might be the problem.
This is my code: spkeras_vgg16.zip
import sys
print(sys.version)
print(tf.__version__)
--------------------------
3.8.8 (default, Apr 13 2021, 19:58:26)
[GCC 7.3.0]
2.5.0
I tested your code under the above environment, it works well.
I tried with tensorflow 2.5.0 and got the same error again. Below I have attached the entire output along with the error.
2022-11-24 13:55:56.577963: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
2022-11-24 13:55:56.577982: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.12) or chardet (3.0.4) doesn't match a supported version!
warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0]
2.5.0
2022-11-24 13:55:57.683418: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.12) or chardet (3.0.4) doesn't match a supported version!
warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
2022-11-24 13:55:58.366131: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
2022-11-24 13:55:58.366179: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcublas.so.11'; dlerror: libcublas.so.11: cannot open shared object file: No such file or directory
2022-11-24 13:55:58.366206: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcublasLt.so.11'; dlerror: libcublasLt.so.11: cannot open shared object file: No such file or directory
2022-11-24 13:55:58.366233: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcufft.so.10'; dlerror: libcufft.so.10: cannot open shared object file: No such file or directory
2022-11-24 13:55:58.366259: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcurand.so.10'; dlerror: libcurand.so.10: cannot open shared object file: No such file or directory
2022-11-24 13:55:58.366286: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcusolver.so.11'; dlerror: libcusolver.so.11: cannot open shared object file: No such file or directory
2022-11-24 13:55:58.366312: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcusparse.so.11'; dlerror: libcusparse.so.11: cannot open shared object file: No such file or directory
2022-11-24 13:55:58.366339: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudnn.so.8'; dlerror: libcudnn.so.8: cannot open shared object file: No such file or directory
2022-11-24 13:55:58.366345: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1766] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2022-11-24 13:55:58.572707: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library libcuda.so.1
2022-11-24 13:55:58.574715: E tensorflow/stream_executor/cuda/cuda_driver.cc:328] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected
2022-11-24 13:55:58.574733: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: TII-SPAWAR01
2022-11-24 13:55:58.574737: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: TII-SPAWAR01
2022-11-24 13:55:58.574797: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:200] libcuda reported version is: 515.65.1
2022-11-24 13:55:58.574811: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:204] kernel reported version is: 515.65.1
2022-11-24 13:55:58.574814: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:310] kernel version seems to match DSO: 515.65.1
[]
2.5.0
2022-11-24 13:55:59.133046: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 32, 32, 3)] 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 32, 32, 64) 1792
_________________________________________________________________
activation_1 (Activation) (None, 32, 32, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 32, 32, 64) 36928
_________________________________________________________________
activation_2 (Activation) (None, 32, 32, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 16, 16, 64) 36928
_________________________________________________________________
activation_3 (Activation) (None, 16, 16, 64) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 16, 16, 128) 73856
_________________________________________________________________
activation_4 (Activation) (None, 16, 16, 128) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 16, 16, 128) 147584
_________________________________________________________________
activation_5 (Activation) (None, 16, 16, 128) 0
_________________________________________________________________
conv2d_6 (Conv2D) (None, 8, 8, 128) 147584
_________________________________________________________________
activation_6 (Activation) (None, 8, 8, 128) 0
_________________________________________________________________
conv2d_7 (Conv2D) (None, 8, 8, 256) 295168
_________________________________________________________________
activation_7 (Activation) (None, 8, 8, 256) 0
_________________________________________________________________
conv2d_8 (Conv2D) (None, 8, 8, 256) 590080
_________________________________________________________________
activation_8 (Activation) (None, 8, 8, 256) 0
_________________________________________________________________
conv2d_9 (Conv2D) (None, 8, 8, 256) 590080
_________________________________________________________________
activation_9 (Activation) (None, 8, 8, 256) 0
_________________________________________________________________
conv2d_10 (Conv2D) (None, 4, 4, 256) 590080
_________________________________________________________________
activation_10 (Activation) (None, 4, 4, 256) 0
_________________________________________________________________
conv2d_11 (Conv2D) (None, 4, 4, 512) 1180160
_________________________________________________________________
activation_11 (Activation) (None, 4, 4, 512) 0
_________________________________________________________________
conv2d_12 (Conv2D) (None, 4, 4, 512) 2359808
_________________________________________________________________
activation_12 (Activation) (None, 4, 4, 512) 0
_________________________________________________________________
conv2d_13 (Conv2D) (None, 4, 4, 512) 2359808
_________________________________________________________________
activation_13 (Activation) (None, 4, 4, 512) 0
_________________________________________________________________
conv2d_14 (Conv2D) (None, 2, 2, 512) 2359808
_________________________________________________________________
activation_14 (Activation) (None, 2, 2, 512) 0
_________________________________________________________________
conv2d_15 (Conv2D) (None, 2, 2, 512) 2359808
_________________________________________________________________
activation_15 (Activation) (None, 2, 2, 512) 0
_________________________________________________________________
conv2d_16 (Conv2D) (None, 2, 2, 512) 2359808
_________________________________________________________________
activation_16 (Activation) (None, 2, 2, 512) 0
_________________________________________________________________
conv2d_17 (Conv2D) (None, 2, 2, 512) 2359808
_________________________________________________________________
activation_17 (Activation) (None, 2, 2, 512) 0
_________________________________________________________________
conv2d_18 (Conv2D) (None, 1, 1, 512) 2359808
_________________________________________________________________
activation_18 (Activation) (None, 1, 1, 512) 0
_________________________________________________________________
flatten (Flatten) (None, 512) 0
_________________________________________________________________
dense (Dense) (None, 512) 262656
_________________________________________________________________
activation_19 (Activation) (None, 512) 0
_________________________________________________________________
dense_1 (Dense) (None, 10) 5130
_________________________________________________________________
activation_20 (Activation) (None, 10) 0
=================================================================
Total params: 20,476,682
Trainable params: 20,476,682
Non-trainable params: 0
_________________________________________________________________
2022-11-24 13:55:59.741497: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:176] None of the MLIR Optimization Passes are enabled (registered 2)
2022-11-24 13:55:59.759633: I tensorflow/core/platform/profile_utils/cpu_utils.cc:114] CPU Frequency: 2496000000 Hz
1407/1407 [==============================] - 725s 515ms/step - loss: 4.7958 - accuracy: 0.1483 - val_loss: 4.7171 - val_accuracy: 0.1804
313/313 [==============================] - 17s 53ms/step - loss: 4.6982 - accuracy: 0.1789
Model: "model"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 32, 32, 3)] 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 32, 32, 64) 1792
_________________________________________________________________
activation_1 (Activation) (None, 32, 32, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 32, 32, 64) 36928
_________________________________________________________________
activation_2 (Activation) (None, 32, 32, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 16, 16, 64) 36928
_________________________________________________________________
activation_3 (Activation) (None, 16, 16, 64) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 16, 16, 128) 73856
_________________________________________________________________
activation_4 (Activation) (None, 16, 16, 128) 0
_________________________________________________________________
conv2d_5 (Conv2D) (None, 16, 16, 128) 147584
_________________________________________________________________
activation_5 (Activation) (None, 16, 16, 128) 0
_________________________________________________________________
conv2d_6 (Conv2D) (None, 8, 8, 128) 147584
_________________________________________________________________
activation_6 (Activation) (None, 8, 8, 128) 0
_________________________________________________________________
conv2d_7 (Conv2D) (None, 8, 8, 256) 295168
_________________________________________________________________
activation_7 (Activation) (None, 8, 8, 256) 0
_________________________________________________________________
conv2d_8 (Conv2D) (None, 8, 8, 256) 590080
_________________________________________________________________
activation_8 (Activation) (None, 8, 8, 256) 0
_________________________________________________________________
conv2d_9 (Conv2D) (None, 8, 8, 256) 590080
_________________________________________________________________
activation_9 (Activation) (None, 8, 8, 256) 0
_________________________________________________________________
conv2d_10 (Conv2D) (None, 4, 4, 256) 590080
_________________________________________________________________
activation_10 (Activation) (None, 4, 4, 256) 0
_________________________________________________________________
conv2d_11 (Conv2D) (None, 4, 4, 512) 1180160
_________________________________________________________________
activation_11 (Activation) (None, 4, 4, 512) 0
_________________________________________________________________
conv2d_12 (Conv2D) (None, 4, 4, 512) 2359808
_________________________________________________________________
activation_12 (Activation) (None, 4, 4, 512) 0
_________________________________________________________________
conv2d_13 (Conv2D) (None, 4, 4, 512) 2359808
_________________________________________________________________
activation_13 (Activation) (None, 4, 4, 512) 0
_________________________________________________________________
conv2d_14 (Conv2D) (None, 2, 2, 512) 2359808
_________________________________________________________________
activation_14 (Activation) (None, 2, 2, 512) 0
_________________________________________________________________
conv2d_15 (Conv2D) (None, 2, 2, 512) 2359808
_________________________________________________________________
activation_15 (Activation) (None, 2, 2, 512) 0
_________________________________________________________________
conv2d_16 (Conv2D) (None, 2, 2, 512) 2359808
_________________________________________________________________
activation_16 (Activation) (None, 2, 2, 512) 0
_________________________________________________________________
conv2d_17 (Conv2D) (None, 2, 2, 512) 2359808
_________________________________________________________________
activation_17 (Activation) (None, 2, 2, 512) 0
_________________________________________________________________
conv2d_18 (Conv2D) (None, 1, 1, 512) 2359808
_________________________________________________________________
activation_18 (Activation) (None, 1, 1, 512) 0
_________________________________________________________________
flatten (Flatten) (None, 512) 0
_________________________________________________________________
dense (Dense) (None, 512) 262656
_________________________________________________________________
activation_19 (Activation) (None, 512) 0
_________________________________________________________________
dense_1 (Dense) (None, 10) 5130
_________________________________________________________________
activation_20 (Activation) (None, 10) 0
=================================================================
Total params: 20,476,682
Trainable params: 20,476,682
Non-trainable params: 0
_________________________________________________________________
{'timesteps': 256, 'thresholding': 0.5, 'amp_factor': 100, 'signed_bit': 0, 'spike_ext': 0, 'epsilon': 0.001, 'use_bias': True, 'scaling_factor': 1, 'noneloss': False, 'method': 1}
Start Converting...
Extracting Lambda...
2/42Activation
4/42Activation
6/42Activation
8/42Activation
10/42Activation
12/42Activation
14/42Activation
16/42Activation
18/42Activation
20/42Activation
22/42Activation
24/42Activation
26/42Activation
28/42Activation
30/42Activation
32/42Activation
34/42Activation
36/42Activation
39/42Activation
41/42Activation
maximum activations: [1.0, 0.5849826, 0.42816296, 0.5129265, 0.8821699, 0.8865767, 0.9058886, 0.74633074, 1.1824455, 0.86154443, 1.7145554, 1.345744, 1.9324017, 11.691247, 7.8629975, 10.439115, 7.9022923, 10.038851, 23.341845, 31.82572, 71.77048]
normalisation factor: [0.5849826, 0.7319243, 1.1979703, 1.7198758, 1.0049955, 1.0217825, 0.8238659, 1.5843452, 0.72861236, 1.9900951, 0.7848939, 1.4359355, 6.0501122, 0.6725542, 1.3276253, 0.75698876, 1.270372, 2.325151, 1.3634621, 2.2551093]
Number of Spiking Layer: 20
threshold: [100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
Note: threshold will be different when weight quantisation applied!
Traceback (most recent call last):
File "spkeras_vgg16 (1).py", line 325, in <module>
snn_model = cnn_to_snn(signed_bit=0)(cnn_model,x_train)
File "/home/sauravpawar/Downloads/spkeras/spkeras/models.py", line 29, in __call__
self.model = self.convert(mdl,x_train,
File "/home/sauravpawar/Downloads/spkeras/spkeras/models.py", line 169, in convert
new_model = model_from_json(new_mdl,
File "/home/sauravpawar/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/model_config.py", line 126, in model_from_json
return deserialize(config, custom_objects=custom_objects)
File "/home/sauravpawar/.local/lib/python3.8/site-packages/tensorflow/python/keras/layers/serialization.py", line 159, in deserialize
return generic_utils.deserialize_keras_object(
File "/home/sauravpawar/.local/lib/python3.8/site-packages/tensorflow/python/keras/utils/generic_utils.py", line 668, in deserialize_keras_object
deserialized_obj = cls.from_config(
File "/home/sauravpawar/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py", line 668, in from_config
input_tensors, output_tensors, created_layers = reconstruct_from_config(
File "/home/sauravpawar/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py", line 1308, in reconstruct_from_config
assert layer_name in created_layers
AssertionError
It seems your tensorflow is not working properly. You need solve the missing files first before you moving to SpKeras.
2022-11-24 13:55:56.577963: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
I did resolve that issue but again getting the same error. Below I have attached the full output.
Resolved tensorflow issues:
AssertionError:
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
Cell In [15], line 7
2 from spkeras.spkeras.models import cnn_to_snn
4 #Current normalisation using cnn_to_snn
5 ##Default: signed_bit=0, amp_factor=100, method=1, epsilon = 0.001
----> 7 snn_model = cnn_to_snn(signed_bit=0)(cnn_model,x_train)
File /opt/ml_team_data/saurav/new/saurav/vgg16/SpKeras/spkeras/spkeras/models.py:29, in cnn_to_snn.__call__(self, mdl, x_train)
27 self.use_bias = use_bias
28 self.get_config()
---> 29 self.model = self.convert(mdl,x_train,
30 thresholding = self.thresholding,
31 scaling_factor = self.scaling_factor,
32 method = self.method,
33 timesteps=self.timesteps)
35 return self
File /opt/ml_team_data/saurav/new/saurav/vgg16/SpKeras/spkeras/spkeras/models.py:169, in cnn_to_snn.convert(self, mdl, x_train, thresholding, scaling_factor, method, timesteps)
167 new_mdl['config']['layers'] = layers
168 new_mdl = json.dumps(new_mdl)
--> 169 new_model = model_from_json(new_mdl,
170 custom_objects={'SpikeActivation':SpikeActivation})
171 input_shape = model.layers[0].input_shape
172 #new_model.build(input_shape)
173 #new_model = keras.Model(inputs=inputs, outputs=outputs)
File ~/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/model_config.py:126, in model_from_json(json_string, custom_objects)
124 config = json_utils.decode(json_string)
125 from tensorflow.python.keras.layers import deserialize # pylint: disable=g-import-not-at-top
--> 126 return deserialize(config, custom_objects=custom_objects)
File ~/.local/lib/python3.8/site-packages/tensorflow/python/keras/layers/serialization.py:159, in deserialize(config, custom_objects)
148 """Instantiates a layer from a config dictionary.
149
150 Args:
(...)
156 Layer instance (may be Model, Sequential, Network, Layer...)
157 """
158 populate_deserializable_objects()
--> 159 return generic_utils.deserialize_keras_object(
160 config,
161 module_objects=LOCAL.ALL_OBJECTS,
162 custom_objects=custom_objects,
163 printable_module_name='layer')
File ~/.local/lib/python3.8/site-packages/tensorflow/python/keras/utils/generic_utils.py:668, in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
665 custom_objects = custom_objects or {}
667 if 'custom_objects' in arg_spec.args:
--> 668 deserialized_obj = cls.from_config(
669 cls_config,
670 custom_objects=dict(
671 list(_GLOBAL_CUSTOM_OBJECTS.items()) +
672 list(custom_objects.items())))
673 else:
674 with CustomObjectScope(custom_objects):
File ~/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py:668, in Functional.from_config(cls, config, custom_objects)
653 """Instantiates a Model from its config (output of `get_config()`).
654
655 Args:
(...)
665 ValueError: In case of improperly formatted config dict.
666 """
667 with generic_utils.SharedObjectLoadingScope():
--> 668 input_tensors, output_tensors, created_layers = reconstruct_from_config(
669 config, custom_objects)
670 model = cls(inputs=input_tensors, outputs=output_tensors,
671 name=config.get('name'))
672 connect_ancillary_layers(model, created_layers)
File ~/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/functional.py:1308, in reconstruct_from_config(config, custom_objects, created_layers)
1306 for layer_data in nest.flatten(output_layers):
1307 layer_name, node_index, tensor_index = layer_data.as_list()
-> 1308 assert layer_name in created_layers
1309 layer = created_layers[layer_name]
1310 node_index = get_node_index(layer, node_index)
AssertionError:
It could be caused by your last layer which is not softmax.
I have updated models.py to fix the problem.
Hi, thanks for the updating models.py. The previous error has been resolved, but now getting a NameError.
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In [18], line 7
2 from spkeras.spkeras.models import cnn_to_snn
4 #Current normalisation using cnn_to_snn
5 ##Default: signed_bit=0, amp_factor=100, method=1, epsilon = 0.001
----> 7 snn_model = cnn_to_snn(signed_bit=0)(cnn_model,x_train)
File /opt/ml_team_data/saurav/new/saurav/vgg16/SpKeras/spkeras/spkeras/models.py:29, in cnn_to_snn.__call__(self, mdl, x_train)
27 self.use_bias = use_bias
28 self.get_config()
---> 29 self.model = self.convert(mdl,x_train,
30 thresholding = self.thresholding,
31 scaling_factor = self.scaling_factor,
32 method = self.method,
33 timesteps=self.timesteps)
35 return self
File /opt/ml_team_data/saurav/new/saurav/vgg16/SpKeras/spkeras/spkeras/models.py:179, in cnn_to_snn.convert(self, mdl, x_train, thresholding, scaling_factor, method, timesteps)
175 #new_model.build(input_shape)
176 #new_model = keras.Model(inputs=inputs, outputs=outputs)
178 m = 0
--> 179 for layer in new_model.layers:
180 layer_type = type(layer).__name__
181 if hasattr(layer, 'activation') and layer_type != 'Activation':
NameError: name 'new_model' is not defined
Did you update your code correctly? new_model is defined in L171.
https://github.com/Dengyu-Wu/spkeras/blob/master/spkeras/models.py#L171
I did make the necessary changes and was able to resolve that issue. But now facing the following issue:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In [17], line 7
2 from spkeras.spkeras.models import cnn_to_snn
4 #Current normalisation using cnn_to_snn
5 ##Default: signed_bit=0, amp_factor=100, method=1, epsilon = 0.001
----> 7 snn_model = cnn_to_snn(signed_bit=0)(cnn_model,x_train)
File /opt/ml_team_data/saurav/new/saurav/vgg16/SpKeras/spkeras/spkeras/models.py:29, in cnn_to_snn.__call__(self, mdl, x_train)
27 self.use_bias = use_bias
28 self.get_config()
---> 29 self.model = self.convert(mdl,x_train,
30 thresholding = self.thresholding,
31 scaling_factor = self.scaling_factor,
32 method = self.method,
33 timesteps=self.timesteps)
35 return self
File /opt/ml_team_data/saurav/new/saurav/vgg16/SpKeras/spkeras/spkeras/models.py:173, in cnn_to_snn.convert(self, mdl, x_train, thresholding, scaling_factor, method, timesteps)
170 new_mdl['config']['output_layers'] = [[inbound_nodes, 0, 0]]
172 # new_mdl = json.dumps(new_mdl)
--> 173 new_model = model_from_json(new_mdl,custom_objects={'SpikeActivation':SpikeActivation})
174 input_shape = model.layers[0].input_shape
175 #new_model.build(input_shape)
176 #new_model = keras.Model(inputs=inputs, outputs=outputs)
File ~/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/model_config.py:124, in model_from_json(json_string, custom_objects)
103 @keras_export('keras.models.model_from_json')
104 def model_from_json(json_string, custom_objects=None):
105 """Parses a JSON model configuration string and returns a model instance.
106
107 Usage:
(...)
122 A Keras model instance (uncompiled).
123 """
--> 124 config = json_utils.decode(json_string)
125 from tensorflow.python.keras.layers import deserialize # pylint: disable=g-import-not-at-top
126 return deserialize(config, custom_objects=custom_objects)
File ~/.local/lib/python3.8/site-packages/tensorflow/python/keras/saving/saved_model/json_utils.py:62, in decode(json_string)
61 def decode(json_string):
---> 62 return json.loads(json_string, object_hook=_decode_helper)
File /opt/conda/envs/test/lib/python3.8/json/__init__.py:341, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
339 else:
340 if not isinstance(s, (bytes, bytearray)):
--> 341 raise TypeError(f'the JSON object must be str, bytes or bytearray, '
342 f'not {s.__class__.__name__}')
343 s = s.decode(detect_encoding(s), 'surrogatepass')
345 if "encoding" in kw:
TypeError: the JSON object must be str, bytes or bytearray, not dict
Can you try the exactly same code as I updated, see if it works?
AssertionError is fixed in updated code, so the issue is closed.
Hi, I did add relu activation layer after each conv2d layer and the previous issue did get resolved. I am facing the following issue.
Issue:
My model architecture:
My model summary: