且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何获取未知PyTorch模型的输入张量形状

更新时间:2023-12-01 23:24:40

您可以以此为起点进行调试

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

之后,得到N,C并通过专门放置H来创建张量,w像这个玩具示例一样,没有像

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')