代码拉取完成,页面将自动刷新
# 把pth模型转onnx类型
import torchvision.models as models
import torch
from torch import nn
if torch.cuda.is_available():
if input("检测到可转gpu运行是否转(y/n):").strip() == 'y':
device = torch.device("cuda")
else:
device = torch.device("cpu")
else:
device = torch.device("cpu")
with open('./classes.txt',encoding='utf-8') as f:
t = f.read().split('\n')
alllb = len(t)
class mubModu(nn.Module):
def __init__(self):
super(mubModu, self).__init__()
self.ks = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=(7, 7), padding=3),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=64, out_channels=192, kernel_size=(3, 3), padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=192, out_channels=256, kernel_size=(3, 3), padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=256, out_channels=128, kernel_size=(3, 3), padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=128, out_channels=256, kernel_size=(3, 3), padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=256, out_channels=256, kernel_size=(1, 1)),
nn.ReLU(inplace=True),
nn.LeakyReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(in_channels=256, out_channels=512, kernel_size=(3, 3), padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=512, out_channels=12, kernel_size=(3, 3), padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(in_channels=12, out_channels=10, kernel_size=(3, 3), padding=1),
nn.Sigmoid(),
)
def forward(self, x):
d2 = self.ks(x)
d2 = d2.permute(0, 2, 3, 1)
d2 = d2.reshape((d2.shape[0], d2.shape[1], d2.shape[2], 2, 5))
out = d2.squeeze(0)
return out
try:
mymodo = torch.load('./mox2.pth', map_location=device)
mymodo.to(device)
mymodo.eval()
input_names = ['input']
output_names = ['output']
x = torch.randn(1, 3, 320, 192).to(device)
torch.onnx.export(mymodo, x, 'sbkuan.onnx', input_names=input_names, output_names=output_names, verbose='True')
print("成功把画框模型转onnx")
except:
pass
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。