Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deserialization by applying RoiWrapper #56

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions torch_em/data/raw_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def compute_len(shape, patch_shape):
n_samples = int(np.prod([float(sh / csh) for sh, csh in zip(shape, patch_shape)]))
return n_samples

@staticmethod
def apply_roi(data, roi, with_channels):
if roi is not None:
if isinstance(roi, slice):
roi = (roi,)
return RoiWrapper(data, (slice(None),) + roi) if with_channels else RoiWrapper(data, roi)
return data

def __init__(
self,
raw_path,
Expand All @@ -38,10 +46,7 @@ def __init__(

self._with_channels = with_channels

if roi is not None:
if isinstance(roi, slice):
roi = (roi,)
self.raw = RoiWrapper(self.raw, (slice(None),) + roi) if self._with_channels else RoiWrapper(self.raw, roi)
self.raw = self.apply_roi(self.raw, roi, self._with_channels)

self.shape = self.raw.shape[1:] if self._with_channels else self.raw.shape
self.roi = roi
Expand Down Expand Up @@ -138,8 +143,11 @@ def __getstate__(self):

def __setstate__(self, state):
raw_path, raw_key = state["raw_path"], state["raw_key"]
try:
state["raw"] = open_file(state["raw_path"], mode="r")[state["raw_key"]]
try: # can still use model even if data is not available anymore
raw = open_file(state["raw_path"], mode="r")[state["raw_key"]]
roi = state["roi"]
with_channels = state["_with_channels"]
state["raw"] = self.apply_roi(raw, roi, with_channels)
except Exception:
msg = f"RawDataset could not be deserialized because of missing {raw_path}, {raw_key}.\n"
msg += "The dataset is deserialized in order to allow loading trained models from a checkpoint.\n"
Expand Down
28 changes: 19 additions & 9 deletions torch_em/data/segmentation_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def compute_len(shape, patch_shape):
n_samples = int(np.prod([float(sh / csh) for sh, csh in zip(shape, patch_shape)]))
return n_samples

@staticmethod
def apply_roi(data, roi, with_channels):
if roi is not None:
if isinstance(roi, slice):
roi = (roi,)
return RoiWrapper(data, (slice(None),) + roi) if with_channels else RoiWrapper(data, roi)
return data

def __init__(
self,
raw_path,
Expand Down Expand Up @@ -49,12 +57,8 @@ def __init__(
self._with_channels = with_channels
self._with_label_channels = with_label_channels

if roi is not None:
if isinstance(roi, slice):
roi = (roi,)
self.raw = RoiWrapper(self.raw, (slice(None),) + roi) if self._with_channels else RoiWrapper(self.raw, roi)
self.labels = RoiWrapper(self.labels, (slice(None),) + roi) if self._with_label_channels else\
RoiWrapper(self.labels, roi)
self.raw = self.apply_roi(self.raw, roi, self._with_channels)
self.labels = self.apply_roi(self.labels, roi, self._with_label_channels)

shape_raw = self.raw.shape[1:] if self._with_channels else self.raw.shape
shape_label = self.labels.shape[1:] if self._with_label_channels else self.labels.shape
Expand Down Expand Up @@ -173,16 +177,22 @@ def __getstate__(self):
def __setstate__(self, state):
raw_path, raw_key = state["raw_path"], state["raw_key"]
label_path, label_key = state["label_path"], state["label_key"]
try:
state["raw"] = open_file(raw_path, mode="r")[raw_key]
try: # can still use model even if data is not available anymore
raw = open_file(state["raw_path"], mode="r")[state["raw_key"]]
roi = state["roi"]
with_channels = state["_with_channels"]
state["raw"] = self.apply_roi(raw, roi, with_channels)
except Exception:
msg = f"SegmentationDataset could not be deserialized because of missing {raw_path}, {raw_key}.\n"
msg += "The dataset is deserialized in order to allow loading trained models from a checkpoint.\n"
msg += "But it cannot be used for further training and wil throw an error."
warnings.warn(msg)
state["raw"] = None
try:
state["labels"] = open_file(label_path, mode="r")[label_key]
labels = open_file(state["label_path"], mode="r")[state["label_key"]]
roi = state["roi"]
with_channels = state["_with_channels"]
state["labels"] = self.apply_roi(labels, roi, with_channels)
except Exception:
msg = f"SegmentationDataset could not be deserialized because of missing {label_path}, {label_key}.\n"
msg += "The dataset is deserialized in order to allow loading trained models from a checkpoint.\n"
Expand Down