代码拉取完成,页面将自动刷新
import sys
sys.path.append('core')
import argparse
import os
import cv2
import glob
import numpy as np
import torch
from PIL import Image
import imageio
import matplotlib.pyplot as plt
from network import RAFTGMA
from utils import flow_viz
from utils.utils import InputPadder
import os
DEVICE = 'cuda'
def load_image(imfile):
img = np.array(Image.open(imfile)).astype(np.uint8)
img = torch.from_numpy(img).permute(2, 0, 1).float()
return img[None].to(DEVICE)
def viz(img, flo, flow_dir):
img = img[0].permute(1, 2, 0).cpu().numpy()
flo = flo[0].permute(1, 2, 0).cpu().numpy()
# map flow to rgb image
flo = flow_viz.flow_to_image(flo)
imageio.imwrite(os.path.join(flow_dir, 'flo.png'), flo)
print(f"Saving optical flow visualisation at {os.path.join(flow_dir, 'flo.png')}")
def normalize(x):
return x / (x.max() - x.min())
def demo(args):
model = torch.nn.DataParallel(RAFTGMA(args))
model.load_state_dict(torch.load(args.model))
print(f"Loaded checkpoint at {args.model}")
model = model.module
model.to(DEVICE)
model.eval()
flow_dir = os.path.join(args.path, args.model_name)
if not os.path.exists(flow_dir):
os.makedirs(flow_dir)
with torch.no_grad():
images = glob.glob(os.path.join(args.path, '*.png')) + \
glob.glob(os.path.join(args.path, '*.jpg'))
images = sorted(images)
for imfile1, imfile2 in zip(images[:-1], images[1:]):
image1 = load_image(imfile1)
image2 = load_image(imfile2)
print(f"Reading in images at {imfile1} and {imfile2}")
padder = InputPadder(image1.shape)
image1, image2 = padder.pad(image1, image2)
flow_low, flow_up = model(image1, image2, iters=12, test_mode=True)
print(f"Estimating optical flow...")
viz(image1, flow_up, flow_dir)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--model', help="restore checkpoint")
parser.add_argument('--model_name', help="define model name", default="GMA")
parser.add_argument('--path', help="dataset for evaluation")
parser.add_argument('--num_heads', default=1, type=int,
help='number of heads in attention and aggregation')
parser.add_argument('--position_only', default=False, action='store_true',
help='only use position-wise attention')
parser.add_argument('--position_and_content', default=False, action='store_true',
help='use position and content-wise attention')
parser.add_argument('--mixed_precision', action='store_true', help='use mixed precision')
args = parser.parse_args()
demo(args)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。