mobilenet_v2导出onnx及图片预测

1、creat_onnx.py

import torch
from torchvision.models import mobilenet_v2


device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model = mobilenet_v2()
model.load_state_dict(torch.load("./mobilenet_v2-b0353104.pth"),strict=False)
model.to(device)
model.eval()

inputs = torch.ones((1,3,224,224)).to(device)

onnxpath = "./mobilenet_v2.onnx"
torch.onnx.export(model,
                  inputs,
                  onnxpath,
                  export_params=True,
                  verbose=False,
                  input_names=['input'],
                  output_names=['output'],
                  opset_version=12)

2、pytorch_predict.py

# sample execution (requires torchvision)
import torch
from PIL import Image
from torchvision import transforms
from torchvision.models import mobilenet_v2
import numpy as np
import onnxruntime


filenames = ["ILSVRC2012_val_00000001_n01751748",
             "ILSVRC2012_val_00000003_n02105855",
             "ILSVRC2012_val_00000095_n02487347",
             "ILSVRC2012_val_00000211_n02123597",
             "ILSVRC2012_val_00000290_n04328186"]
for filename in filenames:
    input_image = Image.open("./"+filename+".JPEG")
    preprocess = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    ])
    input_tensor = preprocess(input_image)
    input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
    input_batch = input_batch.to('cpu')

    # -------pytorch_predict-------
    model = mobilenet_v2()
    model.load_state_dict(torch.load("./mobilenet_v2-b0353104.pth"),strict=False)
    model.eval()    
    model.to('cpu')

    with torch.no_grad():
        output = model(input_batch)
    # Tensor of shape 1000, with confidence scores over Imagenet's 1000 classes
    output[0].numpy().tofile("./pytorch_result/"+filename+".bin")

    # The output has unnormalized scores. To get probabilities, you can run a softmax on it.
    probabilities = torch.nn.functional.softmax(output[0], dim=0)
    # Read the categories
    with open("hub/imagenet_classes.txt", "r") as f:
        categories = [s.strip() for s in f.readlines()]
    # Show top categories per image
    top5_prob, top5_catid = torch.topk(probabilities, 1)
    for i in range(top5_prob.size(0)):
        print(categories[top5_catid[i]], top5_prob[i].item())


    # --------onnxruntime_predict--------
    output_path = 'mobilenet_v2.onnx'
    ort_session = onnxruntime.InferenceSession(output_path, providers=['CPUExecutionProvider'])
    ort_inputs = {ort_session.get_inputs()[0].name: input_batch.numpy()}
    ort_outs = ort_session.run(None, ort_inputs)
    ort_outs[0].tofile("./onnxruntime_result/"+filename+".bin")
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值