MCPcopy Create free account
hub / github.com/ddbourgin/numpy-ml / batch_resample

Function batch_resample

numpy_ml/preprocessing/dsp.py:11–50  ·  view source on GitHub ↗

Resample each image (or similar grid-based 2D signal) in a batch to `new_dim` using the specified resampling strategy. Parameters ---------- X : :py:class:`ndarray ` of shape `(n_ex, in_rows, in_cols, in_channels)` An input image volume new_dim : 2-tu

(X, new_dim, mode="bilinear")

Source from the content-addressed store, hash-verified

9
10
11def batch_resample(X, new_dim, mode="bilinear"):
12 """
13 Resample each image (or similar grid-based 2D signal) in a batch to
14 `new_dim` using the specified resampling strategy.
15
16 Parameters
17 ----------
18 X : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, in_rows, in_cols, in_channels)`
19 An input image volume
20 new_dim : 2-tuple of `(out_rows, out_cols)`
21 The dimension to resample each image to
22 mode : {'bilinear', 'neighbor'}
23 The resampling strategy to employ. Default is 'bilinear'.
24
25 Returns
26 -------
27 resampled : :py:class:`ndarray <numpy.ndarray>` of shape `(n_ex, out_rows, out_cols, in_channels)`
28 The resampled image volume.
29 """
30 if mode == "bilinear":
31 interpolate = bilinear_interpolate
32 elif mode == "neighbor":
33 interpolate = nn_interpolate_2D
34 else:
35 raise NotImplementedError("Unrecognized resampling mode: {}".format(mode))
36
37 out_rows, out_cols = new_dim
38 n_ex, in_rows, in_cols, n_in = X.shape
39
40 # compute coordinates to resample
41 x = np.tile(np.linspace(0, in_cols - 2, out_cols), out_rows)
42 y = np.repeat(np.linspace(0, in_rows - 2, out_rows), out_cols)
43
44 # resample each image
45 resampled = []
46 for i in range(n_ex):
47 r = interpolate(X[i, ...], x, y)
48 r = r.reshape(out_rows, out_cols, n_in)
49 resampled.append(r)
50 return np.dstack(resampled)
51
52
53def nn_interpolate_2D(X, x, y):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected