Skip to content

Commit 111610d

Browse files
committed
finish
1 parent 26c0cd5 commit 111610d

13 files changed

+51
-50
lines changed

data/config.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
_C.filter_min_face = True
3636

3737
# train config
38-
_C.LR_STEPS = (120, 198, 250)
38+
#_C.LR_STEPS = (120, 198, 250)
39+
_C.MAX_STEPS = 200000
40+
_C.LR_STEPS = (80000,100000,120000)
3941
_C.EPOCHES = 300
4042

4143
# anchor config
@@ -48,8 +50,9 @@
4850

4951
# detection config
5052
_C.NMS_THRESH = 0.3
51-
_C.TOP_K = 500
52-
_C.CONF_THRESH = 0.01
53+
_C.NMS_TOP_K = 5000
54+
_C.TOP_K = 750
55+
_C.CONF_THRESH = 0.05
5356

5457
# loss config
5558
_C.NEG_POS_RATIOS = 3

demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ def detect(net, img_path, thresh):
5353
img = np.array(img)
5454
height, width, _ = img.shape
5555
max_im_shrink = np.sqrt(
56-
1800 * 1200 / (img.shape[0] * img.shape[1]))
56+
1700 * 1200 / (img.shape[0] * img.shape[1]))
5757
image = cv2.resize(img, None, None, fx=max_im_shrink,
5858
fy=max_im_shrink, interpolation=cv2.INTER_LINEAR)
5959
#image = cv2.resize(img, (640, 640))
60-
x = to_chw_bgr(img)
60+
x = to_chw_bgr(image)
6161
x = x.astype('float32')
6262
x -= cfg.img_mean
6363
x = x[[2, 1, 0], :, :]

layers/functions/detection.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self, cfg):
2323
self.nms_thresh = cfg.NMS_THRESH
2424
self.conf_thresh = cfg.CONF_THRESH
2525
self.variance = cfg.VARIANCE
26-
self.use_nms = cfg.USE_NMS
26+
self.nms_top_k = cfg.NMS_TOP_K
2727

2828
def forward(self, loc_data, conf_data, prior_data):
2929
"""
@@ -57,21 +57,16 @@ def forward(self, loc_data, conf_data, prior_data):
5757
for cl in range(1, self.num_classes):
5858
c_mask = conf_scores[cl].gt(self.conf_thresh)
5959
scores = conf_scores[cl][c_mask]
60-
60+
6161
if scores.dim() == 0:
6262
continue
6363
l_mask = c_mask.unsqueeze(1).expand_as(boxes)
6464
boxes_ = boxes[l_mask].view(-1, 4)
65-
if self.use_nms:
66-
ids, count = nms(
67-
boxes_, scores, self.nms_thresh, self.top_k)
68-
output[i, cl, :count] = torch.cat((scores[ids[:count]].unsqueeze(1),
69-
boxes_[ids[:count]]), 1)
70-
else:
71-
sort_scores, idx = scores.sort(0,descending=True)
72-
count = sort_scores.size(
73-
0) if sort_scores.size(0) < self.top_k else self.top_k
74-
output[i, cl,:count] = torch.cat(
75-
(sort_scores[:count].unsqueeze(1), boxes_[idx[:count]]), 1)
65+
ids, count = nms(
66+
boxes_, scores, self.nms_thresh, self.nms_top_k)
67+
count = count if count < self.top_k else self.top_k
68+
69+
output[i, cl, :count] = torch.cat((scores[ids[:count]].unsqueeze(1),
70+
boxes_[ids[:count]]), 1)
7671

7772
return output

s3fd.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def weights_init(self, m):
187187
m.bias.data.zero_()
188188

189189

190-
vgg_cfg = [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M',
190+
vgg_cfg = [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'C', 512, 512, 512, 'M',
191191
512, 512, 512, 'M']
192192

193193
extras_cfg = [256, 'S', 512, 128, 'S', 256]
@@ -208,7 +208,7 @@ def vgg(cfg, i, batch_norm=False):
208208
else:
209209
layers += [conv2d, nn.ReLU(inplace=True)]
210210
in_channels = v
211-
conv6 = nn.Conv2d(512, 1024, kernel_size=3, padding=6, dilation=6)
211+
conv6 = nn.Conv2d(512, 1024, kernel_size=3, padding=3, dilation=3)
212212
conv7 = nn.Conv2d(1024, 1024, kernel_size=1)
213213
layers += [conv6,
214214
nn.ReLU(inplace=True), conv7, nn.ReLU(inplace=True)]
@@ -240,7 +240,7 @@ def multibox(vgg, extra_layers, num_classes):
240240
loc_layers += [nn.Conv2d(vgg[14].out_channels, 4,
241241
kernel_size=3, padding=1)]
242242
conf_layers += [nn.Conv2d(vgg[14].out_channels,
243-
(num_classes - 1) * 3 + 1, kernel_size=3, padding=1)]
243+
3 + (num_classes-1), kernel_size=3, padding=1)]
244244

245245
for k, v in enumerate(vgg_source):
246246
loc_layers += [nn.Conv2d(vgg[v].out_channels,
@@ -258,7 +258,7 @@ def multibox(vgg, extra_layers, num_classes):
258258
def build_s3fd(phase, num_classes=2):
259259
base_, extras_, head_ = multibox(
260260
vgg(vgg_cfg, 3), add_extras((extras_cfg), 1024), num_classes)
261-
261+
262262
return S3FD(phase, base_, extras_, head_, num_classes)
263263

264264

tmp/0_Parade_marchingband_1_465.jpg

-301 KB
Binary file not shown.

tmp/test.jpg

-15.2 KB
Loading

tmp/test2.jpg

-93 KB
Loading

tools/afw_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
else:
3939
torch.set_default_tensor_type('torch.FloatTensor')
4040

41-
AFW_IMG_DIR = os.path.join(cfg.AFW_DIR, 'images')
42-
AFW_RESULT_DIR = os.path.join(cfg.AFW_DIR, 's3fd')
41+
AFW_IMG_DIR = os.path.join(cfg.FACE.AFW_DIR, 'images')
42+
AFW_RESULT_DIR = os.path.join(cfg.FACE.AFW_DIR, 's3fd')
4343
AFW_RESULT_IMG_DIR = os.path.join(AFW_RESULT_DIR, 'images')
4444

4545
if not os.path.exists(AFW_RESULT_IMG_DIR):

tools/fddb_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
torch.set_default_tensor_type('torch.FloatTensor')
4040

4141

42-
FDDB_IMG_DIR = os.path.join(cfg.FDDB_DIR, 'images')
43-
FDDB_FOLD_DIR = os.path.join(cfg.FDDB_DIR, 'FDDB-folds')
44-
FDDB_RESULT_DIR = os.path.join(cfg.FDDB_DIR, 's3fd')
42+
FDDB_IMG_DIR = os.path.join(cfg.FACE.FDDB_DIR, 'images')
43+
FDDB_FOLD_DIR = os.path.join(cfg.FACE.FDDB_DIR, 'FDDB-folds')
44+
FDDB_RESULT_DIR = os.path.join(cfg.FACE.FDDB_DIR, 's3fd')
4545
FDDB_RESULT_IMG_DIR = os.path.join(FDDB_RESULT_DIR, 'images')
4646

4747
if not os.path.exists(FDDB_RESULT_IMG_DIR):

tools/pascal_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
else:
3939
torch.set_default_tensor_type('torch.FloatTensor')
4040

41-
PASCAL_IMG_DIR = os.path.join(cfg.PASCAL_DIR, 'images')
42-
PASCAL_RESULT_DIR = os.path.join(cfg.PASCAL_DIR, 's3fd')
41+
PASCAL_IMG_DIR = os.path.join(cfg.FACE.PASCAL_DIR, 'images')
42+
PASCAL_RESULT_DIR = os.path.join(cfg.FACE.PASCAL_DIR, 's3fd')
4343
PASCAL_RESULT_IMG_DIR = os.path.join(PASCAL_RESULT_DIR, 'images')
4444

4545
if not os.path.exists(PASCAL_RESULT_IMG_DIR):

tools/wider_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def detect_face(net, img, shrink):
4646
img = cv2.resize(img, None, None, fx=shrink, fy=shrink,
4747
interpolation=cv2.INTER_LINEAR)
4848

49-
x = to_chw_bgr(image)
49+
x = to_chw_bgr(img)
5050
x = x.astype('float32')
5151
x -= cfg.img_mean
5252
x = x[[2, 1, 0], :, :]
@@ -170,7 +170,7 @@ def get_data():
170170
del wider_face
171171

172172
imgs_path = os.path.join(
173-
cfg.WIDER_DIR, 'WIDER_{}'.format(subset), 'images')
173+
cfg.FACE.WIDER_DIR, 'WIDER_{}'.format(subset), 'images')
174174
save_path = 'eval_tools/s3fd_{}'.format(subset)
175175

176176
return event_list, file_list, imgs_path, save_path
@@ -209,7 +209,7 @@ def get_data():
209209
# (img.shape[0] * img.shape[1])) ** 0.5
210210

211211
max_im_shrink = np.sqrt(
212-
1650 * 1200 / (img.shape[0] * img.shape[1]))
212+
1700 * 1200 / (img.shape[0] * img.shape[1]))
213213

214214
shrink = max_im_shrink if max_im_shrink < 1 else 1
215215
counter += 1

train.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from layers.modules import MultiBoxLoss
2222
from data.factory import dataset_factory, detection_collate
2323

24-
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
24+
#os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
2525

2626

2727
def str2bool(v):
@@ -31,7 +31,7 @@ def str2bool(v):
3131
description='S3FD face Detector Training With Pytorch')
3232
train_set = parser.add_mutually_exclusive_group()
3333
parser.add_argument('--dataset',
34-
default='hand',
34+
default='face',
3535
choices=['hand', 'face', 'head'],
3636
help='Train target')
3737
parser.add_argument('--basenet',
@@ -44,7 +44,7 @@ def str2bool(v):
4444
default=None, type=str,
4545
help='Checkpoint state_dict file to resume training from')
4646
parser.add_argument('--num_workers',
47-
default=8, type=int,
47+
default=4, type=int,
4848
help='Number of workers used in dataloading')
4949
parser.add_argument('--cuda',
5050
default=True, type=str2bool,
@@ -104,6 +104,7 @@ def str2bool(v):
104104
s3fd_net = build_s3fd('train', cfg.NUM_CLASSES)
105105
net = s3fd_net
106106

107+
107108
if args.resume:
108109
print('Resuming training, loading {}...'.format(args.resume))
109110
start_epoch = net.load_weights(args.resume)
@@ -121,9 +122,9 @@ def str2bool(v):
121122

122123
if not args.resume:
123124
print('Initializing weights...')
124-
net.extras.apply(net.weights_init)
125-
net.loc.apply(net.weights_init)
126-
net.conf.apply(net.weights_init)
125+
s3fd_net.extras.apply(s3fd_net.weights_init)
126+
s3fd_net.loc.apply(s3fd_net.weights_init)
127+
s3fd_net.conf.apply(s3fd_net.weights_init)
127128

128129
optimizer = optim.SGD(net.parameters(), lr=args.lr, momentum=args.momentum,
129130
weight_decay=args.weight_decay)
@@ -138,8 +139,7 @@ def train():
138139
iteration = 0
139140
net.train()
140141
for epoch in range(start_epoch, cfg.EPOCHES):
141-
loc_loss = 0
142-
conf_loss = 0
142+
losses = 0
143143
for batch_idx, (images, targets) in enumerate(train_loader):
144144
if args.cuda:
145145
images = Variable(images.cuda())
@@ -149,7 +149,7 @@ def train():
149149
images = Variable(images)
150150
targets = [Variable(ann, volatile=True) for ann in targets]
151151

152-
if epoch in cfg.LR_STEPS:
152+
if iteration in cfg.LR_STEPS:
153153
step_index += 1
154154
adjust_learning_rate(optimizer, args.gamma, step_index)
155155

@@ -162,15 +162,16 @@ def train():
162162
loss.backward()
163163
optimizer.step()
164164
t1 = time.time()
165-
loc_loss += loss_l.data[0]
166-
conf_loss += loss_c.data[0]
165+
losses += loss.data[0]
167166

168167
if iteration % 10 == 0:
168+
tloss = losses / (batch_idx + 1)
169169
print('Timer: %.4f' % (t1 - t0))
170170
print('epoch:' + repr(epoch) + ' || iter:' +
171-
repr(iteration) + ' || Loss:%.4f' % (loss.data[0]))
171+
repr(iteration) + ' || Loss:%.4f' % (tloss))
172172
print('->> conf loss:{:.4f} || loc loss:{:.4f}'.format(
173173
loss_c.data[0], loss_l.data[0]))
174+
print('->>lr:{:.6f}'.format(optimizer.param_groups[0]['lr']))
174175

175176
if iteration != 0 and iteration % 5000 == 0:
176177
print('Saving state, iter:', iteration)
@@ -180,6 +181,8 @@ def train():
180181
iteration += 1
181182

182183
val(epoch)
184+
if iteration == cfg.MAX_STEPS:
185+
break
183186

184187

185188
def val(epoch):
@@ -224,7 +227,7 @@ def val(epoch):
224227
file = 'sfd_{}_checkpoint.pth'.format(args.dataset)
225228
torch.save(states, os.path.join(
226229
args.save_folder, file))
227-
230+
228231

229232
def adjust_learning_rate(optimizer, gamma, step):
230233
"""Sets the learning rate to the initial LR decayed by 10 at every

utils/augmentations.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ def anchor_crop_image_sampling(img,
568568
infDistance = 9999999
569569
bbox_labels = np.array(bbox_labels)
570570
scale = np.array([img_width, img_height, img_width, img_height])
571-
571+
572572
boxes = bbox_labels[:, 1:5] * scale
573573
labels = bbox_labels[:, 0]
574574

@@ -828,10 +828,10 @@ def preprocess(img, bbox_labels, mode, image_path):
828828

829829
img = Image.fromarray(img)
830830

831-
interp_mode = [
832-
Image.BILINEAR, Image.HAMMING, Image.NEAREST, Image.BICUBIC,
833-
Image.LANCZOS
834-
]
831+
interp_mode = [
832+
Image.BILINEAR, Image.HAMMING, Image.NEAREST, Image.BICUBIC,
833+
Image.LANCZOS
834+
]
835835
interp_indx = np.random.randint(0, 5)
836836

837837
img = img.resize((cfg.resize_width, cfg.resize_height),

0 commit comments

Comments
 (0)