forked from notprime/majorphileo
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·62 lines (44 loc) · 1.82 KB
/
utils.py
File metadata and controls
executable file
·62 lines (44 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import math
import random
import numpy as np
import torch
def cosine_scheduler(base_value, final_value, epochs, warmup_epochs=0, start_warmup_value=0, warmup_steps=-1):
warmup_schedule = np.array([])
warmup_iters = warmup_epochs
if warmup_steps > 0:
warmup_iters = warmup_steps
if warmup_epochs > 0:
warmup_schedule = np.linspace(start_warmup_value, base_value, warmup_iters)
iters = np.arange(epochs - warmup_iters)
schedule = np.array(
[final_value + 0.5 * (base_value - final_value) * (1 + math.cos(math.pi * i / (len(iters)))) for i in iters]
)
schedule = np.concatenate((warmup_schedule, schedule))
assert len(schedule) == epochs
return schedule
class RandomChannelDropout(object):
""" Randomly drops one of the channels of the input image with a given probability. """
def __init__(self, p=0.5):
"""
Args:
p (float): probability of the image being transformed. Default value is 0.5.
"""
super(RandomChannelDropout, self).__init__()
self.p = p
def __call__(self, img):
"""
Args:
img (Tensor): Image to be transformed.
Returns:
Tensor: Transformed image with one channel potentially dropped.
"""
# Ensure image is in the form of a PyTorch Tensor
if not isinstance(img, torch.Tensor):
raise TypeError('Input img should be a torch.Tensor but got type {}'.format(type(img)))
# Randomly decide whether to drop a channel based on p
if random.random() < self.p:
# Randomly select a channel index (0 for Red, 1 for Green, 2 for Blue)
channel_to_drop = random.randint(0, img.shape[0] - 1)
# Set the selected channel to zero
img[channel_to_drop, :, :] = 0
return img