gmalivenko / onnx2keras

Convert ONNX model graph to Keras model format.
MIT License
195 stars 116 forks source link

KeyError: 'Cast' #13

Open kaonick opened 5 years ago

kaonick commented 5 years ago

Can you help increase the Cast operator? or Is there any way to increase the custome layer?

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.3\helpers\pydev\pydevd.py", line 1758, in <module>
    main()
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.3\helpers\pydev\pydevd.py", line 1752, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.3\helpers\pydev\pydevd.py", line 1147, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.1.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "E:/WS_Nick/WS_Python/CRAFT/CRAFT-pytorch_nick/20190822_01pytorch2tflite.py", line 70, in <module>
    pytorch2savedmodel(onnx_model_path, keras_model_path,'input.1')
  File "E:\WS_Nick\WS_Python\CRAFT\CRAFT-pytorch_nick\converters.py", line 14, in pytorch2savedmodel
    change_ordering=True, verbose=False)
  File "C:\Users\ai\Desktop\OpenR8-19.11-3\Tools\Python\Python_3.6_GPU\Anaconda3\lib\site-packages\onnx2keras\converter.py", line 145, in onnx_to_keras
    AVAILABLE_CONVERTERS[node_type](
KeyError: 'Cast'
gmalivenko commented 5 years ago

Hello. Can you show your model definition? I will implement more ONNX operations later, it takes some time.

kaonick commented 5 years ago

I am using CRAFT-pytorch(https://github.com/clovaai/CRAFT-pytorch). and want to transfer keras.(pytorch>onnx>keras) Its architecture is VGG-16(backbone). The "Cast" opearator in ONNX(https://github.com/onnx/onnx/blob/master/docs/Operators.md#Cast)

greyhuman commented 5 years ago

Hi, you can delete this module, then clone this repo, install it from it's directory with "pip3 install -e", then go to onnx2keras/layers.py file, in AVAILABLECONVERTERS in strs 13-57 add " 'Cast': convertcast, " at any row, then add, for example in from .operation_layers import convert_clip, convert_exp, convert_reduce_sum, convert_reduce_mean, convert_log, convert_pow, convert_sqrt, convert_split , convert_cast then you should go to onnx2keras/operationlayers.py file and add there def convert_cast(node, params, layers, node_name, keras_name): """ """ if len(node.input) != 1: assert AttributeError('More than 1 input for log layer.')

input_0 = ensure_tf_type(layers[node.input[0]], name="%s_const" % keras_name)

dtype = params['to']

def target_layer(x):
    import keras.backend as K
    return K.cast(x, dtype)

lambda_layer = keras.layers.Lambda(target_layer, name=keras_name)
layers[node_name] = lambda_layer(input_0)

bingo!