直接使用YoloV3进行网络大小的检测有问题,不论设置使用输入图片分辨率,得到的params和Flops都是
640
×
640
640\times640
640×640输入的。
但是,在输入分辨率改变的情况下,网络的Flops一定改变,所以本文展现另一种测试Flops的方式。
使用项目:https://github.com/ultralytics/yolov3
这是ultralytics复现的pytorch的yoloV3.
在项目的models/yolo.pyv中。
将最后几行替换成如下
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--cfg', type=str, default='yolov3.yaml', help='model.yaml')
parser.add_argument('--batch-size', type=int, default=1, help='total batch size for all GPUs')
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
parser.add_argument('--profile', action='store_true', help='profile model speed')
parser.add_argument('--line-profile', action='store_true', help='profile model speed layer by layer')
parser.add_argument('--test', action='store_true', help='test all yolo*.yaml')
opt = parser.parse_args()
opt.cfg = check_yaml(opt.cfg) # check YAML
print_args(vars(opt))
device = select_device(opt.device)
# Create model
im = torch.rand(opt.batch_size, 3, 320, 320).to(device)
model = Model(opt.cfg).to(device)
# Options
# opt.line_profile=True
opt.profile=True
if opt.line_profile: # profile layer by layer
model(im, profile=True)
elif opt.profile: # profile forward-backward
results = profile(input=im, ops=[model], n=3)
elif opt.test: # test all models
for cfg in Path(ROOT / 'models').rglob('yolo*.yaml'):
try:
_ = Model(cfg)
except Exception as e:
print(f'Error in {cfg}: {e}')
else: # report fused model summary
model.fuse()
其中
im = torch.rand(opt.batch_size, 3, 320, 320).to(device)
model = Model(opt.cfg).to(device)
就是用来修改输入分辨率的。
这样就可以测试到网络在不同分辨率输入下的真实大小。