Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot convert keras model: softmax() got an unexpected keyword argument 'axis' #892

Closed
jeffsaremi opened this issue Sep 22, 2018 · 10 comments

Comments

@jeffsaremi
Copy link

I am following the instructions to convert a resnet50 model word by word. And I get this:

D:\repos\webdnn\resnet>python ..\bin\convert_keras.py resnet50.h5 --input_shape "(1,224,224,3)" --out output --backend webgl
C:\Users\jeff\AppData\Local\Continuum\anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
[convert_keras.py] Generating feedforward graph
2018-09-22 07:53:38.725911: I C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
Traceback (most recent call last):
  File "..\bin\convert_keras.py", line 114, in <module>
    main()
  File "..\bin\convert_keras.py", line 61, in main
    model = keras.models.load_model(args.kerasmodel, custom_objects=custom_objects, compile=False)
  File "C:\Users\jeff\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\saving.py", line 260, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "C:\Users\jeff\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\saving.py", line 334, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "C:\Users\jeff\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\layers\__init__.py", line 55, in deserialize
    printable_module_name='layer')
  File "C:\Users\jeff\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\utils\generic_utils.py", line 145, in deserialize_keras_object
    list(custom_objects.items())))
  File "C:\Users\jeff\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\network.py", line 1027, in from_config
    process_node(layer, node_data)
  File "C:\Users\jeff\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\network.py", line 986, in process_node
    layer(unpack_singleton(input_tensors), **kwargs)
  File "C:\Users\jeff\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\base_layer.py", line 457, in __call__
    output = self.call(inputs, **kwargs)
  File "C:\Users\jeff\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\layers\core.py", line 878, in call
    output = self.activation(output)
  File "C:\Users\jeff\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\activations.py", line 29, in softmax
    return K.softmax(x)
  File "C:\Users\jeff\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 3154, in softmax
    return tf.nn.softmax(x, axis=axis)
TypeError: softmax() got an unexpected keyword argument 'axis'

The model was saved using these instructions:

(base) D:\repos\webdnn\resnet>python
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from keras.applications import resnet50
C:\Users\jeff\AppData\Local\Continuum\anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
>>> model = resnet50.ResNet50(include_top=True, weights='imagenet')
2018-09-22 07:27:17.054697: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5
102858752/102853048 [==============================] - 7s 0us/step
>>> model.save("resnet50.h5")
>>> exit()
@milhidaka
Copy link
Member

It seems version mistmatch between keras and tensorflow. Not related to webdnn.
keras-team/keras#9621
In my environment, keras==2.1.3 and tensorflow==1.7.0, the problem does not occur.

@jeffsaremi
Copy link
Author

here's the results are installing keras==2.1.3 and tensorflow==1.7.0


Using TensorFlow backend.
C:\ProgramData\Anaconda3\lib\site-packages\webdnn-1.2.6-py3.6.egg\webdnn\util\console.py:30: Warning: Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\webdnn-1.2.6-py3.6.egg\webdnn\frontend\tensorflow\converter.py", line 27, in <module>
    raise NotImplementedError(f"WebDNN supports TensorFlow >=v1.2.0,<=v1.4.0 Currently, TensorFlow {tf.VERSION} is installed.")
NotImplementedError: WebDNN supports TensorFlow >=v1.2.0,<=v1.4.0 Currently, TensorFlow 1.7.0 is installed.

  warnings.warn(message, category)
[convert_keras.py] Generating feedforward graph
Traceback (most recent call last):
  File "./bin/convert_keras.py", line 114, in <module>
    main()
  File "./bin/convert_keras.py", line 59, in main
    input_shapes = [Shape.parse(input_shape)[0] for input_shape in args.input_shape]
  File "./bin/convert_keras.py", line 59, in <listcomp>
    input_shapes = [Shape.parse(input_shape)[0] for input_shape in args.input_shape]
  File "C:\ProgramData\Anaconda3\lib\site-packages\webdnn-1.2.6-py3.6.egg\webdnn\graph\shape.py", line 33, in parse
    tmp = ast.literal_eval(normalized_text)
  File "C:\ProgramData\Anaconda3\lib\ast.py", line 48, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "C:\ProgramData\Anaconda3\lib\ast.py", line 35, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 1
    (1,224,224,3)
    ^
IndentationError: unexpected indent

@milhidaka
Copy link
Member

For windows, --input_shape '(1,224,224,3)' is not valid quote. Please try --input_shape (1,224,224,3).

@jeffsaremi
Copy link
Author

Thanks. I got passed that. Now Python crashes with no error message.
Below I have two attempts

(base) C:\Repos\webdnn>python ./bin/convert_keras.py resnet50.h5 --input_shape "(1,224,224,3)" --out output
C:\ProgramData\Anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
C:\ProgramData\Anaconda3\lib\site-packages\webdnn-1.2.6-py3.6.egg\webdnn\util\console.py:30: Warning: Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\webdnn-1.2.6-py3.6.egg\webdnn\frontend\tensorflow\converter.py", line 27, in <module>
    raise NotImplementedError(f"WebDNN supports TensorFlow >=v1.2.0,<=v1.4.0 Currently, TensorFlow {tf.VERSION} is installed.")
NotImplementedError: WebDNN supports TensorFlow >=v1.2.0,<=v1.4.0 Currently, TensorFlow 1.7.0 is installed.

  warnings.warn(message, category)
[convert_keras.py] Generating feedforward graph
2018-09-24 06:34:30.780160: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
[convert_keras.py] Generating graph descriptor
[convert_keras.py] BackendName: webgpu

Changing backend to webgl:

(base) C:\Repos\webdnn>python ./bin/convert_keras.py resnet50.h5 --input_shape "(1,224,224,3)" --out output --backend webgl
C:\ProgramData\Anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
C:\ProgramData\Anaconda3\lib\site-packages\webdnn-1.2.6-py3.6.egg\webdnn\util\console.py:30: Warning: Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\webdnn-1.2.6-py3.6.egg\webdnn\frontend\tensorflow\converter.py", line 27, in <module>
    raise NotImplementedError(f"WebDNN supports TensorFlow >=v1.2.0,<=v1.4.0 Currently, TensorFlow {tf.VERSION} is installed.")
NotImplementedError: WebDNN supports TensorFlow >=v1.2.0,<=v1.4.0 Currently, TensorFlow 1.7.0 is installed.

  warnings.warn(message, category)
[convert_keras.py] Generating feedforward graph
2018-09-24 06:35:26.737246: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
[convert_keras.py] Generating graph descriptor
[convert_keras.py] BackendName: webgl

@jeffsaremi
Copy link
Author

I am trying this whole on a second machine in case. This time I get a different error:

(base) C:\Users\jesaremi>pip show keras
Name: Keras
Version: 2.1.3
Summary: Deep Learning for humans
Home-page: https://github.com/keras-team/keras
Author: Francois Chollet
Author-email: francois.chollet@gmail.com
License: MIT
Location: c:\users\jesaremi\appdata\local\continuum\anaconda3\lib\site-packages
Requires: numpy, pyyaml, six, scipy
Required-by:

(base) C:\Users\jesaremi>pip show tensorflow
Name: tensorflow
Version: 1.7.0
Summary: TensorFlow helps the tensors flow
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: opensource@google.com
License: Apache 2.0
Location: c:\users\jesaremi\appdata\local\continuum\anaconda3\lib\site-packages
Requires: gast, astor, absl-py, six, protobuf, tensorboard, grpcio, numpy, termcolor, wheel
Required-by:

D:\repos\webdnn\resnet>python ..\bin\convert_keras.py resnet50.h5 --input_shape "(1,224,224,3)" --out output
C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\h5py_init_.py:36: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type.
from ._conv import register_converters as _register_converters
Using TensorFlow backend.
C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\webdnn-1.2.6-py3.6.egg\webdnn\util\console.py:30: Warning: Traceback (most recent call last):
File "C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\webdnn-1.2.6-py3.6.egg\webdnn\frontend\tensorflow\converter.py", line 27, in
raise NotImplementedError(f"WebDNN supports TensorFlow >=v1.2.0,<=v1.4.0 Currently, TensorFlow {tf.VERSION} is installed.")
NotImplementedError: WebDNN supports TensorFlow >=v1.2.0,<=v1.4.0 Currently, TensorFlow 1.7.0 is installed.

warnings.warn(message, category)
[convert_keras.py] Generating feedforward graph
Traceback (most recent call last):
File "..\bin\convert_keras.py", line 114, in
main()
File "..\bin\convert_keras.py", line 61, in main
model = keras.models.load_model(args.kerasmodel, custom_objects=custom_objects, compile=False)
File "C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\models.py", line 243, in load_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\models.py", line 317, in model_from_config
return layer_module.deserialize(config, custom_objects=custom_objects)
File "C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\layers_init_.py", line 55, in deserialize
printable_module_name='layer')
File "C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\utils\generic_utils.py", line 143, in deserialize_keras_object
list(custom_objects.items())))
File "C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\topology.py", line 2507, in from_config
process_layer(layer_data)
File "C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\topology.py", line 2493, in process_layer
custom_objects=custom_objects)
File "C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\layers_init_.py", line 55, in deserialize
printable_module_name='layer')
File "C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\utils\generic_utils.py", line 145, in deserialize_keras_object
return cls.from_config(config['config'])
File "C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\topology.py", line 1269, in from_config
return cls(**config)
File "C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\layers\core.py", line 483, in init
super(Flatten, self).init(**kwargs)
File "C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\topology.py", line 292, in init
raise TypeError('Keyword argument not understood:', kwarg)
TypeError: ('Keyword argument not understood:', 'data_format')

@jeffsaremi
Copy link
Author

So on my 2nd machine (the one with data_format error) I upgraded keras to 2.2.2 (following this: https://stackoverflow.com/questions/50830736/typeerror-keyword-argument-not-understood-data-format).
Then I ran the following commands. Each time the folder 'output' contains nothing.


D:\repos\webdnn\resnet>python ..\bin\convert_keras.py resnet50.h5 --input_shape "(1,224,224,3)" --out output
C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\webdnn-1.2.6-py3.6.egg\webdnn\util\console.py:30: Warning: Traceback (most recent call last):
  File "C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\webdnn-1.2.6-py3.6.egg\webdnn\frontend\tensorflow\converter.py", line 27, in <module>
    raise NotImplementedError(f"WebDNN supports TensorFlow >=v1.2.0,<=v1.4.0 Currently, TensorFlow {tf.VERSION} is installed.")
NotImplementedError: WebDNN supports TensorFlow >=v1.2.0,<=v1.4.0 Currently, TensorFlow 1.7.0 is installed.

  warnings.warn(message, category)
[convert_keras.py] Generating feedforward graph
2018-09-24 07:00:54.152445: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
[convert_keras.py] Generating graph descriptor
[convert_keras.py] BackendName: webgpu

D:\repos\webdnn\resnet>dir output
 Volume in drive D is DATADRIVE1
 Volume Serial Number is C6E8-9CEF

 Directory of D:\repos\webdnn\resnet\output

09/22/2018  07:36 AM    <DIR>          .
09/22/2018  07:36 AM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  434,755,878,912 bytes free
D:\repos\webdnn\resnet>python ..\bin\convert_keras.py resnet50.h5 --input_shape "(1,224,224,3)" --out output --backend webgl
C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.
C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\webdnn-1.2.6-py3.6.egg\webdnn\util\console.py:30: Warning: Traceback (most recent call last):
  File "C:\Users\jesaremi\AppData\Local\Continuum\anaconda3\lib\site-packages\webdnn-1.2.6-py3.6.egg\webdnn\frontend\tensorflow\converter.py", line 27, in <module>
    raise NotImplementedError(f"WebDNN supports TensorFlow >=v1.2.0,<=v1.4.0 Currently, TensorFlow {tf.VERSION} is installed.")
NotImplementedError: WebDNN supports TensorFlow >=v1.2.0,<=v1.4.0 Currently, TensorFlow 1.7.0 is installed.

  warnings.warn(message, category)
[convert_keras.py] Generating feedforward graph
2018-09-24 07:02:28.208268: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
[convert_keras.py] Generating graph descriptor
[convert_keras.py] BackendName: webgl

D:\repos\webdnn\resnet>dir
 Volume in drive D is DATADRIVE1
 Volume Serial Number is C6E8-9CEF

 Directory of D:\repos\webdnn\resnet

09/22/2018  07:36 AM    <DIR>          .
09/22/2018  07:36 AM    <DIR>          ..
09/22/2018  07:36 AM    <DIR>          output
09/22/2018  07:27 AM       103,045,976 resnet50.h5
               1 File(s)    103,045,976 bytes
               3 Dir(s)  434,755,878,912 bytes free

D:\repos\webdnn\resnet>dir output
 Volume in drive D is DATADRIVE1
 Volume Serial Number is C6E8-9CEF

 Directory of D:\repos\webdnn\resnet\output

09/22/2018  07:36 AM    <DIR>          .
09/22/2018  07:36 AM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  434,755,878,912 bytes free

@milhidaka
Copy link
Member

'Keyword argument not understood:', 'data_format' error is keras version difference between model save and load.
keras-team/keras#10075

Termination of python program without error message is caused by, in my experience, out of memory or error in native library (which is not webdnn itself, so problem in keras or tensorflow).
Is it possible to run simpler program, example/mnist/train_mnist_keras.py?

@jeffsaremi
Copy link
Author

train_mnist_keras went fine
However I was interested in the resnet. While Resnet conversion is going on I monitored the memory. I always have 20GB of free memory.

@milhidaka
Copy link
Member

I recalled that processing resnet may cause stack overflow in windows.

Could you try editing convert_keras.py?
from

if __name__ == "__main__":
    main()

to

import threading

if __name__ == "__main__":
    sys.setrecursionlimit(10000)
    threading.stack_size(64 * 1024 * 1024)
    t = threading.Thread(target=main)
    t.start()
    t.join()

@jeffsaremi
Copy link
Author

That worked! thanks a lot for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants