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

can't convert a simple LeNet from Pytorch even after removing pooling layers. #811

Closed
alirezag opened this issue Apr 9, 2018 · 6 comments

Comments

@alirezag
Copy link

alirezag commented Apr 9, 2018

I have a very simple very standard LeNet in pytorch and I can't convert it to webdnn. The pooling layer give an error with dilation attribute. even after removing them and leaving just convolution I get a similar error but this time from attribute transA.

File "", line 1, in
runfile('/Users/alirezagoudarzi/github/Aya-s-Brain/WebDNN_Test/pytorch/train_model.py', wdir='/Users/alirezagoudarzi/github/Aya-s-Brain/WebDNN_Test/pytorch')

File "/Users/alirezagoudarzi/anaconda/envs/py36/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

File "/Users/alirezagoudarzi/anaconda/envs/py36/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "/Users/alirezagoudarzi/github/Aya-s-Brain/WebDNN_Test/pytorch/train_model.py", line 142, in
graph = PyTorchConverter().convert(net, dummy_input)

File "/Users/alirezagoudarzi/anaconda/envs/py36/lib/python3.6/site-packages/webdnn-1.2.3-py3.6.egg/webdnn/frontend/pytorch/converter.py", line 92, in convert
graph = ONNXConverter().convert(onnx.load(proto_path))

File "/Users/alirezagoudarzi/anaconda/envs/py36/lib/python3.6/site-packages/webdnn-1.2.3-py3.6.egg/webdnn/frontend/onnx/converter.py", line 94, in convert
self._convert_operator(onnx_op)

File "/Users/alirezagoudarzi/anaconda/envs/py36/lib/python3.6/site-packages/webdnn-1.2.3-py3.6.egg/webdnn/frontend/onnx/converter.py", line 114, in _convert_operator
super(ONNXConverter, self)._convert_operator(proto)

File "/Users/alirezagoudarzi/anaconda/envs/py36/lib/python3.6/site-packages/webdnn-1.2.3-py3.6.egg/webdnn/frontend/converter.py", line 117, in _convert_operator
self._handler_map[self.class.name][operator_key](self, operator)

File "/Users/alirezagoudarzi/anaconda/envs/py36/lib/python3.6/site-packages/webdnn-1.2.3-py3.6.egg/webdnn/frontend/onnx/defs/math.py", line 370, in _convert_gemm
y, = Tensordot(None, axes=(A.order.axes[0 if attrs["transA"].i else 1], B.order.axes[1 if attrs["transB"].i else 0]))(A, B)

KeyError: 'transA'

@milhidaka
Copy link
Member

It seems to be some unexpected condition.
Could you upload model definition and training code?

@alirezag
Copy link
Author

Thanks @milhidaka Here is my model definition and training code. I've commented out the webdnn imports and code from this on the top and the bottom.

train_model.zip

@milhidaka
Copy link
Member

Thank you for uploading the code. I will investigate it.

@milhidaka
Copy link
Member

Some attributes may be omitted and have default value in their specification.
I tried to fix the error by the following patch, but failed.

diff --git a/src/graph_transpiler/webdnn/frontend/onnx/defs/math.py b/src/graph_transpiler/webdnn/frontend/onnx/defs/math.py
index a02eaee9..9f8945ed 100644
--- a/src/graph_transpiler/webdnn/frontend/onnx/defs/math.py
+++ b/src/graph_transpiler/webdnn/frontend/onnx/defs/math.py
@@ -367,7 +367,9 @@ def _convert_gemm(converter: ONNXConverter, onnx_op: INodeProto):
     beta = attrs["beta"].f
     broadcast = attrs.get("broadcast", 0)
 
-    y, = Tensordot(None, axes=(A.order.axes[0 if attrs["transA"].i else 1], B.order.axes[1 if attrs["transB"].i else 0]))(A, B)
+    transA = attrs["transA"].i if "transA" in attrs else 0
+    transB = attrs["transB"].i if "transB" in attrs else 0
+    y, = Tensordot(None, axes=(A.order.axes[0 if transA else 1], B.order.axes[1 if transB else 0]))(A, B)
 
     if broadcast:
         check_broadcast_constraints(y, C)

Along with the patch, another is needed in my environment:

diff --git a/src/graph_transpiler/webdnn/frontend/onnx/defs/nn.py b/src/graph_transpiler/webdnn/frontend/onnx/defs/nn.py
index 0f6d55a4..a2c75999 100644
--- a/src/graph_transpiler/webdnn/frontend/onnx/defs/nn.py
+++ b/src/graph_transpiler/webdnn/frontend/onnx/defs/nn.py
@@ -50,9 +50,9 @@ def _convert_max_pool(converter: ONNXConverter, onnx_op: INodeProto):
 
     attrs = attribute_dict(onnx_op)
     ksize = list(attrs["kernel_shape"].ints)
-    dilations = list(attrs["dilations"].ints)
-    if any(d != 1 for d in dilations):
-        raise NotImplementedError("[ONNXConverter] MaxPool is supported only when dilations are 1.")
+    # dilations = list(attrs["dilations"].ints)
+    # if any(d != 1 for d in dilations):
+    #     raise NotImplementedError("[ONNXConverter] MaxPool is supported only when dilations are 1.")
 
     stride = list(attrs["strides"].ints)

The error:

Traceback (most recent call last):
  File "myconvert.py", line 17, in <module>
    graph = PyTorchConverter().convert(net, dummy_input)
  File "/Users/hidaka/miljs/mil-web-dnn/webdnn/src/graph_transpiler/webdnn/frontend/pytorch/converter.py", line 92, in convert
    graph = ONNXConverter().convert(onnx.load(proto_path))
  File "/Users/hidaka/miljs/mil-web-dnn/webdnn/src/graph_transpiler/webdnn/frontend/onnx/converter.py", line 94, in convert
    self._convert_operator(onnx_op)
  File "/Users/hidaka/miljs/mil-web-dnn/webdnn/src/graph_transpiler/webdnn/frontend/onnx/converter.py", line 114, in _convert_operator
    super(ONNXConverter, self)._convert_operator(proto)
  File "/Users/hidaka/miljs/mil-web-dnn/webdnn/src/graph_transpiler/webdnn/frontend/converter.py", line 117, in _convert_operator
    self._handler_map[self.__class__.__name__][operator_key](self, operator)
  File "/Users/hidaka/miljs/mil-web-dnn/webdnn/src/graph_transpiler/webdnn/frontend/onnx/defs/math.py", line 372, in _convert_gemm
    y, = Tensordot(None, axes=(A.order.axes[0 if transA else 1], B.order.axes[1 if transB else 0]))(A, B)
  File "/Users/hidaka/miljs/mil-web-dnn/webdnn/src/graph_transpiler/webdnn/graph/operators/tensordot.py", line 119, in __call__
    """
AssertionError: 
[Tensordot] Reduction size of "A" and "B" must be same:
    (A) = <Variable Variable9 shape=(1, 360), order=[?31, ?32]>
    (B) = <ConstantVariable ConstantVariable4 shape=(10, 250), order=[?18, ?19]>
    (axes) = ((<Axis ?32>,), (<Axis ?19>,))
    (reduction size of A) = 360
    (reduction size of B) = 250

There is another bug which produces incorrect shape variable. I will investigate further.

milhidaka added a commit that referenced this issue Apr 13, 2018
fix to latest onnx spec, allowing LeNet conversion in issue #811
@milhidaka
Copy link
Member

Fixed pooling, convolution error in #813 . Please try master branch. (git clone && python setup.py develop)

@alirezag
Copy link
Author

Thanks a bunch. I'll check it out.

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