“6803b882542d93a22a5f6991efb7d17019903bfc”上不存在“develop/doc/design/kernel_hint_design.html”
提交 eea7f8c0 编写于 作者: D dengkaipeng

fix for random_shape interval

上级 8ceb9849
...@@ -75,12 +75,12 @@ _C.learning_rate = 0.001 ...@@ -75,12 +75,12 @@ _C.learning_rate = 0.001
_C.max_iter = 500000 _C.max_iter = 500000
# warm up to learning rate # warm up to learning rate
_C.warm_up_iter = 4000 _C.warm_up_iter = 1000
_C.warm_up_factor = 0. _C.warm_up_factor = 0.
# lr steps_with_decay # lr steps_with_decay
_C.lr_steps = [350000, 400000, 450000] _C.lr_steps = [400000, 450000]
_C.lr_gamma = [0.5, 0.1, 0.01] _C.lr_gamma = 0.1
# L2 regularization hyperparameter # L2 regularization hyperparameter
_C.weight_decay = 0.0005 _C.weight_decay = 0.0005
......
...@@ -67,6 +67,7 @@ class GeneratorEnqueuer(object): ...@@ -67,6 +67,7 @@ class GeneratorEnqueuer(object):
while(True): while(True):
if self.queues[queue_idx].full(): if self.queues[queue_idx].full():
queue_idx = (queue_idx + 1) % self.size_num queue_idx = (queue_idx + 1) % self.size_num
time.sleep(0.02)
continue continue
else: else:
size = self.random_sizes[queue_idx] size = self.random_sizes[queue_idx]
...@@ -77,6 +78,7 @@ class GeneratorEnqueuer(object): ...@@ -77,6 +78,7 @@ class GeneratorEnqueuer(object):
try: try:
self.queues[queue_idx].put_nowait(generator_output) self.queues[queue_idx].put_nowait(generator_output)
except: except:
timw.sleep(self.wait_time)
continue continue
else: else:
break break
......
...@@ -204,13 +204,13 @@ class YOLOv3(object): ...@@ -204,13 +204,13 @@ class YOLOv3(object):
x=out, x=out,
gtbox=self.gtbox, gtbox=self.gtbox,
gtlabel=self.gtlabel, gtlabel=self.gtlabel,
gtscore=self.gtscore, # gtscore=self.gtscore,
anchors=anchors, anchors=anchors,
anchor_mask=anchor_mask, anchor_mask=anchor_mask,
class_num=class_num, class_num=class_num,
ignore_thresh=ignore_thresh, ignore_thresh=ignore_thresh,
downsample=self.downsample, downsample_ratio=self.downsample,
use_label_smooth=False, # use_label_smooth=False,
name="yolo_loss"+str(i)) name="yolo_loss"+str(i))
self.losses.append(fluid.layers.reduce_mean(loss)) self.losses.append(fluid.layers.reduce_mean(loss))
self.downsample //= 2 self.downsample //= 2
......
...@@ -141,7 +141,7 @@ class DataSetReader(object): ...@@ -141,7 +141,7 @@ class DataSetReader(object):
else: else:
return self._parse_images(is_train=(mode=='train')) return self._parse_images(is_train=(mode=='train'))
def get_reader(self, mode, size=416, batch_size=None, shuffle=False, mixup_iter=0, random_sizes=[], image=None): def get_reader(self, mode, size=416, batch_size=None, shuffle=False, random_shape_iter=0, random_sizes=[], image=None):
assert mode in ['train', 'test', 'infer'], "Unknow mode type!" assert mode in ['train', 'test', 'infer'], "Unknow mode type!"
if mode != 'infer': if mode != 'infer':
assert batch_size is not None, "batch size connot be None in mode {}".format(mode) assert batch_size is not None, "batch size connot be None in mode {}".format(mode)
...@@ -157,11 +157,10 @@ class DataSetReader(object): ...@@ -157,11 +157,10 @@ class DataSetReader(object):
im_scale_x = size / float(w) im_scale_x = size / float(w)
im_scale_y = size / float(h) im_scale_y = size / float(h)
out_img = cv2.resize(im, None, None, fx=im_scale_x, fy=im_scale_y, interpolation=cv2.INTER_LINEAR) out_img = cv2.resize(im, None, None, fx=im_scale_x, fy=im_scale_y, interpolation=cv2.INTER_LINEAR)
# mean = np.array(mean).reshape((1, 1, -1)) mean = np.array(mean).reshape((1, 1, -1))
# std = np.array(std).reshape((1, 1, -1)) std = np.array(std).reshape((1, 1, -1))
# out_img = (out_img / 255.0 - mean) / std out_img = (out_img / 255.0 - mean) / std
# out_img = out_img.transpose((2, 0, 1)) out_img = out_img.transpose((2, 0, 1))
out_img = out_img.astype('float32').transpose((2, 0, 1)) / 255.0
return (out_img, int(img['id']), (h, w)) return (out_img, int(img['id']), (h, w))
...@@ -173,23 +172,12 @@ class DataSetReader(object): ...@@ -173,23 +172,12 @@ class DataSetReader(object):
gt_labels = img['gt_labels'].copy() gt_labels = img['gt_labels'].copy()
gt_scores = np.ones_like(gt_labels) gt_scores = np.ones_like(gt_labels)
# if mixup_img: im, gt_boxes, gt_labels, gt_scores = image_utils.image_augment(im, gt_boxes, gt_labels, gt_scores, size, mean)
# mixup_im = cv2.imread(mixup_img['image'])
# mixup_im = cv2.cvtColor(mixup_im, cv2.COLOR_BGR2RGB)
# mixup_gt_boxes = mixup_img['gt_boxes'].copy()
# mixup_gt_labels = mixup_img['gt_labels'].copy()
# mixup_gt_scores = np.ones_like(mixup_gt_labels)
# im, gt_boxes, gt_labels, gt_scores = image_utils.image_mixup(im, gt_boxes, \
# gt_labels, gt_scores, mixup_im, mixup_gt_boxes, mixup_gt_labels, \
# mixup_gt_scores)
im, gt_boxes, gt_labels, gt_scores = image_utils.image_augment(im, gt_boxes, gt_labels, gt_scores, size, [0.5] * 3) mean = np.array(mean).reshape((1, 1, -1))
std = np.array(std).reshape((1, 1, -1))
# mean = np.array(mean).reshape((1, 1, -1)) out_img = (im / 255.0 - mean) / std
# std = np.array(std).reshape((1, 1, -1)) out_img = out_img.astype('float32').transpose((2, 0, 1))
# out_img = (im / 255.0 - mean) / std
# out_img = out_img.transpose((2, 0, 1)).astype('float32')
out_img = im.astype('float32').transpose((2, 0, 1)) / 255.0
return (out_img, gt_boxes, gt_labels, gt_scores) return (out_img, gt_boxes, gt_labels, gt_scores)
...@@ -198,29 +186,20 @@ class DataSetReader(object): ...@@ -198,29 +186,20 @@ class DataSetReader(object):
return np.random.choice(random_sizes) return np.random.choice(random_sizes)
return size return size
def get_mixup_img(imgs, mixup_iter, total_read_cnt):
if total_read_cnt >= mixup_iter:
return None
mixup_idx = np.random.randint(1, len(imgs))
mixup_img = imgs[(total_read_cnt + mixup_idx) % len(imgs)]
return mixup_img
def reader(): def reader():
if mode == 'train': if mode == 'train':
imgs = self._parse_images_by_mode(mode) imgs = self._parse_images_by_mode(mode)
if shuffle: if shuffle:
np.random.shuffle(imgs) np.random.shuffle(imgs)
read_cnt = 0 read_cnt = 0
total_read_cnt = 0 total_iter = 0
batch_out = [] batch_out = []
img_size = get_img_size(size, random_sizes) img_size = get_img_size(size, random_sizes)
# img_ids = [] # img_ids = []
while True: while True:
img = imgs[read_cnt % len(imgs)] img = imgs[read_cnt % len(imgs)]
mixup_img = get_mixup_img(imgs, mixup_iter, total_read_cnt) mixup_img = None
read_cnt += 1 read_cnt += 1
total_read_cnt += 1
if read_cnt % len(imgs) == 0 and shuffle: if read_cnt % len(imgs) == 0 and shuffle:
np.random.shuffle(imgs) np.random.shuffle(imgs)
im, gt_boxes, gt_labels, gt_scores = img_reader_with_augment(img, img_size, cfg.pixel_means, cfg.pixel_stds, mixup_img) im, gt_boxes, gt_labels, gt_scores = img_reader_with_augment(img, img_size, cfg.pixel_means, cfg.pixel_stds, mixup_img)
...@@ -231,7 +210,8 @@ class DataSetReader(object): ...@@ -231,7 +210,8 @@ class DataSetReader(object):
# print("img_ids: ", img_ids) # print("img_ids: ", img_ids)
yield batch_out yield batch_out
batch_out = [] batch_out = []
if total_read_cnt % 10 == 0: total_iter += 1
if total_iter % 10 == 0:
img_size = get_img_size(size, random_sizes) img_size = get_img_size(size, random_sizes)
# img_ids = [] # img_ids = []
...@@ -262,17 +242,13 @@ dsr = DataSetReader() ...@@ -262,17 +242,13 @@ dsr = DataSetReader()
def train(size=416, def train(size=416,
batch_size=64, batch_size=64,
shuffle=True, shuffle=True,
mixup_iter=0, random_shape_iter=0,
random_sizes=[], random_sizes=[],
interval=10, interval=10,
pyreader_num=1, pyreader_num=1,
use_multiprocessing=True, num_workers=16,
num_workers=12,
max_queue=32): max_queue=32):
generator = dsr.get_reader('train', size, batch_size, shuffle, mixup_iter, random_sizes) generator = dsr.get_reader('train', size, batch_size, shuffle, random_shape_iter, random_sizes)
if not use_multiprocessing:
return generator
def infinite_reader(): def infinite_reader():
while True: while True:
...@@ -282,27 +258,29 @@ def train(size=416, ...@@ -282,27 +258,29 @@ def train(size=416,
def reader(): def reader():
try: try:
enqueuer = GeneratorEnqueuer( enqueuer = GeneratorEnqueuer(
infinite_reader(), use_multiprocessing=use_multiprocessing) infinite_reader(), use_multiprocessing=True)
enqueuer.start(max_queue_size=max_queue, workers=num_workers, random_sizes=random_sizes) enqueuer.start(max_queue_size=max_queue, workers=num_workers, random_sizes=random_sizes)
generator_out = None generator_out = None
np.random.seed(1000) np.random.seed(1000)
intervals = pyreader_num * interval intervals = pyreader_num * interval
total_random_iter = pyreader_num * random_shape_iter
cnt = 0 cnt = 0
idx = np.random.randint(len(random_sizes)) idx = len(random_sizes) - 1
while True: while True:
while enqueuer.is_running(): while enqueuer.is_running():
if not enqueuer.queues[idx].empty(): if not enqueuer.queues[idx].empty():
generator_out = enqueuer.queues[idx].get() generator_out = enqueuer.queues[idx].get()
break break
else: else:
print(idx," empty")
time.sleep(0.02) time.sleep(0.02)
yield generator_out yield generator_out
generator_out = None generator_out = None
cnt += 1 cnt += 1
if cnt % intervals == 0: if cnt % intervals == 0:
idx = np.random.randint(len(random_sizes)) idx = np.random.randint(len(random_sizes))
print("Resizing: ", (idx + 10) * 32) if cnt >= total_random_iter:
idx = -1
print("Resizing: ", random_sizes[idx])
finally: finally:
if enqueuer is not None: if enqueuer is not None:
enqueuer.stop() enqueuer.stop()
......
...@@ -90,17 +90,17 @@ def train(): ...@@ -90,17 +90,17 @@ def train():
else: else:
exe = base_exe exe = base_exe
random_sizes = [] random_sizes = [cfg.input_size]
if cfg.random_shape: if cfg.random_shape:
random_sizes = [32 * i for i in range(10, 20)] random_sizes = [32 * i for i in range(10, 20)]
mixup_iter = cfg.max_iter - cfg.start_iter - cfg.no_mixup_iter random_shape_iter = cfg.max_iter - cfg.start_iter - cfg.tune_iter
if cfg.use_pyreader: if cfg.use_pyreader:
train_reader = reader.train(input_size, batch_size=int(hyperparams['batch'])/devices_num, shuffle=True, mixup_iter=mixup_iter, random_sizes=random_sizes, interval=10, pyreader_num=devices_num, use_multiprocessing=cfg.use_multiprocess) train_reader = reader.train(input_size, batch_size=int(hyperparams['batch'])/devices_num, shuffle=True, random_shape_iter=random_shape_iter, random_sizes=random_sizes, interval=10, pyreader_num=devices_num)
py_reader = model.py_reader py_reader = model.py_reader
py_reader.decorate_paddle_reader(train_reader) py_reader.decorate_paddle_reader(train_reader)
else: else:
train_reader = reader.train(input_size, batch_size=int(hyperparams['batch']), shuffle=True, mixup_iter=mixup_iter, random_sizes=random_sizes, interval=10, use_multiprocessing=cfg.use_multiprocess) train_reader = reader.train(input_size, batch_size=int(hyperparams['batch']), shuffle=True, random_shape_iter=random_shape_iter, random_sizes=random_sizes, interval=10)
feeder = fluid.DataFeeder(place=place, feed_list=model.feeds()) feeder = fluid.DataFeeder(place=place, feed_list=model.feeds())
def save_model(postfix): def save_model(postfix):
...@@ -150,6 +150,7 @@ def train(): ...@@ -150,6 +150,7 @@ def train():
snapshot_loss = 0 snapshot_loss = 0
snapshot_time = 0 snapshot_time = 0
for iter_id, data in enumerate(train_reader()): for iter_id, data in enumerate(train_reader()):
print(len(data), data[0][0].shape)
iter_id += cfg.start_iter iter_id += cfg.start_iter
prev_start_time = start_time prev_start_time = start_time
start_time = time.time() start_time = time.time()
......
...@@ -104,19 +104,16 @@ def parse_args(): ...@@ -104,19 +104,16 @@ def parse_args():
add_arg('class_num', int, 80, "Class number.") add_arg('class_num', int, 80, "Class number.")
add_arg('data_dir', str, 'dataset/coco', "The data root path.") add_arg('data_dir', str, 'dataset/coco', "The data root path.")
add_arg('use_pyreader', bool, True, "Use pyreader.") add_arg('use_pyreader', bool, True, "Use pyreader.")
add_arg('use_multiprocess', bool, True, "Use multiprocessing for train reader.")
add_arg('use_profile', bool, False, "Whether use profiler.") add_arg('use_profile', bool, False, "Whether use profiler.")
add_arg('start_iter', int, 0, "Start iteration.") add_arg('start_iter', int, 0, "Start iteration.")
#SOLVER #SOLVER
add_arg('learning_rate', float, 0.001, "Learning rate.") add_arg('learning_rate', float, 0.001, "Learning rate.")
add_arg('max_iter', int, 500200, "Iter number.") add_arg('max_iter', int, 500200, "Iter number.")
add_arg('snapshot_iter', int, 2000, "Save model every snapshot stride.") add_arg('snapshot_iter', int, 2000, "Save model every snapshot stride.")
# add_arg('log_window', int, 20, "Log smooth window, set 1 for debug, set 20 for train.")
# TRAIN TEST INFER # TRAIN TEST INFER
add_arg('input_size', int, 608, "Image input size of YOLOv3.") add_arg('input_size', int, 608, "Image input size of YOLOv3.")
add_arg('random_shape', bool, True, "Resize to random shape for train reader.") add_arg('random_shape', bool, True, "Resize to random shape for train reader.")
add_arg('label_smooth', bool, False, "Use label smooth in class label.") add_arg('tune_iter', int, 200, "Disable random shape in last N iter.")
add_arg('no_mixup_iter', int, 500200, "Disable mixup in last N iter.")
add_arg('valid_thresh', float, 0.005, "Valid confidence score for NMS.") add_arg('valid_thresh', float, 0.005, "Valid confidence score for NMS.")
add_arg('nms_thresh', float, 0.45, "NMS threshold.") add_arg('nms_thresh', float, 0.45, "NMS threshold.")
add_arg('nms_topk', int, 400, "The number of boxes to perform NMS.") add_arg('nms_topk', int, 400, "The number of boxes to perform NMS.")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册