• 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/binarization.py
1
from typing import Union, Optional, Any
10✔
2
from typing_extensions import Literal
10✔
3
import eagerpy as ep
10✔
4
import numpy as np
10✔
5

6
from ..models import Model
10✔
7

8
from ..criteria import Criterion
10✔
9

10
from ..distances import Distance
10✔
11

12
from .base import FlexibleDistanceMinimizationAttack
10✔
13
from .base import T
10✔
14
from .base import get_is_adversarial
10✔
15
from .base import get_criterion
10✔
16
from .base import raise_if_kwargs
10✔
17
from .base import verify_input_bounds
10✔
18

19

20
class BinarizationRefinementAttack(FlexibleDistanceMinimizationAttack):
10✔
21
    """For models that preprocess their inputs by binarizing the
22
    inputs, this attack can improve adversarials found by other
23
    attacks. It does this by utilizing information about the
24
    binarization and mapping values to the corresponding value in
25
    the clean input or to the right side of the threshold.
26

27
    Args:
28
        threshold : The threshold used by the models binarization. If none,
29
            defaults to (model.bounds()[1] - model.bounds()[0]) / 2.
30
        included_in : Whether the threshold value itself belongs to the lower or
31
            upper interval.
32
    """
33

34
    def __init__(
10✔
35
        self,
36
        *,
37
        distance: Optional[Distance] = None,
38
        threshold: Optional[float] = None,
39
        included_in: Union[Literal["lower"], Literal["upper"]] = "upper",
40
    ):
41
        super().__init__(distance=distance)
6✔
42
        self.threshold = threshold
6✔
43
        self.included_in = included_in
6✔
44

45
    def run(
10✔
46
        self,
47
        model: Model,
48
        inputs: T,
49
        criterion: Union[Criterion, T],
50
        *,
51
        early_stop: Optional[float] = None,
52
        starting_points: Optional[T] = None,
53
        **kwargs: Any,
54
    ) -> T:
55
        raise_if_kwargs(kwargs)
6✔
56
        if starting_points is None:
6✔
57
            raise ValueError("BinarizationRefinementAttack requires starting_points")
6✔
58
        (o, x), restore_type = ep.astensors_(inputs, starting_points)
6✔
59
        del inputs, starting_points, kwargs
6✔
60

61
        verify_input_bounds(x, model)
6✔
62

63
        criterion = get_criterion(criterion)
6✔
64
        is_adversarial = get_is_adversarial(criterion, model)
6✔
65

66
        if self.threshold is None:
6✔
67
            min_, max_ = model.bounds
6✔
68
            threshold = (min_ + max_) / 2.0
6✔
69
        else:
70
            threshold = self.threshold
6✔
71

72
        assert o.dtype == x.dtype
6✔
73

74
        nptype = o.reshape(-1)[0].numpy().dtype.type
6✔
75
        if nptype not in [np.float16, np.float32, np.float64]:
6✔
76
            raise ValueError(  # pragma: no cover
77
                f"expected dtype to be float16, float32 or float64, found '{nptype}'"
78
            )
79

80
        threshold = nptype(threshold)
6✔
81
        offset = nptype(1.0)
6✔
82

83
        if self.included_in == "lower":
6✔
84
            lower_ = threshold
6✔
85
            upper_ = np.nextafter(threshold, threshold + offset)
6✔
86
        elif self.included_in == "upper":
6✔
87
            lower_ = np.nextafter(threshold, threshold - offset)
6✔
88
            upper_ = threshold
6✔
89
        else:
90
            raise ValueError(
6✔
91
                f"expected included_in to be 'lower' or 'upper', found '{self.included_in}'"
92
            )
93

94
        assert lower_ < upper_
6✔
95

96
        p = ep.full_like(o, ep.nan)
6✔
97

98
        lower = ep.ones_like(o) * lower_
6✔
99
        upper = ep.ones_like(o) * upper_
6✔
100

101
        indices = ep.logical_and(o <= lower, x <= lower)
6✔
102
        p = ep.where(indices, o, p)
6✔
103

104
        indices = ep.logical_and(o <= lower, x >= upper)
6✔
105
        p = ep.where(indices, upper, p)
6✔
106

107
        indices = ep.logical_and(o >= upper, x <= lower)
6✔
108
        p = ep.where(indices, lower, p)
6✔
109

110
        indices = ep.logical_and(o >= upper, x >= upper)
6✔
111
        p = ep.where(indices, o, p)
6✔
112

113
        assert not ep.any(ep.isnan(p))
6✔
114

115
        is_adv1 = is_adversarial(x)
6✔
116
        is_adv2 = is_adversarial(p)
6✔
117
        if (is_adv1 != is_adv2).any():
6✔
118
            raise ValueError(
6✔
119
                "The specified threshold does not match what is done by the model."
120
            )
121
        return restore_type(p)
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