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

ControlNet / MARLIN / 9011167809

09 May 2024 01:56AM CUT coverage: 65.763%. First build
9011167809

Pull #25

github

web-flow
Merge 7c160e431 into 23491494b
Pull Request #25: hotfix: add lightning dependancy for _cosine_scheduler_fn(..)

461 of 701 relevant lines covered (65.76%)

0.66 hits per line

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

41.1
/src/marlin_pytorch/util.py
1
from typing import List, Any, TypeVar, Union
1✔
2
from typing import Type, Dict
1✔
3

4
import numpy as np
1✔
5
import torchvision
1✔
6
import yaml
1✔
7
from einops import rearrange
1✔
8
from numpy import ndarray
1✔
9
from torch import Tensor
1✔
10
from torch.nn import functional as F
1✔
11
from tqdm.auto import tqdm
1✔
12

13

14
def read_video(path: str, channel_first: bool = True):
1✔
15
    video, audio, info = torchvision.io.read_video(path)
×
16
    if channel_first:
×
17
        video = rearrange(video, 'T H W C -> T C H W')
×
18
    return video
×
19

20

21
def read_yaml(path: str) -> Dict[str, Any]:
1✔
22
    with open(path, "r") as file:
×
23
        return yaml.load(file, Loader=yaml.Loader)
×
24

25

26
def padding_video(tensor: Tensor, target: int, padding_method: str = "zero", padding_position: str = "tail") -> Tensor:
1✔
27
    t, c, h, w = tensor.shape
×
28
    padding_size = target - t
×
29

30
    pad = _get_padding_pair(padding_size, padding_position)
×
31

32
    if padding_method == "zero":
×
33
        return F.pad(tensor, pad=[0, 0, 0, 0, 0, 0] + pad)
×
34
    elif padding_method == "same":
×
35
        tensor = rearrange(tensor, "t c h w -> c h w t")
×
36
        tensor = F.pad(tensor, pad=pad + [0, 0], mode="replicate")
×
37
        return rearrange(tensor, "c h w t -> t c h w")
×
38
    else:
39
        raise ValueError("Wrong padding method. It should be zero or tail or average.")
×
40

41

42
def _get_padding_pair(padding_size: int, padding_position: str) -> List[int]:
1✔
43
    if padding_position == "tail":
×
44
        pad = [0, padding_size]
×
45
    elif padding_position == "head":
×
46
        pad = [padding_size, 0]
×
47
    elif padding_position == "average":
×
48
        padding_head = padding_size // 2
×
49
        padding_tail = padding_size - padding_head
×
50
        pad = [padding_head, padding_tail]
×
51
    else:
52
        raise ValueError("Wrong padding position. It should be zero or tail or average.")
×
53
    return pad
×
54

55

56
class DownloadProgressBar(tqdm):
1✔
57
    total: int
1✔
58

59
    def update_to(self, b=1, bsize=1, tsize=None):
1✔
60
        if tsize is not None:
1✔
61
            self.total = tsize
1✔
62
        self.update(b * bsize - self.n)
1✔
63

64

65
T = TypeVar("T")
1✔
66

67

68
class Singleton:
1✔
69
    all_instances: Dict[Type, object] = {}
1✔
70

71
    def __new__(cls, clazz: Type[T]) -> T:
1✔
72
        cls.all_instances[clazz] = clazz()
1✔
73
        return cls.all_instances[clazz]
1✔
74

75

76
def crop_with_padding(image: ndarray, x1: int, x2: int, y1: int, y2: int, pad_value: Union[int, float] = 0.,
1✔
77
    batch: bool = False
78
) -> ndarray:
79
    assert y2 > y1 and x2 > x1, "Should follow y2 > y1 and x2 > x1"
×
80

81
    if not batch:
×
82
        image = image[np.newaxis, ...]
×
83

84
    crop_shape = np.array([y2 - y1, x2 - x1])
×
85

86
    if len(image.shape) == 3:
×
87
        b, h, w = image.shape
×
88
        cropped = np.full((b, *crop_shape), pad_value, dtype=image.dtype)
×
89
    elif len(image.shape) == 4:
×
90
        b, h, w, c = image.shape
×
91
        cropped = np.full((b, *crop_shape, c), pad_value, dtype=image.dtype)
×
92
    else:
93
        raise ValueError("Invalid shape, the image should be one of following shapes: ([B,] H, W) or ([B,] H, W, C)")
×
94

95
    # compute cropped index of image
96
    image_y_start, image_x_start = np.clip([y1, x1], 0, [h, w])
×
97
    image_y_end, image_x_end = np.clip([y2, x2], 0, [h, w])
×
98

99
    # compute target index of output
100
    crop_y_start, crop_x_start = np.clip([-y1, -x1], 0, crop_shape)
×
101
    crop_y_end, crop_x_end = crop_shape - np.clip([y2 - h, x2 - w], 0, crop_shape)
×
102

103
    # assign values
104
    cropped[:, crop_y_start:crop_y_end, crop_x_start:crop_x_end] = \
×
105
        image[:, image_y_start:image_y_end, image_x_start:image_x_end]
106

107
    return cropped if batch else cropped[0]
×
108

109

110
class NoArgInit:
1✔
111
    def __init__(self):
1✔
112
        pass
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

© 2025 Coveralls, Inc