-
Notifications
You must be signed in to change notification settings - Fork 27
/
util.py
104 lines (80 loc) · 2.73 KB
/
util.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
import json
import random
import shutil
import numpy as np
from pathlib import Path
from datetime import datetime
from PIL import Image, ImageDraw
import torch
import torchvision.utils as vutils
import torchvision.transforms as T
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
print("Random Seed:", seed)
def init_experiment(args, prefix):
if args.seed is None:
args.seed = random.randint(0, 10000)
set_seed(args.seed)
if not args.name:
args.name = datetime.now().strftime('%Y%m%d%H%M%S%f')
out_dir = Path('output') / args.dataset / prefix / args.name
out_dir.mkdir(parents=True, exist_ok=True)
json_path = out_dir / 'args.json'
with json_path.open('w') as f:
json.dump(vars(args), f, indent=2)
return out_dir
def save_checkpoint(state, is_best, out_dir):
out_path = Path(out_dir) / 'checkpoint.pth.tar'
torch.save(state, out_path)
if is_best:
best_path = Path(out_dir) / 'model_best.pth.tar'
shutil.copyfile(out_path, best_path)
def convert_xywh_to_ltrb(bbox):
xc, yc, w, h = bbox
x1 = xc - w / 2
y1 = yc - h / 2
x2 = xc + w / 2
y2 = yc + h / 2
return [x1, y1, x2, y2]
def convert_layout_to_image(boxes, labels, colors, canvas_size):
H, W = canvas_size
img = Image.new('RGB', (int(W), int(H)), color=(255, 255, 255))
draw = ImageDraw.Draw(img, 'RGBA')
# draw from larger boxes
area = [b[2] * b[3] for b in boxes]
indices = sorted(range(len(area)),
key=lambda i: area[i],
reverse=True)
for i in indices:
bbox, color = boxes[i], colors[labels[i]]
c_fill = color + (100,)
x1, y1, x2, y2 = convert_xywh_to_ltrb(bbox)
x1, x2 = x1 * (W - 1), x2 * (W - 1)
y1, y2 = y1 * (H - 1), y2 * (H - 1)
draw.rectangle([x1, y1, x2, y2],
outline=color,
fill=c_fill)
return img
def save_image(batch_boxes, batch_labels, batch_mask,
dataset_colors, out_path, canvas_size=(60, 40),
nrow=None):
# batch_boxes: [B, N, 4]
# batch_labels: [B, N]
# batch_mask: [B, N]
imgs = []
B = batch_boxes.size(0)
to_tensor = T.ToTensor()
for i in range(B):
mask_i = batch_mask[i]
boxes = batch_boxes[i][mask_i]
labels = batch_labels[i][mask_i]
img = convert_layout_to_image(boxes, labels,
dataset_colors,
canvas_size)
imgs.append(to_tensor(img))
image = torch.stack(imgs)
if nrow is None:
nrow = int(np.ceil(np.sqrt(B)))
vutils.save_image(image, out_path, normalize=False, nrow=nrow)