-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
193 lines (147 loc) · 5.69 KB
/
utils.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import numpy as np
from PIL import Image
from torchvision.models import (
mobilenet_v3_small, MobileNet_V3_Small_Weights,
mobilenet_v3_large, MobileNet_V3_Large_Weights,
)
from torchvision.models.detection import fasterrcnn_resnet50_fpn_v2
from torch.utils.data import DataLoader, Dataset, random_split
from torchvision.transforms import v2 as transforms
import torchvision
from torchvision.utils import draw_bounding_boxes, draw_keypoints
import torch
import torch.nn as nn
import torch.optim as optim
import sys
from tqdm import tqdm
class ResNetModel(nn.Module):
def __init__(self):
super(ResNetModel, self).__init__()
fasterrcnn = fasterrcnn_resnet50_fpn_v2(pretrained=True)
self.backbone = fasterrcnn.backbone
self.conv_block = nn.Sequential(
nn.Conv2d(256, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.AdaptiveAvgPool2d((1, 1))
)
self.fc1 = nn.Linear(64, 16)
self.fc2 = nn.Linear(16, 4)
def forward(self, x):
features = self.backbone(x)
x = self.conv_block(features['3'])
x = x.view(x.size(0), -1)
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
return x
X = 0
Y = 1
W = 2
H = 3
X1 = 0
Y1 = 1
X2 = 2
Y2 = 3
transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
])
random_transform = transforms.Compose([
# transform,
transforms.GaussianNoise(0., 0.05),
# transforms.ElasticTransform(alpha=10., sigma=10.),
transforms.ColorJitter(brightness=0.1, contrast=0.1, saturation=0.1),
])
normalize = transforms.Compose([
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
inverse_normalize = transforms.Compose([
transforms.Normalize(mean=[0., 0., 0.], std=[1/0.229, 1/0.224, 1/0.225]),
transforms.Normalize(mean=[-0.485, -0.456, -0.406], std=[1., 1., 1.]),
])
def match(original_hash, modified_hash):
# difference = original_hash ^ modified_hash
# mask = 0xFFFFFFFF
# matching = ~(difference) & mask
# matching_bits_count = bin(matching).count('1')
return (original_hash == modified_hash).sum()
def create_bokehs(image, blurred, masks):
image = np.array(image)
blurred = np.array(blurred)
bokeh_images = np.repeat(blurred[np.newaxis, ...], len(masks), axis=0)
for i, mask in enumerate(masks):
bokeh_images[i][mask] = image[mask]
return bokeh_images
def bbox_to_ltrb(bbox):
x, y, w, h = bbox
return x, y, x+w, y+h
def clip_to_image(box, width, height):
x = max(0, box[X])
y = max(0, box[Y])
w = min(width, box[X]+box[W]) - x
h = min(height, box[Y]+box[H]) - y
return [round(x), round(y), round(w), round(h)]
def create_model(checkpoint=None, backbone="mobilenet"):
if backbone == "mobilenet":
model = mobilenet_v3_large(weights=MobileNet_V3_Large_Weights.DEFAULT)
model.classifier = nn.Sequential(
nn.Linear(model.classifier[0].in_features, 1280),
nn.Hardswish(),
# nn.Dropout(p=0.2, inplace=True),
nn.Linear(1280, 6),
# nn.ReLU()
)
elif backbone == "mobilenet_old":
model = mobilenet_v3_large(weights=MobileNet_V3_Large_Weights.DEFAULT)
model.classifier = nn.Sequential(
nn.Linear(model.classifier[0].in_features, 1280),
nn.Hardswish(),
# nn.Dropout(p=0.2, inplace=True),
nn.Linear(1280, 4),
nn.ReLU()
)
elif backbone == "resnet":
model = ResNetModel()
else:
raise ValueError("Invalid backbone")
if checkpoint is not None:
model.load_state_dict(torch.load(checkpoint))
return model.cuda()
def tilize(image, n_tiles):
width, height = image.size
tile_size = width / n_tiles
tiles = []
for y in np.linspace(0., float(height), n_tiles, endpoint=False):
for x in np.linspace(0, float(width), n_tiles, endpoint=False):
tile = image.crop((x, y, x+tile_size, y+tile_size))
tiles.append(tile)
return tiles
def tilize_by_anchors(image, n_breaks, anchors):
width, height = image.size
tile_width = float((anchors[X2] - anchors[X1])/n_breaks)
tile_height = float((anchors[Y2] - anchors[Y1])/n_breaks)
# compute number of grid lines in left and right directions
n_max_pos_width = (1 - anchors[X1] + 1e-6) // tile_width
n_max_neg_width = - ((anchors[X1] + 1e-6) // tile_width)
n_values = np.arange(n_max_neg_width, n_max_pos_width + 1)
# Compute the corresponding values of x + n * dx
width_values = (anchors[X1] + n_values * tile_width) * width
# compute number of grid lines in up and down directions
n_max_pos_height = (1 - anchors[Y1] + 1e-6) // tile_height
n_max_neg_height = - ((anchors[Y1] + 1e-6) // tile_height)
n_values = np.arange(n_max_neg_height, n_max_pos_height + 1)
# Compute the corresponding values of y + n * dy
height_values = (anchors[Y1] + n_values * tile_height) * height
tile_width *= width
tile_height *= height
tiles = [image.crop((x, y, x+tile_width, y+tile_height)) for y in height_values[:-1] for x in width_values[:-1]]
n_range = (n_max_neg_width, n_max_neg_height, n_max_pos_width, n_max_pos_height) #ltrb
return tiles, [int(num) for num in n_range]
@torch.no_grad
def chunk_call(model, inputs, batchsize=128):
if model is None:
return torch.zeros(inputs.size(0), 4)
outputs = []
for i in tqdm(range(0, len(inputs), batchsize)):
outputs.append(model(inputs[i:i+batchsize].cuda()).cpu())
return torch.cat(outputs)