Passed
Pull Request — master (#182)
by Fernando
01:01
created

GridAggregator.crop_batch()   A

Complexity

Conditions 2

Size

Total Lines 28
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 24
nop 4
dl 0
loc 28
rs 9.304
c 0
b 0
f 0
1
import warnings
2
from typing import Tuple
3
import torch
4
import numpy as np
5
from ...torchio import TypeData, CHANNELS_DIMENSION
6
from .grid_sampler import GridSampler
7
8
9
class GridAggregator:
10
    r"""Aggregate patches for dense inference.
11
12
    This class is typically used to build a volume made of batches after
13
    inference of patches extracted by a :py:class:`~torchio.data.GridSampler`.
14
15
    Args:
16
        sampler: Instance of :py:class:`~torchio.data.GridSampler` used to
17
            extract the patches.
18
19
    .. note:: Adapted from NiftyNet. See `this NiftyNet tutorial
20
        <https://niftynet.readthedocs.io/en/dev/window_sizes.html>`_ for more
21
        information.
22
    """
23
    def __init__(self, sampler: GridSampler):
24
        sample = sampler.sample
25
        self.spatial_shape = sample.spatial_shape
26
        self._output_tensor = None
27
        self.patch_overlap = sampler.patch_overlap
28
29
    def crop_batch(
30
            self,
31
            batch: torch.Tensor,
32
            location: np.ndarray,
33
            overlap: np.ndarray,
34
            ) -> Tuple[TypeData, np.ndarray]:
35
        border = np.array(overlap) // 2  # overlap is even in grid sampler
36
        crop_locations = location.astype(int).copy()
37
        spatial_shape = batch.shape[2:]  # ignore batch and channels dim
38
        indices_ini, indices_fin = crop_locations[:, :3], crop_locations[:, 3:]
39
        num_locations = len(crop_locations)
40
        border_ini = np.tile(border, (num_locations, 1))
41
        border_fin = border_ini.copy()
42
43
        # Do not crop patches at the border of the volume
44
        border_ini[indices_ini == 0] = 0
45
        for axis, size in enumerate(self.spatial_shape):
46
            border_fin[indices_fin[:, axis] == size, axis] = 0
47
48
        indices_ini += border
49
        indices_fin -= border
50
        cropped_shape = np.max(indices_fin - indices_ini, axis=0)
51
        diff = spatial_shape - cropped_shape
52
        left = np.floor(diff / 2).astype(np.int)
53
        i_ini, j_ini, k_ini = left
54
        i_fin, j_fin, k_fin = left + cropped_shape
55
        cropped_batch = batch[..., i_ini:i_fin, j_ini:j_fin, k_ini:k_fin]
56
        return cropped_batch, crop_locations
57
58
    def initialize_output_tensor(self, batch: torch.Tensor) -> None:
59
        if self._output_tensor is not None:
60
            return
61
        num_channels = batch.shape[CHANNELS_DIMENSION]
62
        self._output_tensor = torch.zeros(
63
            num_channels,
64
            *self.spatial_shape,
65
            dtype=batch.dtype,
66
        )
67
68
    def add_batch(self, batch: torch.Tensor, locations: TypeData) -> None:
69
        batch = batch.cpu()
70
        locations = locations.cpu().numpy()
71
        self.initialize_output_tensor(batch)
72
        cropped_batch, crop_locations = self.crop_batch(
73
            batch,
74
            locations,
75
            self.patch_overlap,
76
        )
77
        for patch, location in zip(cropped_batch, crop_locations):
78
            i_ini, j_ini, k_ini, i_fin, j_fin, k_fin = location
79
            for channel_idx, tensor in enumerate(patch):
80
                self._output_tensor[
81
                    channel_idx,
82
                    i_ini:i_fin,
83
                    j_ini:j_fin,
84
                    k_ini:k_fin] = tensor
85
86
    def get_output_tensor(self) -> torch.Tensor:
87
        if self._output_tensor.dtype == torch.int64:
88
            message = (
89
                'Medical image frameworks such as ITK do not support int64.'
90
                ' Casting to int32...'
91
            )
92
            warnings.warn(message)
93
            self._output_tensor = self._output_tensor.type(torch.int32)
94
        return self._output_tensor
95