pytorch使用profiler对模型性能分析时报错

pytorch使用profiler对模型性能分析时报错,第1张

源码(参考自:PyTorch模型性能分析、优化及部署 (aliyun.com)):

def analysis():
    from torch.profiler import profile, tensorboard_trace_handler
    import time

    img_size = [96, 96, 96]
    model = VAN(img_size=img_size, in_chans=4, num_classes=3)
    model.eval()
    inputs = torch.randn((1, 4, *img_size))

    with profile(
            activities=[torch.profiler.ProfilerActivity.CPU, ],
            on_trace_ready=tensorboard_trace_handler('./logs'),
            profile_memory=True,
            record_shapes=True,
            with_stack=True
    ) as profiler:
        start = time.time()
        outputs = model(inputs)
        cost = time.time() - start
        print(f"predict_cost = {cost}")

        profiler.step()


analysis()

运行结束后使用命令进行可视化

 tensorboard.exe  --logdir .

结果提示如下错误:

W0421 10:34:30.495812 18184 loader.py:103] Failed to parse profile data for Run logs on DESKTOP-NQOQ6MO_20472. Exception=Invalid \escape: line 10 column 246 (char 421)
Traceback (most recent call last):
  File "d:\python3.7\lib\site-packages\torch_tb_profiler\profiler\data.py", line 126, in _preprocess_file
    trace_json = json.loads(data)
  File "d:\python3.7\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "d:\python3.7\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "d:\python3.7\lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid \escape: line 10 column 246 (char 421)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:\python3.7\lib\site-packages\torch_tb_profiler\profiler\data.py", line 131, in _preprocess_file
    trace_json = json.loads(data, strict=False)
  File "d:\python3.7\lib\json\__init__.py", line 361, in loads
    return cls(**kw).decode(s)
  File "d:\python3.7\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "d:\python3.7\lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid \escape: line 10 column 246 (char 421)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:\python3.7\lib\site-packages\torch_tb_profiler\profiler\loader.py", line 88, in _process_data
    data = RunProfileData.parse(worker, span, local_file, self.caches.cache_dir)
  File "d:\python3.7\lib\site-packages\torch_tb_profiler\profiler\data.py", line 101, in parse
    trace_path, trace_json = RunProfileData._preprocess_file(path, cache_dir)
  File "d:\python3.7\lib\site-packages\torch_tb_profiler\profiler\data.py", line 137, in _preprocess_file
    trace_json = json.loads(fout.getvalue())
  File "d:\python3.7\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "d:\python3.7\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "d:\python3.7\lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Invalid \escape: line 10 column 246 (char 421)
I0421 10:34:30.507813 24200 plugin.py:497] Run logs loaded
I0421 10:34:30.517814 19016 plugin.py:467] Add run logs
W0421 10:34:35.542793  7900 security_validator.py:46] In 3.0, this warning will become an error:
Requires default-src for Content-Security-Policy
W0421 10:34:35.543481  9116 security_validator.py:46] In 3.0, this warning will become an error:
Requires default-src for Content-Security-Policy

用Vscode 查看了下生成的json文件,文件里面内部的路径分割赴全是'\'(单个'\'在json中表示转义符),因而导致了报错,解决思路是将'\'替换为"\\"。使用noetpad++或者其他文本编辑器查找替换即可,notepad++的快捷键为ctrl+H(vscode没有替换成功,暂时不知道原因)。下面放一张成功的图:

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/718524.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存