ai-station-code/wudingpv/taihuyuan_pv/compared_experiment/unet/tools/testfps.py

51 lines
1.5 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@project:
@File : testfps
@Author : qiqq
@create_time : 2023/11/6 16:40
"""
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import torch
import numpy as np
from taihuyuan_pv.compared_experiment.unet.model.unet_model import UNet
model = UNet(n_channels=3, n_classes=2)
model_path = "/home/qiqq/q3dl/code/applicationproject/taihuyuan_pv/compared_experiment/unet/train_val_test/checkpoints/2023-08-08-15.15/best.pth"
model_dict = torch.load(model_path)
model.load_state_dict(model_dict['net'])
print("权重加载")
model.eval()
model.cuda()
device = torch.device("cuda")
model.to(device)
dummy_input = torch.randn(1, 3, 512, 512,dtype=torch.float).to(device)
starter, ender = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True)
repetitions = 300
timings=np.zeros((repetitions,1))
#GPU-WARM-UP
for _ in range(10):
_ = model(dummy_input)
# MEASURE PERFORMANCE
with torch.no_grad():
for rep in range(repetitions):
starter.record()
_ = model(dummy_input)
ender.record()
# WAIT FOR GPU SYNC
torch.cuda.synchronize()
curr_time = starter.elapsed_time(ender)
timings[rep] = curr_time
mean_syn = np.sum(timings) / repetitions
std_syn = np.std(timings)
mean_fps = 1000. / mean_syn
print(' * Mean@1 {mean_syn:.3f}ms Std@5 {std_syn:.3f}ms FPS@1 {mean_fps:.2f}'.format(mean_syn=mean_syn, std_syn=std_syn, mean_fps=mean_fps))
print(mean_syn)