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

cosanlab / py-feat / 15090929758

19 Oct 2024 05:10AM UTC coverage: 54.553%. First build
15090929758

push

github

web-flow
Merge pull request #228 from cosanlab/huggingface

WIP: Huggingface Integration

702 of 1620 new or added lines in 46 files covered. (43.33%)

3409 of 6249 relevant lines covered (54.55%)

3.27 hits per line

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

80.36
/feat/transforms.py
1
"""
2
Custom transforms for torch.Datasets
3
"""
4

5
from torchvision.transforms import Compose, Resize, Pad
6✔
6
import numpy as np
6✔
7

8

9
class Rescale(object):
6✔
10
    """Rescale the image in a sample to a given size.
11

12
    Args:
13
        output_size (tuple or int): Desired output size. If tuple, output is
14
                                    matched to output_size. If int, will set largest edge
15
                                    to output_size if target size is bigger,
16
                                    or smallest edge if target size is smaller
17
                                    to keep aspect ratio the same.
18
        preserve_aspect_ratio (bool): Output size is matched to preserve aspect ratio.
19
                                    Note that longest edge of output size is preserved,
20
                                    but actual output may differ from intended output_size.
21
        padding (bool): Transform image to exact output_size. If tuple,
22
                        will preserve aspect ratio by adding padding.
23
                        If int, will set both sides to the same size.
24

25
    Returns:
26
        dict: {'Image':transformed tensor, 'Scale':image scaling for transformation}
27

28
    """
29

30
    def __init__(self, output_size, preserve_aspect_ratio=True, padding=False):
6✔
31
        if not isinstance(output_size, (int, tuple)):
6✔
NEW
32
            raise ValueError(f"output_size must be (int, tuple) not {type(output_size)}.")
×
33

34
        self.output_size = output_size
6✔
35
        self.preserve_aspect_ratio = preserve_aspect_ratio
6✔
36
        self.padding = padding
6✔
37

38
    def __call__(self, image):
6✔
39
        height, width = image.shape[-2:]
6✔
40

41
        if isinstance(self.output_size, int):
6✔
42
            scale = self.output_size / max(height, width)
6✔
43
            new_height, new_width = (scale * np.array([height, width])).astype(int)
6✔
44
        else:
45
            scale = max(self.output_size) / max(height, width)
6✔
46
            new_height, new_width = np.array(self.output_size).astype(int)
6✔
47

48
        if self.preserve_aspect_ratio or self.padding:
6✔
49
            # Calculate Scaling Value
50
            if isinstance(self.output_size, int):
6✔
51
                scale = self.output_size / max(height, width)
6✔
52
            else:
53
                if (
6✔
54
                    new_height >= height & new_width >= width
55
                ):  # output size is bigger than image
56
                    if height > width:
×
57
                        scale = new_height / height
×
58
                    else:
59
                        scale = new_width / width
×
60
                else:  # output size is smaller than image
61
                    if (height > new_height) & (width <= new_width):
6✔
62
                        scale = new_height / height
×
63
                    elif (width > new_width) & (height <= new_height):
6✔
64
                        scale = new_width / width
×
65
                    else:
66
                        if height > width:
6✔
67
                            scale = new_height / height
×
68
                        else:
69
                            scale = new_width / width
6✔
70

71
            # Compute new height and width
72
            if isinstance(self.output_size, int):
6✔
73
                new_height = int(height * scale)
6✔
74
                new_width = int(width * scale)
6✔
75
            else:
76
                new_height, new_width = (scale * np.array([height, width])).astype(int)
6✔
77

78
            if self.padding:
6✔
79
                if isinstance(self.output_size, int):
6✔
80
                    output_height, output_width = (self.output_size, self.output_size)
6✔
81
                else:
82
                    output_height, output_width = self.output_size
6✔
83

84
                if new_height < output_height:
6✔
85
                    padding_height = output_height - new_height
6✔
86
                    if (padding_height) % 2 == 0:
6✔
87
                        padding_top, padding_bottom = [int(padding_height / 2)] * 2
6✔
88
                    else:
89
                        padding_top, padding_bottom = (
6✔
90
                            padding_height // 2,
91
                            1 + (padding_height // 2),
92
                        )
93
                else:
94
                    padding_top, padding_bottom = (0, 0)
6✔
95
                if new_width < output_width:
6✔
96
                    padding_width = output_width - new_width
×
97
                    if (padding_width) % 2 == 0:
×
98
                        padding_left, padding_right = [int(padding_width / 2)] * 2
×
99
                    else:
100
                        padding_left, padding_right = (
×
101
                            padding_width // 2,
102
                            1 + (padding_width // 2),
103
                        )
104
                else:
105
                    padding_left, padding_right = (0, 0)
6✔
106

107
        if self.padding:
6✔
108
            padding_dict = {
6✔
109
                "Left": int(padding_left),
110
                "Top": int(padding_top),
111
                "Right": int(padding_right),
112
                "Bottom": int(padding_bottom),
113
            }
114
            transform = Compose(
6✔
115
                [
116
                    Resize((int(new_height), int(new_width))),
117
                    Pad(
118
                        (
119
                            padding_dict["Left"],
120
                            padding_dict["Top"],
121
                            padding_dict["Right"],
122
                            padding_dict["Bottom"],
123
                        )
124
                    ),
125
                ]
126
            )
127
        else:
128
            transform = Compose([Resize((int(new_height), int(new_width)))])
6✔
129
            padding_dict = {"Left": 0, "Top": 0, "Right": 0, "Bottom": 0}
6✔
130

131
        return {"Image": transform(image), "Scale": scale, "Padding": padding_dict}
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