gmalivenko / pytorch2keras

PyTorch to Keras model convertor
https://pytorch2keras.readthedocs.io/en/latest/
MIT License
857 stars 143 forks source link

upsampling_nearest test fails #92

Open MichaelRamamonjisoa opened 5 years ago

MichaelRamamonjisoa commented 5 years ago

Describe the bug Any upsampling test fails: here I tested only upsampling_nearest since I have read in many places that onnx does not work with upsampling_bilinear. I have tested using many versions of python and the result is always the same. Of course, after that any model with upsampling layers or F.interpolate functions will fail to convert.

To Reproduce

pip3 uninstall keras
pip3 uninstall tensorflow
pip3 uninstall tensorflow-gpu
git clone https://github.com/nerox8664/pytorch2keras.git
pip3 install pytorch2keras
python3 pytorch2keras/tests/layers/upsampling_nearest.py

Logs

Using TensorFlow backend.
/home/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:516: 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/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:517: 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/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:518: 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/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:519: 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/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:520: 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/michael/.virtualenvs/keras_tf/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_resource = np.dtype([("resource", np.ubyte, 1)])
/home/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: 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/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: 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/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: 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/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: 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/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: 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/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: 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)])
INFO:pytorch2keras:Converter is called.
WARNING:pytorch2keras:Custom shapes isn't supported now.
DEBUG:pytorch2keras:Input_names:
DEBUG:pytorch2keras:['input_0']
DEBUG:pytorch2keras:Output_names:
DEBUG:pytorch2keras:['output_0']
graph(%input_0 : Float(1, 3, 224, 224)):
  %1 : Long() = onnx::Constant[value={2}](), scope: LayerTest/UpsamplingNearest2d[up]
  %2 : Tensor = onnx::Shape(%input_0), scope: LayerTest/UpsamplingNearest2d[up]
  %3 : Long() = onnx::Gather[axis=0](%2, %1), scope: LayerTest/UpsamplingNearest2d[up] # /home/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/torch/nn/functional.py:2466:0
  %4 : Float() = onnx::Cast[to=1](%3), scope: LayerTest/UpsamplingNearest2d[up] # /home/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/torch/nn/functional.py:2466:0
  %5 : Float() = onnx::Floor(%4), scope: LayerTest/UpsamplingNearest2d[up] # /home/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/torch/nn/functional.py:2466:0
  %6 : Long() = onnx::Constant[value={3}](), scope: LayerTest/UpsamplingNearest2d[up]
  %7 : Tensor = onnx::Shape(%input_0), scope: LayerTest/UpsamplingNearest2d[up]
  %8 : Long() = onnx::Gather[axis=0](%7, %6), scope: LayerTest/UpsamplingNearest2d[up] # /home/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/torch/nn/functional.py:2466:0
  %9 : Float() = onnx::Cast[to=1](%8), scope: LayerTest/UpsamplingNearest2d[up] # /home/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/torch/nn/functional.py:2466:0
  %10 : Float() = onnx::Floor(%9), scope: LayerTest/UpsamplingNearest2d[up] # /home/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/torch/nn/functional.py:2466:0
  %11 : Tensor = onnx::Unsqueeze[axes=[0]](%5)
  %12 : Tensor = onnx::Unsqueeze[axes=[0]](%10)
  %13 : Tensor = onnx::Concat[axis=0](%11, %12)
  %14 : Tensor = onnx::Constant[value= 1  1 [ Variable[CPUFloatType]{2} ]](), scope: LayerTest/UpsamplingNearest2d[up]
  %15 : Tensor = onnx::Cast[to=1](%13), scope: LayerTest/UpsamplingNearest2d[up]
  %16 : Tensor = onnx::Shape(%input_0), scope: LayerTest/UpsamplingNearest2d[up]
  %17 : Tensor = onnx::Slice[axes=[0], ends=[4], starts=[2]](%16), scope: LayerTest/UpsamplingNearest2d[up]
  %18 : Tensor = onnx::Cast[to=1](%17), scope: LayerTest/UpsamplingNearest2d[up]
  %19 : Tensor = onnx::Div(%15, %18), scope: LayerTest/UpsamplingNearest2d[up]
  %20 : Tensor = onnx::Concat[axis=0](%14, %19), scope: LayerTest/UpsamplingNearest2d[up]
  %output_0 : Float(1, 3, 224, 224) = onnx::Upsample[mode="nearest"](%input_0, %20), scope: LayerTest/UpsamplingNearest2d[up] # /home/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/torch/nn/functional.py:2485:0
  return (%output_0)

INFO:onnx2keras:Converter is called.
DEBUG:onnx2keras:List input shapes:
DEBUG:onnx2keras:[(3, 224, 224)]
DEBUG:onnx2keras:List inputs:
DEBUG:onnx2keras:Input 0 -> input_0.
DEBUG:onnx2keras:List outputs:
DEBUG:onnx2keras:Output 0 -> output_0.
DEBUG:onnx2keras:Gathering weights to dictionary.
WARNING:tensorflow:From /home/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:541: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

WARNING:tensorflow:From /home/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:541: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

DEBUG:onnx2keras:Found input input_0 with shape (3, 224, 224)
DEBUG:onnx2keras:######
DEBUG:onnx2keras:...
DEBUG:onnx2keras:Converting ONNX operation
DEBUG:onnx2keras:type: Constant
DEBUG:onnx2keras:node_name: 1
DEBUG:onnx2keras:node_params: {'value': array(2)}
DEBUG:onnx2keras:...
DEBUG:onnx2keras:Check if all inputs are available:
DEBUG:onnx2keras:... found all, continue
DEBUG:onnx2keras:######
DEBUG:onnx2keras:...
DEBUG:onnx2keras:Converting ONNX operation
DEBUG:onnx2keras:type: Shape
DEBUG:onnx2keras:node_name: 2
DEBUG:onnx2keras:node_params: {}
DEBUG:onnx2keras:...
DEBUG:onnx2keras:Check if all inputs are available:
DEBUG:onnx2keras:Check input 0 (name input_0).
DEBUG:onnx2keras:... found all, continue
DEBUG:onnx2keras:shape:Actual result:
DEBUG:onnx2keras:shape:[None 3 224 224]
DEBUG:onnx2keras:######
DEBUG:onnx2keras:...
DEBUG:onnx2keras:Converting ONNX operation
DEBUG:onnx2keras:type: Gather
DEBUG:onnx2keras:node_name: 3
DEBUG:onnx2keras:node_params: {'axis': 0}
DEBUG:onnx2keras:...
DEBUG:onnx2keras:Check if all inputs are available:
DEBUG:onnx2keras:Check input 0 (name 2).
DEBUG:onnx2keras:Check input 1 (name 1).
DEBUG:onnx2keras:... found all, continue
DEBUG:onnx2keras:gather:Gather from numpy array
DEBUG:onnx2keras:######
DEBUG:onnx2keras:...
DEBUG:onnx2keras:Converting ONNX operation
DEBUG:onnx2keras:type: Cast
DEBUG:onnx2keras:node_name: 4
DEBUG:onnx2keras:node_params: {'to': 1}
DEBUG:onnx2keras:...
DEBUG:onnx2keras:Check if all inputs are available:
DEBUG:onnx2keras:Check input 0 (name 3).
DEBUG:onnx2keras:... found all, continue
Traceback (most recent call last):
  File "layers/upsamplings/upsampling_nearest.py", line 48, in <module>
    k_model = pytorch_to_keras(model, input_var, (3, 224, 224,), verbose=True)
  File "/home/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/pytorch2keras/converter.py", line 73, in pytorch_to_keras
    verbose=verbose, change_ordering=change_ordering)
  File "/home/michael/.virtualenvs/keras_tf/lib/python3.6/site-packages/onnx2keras/converter.py", line 142, in onnx_to_keras
    AVAILABLE_CONVERTERS[node_type](
KeyError: 'Cast'

Environment (please complete the following information):

Qianyu97 commented 4 years ago

Hi man, have you fixed the bug? I am in the same situation now and really sick for fighting this

MichaelRamamonjisoa commented 4 years ago

Hi, no I gave up for two reasons. First (obviously) the bug in unsolved and doesnt seem to be on the todo list. Second, I originally wanted to use bilinear upsampling otherwise my network produced artifacts. But bilinear upsampling was apparently not implemented either.

SWHL commented 4 years ago

I also am in the same situation now.