Skip to content

Commit 79e0966

Browse files
committed
Autofix
Signed-off-by: Eric Kerfoot <[email protected]>
1 parent 28c7df2 commit 79e0966

File tree

5 files changed

+17
-19
lines changed

5 files changed

+17
-19
lines changed

monai/data/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@
7777
from .test_time_augmentation import TestTimeAugmentation
7878
from .thread_buffer import ThreadBuffer, ThreadDataLoader
7979
from .torchscript_utils import load_net_with_metadata, save_net_with_metadata
80-
from .utils import (
81-
# PICKLE_KEY_SUFFIX,
80+
from .utils import ( # PICKLE_KEY_SUFFIX,
8281
affine_to_spacing,
8382
compute_importance_map,
8483
compute_shape_offset,

monai/data/dataset.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
from __future__ import annotations
1313

1414
import collections.abc
15-
from io import BytesIO
1615
import math
17-
from pickle import UnpicklingError
1816
import shutil
1917
import sys
2018
import tempfile
@@ -23,9 +21,11 @@
2321
import warnings
2422
from collections.abc import Callable, Sequence
2523
from copy import copy, deepcopy
24+
from io import BytesIO
2625
from multiprocessing.managers import ListProxy
2726
from multiprocessing.pool import ThreadPool
2827
from pathlib import Path
28+
from pickle import UnpicklingError
2929
from typing import IO, TYPE_CHECKING, Any, cast
3030

3131
import numpy as np
@@ -254,7 +254,7 @@ def __init__(
254254
this arg is used by `torch.save`, for more details, please check:
255255
https://pytorch.org/docs/stable/generated/torch.save.html#torch.save,
256256
and ``monai.data.utils.SUPPORTED_PICKLE_MOD``.
257-
pickle_protocol: specifies pickle protocol when saving, with `torch.save`.
257+
pickle_protocol: specifies pickle protocol when saving, with `torch.save`.
258258
Defaults to torch.serialization.DEFAULT_PROTOCOL. For more details, please check:
259259
https://pytorch.org/docs/stable/generated/torch.save.html#torch.save.
260260
hash_transform: a callable to compute hash from the transform information when caching.
@@ -461,7 +461,7 @@ def __init__(
461461
this arg is used by `torch.save`, for more details, please check:
462462
https://pytorch.org/docs/stable/generated/torch.save.html#torch.save,
463463
and ``monai.data.utils.SUPPORTED_PICKLE_MOD``.
464-
pickle_protocol: specifies pickle protocol when saving, with `torch.save`.
464+
pickle_protocol: specifies pickle protocol when saving, with `torch.save`.
465465
Defaults to torch.serialization.DEFAULT_PROTOCOL. For more details, please check:
466466
https://pytorch.org/docs/stable/generated/torch.save.html#torch.save.
467467
hash_transform: a callable to compute hash from the transform information when caching.
@@ -557,7 +557,7 @@ def __init__(
557557
defaults to `monai.data.utils.pickle_hashing`.
558558
db_name: lmdb database file name. Defaults to "monai_cache".
559559
progress: whether to display a progress bar.
560-
pickle_protocol: specifies pickle protocol when saving, with `torch.save`.
560+
pickle_protocol: specifies pickle protocol when saving, with `torch.save`.
561561
Defaults to torch.serialization.DEFAULT_PROTOCOL. For more details, please check:
562562
https://pytorch.org/docs/stable/generated/torch.save.html#torch.save.
563563
hash_transform: a callable to compute hash from the transform information when caching.
@@ -601,15 +601,14 @@ def set_data(self, data: Sequence):
601601
super().set_data(data=data)
602602
self._read_env = self._fill_cache_start_reader(show_progress=self.progress)
603603

604-
def _safe_serialize(self,val):
605-
out=BytesIO()
606-
torch.save(convert_to_tensor(val), out, pickle_protocol =self.pickle_protocol)
604+
def _safe_serialize(self, val):
605+
out = BytesIO()
606+
torch.save(convert_to_tensor(val), out, pickle_protocol=self.pickle_protocol)
607607
out.seek(0)
608608
return out.read()
609609

610-
def _safe_deserialize(self,val):
611-
out=BytesIO(val)
612-
return torch.load(out,weights_only=True)
610+
def _safe_deserialize(self, val):
611+
return torch.load(BytesIO(val), map_location="cpu", weights_only=True)
613612

614613
def _fill_cache_start_reader(self, show_progress=True):
615614
"""
@@ -637,7 +636,7 @@ def _fill_cache_start_reader(self, show_progress=True):
637636
if val is None:
638637
val = self._pre_transform(deepcopy(item)) # keep the original hashed
639638
# val = pickle.dumps(val, protocol=self.pickle_protocol)
640-
val=self._safe_serialize(val)
639+
val = self._safe_serialize(val)
641640
with env.begin(write=True) as txn:
642641
txn.put(key, val)
643642
done = True

monai/data/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ def list_data_collate(batch: Sequence):
500500
collate_fn = default_collate
501501
try:
502502
# if config.USE_META_DICT:
503-
# data = pickle_operations(data) # bc 0.9.0
503+
# data = pickle_operations(data) # bc 0.9.0
504504
if isinstance(elem, Mapping):
505505
ret = {}
506506
for k in elem:
@@ -654,14 +654,14 @@ def decollate_batch(batch, detach: bool = True, pad=True, fill_value=None):
654654
_gen = zip_longest(*deco.values(), fillvalue=fill_value) if pad else zip(*deco.values())
655655
ret = [dict(zip(deco, item)) for item in _gen]
656656
# if not config.USE_META_DICT:
657-
# return ret
657+
# return ret
658658
# return pickle_operations(ret, is_encode=False) # bc 0.9.0
659659
return ret
660660
if isinstance(deco, Iterable):
661661
_gen = zip_longest(*deco, fillvalue=fill_value) if pad else zip(*deco)
662662
ret_list = [list(item) for item in _gen]
663663
# if not config.USE_META_DICT:
664-
# return ret_list
664+
# return ret_list
665665
# return pickle_operations(ret_list, is_encode=False) # bc 0.9.0
666666
return ret_list
667667
raise NotImplementedError(f"Unable to de-collate: {batch}, type: {type(batch)}.")

monai/utils/state_cacher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __init__(
6464
pickle_module: module used for pickling metadata and objects, default to `pickle`.
6565
this arg is used by `torch.save`, for more details, please check:
6666
https://pytorch.org/docs/stable/generated/torch.save.html#torch.save.
67-
pickle_protocol: specifies pickle protocol when saving, with `torch.save`.
67+
pickle_protocol: specifies pickle protocol when saving, with `torch.save`.
6868
Defaults to torch.serialization.DEFAULT_PROTOCOL. For more details, please check:
6969
https://pytorch.org/docs/stable/generated/torch.save.html#torch.save.
7070

monai/utils/type_conversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def convert_to_tensor(
117117
wrap_sequence: bool = False,
118118
track_meta: bool = False,
119119
safe: bool = False,
120-
convert_numeric: bool = True
120+
convert_numeric: bool = True,
121121
) -> Any:
122122
"""
123123
Utility to convert the input data to a PyTorch Tensor, if `track_meta` is True, the output will be a `MetaTensor`,

0 commit comments

Comments
 (0)