如何在保存为 .pth 文件的 AI 模型上获取图层执行时间?

How to get layer execution time on an AI model saved as .pth file?

我正在尝试 运行 在 CPU 上构建类似 Resnet 的图像分类模型,并且想知道 运行 的每一层所花费的时间细分型号。

我面临的问题是 github link https://github.com/facebookresearch/semi-supervised-ImageNet1K-models 将模型保存为 .pth 文件。它非常大(100 MB),我不知道它与 pytorch 有何不同,除了它是二进制的。 我使用以下脚本从该文件加载模型。但是我没有看到修改模型或在模型层之间插入 t = time.time() variables/statements 以分解每一层的时间的方法。

问题:

  1. 运行在以下脚本中使用模型是否会正确估计 运行 上的模型所需的端到端时间 (t2-t1) CPU,还是也包括 pytorch 编译时间?

  2. 如何在连续层之间插入时间语句以获得细分?

  3. githublink处没有inference/training脚本,只有.pth文件。那么应该如何 运行 推理或训练呢?如何在.pth模型的连续层之间插入附加层并保存?

#!/usr/bin/env python
import torch torchvision time

model=torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext50_32x4d_swsl', force_reload=False)
in = torch.randn(1, 3, 224, 224)
t1 = time.time()
out = model.forward(in)
t2 = time.time()
```**strong text**

实现这种要求的一种简单方法是在模型的每个模块上注册前向挂钩,更新全局变量以存储时间并计算上次和当前计算之间的时间差。

例如:

import torch
import torchvision
import time

global_time = None
exec_times = []


def store_time(self, input, output):
    global global_time, exec_times
    exec_times.append(time.time() - global_time)
    global_time = time.time()


model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext50_32x4d_swsl', force_reload=False)
x = torch.randn(1, 3, 224, 224)

# Register a hook for each module for computing the time difference
for module in model.modules():
    module.register_forward_hook(store_time)

global_time = time.time()
out = model(x)
t2 = time.time()

for module, t in zip(model.modules(), exec_times):
    print(f"{module.__class__}: {t}")

我得到的输出是:

<class 'torchvision.models.resnet.ResNet'>: 0.004999876022338867
<class 'torch.nn.modules.conv.Conv2d'>: 0.002006053924560547
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0009946823120117188
<class 'torch.nn.modules.activation.ReLU'>: 0.007998466491699219
<class 'torch.nn.modules.pooling.MaxPool2d'>: 0.0010004043579101562
<class 'torch.nn.modules.container.Sequential'>: 0.0020003318786621094
<class 'torchvision.models.resnet.Bottleneck'>: 0.0010023117065429688
<class 'torch.nn.modules.conv.Conv2d'>: 0.017997026443481445
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0010018348693847656
<class 'torch.nn.modules.conv.Conv2d'>: 0.0009999275207519531
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.003000497817993164
<class 'torch.nn.modules.conv.Conv2d'>: 0.003999948501586914
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.001997232437133789
<class 'torch.nn.modules.activation.ReLU'>: 0.004001140594482422
<class 'torch.nn.modules.container.Sequential'>: 0.0
<class 'torch.nn.modules.conv.Conv2d'>: 0.001999378204345703
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0
<class 'torchvision.models.resnet.Bottleneck'>: 0.003001689910888672
<class 'torch.nn.modules.conv.Conv2d'>: 0.0020008087158203125
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0009992122650146484
<class 'torch.nn.modules.conv.Conv2d'>: 0.0019991397857666016
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0010001659393310547
<class 'torch.nn.modules.conv.Conv2d'>: 0.0009999275207519531
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.002998828887939453
<class 'torch.nn.modules.activation.ReLU'>: 0.0010013580322265625
<class 'torchvision.models.resnet.Bottleneck'>: 0.0029997825622558594
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.002999544143676758
<class 'torch.nn.modules.conv.Conv2d'>: 0.0010006427764892578
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.001001119613647461
<class 'torch.nn.modules.conv.Conv2d'>: 0.0019979476928710938
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0010018348693847656
<class 'torch.nn.modules.activation.ReLU'>: 0.0010001659393310547
<class 'torch.nn.modules.container.Sequential'>: 0.00299835205078125
<class 'torchvision.models.resnet.Bottleneck'>: 0.002004384994506836
<class 'torch.nn.modules.conv.Conv2d'>: 0.0009975433349609375
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.005999088287353516
<class 'torch.nn.modules.conv.Conv2d'>: 0.0020003318786621094
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0010001659393310547
<class 'torch.nn.modules.activation.ReLU'>: 0.0020017623901367188
<class 'torch.nn.modules.container.Sequential'>: 0.0009970664978027344
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0029997825622558594
<class 'torchvision.models.resnet.Bottleneck'>: 0.0010008811950683594
<class 'torch.nn.modules.conv.Conv2d'>: 0.00500035285949707
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0009984970092773438
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0020020008087158203
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0019979476928710938
<class 'torch.nn.modules.activation.ReLU'>: 0.0010018348693847656
<class 'torchvision.models.resnet.Bottleneck'>: 0.0
<class 'torch.nn.modules.conv.Conv2d'>: 0.00099945068359375
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.001001119613647461
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.002997875213623047
<class 'torch.nn.modules.conv.Conv2d'>: 0.0010013580322265625
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.002000570297241211
<class 'torch.nn.modules.activation.ReLU'>: 0.0
<class 'torchvision.models.resnet.Bottleneck'>: 0.001997232437133789
<class 'torch.nn.modules.conv.Conv2d'>: 0.0010008811950683594
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0
<class 'torch.nn.modules.conv.Conv2d'>: 0.001001596450805664
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.00099945068359375
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.002998828887939453
<class 'torch.nn.modules.activation.ReLU'>: 0.0010020732879638672
<class 'torch.nn.modules.container.Sequential'>: 0.0010020732879638672
<class 'torchvision.models.resnet.Bottleneck'>: 0.0
<class 'torch.nn.modules.conv.Conv2d'>: 0.001995563507080078
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.002001523971557617
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0010001659393310547
<class 'torch.nn.modules.conv.Conv2d'>: 0.0010008811950683594
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0
<class 'torch.nn.modules.activation.ReLU'>: 0.0029985904693603516
<class 'torch.nn.modules.container.Sequential'>: 0.0009989738464355469
<class 'torch.nn.modules.conv.Conv2d'>: 0.0010068416595458984
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0
<class 'torchvision.models.resnet.Bottleneck'>: 0.0
<class 'torch.nn.modules.conv.Conv2d'>: 0.004993438720703125
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0010013580322265625
<class 'torch.nn.modules.conv.Conv2d'>: 0.0010001659393310547
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0010018348693847656
<class 'torch.nn.modules.conv.Conv2d'>: 0.001997709274291992
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0
<class 'torch.nn.modules.activation.ReLU'>: 0.0019991397857666016
<class 'torchvision.models.resnet.Bottleneck'>: 0.0029990673065185547
<class 'torch.nn.modules.conv.Conv2d'>: 0.0030128955841064453
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0019872188568115234
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0029993057250976562
<class 'torch.nn.modules.activation.ReLU'>: 0.0010008811950683594
<class 'torchvision.models.resnet.Bottleneck'>: 0.0
<class 'torch.nn.modules.conv.Conv2d'>: 0.0010006427764892578
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0009992122650146484
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.003001689910888672
<class 'torch.nn.modules.conv.Conv2d'>: 0.0019986629486083984
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0010008811950683594
<class 'torch.nn.modules.activation.ReLU'>: 0.0
<class 'torchvision.models.resnet.Bottleneck'>: 0.002000093460083008
<class 'torch.nn.modules.conv.Conv2d'>: 0.0019986629486083984
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0020012855529785156
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0019981861114501953
<class 'torch.nn.modules.activation.ReLU'>: 0.0030014514923095703
<class 'torchvision.models.resnet.Bottleneck'>: 0.0
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0029985904693603516
<class 'torch.nn.modules.conv.Conv2d'>: 0.0010008811950683594
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0
<class 'torch.nn.modules.conv.Conv2d'>: 0.0010013580322265625
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0009989738464355469
<class 'torch.nn.modules.activation.ReLU'>: 0.0
<class 'torch.nn.modules.container.Sequential'>: 0.002998828887939453
<class 'torchvision.models.resnet.Bottleneck'>: 0.002000570297241211
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0
<class 'torch.nn.modules.conv.Conv2d'>: 0.003000497817993164
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0020020008087158203
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0009982585906982422
<class 'torch.nn.modules.activation.ReLU'>: 0.0009996891021728516
<class 'torch.nn.modules.container.Sequential'>: 0.0
<class 'torch.nn.modules.conv.Conv2d'>: 0.0029990673065185547
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0020003318786621094
<class 'torchvision.models.resnet.Bottleneck'>: 0.0010025501251220703
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0019981861114501953
<class 'torch.nn.modules.conv.Conv2d'>: 0.0019996166229248047
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0019996166229248047
<class 'torch.nn.modules.activation.ReLU'>: 0.0
<class 'torchvision.models.resnet.Bottleneck'>: 0.0030002593994140625
<class 'torch.nn.modules.conv.Conv2d'>: 0.0020012855529785156
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0
<class 'torch.nn.modules.conv.Conv2d'>: 0.0
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0
<class 'torch.nn.modules.conv.Conv2d'>: 0.006000518798828125
<class 'torch.nn.modules.batchnorm.BatchNorm2d'>: 0.0019979476928710938
<class 'torch.nn.modules.activation.ReLU'>: 0.0
<class 'torch.nn.modules.pooling.AdaptiveAvgPool2d'>: 0.002003192901611328
<class 'torch.nn.modules.linear.Linear'>: 0.0019965171813964844

Process finished with exit code 0