tensor_proto.raw_data().empty() || !tensor_proto.float_data().empty() || !tensor_proto.double_data()

应该是上采样

tensor_proto.raw_data().empty() || !tensor_proto.float_data().empty() || !tensor_proto.double_data().empty() || !tensor_proto.int64_data().empty()

报错代码:

# -*- coding: utf-8 -*-
import onnx
import os
import time

import cv2
import numpy as np


net = cv2.dnn.readNetFromONNX(r'onnx_model/0.onnx')
print("net load")

临时解决方法:

session = onnxruntime.InferenceSession(onnx_path)

生成代码: 



import argparse
import os

import numpy as np
import onnx
import torch
import torch.nn as nn


class TinyModel(nn.Module):
    def __init__(self, upsample_mode):
        super().__init__()
        self.expander = nn.Conv2d(3, 192, 1, 1)
        upsamples = [nn.Upsample((256, 256), mode='nearest')]
        # upsamples = [nn.Upsample(scale_factor=2, mode='nearest'),
        #              nn.Upsample(scale_factor=2, mode='nearest'),
        #              nn.Upsample(scale_factor=2, mode='nearest'),
        #              nn.Upsample((256, 256), mode='nearest'),
        #              nn.Upsample((256, 256), mode='nearest'),
        #              nn.Upsample((256, 256), mode='nearest')]

        self.upsample = upsamples[upsample_mode]

    def forward(self, x):
        x = self.expander(x)
        x = self.upsample(x)
        return x


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--save_folder', type=str,default="onnx_model")
    args = parser.parse_args()
    os.makedirs(args.save_folder,exist_ok=True)

    # device = torch.device('cuda:0')
    sample_input = torch.rand(1, 3, 128, 128)#.to(device)
    for i in range(1):
        model = TinyModel(upsample_mode=i)#.to(device)
        model.eval()

        # export onnx file
        onnx_path = os.path.join(args.save_folder, '{}.onnx'.format(i))
        torch.onnx.export(model, sample_input, onnx_path,
                          input_names=['input_img'],
                          output_names=['output'],
                          opset_version=11)

        # save output
        sample_out_path = os.path.join(args.save_folder, str(i))
        sample_output = model(sample_input)
        np.save(sample_out_path + '_inp.npy', sample_input.data.cpu().numpy())
        np.save(sample_out_path + '_out.npy', sample_output.data.cpu().numpy())

        # check valid is graph or not
        try:
            onnx_model = onnx.load(onnx_path)
            onnx.checker.check_graph(onnx_model)
        except Exception as e:
            print(e)
发布了2672 篇原创文章 · 获赞 973 · 访问量 527万+

猜你喜欢

转载自blog.csdn.net/jacke121/article/details/104409847