• 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/utils.py
1
from typing import Optional, Tuple, Any
10✔
2
import eagerpy as ep
10✔
3
import warnings
10✔
4
import os
10✔
5
import numpy as np
10✔
6

7
from .types import Bounds
10✔
8
from .models import Model
10✔
9

10

11
def accuracy(fmodel: Model, inputs: Any, labels: Any) -> float:
10✔
12
    inputs_, labels_ = ep.astensors(inputs, labels)
8✔
13
    del inputs, labels
8✔
14

15
    predictions = fmodel(inputs_).argmax(axis=-1)
8✔
16
    accuracy = (predictions == labels_).float32().mean()
8✔
17
    return accuracy.item()
8✔
18

19

20
def samples(
10✔
21
    fmodel: Model,
22
    dataset: str = "imagenet",
23
    index: int = 0,
24
    batchsize: int = 1,
25
    shape: Tuple[int, int] = (224, 224),
26
    data_format: Optional[str] = None,
27
    bounds: Optional[Bounds] = None,
28
) -> Any:
29
    if hasattr(fmodel, "data_format"):
10✔
30
        if data_format is None:
8✔
31
            data_format = fmodel.data_format
8✔
32
        elif data_format != fmodel.data_format:
8✔
33
            raise ValueError(
8✔
34
                f"data_format ({data_format}) does not match model.data_format ({fmodel.data_format})"
35
            )
36
    elif data_format is None:
10✔
37
        raise ValueError(
2✔
38
            "data_format could not be inferred, please specify it explicitly"
39
        )
40

41
    if bounds is None:
10✔
42
        bounds = fmodel.bounds
10✔
43

44
    images, labels = _samples(
10✔
45
        dataset=dataset,
46
        index=index,
47
        batchsize=batchsize,
48
        shape=shape,
49
        data_format=data_format,
50
        bounds=bounds,
51
    )
52

53
    if hasattr(fmodel, "dummy") and fmodel.dummy is not None:
10✔
54
        images = ep.from_numpy(fmodel.dummy, images).raw
6✔
55
        labels = ep.from_numpy(fmodel.dummy, labels).raw
6✔
56
    else:
57
        warnings.warn(f"unknown model type {type(fmodel)}, returning NumPy arrays")
10✔
58

59
    return images, labels
10✔
60

61

62
def _samples(
10✔
63
    dataset: str,
64
    index: int,
65
    batchsize: int,
66
    shape: Tuple[int, int],
67
    data_format: str,
68
    bounds: Bounds,
69
) -> Tuple[Any, Any]:
70
    # TODO: this was copied from foolbox v2
71

72
    from PIL import Image
10✔
73

74
    images, labels = [], []
10✔
75
    basepath = os.path.dirname(__file__)
10✔
76
    samplepath = os.path.join(basepath, "data")
10✔
77
    files = os.listdir(samplepath)
10✔
78

79
    if batchsize > 20:
10✔
80
        warnings.warn(
8✔
81
            "samples() has only 20 samples and repeats itself if batchsize > 20"
82
        )
83

84
    for idx in range(index, index + batchsize):
10✔
85
        i = idx % 20
10✔
86

87
        # get filename and label
88
        file = [n for n in files if f"{dataset}_{i:02d}_" in n][0]
10✔
89
        label = int(file.split(".")[0].split("_")[-1])
10✔
90

91
        # open file
92
        path = os.path.join(samplepath, file)
10✔
93
        image = Image.open(path)
10✔
94

95
        if dataset == "imagenet":
10✔
96
            image = image.resize(shape)
10✔
97

98
        image = np.asarray(image, dtype=np.float32)
10✔
99

100
        if image.ndim == 2:
10✔
101
            image = image[..., np.newaxis]
8✔
102

103
        assert image.ndim == 3
10✔
104

105
        if data_format == "channels_first":
10✔
106
            image = np.transpose(image, (2, 0, 1))
10✔
107

108
        images.append(image)
10✔
109
        labels.append(label)
10✔
110

111
    images_ = np.stack(images)
10✔
112
    labels_ = np.array(labels).astype(np.int64)
10✔
113

114
    if bounds != (0, 255):
10✔
115
        images_ = images_ / 255 * (bounds[1] - bounds[0]) + bounds[0]
10✔
116
    return images_, labels_
10✔
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

© 2026 Coveralls, Inc