当前位置:   article > 正文

测试方法介绍-计算模型复杂度(GMac)、模型大小(M)、计算速度(FPS)_get_model_complexity_info

get_model_complexity_info

PRNet-V

计算复杂度为 48.76GMac
参数数量为34.73M (PRNet测试结果)(IEO在12345层)
参数数量为27.57M (PRNet测试结果)(IEO在345层)

计算图片读入和写出的时间
若batch=1,在ECSSD数据集上的测试结果为39FPS
不计算图片读入和写出的时间

若batch=1,在ECSSD数据集上的测试结果为74FPS

 PRNet-V-GLF(含冗余IEO12)

PRNet-V-GLF(不含冗余IEO12)

 参数  39.46M

计算复杂度  54.81GMac

PRNet-R

计算复杂度为 21.0GMac
参数数量为35.85M (这是我利用MINet网络架构设计的自己的网络的测试结果)
不计算图片读入和写出的时间

若batch=1,在ECSSD数据集上的测试结果为53FPS

ResNet+FPN的网络
16.19GMac
25.94M

简单总结PRNet

首先梳理算法结构

1 主文件main.py

里面有solver = Solver(exp_name, arg_config, path_config)
这个文件来自于 # from utils.solver import Solver

 2 utils里面的solver.py文件

里面有
if hasattr(network_lib, self.arg_dict["model"]): 
      self.net = getattr(network_lib, self.arg_dict["model"])().to(self.dev)
这个文件来自于
network_lib的由来 =>
import network as network_lib
model的由来 main.py => "model": "MINet_VGG16",

 

3 添加计算复杂度的语句get_model_complexity_info()

这些不重要,重要的是
self.net就是可以用于测试计算复杂度(GMac)、模型大小(M)的文件
写成如下格式即可:
注意调整(3,320,320),也就是输入图片的通道数、H、W

  1. with torch.cuda.device(0): # todo 1
  2. # net = self.net # Network(self.cfg)
  3. macs, params = get_model_complexity_info(self.net, (3, 320, 320), as_strings=True,
  4. print_per_layer_stat=True,
  5. verbose=True)
  6. print('{:<30} {:<8}'.format('Computational complexity: ', macs))
  7. print('{:<30} {:<8}'.format('Number of parameters: ', params))

4 此时get_model_complexity_info一定不要忘了import对应的库文件

 如下即可

  1. # 计算模型Flops
  2. from ptflops import get_model_complexity_info

5 运行程序就可以得到得到对应参数  Efficiency


计算复杂度为 48.76GMac
参数数量为34.73M (这是我利用MINet网络架构设计的自己的网络的测试结果)

在ECSSD数据集上的测试结果为16FPS

下面来计算速度 FPS

1 注释掉评测部分,这会导致测试速度慢

由于MINet的测试代码main.py->solver.py里面的def test(self):里面是测试的同时进行评测
因此需要注释掉
A原文件

B注释掉文件

 2 测试PFS需要做5件事(其中ABC需要放在for循环外面DE放在里面)

=>A import time

=>B idx = 0

=>C time_spent = []

=>D start_time = time.time()

=>E  time_spent.append(time.time() - start_time)
                if idx % 100 == 0:
                    time_spent = []
                if idx % 99 == 0:
                    print('time/FPS', np.mean(time_spent), 1 * 1 // np.mean(time_spent))
                idx = idx + 1

这个E步骤的计算逻辑是每循环99次统计一下平均时间,每循环100次做一次清空。
值得注意的是我们在测试的时候把batch设置为4(因为训练时候就是4),此时没循环一次是4张图片,直接用计算FPS结果x4即可.

若,把batch设置为1,此时没循环一次是一张图片,直接用计算FPS结果即可.
但是网络模型设置的是batch=4,这与这个训练架构有关,需要设置为1。

然后会生成一个BS1的文件里面的pth是空的,把右边BS4的模型放进BS1文件夹里面就可以以batch=1的方式运行测试程序了。

  1. # => A
  2. import time
  3. # => B
  4. idx = 0
  5. # => C
  6. time_spent = []
  7. for test_batch_id, test_data in tqdm_iter:
  8. # => D
  9. start_time = time.time()
  10. xxx
  11. xxx
  12. xxx
  13. xxx
  14. xxx
  15. xxx
  16. # => E
  17. time_spent.append(time.time() - start_time)
  18. if idx % 100 == 0:
  19. time_spent = []
  20. if idx % 99 == 0:
  21. print('time/FPS', np.mean(time_spent), 1 * 1 // np.mean(time_spent))
  22. idx = idx + 1

 实际情况如下
A

BCD

E

 具体全部代码如下
 

  1. # => B
  2. idx = 0
  3. # => C
  4. time_spent = []
  5. for test_batch_id, test_data in tqdm_iter:
  6. # => D
  7. start_time = time.time()
  8. tqdm_iter.set_description(f"{self.exp_name}: te=>{test_batch_id + 1}")
  9. with torch.no_grad():
  10. in_imgs, in_mask_paths, in_names = test_data
  11. in_imgs = in_imgs.to(self.dev, non_blocking=True)
  12. outputs = self.net(in_imgs)
  13. outputs_np = outputs.sigmoid().cpu().detach()
  14. for item_id, out_item in enumerate(outputs_np):
  15. gimg_path = os.path.join(in_mask_paths[item_id])
  16. gt_img = Image.open(gimg_path).convert("L")
  17. out_img = self.to_pil(out_item).resize(gt_img.size, resample=Image.NEAREST)
  18. if save_pre:
  19. oimg_path = os.path.join(self.save_path, in_names[item_id] + ".png")
  20. out_img.save(oimg_path)
  21. # => E
  22. time_spent.append(time.time() - start_time)
  23. if idx % 100 == 0:
  24. time_spent = []
  25. if idx % 99 == 0:
  26. print('time/FPS', np.mean(time_spent), 1 * 1 // np.mean(time_spent))
  27. idx = idx + 1
  28. # 下面是为了计算Fmax\Favg\MAE设置因此可以注释掉
  29. # gt_img = np.asarray(gt_img)
  30. # out_img = np.array(out_img)
  31. # ps, rs, mae, meanf = cal_pr_mae_meanf(out_img, gt_img)
  32. # for pidx, pdata in enumerate(zip(ps, rs)):
  33. # p, r = pdata
  34. # pres[pidx].update(p)
  35. # recs[pidx].update(r)
  36. # maes.update(mae)
  37. # meanfs.update(meanf)
  38. # maxf = cal_maxf([pre.avg for pre in pres], [rec.avg for rec in recs])
  39. # results = {"MAXF": maxf, "MEANF": meanfs.avg, "MAE": maes.avg}
  40. results = {}
  41. return results

测试结果(计算读入和写出时间)
若batch=4
在ECSSD数据集上的测试结果为16X4FPS=64FPS(并行计算了这个测试不准确不采纳)

若batch=1
在ECSSD数据集上的测试结果为39FPS

测试结果(不计算读入和写出时间)
若batch=4 torch.Size([4, 3, 320, 320])
在ECSSD数据集上的测试结果为68X4FPS (并行计算了这个测试不准确不采纳)

若batch=1 torch.Size([1, 3, 320, 320])
在ECSSD数据集上的测试结果为74FPS

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/514471
推荐阅读
相关标签
  

闽ICP备14008679号