Skip to content

Commit 4430c42

Browse files
committed
share codes
1 parent 0be7e31 commit 4430c42

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+6643
-202
lines changed

README.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,13 @@ Prepare CityPersons dataset as the original codes doing
3434

3535

3636

37-
Training
37+
Training & val
3838

3939
~~~
40-
python train.py
40+
python trainval_torchstyle.py
41+
python trainval_caffestyle.py
4142
~~~
4243

44+
NOTE
4345

44-
45-
Demo
46-
47-
~~~
48-
python demo.py
49-
~~~
50-
51-
## Todo
52-
53-
the code only support 1 GPU training, you can use nn.DataParaller to modify the code for multi-GPUs training.
46+
using caffe style, you need to download additional pre-trained weight.

config.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Config(object):
22
def __init__(self):
3-
self.gpu_ids = '0'
3+
self.gpu_ids = [0, 1]
44
self.onegpu = 2
55
self.num_epochs = 150
66
self.add_epoch = 0
@@ -25,6 +25,22 @@ def __init__(self):
2525
self.use_horizontal_flips = True
2626
self.brightness = (0.5, 2, 0.5)
2727
self.size_train = (336, 448)
28+
self.size_test = (336, 338)
2829

2930
# image channel-wise mean to subtract, the order is BGR
3031
self.img_channel_mean = [103.939, 116.779, 123.68]
32+
33+
# whether or not use caffe style training which is used in paper
34+
self.caffemodel = False
35+
36+
# use teacher
37+
self.teacher = True
38+
39+
self.test_path = './data/citypersons'
40+
41+
# whether or not to do validation during training
42+
self.val = True
43+
self.val_frequency = 10
44+
45+
def print_conf(self):
46+
print '\n'.join(['%s:%s' % item for item in self.__dict__.items()])

data/citypersons

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../dataset/Citypersons/

dataloader/data_augment.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
from __future__ import division
2+
import cv2
3+
import numpy as np
4+
import copy
5+
6+
7+
def _brightness(image, min=0.5, max=2.0):
8+
'''
9+
Randomly change the brightness of the input image.
10+
Protected against overflow.
11+
'''
12+
hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
13+
14+
random_br = np.random.uniform(min, max)
15+
16+
# To protect against overflow: Calculate a mask for all pixels
17+
# where adjustment of the brightness would exceed the maximum
18+
# brightness value and set the value to the maximum at those pixels.
19+
mask = hsv[:, :, 2] * random_br > 255
20+
v_channel = np.where(mask, 255, hsv[:, :, 2] * random_br)
21+
hsv[:, :, 2] = v_channel
22+
23+
return cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)
24+
25+
26+
def resize_image(image, gts, igs, scale=(0.4, 1.5)):
27+
height, width = image.shape[0:2]
28+
ratio = np.random.uniform(scale[0], scale[1])
29+
# if len(gts)>0 and np.max(gts[:,3]-gts[:,1])>300:
30+
# ratio = np.random.uniform(scale[0], 1.0)
31+
new_height, new_width = int(ratio * height), int(ratio * width)
32+
image = cv2.resize(image, (new_width, new_height))
33+
if len(gts) > 0:
34+
gts = np.asarray(gts, dtype=float)
35+
gts[:, 0:4:2] *= ratio
36+
gts[:, 1:4:2] *= ratio
37+
38+
if len(igs) > 0:
39+
igs = np.asarray(igs, dtype=float)
40+
igs[:, 0:4:2] *= ratio
41+
igs[:, 1:4:2] *= ratio
42+
43+
return image, gts, igs
44+
45+
46+
def random_crop(image, gts, igs, crop_size, limit=8):
47+
img_height, img_width = image.shape[0:2]
48+
crop_h, crop_w = crop_size
49+
50+
if len(gts) > 0:
51+
sel_id = np.random.randint(0, len(gts))
52+
sel_center_x = int((gts[sel_id, 0] + gts[sel_id, 2]) / 2.0)
53+
sel_center_y = int((gts[sel_id, 1] + gts[sel_id, 3]) / 2.0)
54+
else:
55+
sel_center_x = int(np.random.randint(0, img_width - crop_w + 1) + crop_w * 0.5)
56+
sel_center_y = int(np.random.randint(0, img_height - crop_h + 1) + crop_h * 0.5)
57+
58+
crop_x1 = max(sel_center_x - int(crop_w * 0.5), int(0))
59+
crop_y1 = max(sel_center_y - int(crop_h * 0.5), int(0))
60+
diff_x = max(crop_x1 + crop_w - img_width, int(0))
61+
crop_x1 -= diff_x
62+
diff_y = max(crop_y1 + crop_h - img_height, int(0))
63+
crop_y1 -= diff_y
64+
cropped_image = np.copy(image[crop_y1:crop_y1 + crop_h, crop_x1:crop_x1 + crop_w])
65+
# crop detections
66+
if len(igs) > 0:
67+
igs[:, 0:4:2] -= crop_x1
68+
igs[:, 1:4:2] -= crop_y1
69+
igs[:, 0:4:2] = np.clip(igs[:, 0:4:2], 0, crop_w)
70+
igs[:, 1:4:2] = np.clip(igs[:, 1:4:2], 0, crop_h)
71+
keep_inds = ((igs[:, 2] - igs[:, 0]) >= 8) & \
72+
((igs[:, 3] - igs[:, 1]) >= 8)
73+
igs = igs[keep_inds]
74+
if len(gts) > 0:
75+
ori_gts = np.copy(gts)
76+
gts[:, 0:4:2] -= crop_x1
77+
gts[:, 1:4:2] -= crop_y1
78+
gts[:, 0:4:2] = np.clip(gts[:, 0:4:2], 0, crop_w)
79+
gts[:, 1:4:2] = np.clip(gts[:, 1:4:2], 0, crop_h)
80+
81+
before_area = (ori_gts[:, 2] - ori_gts[:, 0]) * (ori_gts[:, 3] - ori_gts[:, 1])
82+
after_area = (gts[:, 2] - gts[:, 0]) * (gts[:, 3] - gts[:, 1])
83+
84+
keep_inds = ((gts[:, 2] - gts[:, 0]) >= limit) & \
85+
(after_area >= 0.5 * before_area)
86+
gts = gts[keep_inds]
87+
88+
return cropped_image, gts, igs
89+
90+
91+
def random_pave(image, gts, igs, pave_size, limit=8):
92+
img_height, img_width = image.shape[0:2]
93+
pave_h, pave_w = pave_size
94+
# paved_image = np.zeros((pave_h, pave_w, 3), dtype=image.dtype)
95+
paved_image = np.ones((pave_h, pave_w, 3), dtype=image.dtype) * np.mean(image, dtype=int)
96+
pave_x = int(np.random.randint(0, pave_w - img_width + 1))
97+
pave_y = int(np.random.randint(0, pave_h - img_height + 1))
98+
paved_image[pave_y:pave_y + img_height, pave_x:pave_x + img_width] = image
99+
# pave detections
100+
if len(igs) > 0:
101+
igs[:, 0:4:2] += pave_x
102+
igs[:, 1:4:2] += pave_y
103+
keep_inds = ((igs[:, 2] - igs[:, 0]) >= 8) & \
104+
((igs[:, 3] - igs[:, 1]) >= 8)
105+
igs = igs[keep_inds]
106+
107+
if len(gts) > 0:
108+
gts[:, 0:4:2] += pave_x
109+
gts[:, 1:4:2] += pave_y
110+
keep_inds = ((gts[:, 2] - gts[:, 0]) >= limit)
111+
gts = gts[keep_inds]
112+
113+
return paved_image, gts, igs
114+
115+
116+
def augment(img_data, c, img):
117+
assert 'filepath' in img_data
118+
assert 'bboxes' in img_data
119+
img_data_aug = copy.deepcopy(img_data)
120+
if img is None:
121+
img = cv2.imread(img_data_aug['filepath'])
122+
img_height, img_width = img.shape[:2]
123+
124+
# random brightness
125+
if c.brightness and np.random.randint(0, 2) == 0:
126+
img = _brightness(img, min=c.brightness[0], max=c.brightness[1])
127+
# random horizontal flip
128+
if c.use_horizontal_flips and np.random.randint(0, 2) == 0:
129+
img = cv2.flip(img, 1)
130+
if len(img_data_aug['bboxes']) > 0:
131+
img_data_aug['bboxes'][:, [0, 2]] = img_width - img_data_aug['bboxes'][:, [2, 0]]
132+
if len(img_data_aug['ignoreareas']) > 0:
133+
img_data_aug['ignoreareas'][:, [0, 2]] = img_width - img_data_aug['ignoreareas'][:, [2, 0]]
134+
135+
gts = np.copy(img_data_aug['bboxes'])
136+
igs = np.copy(img_data_aug['ignoreareas'])
137+
138+
img, gts, igs = resize_image(img, gts, igs, scale=(0.4, 1.5))
139+
if img.shape[0] >= c.size_train[0]:
140+
img, gts, igs = random_crop(img, gts, igs, c.size_train, limit=16)
141+
else:
142+
img, gts, igs = random_pave(img, gts, igs, c.size_train, limit=16)
143+
144+
img_data_aug['bboxes'] = gts
145+
img_data_aug['ignoreareas'] = igs
146+
147+
img_data_aug['width'] = c.size_train[1]
148+
img_data_aug['height'] = c.size_train[0]
149+
150+
return img_data_aug, img

0 commit comments

Comments
 (0)