Open1
SRH-NetのONNXエクスポート
- test_img.py
from __future__ import print_function
import argparse
import skimage
import skimage.io
import skimage.transform
from PIL import Image
from math import log10
import sys
import shutil
import os
import re
from struct import unpack
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
from torch.autograd import Variable
from torch.utils.data import DataLoader
from models.SHRNet import SHRNet
import time
#from dataloader.data import get_test_set
import numpy as np
parser = argparse.ArgumentParser(description='PyTorch GANet Example')
parser.add_argument('--crop_height', type=int, required=True, help="crop height")
parser.add_argument('--crop_width', type=int, required=True, help="crop width")
parser.add_argument('--max_disp', type=int, default=192, help="max disp")
parser.add_argument('--resume', type=str, default='', help="resume from saved model")
parser.add_argument('--cuda', type=bool, default=True, help='use cuda?')
parser.add_argument('--leftimg', default= '', help='load model')
parser.add_argument('--rightimg', default= '', help='load model')
opt = parser.parse_args()
print(opt)
cuda = opt.cuda
#cuda = True
if cuda and not torch.cuda.is_available():
raise Exception("No GPU found, please run without --cuda")
#print('===> Loading datasets')
#test_set = get_test_set(opt.data_path, opt.test_list, [opt.crop_height, opt.crop_width], false, opt.kitti, opt.kitti2015)
#testing_data_loader = DataLoader(dataset=test_set, num_workers=opt.threads, batch_size=opt.testBatchSize, shuffle=False)
print('===> Building model')
model = SHRNet(opt.max_disp)
model.cpu()
# if cuda:
# model = torch.nn.DataParallel(model).cuda()
if opt.resume:
if os.path.isfile(opt.resume):
print("=> loading checkpoint '{}'".format(opt.resume))
checkpoint = torch.load(opt.resume)
model.load_state_dict(checkpoint['state_dict'], strict=False)
else:
print("=> no checkpoint found at '{}'".format(opt.resume))
model.eval()
with torch.no_grad():
H=opt.crop_height
W=opt.crop_width
MODEL='sceneflow'
onnx_file = f"srh_net_{MODEL}_maxdisp{opt.max_disp}_{H}x{W}.onnx"
x = torch.randn(1, 3, H, W)#.cuda()
torch.onnx.export(
model,
args=(x,x),
f=onnx_file,
opset_version=11
)
import onnx
from onnxsim import simplify
model = onnx.load(onnx_file)
model_simp, check = simplify(model)
onnx.save(model_simp, onnx_file)
import sys
sys.exit(0)
def test_transform(temp_data, crop_height, crop_width):
_, h, w=np.shape(temp_data)
if h <= crop_height and w <= crop_width:
temp = temp_data
temp_data = np.zeros([6, crop_height, crop_width], 'float32')
temp_data[:, crop_height - h: crop_height, crop_width - w: crop_width] = temp
else:
start_x = int((w - crop_width) / 2)
start_y = int((h - crop_height) / 2)
temp_data = temp_data[:, start_y: start_y + crop_height, start_x: start_x + crop_width]
left = np.ones([1, 3,crop_height,crop_width],'float32')
left[0, :, :, :] = temp_data[0: 3, :, :]
right = np.ones([1, 3, crop_height, crop_width], 'float32')
right[0, :, :, :] = temp_data[3: 6, :, :]
return torch.from_numpy(left).float(), torch.from_numpy(right).float(), h, w
def load_data(leftname, rightname):
left = Image.open(leftname)
right = Image.open(rightname)
size = np.shape(left)
height = size[0]
width = size[1]
temp_data = np.zeros([6, height, width], 'float32')
left = np.asarray(left)
right = np.asarray(right)
r = left[:, :, 0]
g = left[:, :, 1]
b = left[:, :, 2]
temp_data[0, :, :] = (r - np.mean(r[:])) / np.std(r[:])
temp_data[1, :, :] = (g - np.mean(g[:])) / np.std(g[:])
temp_data[2, :, :] = (b - np.mean(b[:])) / np.std(b[:])
r = right[:, :, 0]
g = right[:, :, 1]
b = right[:, :, 2]
#r,g,b,_ = right.split()
temp_data[3, :, :] = (r - np.mean(r[:])) / np.std(r[:])
temp_data[4, :, :] = (g - np.mean(g[:])) / np.std(g[:])
temp_data[5, :, :] = (b - np.mean(b[:])) / np.std(b[:])
return temp_data
def test(leftname, rightname):
input1, input2, height, width = test_transform(load_data(leftname, rightname), opt.crop_height, opt.crop_width)
input1 = Variable(input1, requires_grad = False)
input2 = Variable(input2, requires_grad = False)
model.eval()
if cuda:
input1 = input1.cuda()
input2 = input2.cuda()
with torch.no_grad():
prediction = model(input1, input2)
temp = prediction.cpu()
temp = temp.detach().numpy()
if height <= opt.crop_height and width <= opt.crop_width:
temp = temp[0, opt.crop_height - height: opt.crop_height, opt.crop_width - width: opt.crop_width]
else:
temp = temp[0, :, :]
skimage.io.imsave('test_disp.png', (temp * 256).astype('uint16'))
return temp
if __name__ == "__main__":
leftname = opt.leftimg
rightname = opt.rightimg
test(leftname, rightname)