1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import torch
import time

for imgpath in imagepaths:
img = Image.open(imgpath)

//同步GPU,开始计时
torch.cuda.synchronize()
time_start = time.time()

img = data_transform(img)
img = torch.unsqueeze(img, dim=0)
with torch.no_grad():
output = torch.squeeze(model(img.to(device))).cpu()
predict = torch.softmax(output, dim=0)
predict_cla = torch.argmax(predict).numpy()

//同步GPU,结束计时
torch.cuda.synchronize()
time_end = time.time()
time_sum = time_end - time_start
print("imageid path:",imgpath,"inference time: ", round(time_sum * 1E3,3), "ms")