We have an exciting announcement about badges coming in May 2025. Until then, we will temporarily stop issuing new badges for course completions and certifications. However, all completions will be recorded and fulfilled after May 2025.
Embedded Software

Embedded Software

Topics related to SCADE Suite, SCADE Display, SCADE One and more.

SCADE Neural Network Package not compatible with newer keras models

TAGGED: ,

    • mihnea.apostolescu
      Subscriber

      Hello!

      I encountered two issues while I was trying to convert a .keras convolutional neural networked trained and saved using tensorflow 2.16.1. I encountered the error below when I called the method : keras_importer.importer.model_import(loaded_model, "scade_model_path"), where loaded_model was my .keras CNN loaded using the tensorflow method keras_CNN_model = tf.keras.models.load_model(keras_CNN_path, compile=False)

      "ANSYS(R) SCADE(R) - Keras Importer - Version 20220216.16 (C) 2022 ANSYS, Inc. Unauthorized use, distribution, or duplication is prohibited. Importing keras model to scade ... Translation of Conv2D layer: conv2d (id: 2320059174448) 'Conv2D' object has no attribute 'input_shape' Error: Keras Importer execution failure
      c:\Users\\AppData\Local\Programs\Python\Python312\Lib\site-packages\keras\src\layers\core\input_layer.py:25: UserWarning: Argument input_shape is deprecated. Use shape instead. warnings.warn(

      "

      That "1" is printed due to the return in the model_import method of the keras importer, which is part of the SCADE NN Package.

       

      I tried converting a Keras model trained and saved using tensorflow 2.16.1 because of the compatibilities that the Nerual Networks Package described in its documentation :
      However, I tried installing older versions of tensorflow (mostly 2.6.0) on a virtual environment which used python 3.9.4. Tensorflow 2.6.0 was not compatible with Python 3.12 which I used in the begining.

      However, even if I tried installing the 2.6.0 versions of tensorflow and its tensorboard and tensorflow-estimator dependencies, I haven't succeeded in importing the keras importer of the SCADE NN Package afterwards. This is the error I encountered :

      ---------------------------------------------------------------------------
      ImportError                               Traceback (most recent call last)
      Cell In[1], line 5
            3 SCADE_keras_importer_path = os.path.join(os.getcwd(), 'ACT_Neural_Networks_Package_v1', 'src')
            4 sys.path.append(SCADE_keras_importer_path)
      ----> 5 import keras_importer.importer as ki
            6 import tensorflow as tf
       
      File c:\\SCADE implementation\ACT_Neural_Networks_Package_v1\src\keras_importer\importer.py:101
           96     return opts
           98 ###########################################################################
           99 # Model translation
      --> 101 def import_model(model:tf.keras.Model) -> scade.Model:
          102     # initialize project from template
          103     dir = module.opts['target_dir']
          104     res_dir = pathlib.Path(script_dir) / ".." / ".." / "resources" / "keras_importer"
       
      File c:\\scade_environment\lib\site-packages\tensorflow\python\util\lazy_loader.py:62, in LazyLoader.__getattr__(self, item)
           61 def __getattr__(self, item):
      ---> 62   module = self._load()
           63   return getattr(module, item)
       
      File c:\\scade_environment\lib\site-packages\tensorflow\python\util\lazy_loader.py:45, in LazyLoader._load(self)
           43 """Load the module and insert it into the parent's globals."""
           44 # Import the target module and insert it into the parent's namespace
      ---> 45 module = importlib.import_module(self.__name__)
           46 self._parent_module_globals[self._local_name] = module
           48 # Emit a warning if one was specified
       
      File C:\Python39\lib\importlib\__init__.py:127, in import_module(name, package)
          125             break
          126         level += 1
      --> 127 return _bootstrap._gcd_import(name[level:], package, level)
       
      File c:\\scade_environment\lib\site-packages\keras\__init__.py:3
            1 """AUTOGENERATED. DO NOT EDIT."""
      ----> 3 from keras import __internal__
            4 from keras import activations
            5 from keras import applications
       
      File c:\\scade_environment\lib\site-packages\keras\__internal__\__init__.py:3
            1 """AUTOGENERATED. DO NOT EDIT."""
      ----> 3 from keras.__internal__ import backend
            4 from keras.__internal__ import layers
            5 from keras.__internal__ import losses
       
      File c:\\scade_environment\lib\site-packages\keras\__internal__\backend\__init__.py:3
            1 """AUTOGENERATED. DO NOT EDIT."""
      ----> 3 from keras.src.backend import _initialize_variables as initialize_variables
            4 from keras.src.backend import track_variable
       
      File c:\\scade_environment\lib\site-packages\keras\src\__init__.py:21
            1 # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
            2 #
            3 # Licensed under the Apache License, Version 2.0 (the "License");
         (...)
           13 # limitations under the License.
           14 # ==============================================================================
           15 """Implementation of the Keras API, the high-level API of TensorFlow.
           16 
           17 Detailed documentation and user guides are available at
           18 [keras.io](https://keras.io).
           19 """
      ---> 21 from keras.src import applications
           22 from keras.src import distribute
           23 from keras.src import models
       
      File c:\\scade_environment\lib\site-packages\keras\src\applications\__init__.py:18
            1 # Copyright 2016 The TensorFlow Authors. All Rights Reserved.
            2 #
            3 # Licensed under the Apache License, Version 2.0 (the "License");
         (...)
           13 # limitations under the License.
           14 # ==============================================================================
           15 """Keras Applications are premade architectures with pre-trained weights."""
      ---> 18 from keras.src.applications.convnext import ConvNeXtBase
           19 from keras.src.applications.convnext import ConvNeXtLarge
           20 from keras.src.applications.convnext import ConvNeXtSmall
       
      File c:\\scade_environment\lib\site-packages\keras\src\applications\convnext.py:28
           25 import numpy as np
           26 import tensorflow.compat.v2 as tf
      ---> 28 from keras.src import backend
           29 from keras.src import initializers
           30 from keras.src import layers
       
      File c:\\scade_environment\lib\site-packages\keras\src\backend.py:34
           32 from keras.src import backend_config
           33 from keras.src.distribute import distribute_coordinator_utils as dc
      ---> 34 from keras.src.dtensor import dtensor_api as dtensor
           35 from keras.src.engine import keras_tensor
           36 from keras.src.utils import control_flow_util
       
      File c:\\scade_environment\lib\site-packages\keras\src\dtensor\__init__.py:18
            1 # Copyright 2022 The TensorFlow Authors. All Rights Reserved.
            2 #
            3 # Licensed under the Apache License, Version 2.0 (the "License");
         (...)
           13 # limitations under the License.
           14 # ==============================================================================
           15 """Keras' DTensor library."""
      ---> 18 from tensorflow.compat.v2.experimental import dtensor as dtensor_api
       
      ImportError: cannot import name 'dtensor' from 'tensorflow.compat.v2.experimental' (c:\\scade_environment\lib\site-packages\tensorflow\_api\v2\compat\v2\experimental\__init__.py)
       
       
      Could you please help me solve these issues and find a solution for using the keras importer and the python wrapper included in the SCADE NN Package? 
      Thank you in advance!
    • Romain Andrieux
      Ansys Employee

      Hello,

      Indeed the SCADE Neural Network Package is not updated to work with the latest verion of Keras & Tensorflow.

      I could make it work with the following environment:

      • Python 3.11
      • Keras 2.12.0
      • Tensorflow 2.12.0

      Try to run the exemple lenet.py to see if your environment is correct.

      Note: to make the exemple lenet.py work, you'll need to download all the training data from:

      https://github.com/golbin/TensorFlow-MNIST/tree/master/mnist/data

      into examples/lenet/data folder, and comment out lines 134 & 135.

      Let me know if that solves your issue.

      Regards 

      Romain

      • mihnea.apostolescu
        Subscriber

        Hello Romain,

         

        Thank you for your answer. Unfortunately, the LeNet example didn't work with none of 2.16.1, 2.12.0 or 2.6.0 tensorflow versions.

        This is the output I received in the environment I installed tensorflow 2.16.1 on :

        WARNING:absl:You are saving your model as an HDF5 file via model.save() or keras.saving.save_model(model). This file format is considered legacy. We recommend using instead the native Keras format, e.g. model.save('my_model.keras') or keras.saving.save_model(model, 'my_model.keras').
        Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
        11490434/11490434 ━━━━━━━━━━━━━━━━━━━━ 1s 0us/step 
        channels_last
        x_train shape: (60000, 28, 28, 1)
        60000 train samples
        10000 test samples
        Traceback (most recent call last):
          File "\SCADE implementation\ACT_Neural_Networks_Package_v1\examples\lenet\lenet.py", line 147, in
            lenet.step2_train()
          File "\SCADE implementation\ACT_Neural_Networks_Package_v1\examples\lenet\lenet.py", line 89, in step2_train
            y_train = keras.utils.np_utils.to_categorical(y_train, self.num_classes)
                      ^^^^^^^^^^^^^^^^^^^^
        AttributeError: module 'keras.utils' has no attribute 'np_utils'

        This is the output I received in the environment I installed tensorflow 2.12.0 on :

        (The command I used to install it in order to satisfy all the necessary dependencies is this : pip install "tensorflow==2.12.0" "tensorboard==2.12.0" "tensorflow-estimator==2.12.0" "protobuf==4.0.0rc2" "numpy==1.22.4")

        PS \SCADE implementation\ACT_Neural_Networks_Package_v1\examples\lenet> python .\lenet.py
        Traceback (most recent call last):
          File "\SCADE implementation\ACT_Neural_Networks_Package_v1\examples\lenet\lenet.py", line 10, in
            import keras
          File "\scade_environment\lib\site-packages\keras\__init__.py", line 21, in        
            from keras import models
          File "\scade_environment\lib\site-packages\keras\models\__init__.py", line 18, in  
            from keras.engine.functional import Functional
          File "\scade_environment\lib\site-packages\keras\engine\functional.py", line 24, in
            import tensorflow.compat.v2 as tf
          File "\scade_environment\lib\site-packages\tensorflow\__init__.py", line 37, in   
            from tensorflow.python.tools import module_util as _module_util
          File "\scade_environment\lib\site-packages\tensorflow\python\__init__.py", line 37, in
            from tensorflow.python.eager import context
          File "\scade_environment\lib\site-packages\tensorflow\python\eager\context.py", line 28, in
            from tensorflow.core.framework import function_pb2
          File "\scade_environment\lib\site-packages\tensorflow\core\framework\function_pb2.py", line 5, in
            from google.protobuf.internal import builder as _builder
        ImportError: cannot import name 'builder' from 'google.protobuf.internal' (\scade_environment\lib\site-packages\google\protobuf\internal\__init__.py)

        This is the output I received in the environment I installed tensorflow 2.6.0 on :

        (scade_environment) PS \SCADE implementation\ACT_Neural_Networks_Package_v1\examples\lenet> python .\lenet.py
        2025-04-02 22:35:49.653427: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
        2025-04-02 22:35:49.654636: 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.
        Traceback (most recent call last):
          File "\SCADE implementation\ACT_Neural_Networks_Package_v1\examples\lenet\lenet.py", line 10, in
            import keras
          File "\scade_environment\lib\site-packages\keras\__init__.py", line 3, in
            from keras import __internal__
          File "\scade_environment\lib\site-packages\keras\__internal__\__init__.py", line 3, in
            from keras.__internal__ import backend
          File "\scade_environment\lib\site-packages\keras\__internal__\backend\__init__.py", line 3, in
            from keras.src.backend import _initialize_variables as initialize_variables
          File "\scade_environment\lib\site-packages\keras\src\__init__.py", line 21, in    
            from keras.src import applications
          File "\scade_environment\lib\site-packages\keras\src\applications\__init__.py", line 18, in
            from keras.src.applications.convnext import ConvNeXtBase
          File "\scade_environment\lib\site-packages\keras\src\applications\convnext.py", line 28, in
            from keras.src import backend
          File "\scade_environment\lib\site-packages\keras\src\backend.py", line 34, in     
            from keras.src.dtensor import dtensor_api as dtensor
          File "\scade_environment\lib\site-packages\keras\src\dtensor\__init__.py", line 18, in
            from tensorflow.compat.v2.experimental import dtensor as dtensor_api
        ImportError: cannot import name 'dtensor' from 'tensorflow.compat.v2.experimental' (\scade_environment\lib\site-packages\tensorflow\_api\v2\compat\v2\experimental\__init__.py)

         

        Could you help me with this? Thank you!

Viewing 1 reply thread
  • You must be logged in to reply to this topic.