调用model = torch.load(‘*.pth’)时报错:OSError: [Errno 22] Invalid argument

一、问题描述

在使用torch.load()出现:line 581, in _load deserialized_objects[key]._set_from_file(f, offset, f_should_read_directly)
OSError: [Errno 22] Invalid argument

错误详情信息及截图如下:

Traceback (most recent call last):
  File "D:/Rio/test_nature/test_model.py", line 40, in 
    model = torch.load(model_path)
  File "E:\Anaconda\envs\tensorflow-gpu\lib\site-packages\torch\serialization.py", line 386, in load
    return _load(f, map_location, pickle_module, **pickle_load_args)
  File "E:\Anaconda\envs\tensorflow-gpu\lib\site-packages\torch\serialization.py", line 581, in _load
    deserialized_objects[key]._set_from_file(f, offset, f_should_read_directly)
OSError: [Errno 22] Invalid argument

调用model = torch.load(‘*.pth’)时报错:OSError: [Errno 22] Invalid argument_第1张图片

二、解决方案

1、通过如下报错信息找到serialization.py文件这个python文件下的如下代码块:

(pycharm可直接点击文件路径获得serialization.py文件)

  File "E:\Anaconda\envs\tensorflow-gpu\lib\site-packages\torch\serialization.py", line 581, in _load
    deserialized_objects[key]._set_from_file(f, offset, f_should_read_directly)
OSError: [Errno 22] Invalid argument

2. 找到如下代码块:(在serialization.py的最后面

    offset = f.tell() if f_should_read_directly else None
    for key in deserialized_storage_keys:
        assert key in deserialized_objects
        deserialized_objects[key]._set_from_file(f, offset, f_should_read_directly)
        if offset is not None:
            offset = f.tell()

3. 在代码块前添加:f_should_read_directly = False,结果如下:

    f_should_read_directly = False # 添加该句
    offset = f.tell() if f_should_read_directly else None
    for key in deserialized_storage_keys:
        assert key in deserialized_objects
        deserialized_objects[key]._set_from_file(f, offset, f_should_read_directly)
        if offset is not None:
            offset = f.tell()

三、原因分析

Windows限制了读取模型文件的大小,当读取模型大于2G时就会报这个错误,Linux下不会。

参考链接:

https://www.codeleading.com/article/55452592906/

你可能感兴趣的:(深度学习项目实战)