forked from rosinality/fcos-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.py
123 lines (86 loc) · 2.82 KB
/
transform.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import random
import torch
import torchvision
from torchvision.transforms import functional as F
class Compose:
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, img, target):
for t in self.transforms:
img, target = t(img, target)
return img, target
def __repr__(self):
format_str = self.__class__.__name__ + '('
for t in self.transforms:
format_str += '\n'
format_str += f' {t}'
format_str += '\n)'
return format_str
class Resize:
def __init__(self, min_size, max_size):
if not isinstance(min_size, (list, tuple)):
min_size = (min_size,)
self.min_size = min_size
self.max_size = max_size
def get_size(self, img_size):
w, h = img_size
size = random.choice(self.min_size)
max_size = self.max_size
if max_size is not None:
min_orig = float(min((w, h)))
max_orig = float(max((w, h)))
if max_orig / min_orig * size > max_size:
size = int(round(max_size * min_orig / max_orig))
if (w <= h and w == size) or (h <= w and h == size):
return h, w
if w < h:
ow = size
oh = int(size * h / w)
else:
oh = size
ow = int(size * w / h)
return oh, ow
def __call__(self, img, target):
size = self.get_size(img.size)
img = F.resize(img, size)
target = target.resize(img.size)
return img, target
class RandomHorizontalFlip:
def __init__(self, p=0.5):
self.p = p
def __call__(self, img, target):
if random.random() < self.p:
img = F.hflip(img)
target = target.transpose(0)
return img, target
class ToTensor:
def __call__(self, img, target):
return F.to_tensor(img), target
class Normalize:
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, img, target):
img = F.normalize(img, mean=self.mean, std=self.std)
return img, target
def preset_transform(config, train=True):
if train:
if config.train_min_size_range[0] == -1:
min_size = config.train_min_size
else:
min_size = list(
range(
config.train_min_size_range[0], config.train_min_size_range[1] + 1
)
)
max_size = config.train_max_size
flip = 0.5
else:
min_size = config.test_min_size
max_size = config.test_max_size
flip = 0
normalize = Normalize(mean=config.pixel_mean, std=config.pixel_std)
transform = Compose(
[Resize(min_size, max_size), RandomHorizontalFlip(flip), ToTensor(), normalize]
)
return transform