MMdnn icon indicating copy to clipboard operation
MMdnn copied to clipboard

RELU Error on transferring Keras weight to pytorch

Open junikkoma opened this issue 4 years ago • 1 comments

Platform (like ubuntu 16.04/win10): Ubuntu 18.04, using docker image provided

Python version: 3.6.9

Source framework with version (like Tensorflow 1.4.1 with GPU): Keras 2.1.6 / 2.2.2, tensorflow 2.2.0

Destination framework with version (like CNTK 2.3 with GPU): pytorch 1.8.1

Pre-trained model path (webpath or webdisk path): custom weights

Running scripts: $ mmconvert -sf keras -df pytorch -om trial.pth -iw my_custom_weights.h5

On running script above under Keras 2.2.2 enviornment, i got following errrors:

Traceback (most recent call last):
  File "/home/junikkoma/.local/lib/python3.6/site-packages/mmdnn/conversion/keras/keras2_parser.py", line 104, in __init__
    from keras.applications.mobilenet import relu6
ImportError: cannot import name 'relu6'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/junikkoma/.local/bin/mmconvert", line 8, in <module>
    sys.exit(_main())
  File "/home/junikkoma/.local/lib/python3.6/site-packages/mmdnn/conversion/_script/convert.py", line 102, in _main
    ret = convertToIR._convert(ir_args)
  File "/home/junikkoma/.local/lib/python3.6/site-packages/mmdnn/conversion/_script/convertToIR.py", line 46, in _convert
    parser = Keras2Parser(model)
  File "/home/junikkoma/.local/lib/python3.6/site-packages/mmdnn/conversion/keras/keras2_parser.py", line 120, in __init__
    'DepthwiseConv2D': layers.DepthwiseConv2D
  File "/home/junikkoma/.local/lib/python3.6/site-packages/keras/engine/saving.py", line 258, in load_model
    raise ValueError('No model found in config file.')
ValueError: No model found in config file.

on inspecting first error on keras2_parser.py", line 104, I noticed downgrading Keras version to 2.1.6 might help. on my second trial(with same script) under keras 2.1.6 enviornment, i got following errors

Traceback (most recent call last):
  File "/home/junikkoma/.local/lib/python3.6/site-packages/mmdnn/conversion/keras/keras2_parser.py", line 110, in __init__
    'DepthwiseConv2D': _keras.applications.mobilenet.DepthwiseConv2D
  File "/home/junikkoma/.local/lib/python3.6/site-packages/keras/models.py", line 268, in load_model
    raise ValueError('No model found in config file.')
ValueError: No model found in config file.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/junikkoma/.local/bin/mmconvert", line 8, in <module>
    sys.exit(_main())
  File "/home/junikkoma/.local/lib/python3.6/site-packages/mmdnn/conversion/_script/convert.py", line 102, in _main
    ret = convertToIR._convert(ir_args)
  File "/home/junikkoma/.local/lib/python3.6/site-packages/mmdnn/conversion/_script/convertToIR.py", line 46, in _convert
    parser = Keras2Parser(model)
  File "/home/junikkoma/.local/lib/python3.6/site-packages/mmdnn/conversion/keras/keras2_parser.py", line 119, in __init__
    'relu6': layers.ReLU(6, name='relu6'),
AttributeError: module 'keras.layers' has no attribute 'ReLU'

Sine similar errors with ReLU and Conv2D, I was wondering if there would be some solution

junikkoma avatar May 10 '21 06:05 junikkoma

Hi,

Have you solve this problem? I met this same problem and did exactly what you did. I tried with many tensorflow and keras version. Still same error

Minor update: keras==2.2.2 will solve the ImportError: cannot import name 'relu6 error but run into another one.

(mmdnn) stly@hula-01:~/Data/HULA/code/rebecca/NUCLEAR_SEG/mmdnn$ mmtoir --srcFramework keras --weights pretrained_weights.h5 --network rebeccanet_model.json --dstPath rebeccanet_k2p Using TensorFlow backend. /home/cougarnet.uh.edu/stly/Data/Soft/anaconda3/envs/mmdnn/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:523: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint8 = np.dtype([("qint8", np.int8, 1)]) /home/cougarnet.uh.edu/stly/Data/Soft/anaconda3/envs/mmdnn/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:524: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_quint8 = np.dtype([("quint8", np.uint8, 1)]) /home/cougarnet.uh.edu/stly/Data/Soft/anaconda3/envs/mmdnn/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint16 = np.dtype([("qint16", np.int16, 1)]) /home/cougarnet.uh.edu/stly/Data/Soft/anaconda3/envs/mmdnn/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_quint16 = np.dtype([("quint16", np.uint16, 1)]) /home/cougarnet.uh.edu/stly/Data/Soft/anaconda3/envs/mmdnn/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. _np_qint32 = np.dtype([("qint32", np.int32, 1)]) /home/cougarnet.uh.edu/stly/Data/Soft/anaconda3/envs/mmdnn/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:532: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'. np_resource = np.dtype([("resource", np.ubyte, 1)]) Traceback (most recent call last): File "/home/cougarnet.uh.edu/stly/Data/Soft/anaconda3/envs/mmdnn/bin/mmtoir", line 8, in <module> sys.exit(_main()) File "/home/cougarnet.uh.edu/stly/Data/Soft/anaconda3/envs/mmdnn/lib/python3.6/site-packages/mmdnn/conversion/_script/convertToIR.py", line 197, in _main ret = _convert(args) File "/home/cougarnet.uh.edu/stly/Data/Soft/anaconda3/envs/mmdnn/lib/python3.6/site-packages/mmdnn/conversion/_script/convertToIR.py", line 45, in _convert from mmdnn.conversion.keras.keras2_parser import Keras2Parser File "/home/cougarnet.uh.edu/stly/Data/Soft/anaconda3/envs/mmdnn/lib/python3.6/site-packages/mmdnn/conversion/keras/keras2_parser.py", line 8, in <module> import keras as _keras File "/home/cougarnet.uh.edu/stly/Data/Soft/anaconda3/envs/mmdnn/lib/python3.6/site-packages/keras/__init__.py", line 5, in <module> from . import applications File "/home/cougarnet.uh.edu/stly/Data/Soft/anaconda3/envs/mmdnn/lib/python3.6/site-packages/keras/applications/__init__.py", line 13, in <module> keras_applications.set_keras_submodules( AttributeError: module 'keras_applications' has no attribute 'set_keras_submodules'

tsly123 avatar Nov 24 '21 19:11 tsly123