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

convert Onnx Slice operator to caffe2 failed #20858

Closed
hexiaoting opened this issue May 23, 2019 · 1 comment
Closed

convert Onnx Slice operator to caffe2 failed #20858

hexiaoting opened this issue May 23, 2019 · 1 comment
Labels
caffe2 triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@hexiaoting
Copy link
Contributor

🐛 Bug

When I convert tensorflow inceptionv4 model to onnx, then to caffe2, and test this model, it failed.

Original python traceback for operator 504 in network `Inception-v4` in exception above (most recent call last):
Exception KeyError: KeyError(<weakref at 0x7f15ace61998; to 'tqdm' at 0x7f15ace5ca90>,) in <bound method tqdm.__del__ of   0%|                                                                                                                                 | 0/500 [00:17<?, ?it/s]> ignored
Traceback (most recent call last):
  File "examples/evaluate_imagenet_net.py", line 131, in <module>
    run_main(args)
  File "examples/evaluate_imagenet_net.py", line 103, in run_main
    workspace.RunNet(val_model.net)
  File "/home/zhibin/qzhong/thirdparty/caffe2-python2/lib/python2.7/site-packages/caffe2/python/workspace.py", line 215, in RunNet
    StringifyNetName(name), num_iter, allow_fail,
  File "/home/zhibin/qzhong/thirdparty/caffe2-python2/lib/python2.7/site-packages/caffe2/python/workspace.py", line 177, in CallWithExceptionIntercept
    return func(*args, **kwargs)
RuntimeError: [enforce fail at tensor.h:495] IsType<T>(). Tensor type mismatch, caller expects elements to be int while tensor contains long Error from operator:
input: "InceptionV4/Logits/PreLogitsFlatten/flatten/Shape__639:0" input: "OC2_DUMMY_28" input: "OC2_DUMMY_30" output: "InceptionV4/Logits/PreLogitsFlatten/flatten/strided_slice:0" name: "InceptionV4/Logits/PreLogitsFlatten/flatten/strided_slice" type: "Slice" device_option { device_type: 0 cuda_gpu_id: 0 }
(caffe2-python2venv) zhibin at gpu80 ~/qzhong/caffe2/caffe2_model_zoo/scripts

Slice operator in ONNX is :

  node {
    input: "InceptionV4/Logits/PreLogitsFlatten/flatten/Shape__639:0"
    output: "InceptionV4/Logits/PreLogitsFlatten/flatten/strided_slice:0"
    name: "InceptionV4/Logits/PreLogitsFlatten/flatten/strided_slice"
    op_type: "Slice"
    attribute {
      name: "starts"
      ints: 0
      type: INTS
    }
    attribute {
      name: "ends"
      ints: 1
      type: INTS
    }
    attribute {
      name: "axes"
      ints: 0
      type: INTS
    }
  }

The converted node to caffe2 is:

8629 op {
8630   input: "OC2_DUMMY_25"
8631   output: "OC2_DUMMY_28"
8632   name: ""
8633   type: "ConstantFill"
8634   arg {
8635     name: "dtype"
8636     i: 10
8637   }
8638   arg {
8639     name: "value"
8640     i: 0
8641   }
8642 }
8643 op {
8644   input: "OC2_DUMMY_28"
8645   input: "OC2_DUMMY_26"
8646   input: "OC2_DUMMY_27"
8647   output: "OC2_DUMMY_28"
8648   name: ""
8649   type: "ScatterAssign"
8650 }
8651 op {
8652   output: "OC2_DUMMY_29"
8653   name: ""
8654   type: "GivenTensorInt64Fill"
8655   arg {
8656     name: "shape"
8657     ints: 1
8658   }
8659   arg {
8660     name: "values"
8661     ints: 1
8662   }
8663 }
8664 op {
8665   input: "OC2_DUMMY_25"
8666   output: "OC2_DUMMY_30"
8667   name: ""
8668   type: "ConstantFill"
8669   arg {
8670     name: "dtype"
8671     i: 10
8672   }
8673   arg {
8674     name: "value"
8675     i: -1
8676   }
8677 }
8678 op {
8679   input: "OC2_DUMMY_30"
8680   input: "OC2_DUMMY_26"
8681   input: "OC2_DUMMY_29"
8682   output: "OC2_DUMMY_30"
8683   name: ""
8684   type: "ScatterAssign"
8685 }
8686 op {
8687   input: "InceptionV4/Logits/PreLogitsFlatten/flatten/Shape__639:0"
8688   input: "OC2_DUMMY_28"
8689   input: "OC2_DUMMY_30"
8690   output: "InceptionV4/Logits/PreLogitsFlatten/flatten/strided_slice:0"
8691   name: "InceptionV4/Logits/PreLogitsFlatten/flatten/strided_slice"
8692   type: "Slice"
8693 }

To Reproduce

Steps to reproduce the behavior:

1.Original model: tensorflow's inceptionv4 model
2. convert to onnx:

python -m tf2onnx.convert --input frozen_graph.pb --output inception_v4.onnx --inputs input:0 --outputs InceptionV4/Logits/Predictions:0
  1. convert to caffe2
convert-onnx-to-caffe2 ./inception_v4/inception_v4.onnx --output ./inception_v4/onnx_predict_net.pb --init-net-output ./inception_v4/onnx_init_net.pb

Expected behavior

produce onnx_predict_net.pb and onnx_init_net.pb successfully.
So I found that the reason for this error is:
Input(1) is int64 type (which is set in onnx_predict_net.pb with ConstantFill operator and dtype=10) but we only need int type.
we can fix this issue by modify file:

pytorch/caffe2/onnx/backend.cc【function:     Caffe2Backend::CreateSlice】
diff --git a/caffe2/onnx/backend.cc b/caffe2/onnx/backend.cc
index 893117b..76619f7 100644
--- a/caffe2/onnx/backend.cc
+++ b/caffe2/onnx/backend.cc
@@ -918,7 +918,7 @@ Caffe2Ops Caffe2Backend::CreateSlice(
     shape_starts.add_ints(starts_vals.ints_size());
     BuildOperator(
         c2_op,
-        "GivenTensorInt64Fill",
+        "GivenTensorInt32Fill",
         {},
         {starts_vals_tensor},
         {shape_starts, starts_vals});
@@ -926,7 +926,7 @@ Caffe2Ops Caffe2Backend::CreateSlice(

   caffe2::Argument dtype;
   dtype.set_name("dtype");
-  dtype.set_i(static_cast<int64_t>(caffe2::TensorProto::INT64));
+  dtype.set_i(static_cast<int32_t>(caffe2::TensorProto::INT32));
   caffe2::Argument constant;
   constant.set_name("value");
   constant.set_i(0);
@@ -957,7 +957,7 @@ Caffe2Ops Caffe2Backend::CreateSlice(
     shape_ends.add_ints(ends_vals.ints_size());
     BuildOperator(
         c2_op,
-        "GivenTensorInt64Fill",
+        "GivenTensorInt32Fill",
         {},
         {ends_vals_tensor},
         {shape_ends, ends_vals});

Environment

Python version: 3.6
Is CUDA available: N/A
CUDA runtime version: 9.0.176
GPU models and configuration:
GPU 0: Tesla K80
GPU 1: Tesla K80
GPU 2: Tesla K80
GPU 3: Tesla K80

Nvidia driver version: 410.48
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.2.1

Versions of relevant libraries:
[pip3] numpy==1.16.2
[pip3] torch==1.1.0a0+d35a587
[pip3] torchvision==0.2.3a0+ccbb322
[conda] Could not collect

@pytorchbot pytorchbot added the module: onnx Related to torch.onnx label May 23, 2019
@mrshenli
Copy link
Contributor

cc @houseroad

@mrshenli mrshenli added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label May 24, 2019
@garymm garymm added caffe2 and removed module: onnx Related to torch.onnx labels Sep 21, 2021
@drisspg drisspg closed this as completed Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
caffe2 triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

5 participants