How to get input tensor shape of an unknown PyTorch model

Gunjan

I am writing a python script, which converts any deep learning models from popular frameworks (TensorFlow, Keras, PyTorch) to ONNX format. Currently I have used tf2onnx for tensorflow and keras2onnx for keras to ONNX conversion, and those work.

Now PyTorch has integrated ONNX support, so I can save ONNX models from PyTorch directly. But the problem is I will need input tensor shape for that model, in order to save it in ONNX format. As you already might have guessed, I am writing this script to convert unknown deep learning models.

Here is PyTorch's tutorial for ONNX conversion. There it's written:

Limitations¶ The ONNX exporter is a trace-based exporter, which means that it operates by executing your model once, and exporting the operators which were actually run during this run. This means that if your model is dynamic, e.g., changes behavior depending on input data, the export won’t be accurate.

Similarly, a trace is might be valid only for a specific input size (which is one reason why we require explicit inputs on tracing). Most of the operators export size-agnostic versions and should work on different batch sizes or input sizes. We recommend examining the model trace and making sure the traced operators look reasonable.


The code snippet I am using is this:

import torch

def convert_pytorch2onnx(self):
    """pytorch -> onnx"""

    model = torch.load(self._model_file_path)

    # Don't know how to get this INPUT_SHAPE
    dummy_input = torch.randn(INPUT_SHAPE)
    torch.onnx.export(model, dummy_input, self._onnx_file_path)
    return

So how do I know the INPUT_SHAPE of the input tensor of that unknown PyTorch model? Or is there any other way to convert the PyTorch model to ONNX?

Mughees

you can follow this as a starting point to debug

list(model.parameters())[0].shape # weights of the first layer in the format (N,C,Kernel dimensions) # 64, 3, 7 ,7

after that get the N,C and create a tensor out of that by specially putting H,W as None like this toy example

import torch
import torchvision

net = torchvision.models.resnet18(pretrained = True)

shape_of_first_layer = list(net.parameters())[0].shape #shape_of_first_layer

N,C = shape_of_first_layer[:2]

dummy_input = torch.Tensor(N,C)

dummy_input = dummy_input[...,:, None,None] #adding the None for height and weight

torch.onnx.export(net, dummy_input, './alpha')

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do i get the input_shape of a frozen Tensorflow-Model for TOCO tf_convert

分類Dev

Use "Flatten" or "Reshape" to get 1D output of unknown input shape in keras

分類Dev

In Tensorflow How can I add a Tensor to another Tensor with different shape?

分類Dev

Keras input_shape、output_shape、get_weights、get_config、summaryのPyTorchの代替手段は何ですか

分類Dev

Pytorch how to stack tensor like for loop

分類Dev

Given a tensor flow model graph, how to find the input node and output node names

分類Dev

get result from tensor flow model

分類Dev

How to explicitly broadcast a tensor to match another's shape in tensorflow?

分類Dev

how to shape tensor from fully connected to to 4-D

分類Dev

How to apply mask to a tensor and keep its original shape

分類Dev

How to get the value of a tensor? Python

分類Dev

How do you invert a tensor of boolean values in Pytorch?

分類Dev

reshaping a tensor with padding in pytorch

分類Dev

How do I declare an array of unknown size then take input till I desire and then get the size of the array?

分類Dev

how to fit train CNN with the appropriate input shape?

分類Dev

Why doesn't this input pass through this simple PyTorch model?

分類Dev

Running Keras DNN model (UNet) using OpenCV readNetFromTensorFlow: Error: Unknown layer type Shape in op decoder_stage0_upsampling/Shape

分類Dev

What is the shape of an image tensor in TensorFlow

分類Dev

Save a Hash Value as a Tensor in pytorch

分類Dev

Pytorch transform tensor to one hot

分類Dev

Invalid argument: Input to reshape is a tensor with x values, but requested shape requires a multiple of y. {node Reshape_13}

分類Dev

How to get the output shape of a layer via Pycaffe

分類Dev

How to get coordinates of shape in matplotlib treemap?

分類Dev

How to get a rectangle shape with no background color?

分類Dev

How to use keras embedding layer with 3D tensor input?

分類Dev

How to solve "Variable is available in checkpoint, but has an incompatible shape with model variable"?

分類Dev

Pytorch Unfold and Fold: How do I put this image tensor back together again?

分類Dev

Keras LSTM training. How to shape my input data?

分類Dev

How to load the saved tokenizer from pretrained model in Pytorch

Related 関連記事

  1. 1

    How do i get the input_shape of a frozen Tensorflow-Model for TOCO tf_convert

  2. 2

    Use "Flatten" or "Reshape" to get 1D output of unknown input shape in keras

  3. 3

    In Tensorflow How can I add a Tensor to another Tensor with different shape?

  4. 4

    Keras input_shape、output_shape、get_weights、get_config、summaryのPyTorchの代替手段は何ですか

  5. 5

    Pytorch how to stack tensor like for loop

  6. 6

    Given a tensor flow model graph, how to find the input node and output node names

  7. 7

    get result from tensor flow model

  8. 8

    How to explicitly broadcast a tensor to match another's shape in tensorflow?

  9. 9

    how to shape tensor from fully connected to to 4-D

  10. 10

    How to apply mask to a tensor and keep its original shape

  11. 11

    How to get the value of a tensor? Python

  12. 12

    How do you invert a tensor of boolean values in Pytorch?

  13. 13

    reshaping a tensor with padding in pytorch

  14. 14

    How do I declare an array of unknown size then take input till I desire and then get the size of the array?

  15. 15

    how to fit train CNN with the appropriate input shape?

  16. 16

    Why doesn't this input pass through this simple PyTorch model?

  17. 17

    Running Keras DNN model (UNet) using OpenCV readNetFromTensorFlow: Error: Unknown layer type Shape in op decoder_stage0_upsampling/Shape

  18. 18

    What is the shape of an image tensor in TensorFlow

  19. 19

    Save a Hash Value as a Tensor in pytorch

  20. 20

    Pytorch transform tensor to one hot

  21. 21

    Invalid argument: Input to reshape is a tensor with x values, but requested shape requires a multiple of y. {node Reshape_13}

  22. 22

    How to get the output shape of a layer via Pycaffe

  23. 23

    How to get coordinates of shape in matplotlib treemap?

  24. 24

    How to get a rectangle shape with no background color?

  25. 25

    How to use keras embedding layer with 3D tensor input?

  26. 26

    How to solve "Variable is available in checkpoint, but has an incompatible shape with model variable"?

  27. 27

    Pytorch Unfold and Fold: How do I put this image tensor back together again?

  28. 28

    Keras LSTM training. How to shape my input data?

  29. 29

    How to load the saved tokenizer from pretrained model in Pytorch

ホットタグ

アーカイブ