• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bethgelab / foolbox / 8137716344

22 Jan 2024 10:53PM UTC coverage: 98.47%. Remained the same
8137716344

push

github

web-flow
Bump pillow from 10.1.0 to 10.2.0 in /tests (#718)

Bumps [pillow](https://github.com/python-pillow/Pillow) from 10.1.0 to 10.2.0.
- [Release notes](https://github.com/python-pillow/Pillow/releases)
- [Changelog](https://github.com/python-pillow/Pillow/blob/main/CHANGES.rst)
- [Commits](https://github.com/python-pillow/Pillow/compare/10.1.0...10.2.0)

---
updated-dependencies:
- dependency-name: pillow
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

3475 of 3529 relevant lines covered (98.47%)

7.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/foolbox/attacks/gen_attack_utils.py
1
from typing import Union, List, Tuple
10✔
2
import eagerpy as ep
10✔
3

4

5
def rescale_jax(x: ep.JAXTensor, target_shape: List[int]) -> ep.JAXTensor:
10✔
6
    # img must be in channel_last format
7

8
    # modified according to https://github.com/google/jax/issues/862
9
    import jax.numpy as np
2✔
10

11
    img = x.raw
2✔
12

13
    resize_rates = (target_shape[1] / x.shape[1], target_shape[2] / x.shape[2])
2✔
14

15
    def interpolate_bilinear(
2✔
16
        im: np.ndarray, rows: np.ndarray, cols: np.ndarray
17
    ) -> np.ndarray:
18
        # based on http://stackoverflow.com/a/12729229
19
        col_lo = np.floor(cols).astype(int)
2✔
20
        col_hi = col_lo + 1
2✔
21
        row_lo = np.floor(rows).astype(int)
2✔
22
        row_hi = row_lo + 1
2✔
23

24
        def cclip(cols: np.ndarray) -> np.ndarray:
2✔
25
            return np.clip(cols, 0, ncols - 1)  # type: ignore
2✔
26

27
        def rclip(rows: np.ndarray) -> np.ndarray:
2✔
28
            return np.clip(rows, 0, nrows - 1)  # type: ignore
2✔
29

30
        nrows, ncols = im.shape[-3:-1]
2✔
31

32
        Ia = im[..., rclip(row_lo), cclip(col_lo), :]
2✔
33
        Ib = im[..., rclip(row_hi), cclip(col_lo), :]
2✔
34
        Ic = im[..., rclip(row_lo), cclip(col_hi), :]
2✔
35
        Id = im[..., rclip(row_hi), cclip(col_hi), :]
2✔
36

37
        wa = np.expand_dims((col_hi - cols) * (row_hi - rows), -1)
2✔
38
        wb = np.expand_dims((col_hi - cols) * (rows - row_lo), -1)
2✔
39
        wc = np.expand_dims((cols - col_lo) * (row_hi - rows), -1)
2✔
40
        wd = np.expand_dims((cols - col_lo) * (rows - row_lo), -1)
2✔
41

42
        return wa * Ia + wb * Ib + wc * Ic + wd * Id
2✔
43

44
    nrows, ncols = img.shape[-3:-1]
2✔
45
    deltas = (0.5 / resize_rates[0], 0.5 / resize_rates[1])
2✔
46

47
    rows = np.linspace(deltas[0], nrows - deltas[0], int(resize_rates[0] * nrows))
2✔
48
    cols = np.linspace(deltas[1], ncols - deltas[1], int(resize_rates[1] * ncols))
2✔
49
    rows_grid, cols_grid = np.meshgrid(rows - 0.5, cols - 0.5, indexing="ij")
2✔
50

51
    img_resize_vec = interpolate_bilinear(img, rows_grid.flatten(), cols_grid.flatten())
2✔
52
    img_resize = np.reshape(
2✔
53
        img_resize_vec, img.shape[:-3] + (len(rows), len(cols)) + img.shape[-1:]
54
    )
55

56
    return ep.JAXTensor(img_resize)
2✔
57

58

59
def rescale_numpy(x: ep.NumPyTensor, target_shape: List[int]) -> ep.NumPyTensor:
10✔
60
    import numpy as np
6✔
61

62
    img = x.raw
6✔
63

64
    resize_rates = (target_shape[1] / x.shape[1], target_shape[2] / x.shape[2])
6✔
65

66
    def interpolate_bilinear(
6✔
67
        im: np.ndarray, rows: np.ndarray, cols: np.ndarray
68
    ) -> np.ndarray:
69
        # based on http://stackoverflow.com/a/12729229
70
        col_lo = np.floor(cols).astype(int)
6✔
71
        col_hi = col_lo + 1
6✔
72
        row_lo = np.floor(rows).astype(int)
6✔
73
        row_hi = row_lo + 1
6✔
74

75
        def cclip(cols: np.ndarray) -> np.ndarray:
6✔
76
            return np.clip(cols, 0, ncols - 1)
6✔
77

78
        def rclip(rows: np.ndarray) -> np.ndarray:
6✔
79
            return np.clip(rows, 0, nrows - 1)
6✔
80

81
        nrows, ncols = im.shape[-3:-1]
6✔
82

83
        Ia = im[..., rclip(row_lo), cclip(col_lo), :]
6✔
84
        Ib = im[..., rclip(row_hi), cclip(col_lo), :]
6✔
85
        Ic = im[..., rclip(row_lo), cclip(col_hi), :]
6✔
86
        Id = im[..., rclip(row_hi), cclip(col_hi), :]
6✔
87

88
        wa = np.expand_dims((col_hi - cols) * (row_hi - rows), -1)
6✔
89
        wb = np.expand_dims((col_hi - cols) * (rows - row_lo), -1)
6✔
90
        wc = np.expand_dims((cols - col_lo) * (row_hi - rows), -1)
6✔
91
        wd = np.expand_dims((cols - col_lo) * (rows - row_lo), -1)
6✔
92

93
        return wa * Ia + wb * Ib + wc * Ic + wd * Id  # type: ignore
6✔
94

95
    nrows, ncols = img.shape[-3:-1]
6✔
96
    deltas = (0.5 / resize_rates[0], 0.5 / resize_rates[1])
6✔
97

98
    rows = np.linspace(deltas[0], nrows - deltas[0], np.int32(resize_rates[0] * nrows))
6✔
99
    cols = np.linspace(deltas[1], ncols - deltas[1], np.int32(resize_rates[1] * ncols))
6✔
100
    rows_grid, cols_grid = np.meshgrid(rows - 0.5, cols - 0.5, indexing="ij")
6✔
101

102
    img_resize_vec = interpolate_bilinear(img, rows_grid.flatten(), cols_grid.flatten())
6✔
103
    img_resize = img_resize_vec.reshape(
6✔
104
        img.shape[:-3] + (len(rows), len(cols)) + img.shape[-1:]
105
    )
106

107
    return ep.NumPyTensor(img_resize)
6✔
108

109

110
def rescale_tensorflow(
10✔
111
    x: ep.TensorFlowTensor, target_shape: List[int]
112
) -> ep.TensorFlowTensor:
113
    import tensorflow
2✔
114

115
    img = x.raw
2✔
116

117
    img_resized = tensorflow.image.resize(img, size=target_shape[1:-1])
2✔
118

119
    return ep.TensorFlowTensor(img_resized)
2✔
120

121

122
def rescale_pytorch(x: ep.PyTorchTensor, target_shape: List[int]) -> ep.PyTorchTensor:
10✔
123
    import torch
2✔
124

125
    img = x.raw
2✔
126

127
    img_resized = torch.nn.functional.interpolate(
2✔
128
        img, size=target_shape[2:], mode="bilinear", align_corners=False
129
    )
130

131
    return ep.PyTorchTensor(img_resized)
2✔
132

133

134
def swap_axes(x: ep.TensorType, dim0: int, dim1: int) -> ep.TensorType:
10✔
135
    assert dim0 < x.ndim
6✔
136
    assert dim1 < x.ndim
6✔
137

138
    axes = list(range(x.ndim))
6✔
139
    axes[dim0] = dim1
6✔
140
    axes[dim1] = dim0
6✔
141

142
    return ep.transpose(x, tuple(axes))
6✔
143

144

145
def rescale_images(
10✔
146
    x: ep.TensorType, target_shape: Union[Tuple[int, ...], List[int]], channel_axis: int
147
) -> ep.TensorType:
148
    target_shape = list(target_shape)
6✔
149

150
    if channel_axis < 0:
6✔
151
        channel_axis = x.ndim - 1 + channel_axis
6✔
152

153
    if isinstance(x, ep.PyTorchTensor):
6✔
154
        if channel_axis != 1:
2✔
155
            x = swap_axes(x, channel_axis, 1)  # type: ignore
2✔
156

157
            target_shape[channel_axis], target_shape[1] = (
2✔
158
                target_shape[1],
159
                target_shape[channel_axis],
160
            )
161

162
        x = rescale_pytorch(x, target_shape)  # type: ignore
2✔
163

164
        if channel_axis != 1:
2✔
165
            x = swap_axes(x, channel_axis, 1)  # type: ignore
2✔
166

167
    elif isinstance(x, ep.TensorFlowTensor):
6✔
168
        if channel_axis != x.ndim - 1:
2✔
169
            x = swap_axes(x, channel_axis, x.ndim - 1)  # type: ignore
2✔
170

171
            target_shape[channel_axis], target_shape[x.ndim - 1] = (
2✔
172
                target_shape[x.ndim - 1],
173
                target_shape[channel_axis],
174
            )
175

176
        x = rescale_tensorflow(x, target_shape)  # type: ignore
2✔
177

178
        if channel_axis != x.ndim - 1:
2✔
179
            x = swap_axes(x, channel_axis, x.ndim - 1)  # type: ignore
2✔
180

181
    elif isinstance(x, ep.NumPyTensor):
6✔
182
        if channel_axis != x.ndim - 1:
6✔
183
            x = swap_axes(x, channel_axis, x.ndim - 1)  # type: ignore
6✔
184

185
            target_shape[channel_axis], target_shape[x.ndim - 1] = (
6✔
186
                target_shape[x.ndim - 1],
187
                target_shape[channel_axis],
188
            )
189

190
        x = rescale_numpy(x, target_shape)  # type: ignore
6✔
191
        if channel_axis != x.ndim - 1:
6✔
192
            x = swap_axes(x, channel_axis, x.ndim - 1)  # type: ignore
6✔
193

194
    elif isinstance(x, ep.JAXTensor):
2✔
195
        if channel_axis != x.ndim - 1:
2✔
196
            x = swap_axes(x, channel_axis, x.ndim - 1)  # type: ignore
2✔
197

198
            target_shape[channel_axis], target_shape[x.ndim - 1] = (
2✔
199
                target_shape[x.ndim - 1],
200
                target_shape[channel_axis],
201
            )
202

203
        x = rescale_jax(x, target_shape)  # type: ignore
2✔
204
        if channel_axis != x.ndim - 1:
2✔
205
            x = swap_axes(x, channel_axis, x.ndim - 1)  # type: ignore
2✔
206

207
    return x
6✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc