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

bethgelab / foolbox / 8139141456

04 Mar 2024 11:03AM UTC coverage: 37.923% (-60.6%) from 98.477%
8139141456

Pull #722

github

web-flow
Merge 5663238db into 17e0e9b31
Pull Request #722: Fix guide compilation

1344 of 3544 relevant lines covered (37.92%)

0.38 hits per line

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

75.86
/foolbox/utils.py
1
from typing import Optional, Tuple, Any
1✔
2
import eagerpy as ep
1✔
3
import warnings
1✔
4
import os
1✔
5
import numpy as np
1✔
6

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

10

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

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

19

20
def samples(
1✔
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"):
1✔
30
        if data_format is None:
×
31
            data_format = fmodel.data_format
×
32
        elif data_format != fmodel.data_format:
×
33
            raise ValueError(
×
34
                f"data_format ({data_format}) does not match model.data_format ({fmodel.data_format})"
35
            )
36
    elif data_format is None:
1✔
37
        raise ValueError(
×
38
            "data_format could not be inferred, please specify it explicitly"
39
        )
40

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

44
    images, labels = _samples(
1✔
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:
1✔
54
        images = ep.from_numpy(fmodel.dummy, images).raw
×
55
        labels = ep.from_numpy(fmodel.dummy, labels).raw
×
56
    else:
57
        warnings.warn(f"unknown model type {type(fmodel)}, returning NumPy arrays")
1✔
58

59
    return images, labels
1✔
60

61

62
def _samples(
1✔
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
1✔
73

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

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

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

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

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

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

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

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

103
        assert image.ndim == 3
1✔
104

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

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

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

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