-
Notifications
You must be signed in to change notification settings - Fork 40
/
l_s50_twox_dfp_tal_flip.py
197 lines (159 loc) · 7.1 KB
/
l_s50_twox_dfp_tal_flip.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
194
195
196
# encoding: utf-8
import os
import sys
import torch
import torch.distributed as dist
from yolox.exp import Exp as MyExp
import torch.nn as nn
class Exp(MyExp):
def __init__(self):
super(Exp, self).__init__()
self.depth = 1.0
self.width = 1.0
self.data_num_workers = 6
self.num_classes = 8
self.input_size = (600, 960) # (h,w)
self.random_size = (50, 70)
self.test_size = (600, 960)
#
self.basic_lr_per_img = 0.001 / 64.0
self.warmup_epochs = 1
self.max_epoch = 15
self.no_aug_epochs = 15
self.eval_interval = 1
self.train_ann = 'train.json'
self.val_ann = 'val.json'
self.exp_name = os.path.split(os.path.realpath(__file__))[1].split(".")[0]
self.output_dir = '/data/output/stream_yolo'
def get_model(self):
from exps.model.yolox import YOLOX
from exps.model.dfp_pafpn import DFPPAFPN
from exps.model.tal_head import TALHead
import torch.nn as nn
def init_yolo(M):
for m in M.modules():
if isinstance(m, nn.BatchNorm2d):
m.eps = 1e-3
m.momentum = 0.03
if getattr(self, "model", None) is None:
in_channels = [256, 512, 1024]
backbone = DFPPAFPN(self.depth, self.width, in_channels=in_channels)
head = TALHead(self.num_classes, self.width, in_channels=in_channels, gamma=1.0,
ignore_thr=0.4, ignore_value=1.5)
self.model = YOLOX(backbone, head)
self.model.apply(init_yolo)
self.model.head.initialize_biases(1e-2)
return self.model
def get_data_loader(self, batch_size, is_distributed, no_aug=False, local_rank=0, cache_img=False):
from exps.dataset.tal_flip_two_future_argoversedataset import TWO_ARGOVERSEDataset
from exps.data.tal_flip_mosaicdetection import MosaicDetection
from exps.data.data_augment_flip import DoubleTrainTransform
from yolox.data import (
YoloBatchSampler,
DataLoader,
InfiniteSampler,
worker_init_reset_seed,
)
dataset = TWO_ARGOVERSEDataset(
data_dir='/data',
json_file=self.train_ann,
name='train',
img_size=self.input_size,
preproc=DoubleTrainTransform(max_labels=50, hsv=False, flip=True),
cache=cache_img,
)
dataset = MosaicDetection(dataset,
mosaic=not no_aug,
img_size=self.input_size,
preproc=DoubleTrainTransform(max_labels=120, hsv=False, flip=True),
degrees=self.degrees,
translate=self.translate,
scale=self.mosaic_scale,
shear=self.shear,
perspective=0.0,
enable_mixup=self.enable_mixup,
mosaic_prob=self.mosaic_prob,
mixup_prob=self.mixup_prob,
)
self.dataset = dataset
if is_distributed:
batch_size = batch_size // dist.get_world_size()
sampler = InfiniteSampler(len(self.dataset), seed=self.seed if self.seed else 0)
batch_sampler = YoloBatchSampler(
sampler=sampler,
batch_size=batch_size,
drop_last=False,
mosaic=not no_aug)
dataloader_kwargs = {"num_workers": self.data_num_workers, "pin_memory": True}
dataloader_kwargs["batch_sampler"] = batch_sampler
# Make sure each process has different random seed, especially for 'fork' method
dataloader_kwargs["worker_init_fn"] = worker_init_reset_seed
train_loader = DataLoader(self.dataset, **dataloader_kwargs)
return train_loader
def get_eval_loader(self, batch_size, is_distributed, testdev=False):
from exps.StreamYOLO.dataset.tal_flip_two_future_argoversedataset import TWO_ARGOVERSEDataset
from exps.StreamYOLO.data.data_augment_flip import DoubleTrainTransform
valdataset = TWO_ARGOVERSEDataset(
data_dir='/data',
json_file='val.json',
name='val',
img_size=self.test_size,
preproc=DoubleTrainTransform(),
)
if is_distributed:
batch_size = batch_size // dist.get_world_size()
sampler = torch.utils.data.distributed.DistributedSampler(valdataset, shuffle=False)
else:
sampler = torch.utils.data.SequentialSampler(valdataset)
dataloader_kwargs = {"num_workers": self.data_num_workers, "pin_memory": True, "sampler": sampler}
dataloader_kwargs["batch_size"] = batch_size
val_loader = torch.utils.data.DataLoader(valdataset, **dataloader_kwargs)
return val_loader
def random_resize(self, data_loader, epoch, rank, is_distributed):
import random
tensor = torch.LongTensor(2).cuda()
if rank == 0:
if epoch >= self.max_epoch - 1:
size = self.input_size
else:
size_factor = self.input_size[0] * 1.0 / self.input_size[1]
size = random.randint(*self.random_size)
size = (16 * int(size * size_factor), int(16 * size))
tensor[0] = size[0]
tensor[1] = size[1]
if is_distributed:
dist.barrier()
dist.broadcast(tensor, 0)
input_size = (tensor[0].item(), tensor[1].item())
return input_size
def preprocess(self, inputs, targets, tsize):
scale_y = tsize[0] / self.input_size[0]
scale_x = tsize[1] / self.input_size[1]
if scale_x != 1 or scale_y != 1:
inputs = nn.functional.interpolate(
inputs, size=tsize, mode="bilinear", align_corners=False
)
targets[0][..., 1::2] = targets[0][..., 1::2] * scale_x
targets[0][..., 2::2] = targets[0][..., 2::2] * scale_y
targets[1][..., 1::2] = targets[1][..., 1::2] * scale_x
targets[1][..., 2::2] = targets[1][..., 2::2] * scale_y
return inputs, targets
def get_evaluator(self, batch_size, is_distributed, testdev=False):
from exps.evaluators.twox_stream_evaluator import TWOX_COCOEvaluator
val_loader = self.get_eval_loader(batch_size, is_distributed, testdev)
evaluator = TWOX_COCOEvaluator(
dataloader=val_loader,
img_size=self.test_size,
confthre=self.test_conf,
nmsthre=self.nmsthre,
num_classes=self.num_classes,
testdev=testdev,
)
return evaluator
def get_trainer(self, args):
from exps.train_utils.double_trainer import Trainer
trainer = Trainer(self, args)
# NOTE: trainer shouldn't be an attribute of exp object
return trainer
def eval(self, model, evaluator, is_distributed, half=False):
return evaluator.evaluate(model, is_distributed, half)