本文整理汇总了Python中torch.serialization方法的典型用法代码示例。如果您正苦于以下问题:Python torch.serialization方法的具体用法?Python torch.serialization怎么用?Python torch.serialization使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch
的用法示例。
在下文中一共展示了torch.serialization方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: restricted_loads
# 需要导入模块: import torch [as 别名]
# 或者: from torch import serialization [as 别名]
def restricted_loads(s):
result = RestrictedUnpickler(io.BytesIO(s)).load()
if torch.is_tensor(result) or isinstance(result, torch.nn.Module):
_check_hooks_are_valid(result, "_backward_hooks")
return result
# Adapt torch.load to use RestrictedUnpickler - patched for torch.storage._load_from_bytes
# (Adapted from https://github.com/pytorch/pytorch/blob/master/torch/serialization.py#L602-L773)
示例2: _patch_model_io
# 需要导入模块: import torch [as 别名]
# 或者: from torch import serialization [as 别名]
def _patch_model_io():
if PatchPyTorchModelIO.__patched:
return
if 'torch' not in sys.modules:
return
PatchPyTorchModelIO.__patched = True
# noinspection PyBroadException
try:
import torch
torch.save = _patched_call(torch.save, PatchPyTorchModelIO._save)
torch.load = _patched_call(torch.load, PatchPyTorchModelIO._load)
# no need to worry about recursive calls, _patched_call takes care of that
if hasattr(torch, 'serialization') and hasattr(torch.serialization, '_save'):
torch.serialization._save = _patched_call(
torch.serialization._save, PatchPyTorchModelIO._save)
if hasattr(torch, 'serialization') and hasattr(torch.serialization, '_load'):
torch.serialization._load = _patched_call(
torch.serialization._load, PatchPyTorchModelIO._load)
if hasattr(torch, 'serialization') and hasattr(torch.serialization, '_legacy_save'):
torch.serialization._legacy_save = _patched_call(
torch.serialization._legacy_save, PatchPyTorchModelIO._save)
if hasattr(torch, 'serialization') and hasattr(torch.serialization, '_legacy_load'):
torch.serialization._legacy_load = _patched_call(
torch.serialization._legacy_load, PatchPyTorchModelIO._load)
except ImportError:
pass
except Exception:
pass # print('Failed patching pytorch')
示例3: build_model
# 需要导入模块: import torch [as 别名]
# 或者: from torch import serialization [as 别名]
def build_model(self, args):
model = super().build_model(args)
if args.pretrained is not None: # load pretrained model:
if not os.path.exists(args.pretrained):
raise ValueError('Could not load pretrained weights \
- from {}'.format(args.pretrained))
from torch.serialization import default_restore_location
saved_state = torch.load(
args.pretrained,
map_location=lambda s, l: default_restore_location(s, 'cpu')
)
self.adapt_state(saved_state['model'], model)
return model