123456789101112131415161718192021222324import torchimport timefor 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")