提交 eea7f8c0 编写于 作者: D dengkaipeng

fix for random_shape interval

上级 8ceb9849
......@@ -75,12 +75,12 @@ _C.learning_rate = 0.001
_C.max_iter = 500000
# warm up to learning rate
_C.warm_up_iter = 4000
_C.warm_up_iter = 1000
_C.warm_up_factor = 0.
# lr steps_with_decay
_C.lr_steps = [350000, 400000, 450000]
_C.lr_gamma = [0.5, 0.1, 0.01]
_C.lr_steps = [400000, 450000]
_C.lr_gamma = 0.1
# L2 regularization hyperparameter
_C.weight_decay = 0.0005
......
......@@ -67,6 +67,7 @@ class GeneratorEnqueuer(object):
while(True):
if self.queues[queue_idx].full():
queue_idx = (queue_idx + 1) % self.size_num
time.sleep(0.02)
continue
else:
size = self.random_sizes[queue_idx]
......@@ -77,6 +78,7 @@ class GeneratorEnqueuer(object):
try:
self.queues[queue_idx].put_nowait(generator_output)
except:
timw.sleep(self.wait_time)
continue
else:
break
......
......@@ -204,13 +204,13 @@ class YOLOv3(object):
x=out,
gtbox=self.gtbox,
gtlabel=self.gtlabel,
gtscore=self.gtscore,
# gtscore=self.gtscore,
anchors=anchors,
anchor_mask=anchor_mask,
class_num=class_num,
ignore_thresh=ignore_thresh,
downsample=self.downsample,
use_label_smooth=False,
downsample_ratio=self.downsample,
# use_label_smooth=False,
name="yolo_loss"+str(i))
self.losses.append(fluid.layers.reduce_mean(loss))
self.downsample //= 2
......
......@@ -141,7 +141,7 @@ class DataSetReader(object):
else:
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!"
if mode != 'infer':
assert batch_size is not None, "batch size connot be None in mode {}".format(mode)
......@@ -157,11 +157,10 @@ class DataSetReader(object):
im_scale_x = size / float(w)
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)
# mean = np.array(mean).reshape((1, 1, -1))
# std = np.array(std).reshape((1, 1, -1))
# out_img = (out_img / 255.0 - mean) / std
# out_img = out_img.transpose((2, 0, 1))
out_img = out_img.astype('float32').transpose((2, 0, 1)) / 255.0
mean = np.array(mean).reshape((1, 1, -1))
std = np.array(std).reshape((1, 1, -1))
out_img = (out_img / 255.0 - mean) / std
out_img = out_img.transpose((2, 0, 1))
return (out_img, int(img['id']), (h, w))
......@@ -173,23 +172,12 @@ class DataSetReader(object):
gt_labels = img['gt_labels'].copy()
gt_scores = np.ones_like(gt_labels)
# if mixup_img:
# 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, mean)
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))
# 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
mean = np.array(mean).reshape((1, 1, -1))
std = np.array(std).reshape((1, 1, -1))
out_img = (im / 255.0 - mean) / std
out_img = out_img.astype('float32').transpose((2, 0, 1))
return (out_img, gt_boxes, gt_labels, gt_scores)
......@@ -198,29 +186,20 @@ class DataSetReader(object):
return np.random.choice(random_sizes)
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():
if mode == 'train':
imgs = self._parse_images_by_mode(mode)
if shuffle:
np.random.shuffle(imgs)
read_cnt = 0
total_read_cnt = 0
total_iter = 0
batch_out = []
img_size = get_img_size(size, random_sizes)
# img_ids = []
while True:
img = imgs[read_cnt % len(imgs)]
mixup_img = get_mixup_img(imgs, mixup_iter, total_read_cnt)
mixup_img = None
read_cnt += 1
total_read_cnt += 1
if read_cnt % len(imgs) == 0 and shuffle:
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)
......@@ -231,7 +210,8 @@ class DataSetReader(object):
# print("img_ids: ", img_ids)
yield 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_ids = []
......@@ -262,17 +242,13 @@ dsr = DataSetReader()
def train(size=416,
batch_size=64,
shuffle=True,
mixup_iter=0,
random_shape_iter=0,
random_sizes=[],
interval=10,
pyreader_num=1,
use_multiprocessing=True,
num_workers=12,
num_workers=16,
max_queue=32):
generator = dsr.get_reader('train', size, batch_size, shuffle, mixup_iter, random_sizes)
if not use_multiprocessing:
return generator
generator = dsr.get_reader('train', size, batch_size, shuffle, random_shape_iter, random_sizes)
def infinite_reader():
while True:
......@@ -282,27 +258,29 @@ def train(size=416,
def reader():
try:
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)
generator_out = None
np.random.seed(1000)
intervals = pyreader_num * interval
total_random_iter = pyreader_num * random_shape_iter
cnt = 0
idx = np.random.randint(len(random_sizes))
idx = len(random_sizes) - 1
while True:
while enqueuer.is_running():
if not enqueuer.queues[idx].empty():
generator_out = enqueuer.queues[idx].get()
break
else:
print(idx," empty")
time.sleep(0.02)
yield generator_out
generator_out = None
cnt += 1
if cnt % intervals == 0:
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:
if enqueuer is not None:
enqueuer.stop()
......
......@@ -90,17 +90,17 @@ def train():
else:
exe = base_exe
random_sizes = []
random_sizes = [cfg.input_size]
if cfg.random_shape:
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:
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.decorate_paddle_reader(train_reader)
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())
def save_model(postfix):
......@@ -150,6 +150,7 @@ def train():
snapshot_loss = 0
snapshot_time = 0
for iter_id, data in enumerate(train_reader()):
print(len(data), data[0][0].shape)
iter_id += cfg.start_iter
prev_start_time = start_time
start_time = time.time()
......
......@@ -104,19 +104,16 @@ def parse_args():
add_arg('class_num', int, 80, "Class number.")
add_arg('data_dir', str, 'dataset/coco', "The data root path.")
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('start_iter', int, 0, "Start iteration.")
#SOLVER
add_arg('learning_rate', float, 0.001, "Learning rate.")
add_arg('max_iter', int, 500200, "Iter number.")
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
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('label_smooth', bool, False, "Use label smooth in class label.")
add_arg('no_mixup_iter', int, 500200, "Disable mixup in last N iter.")
add_arg('tune_iter', int, 200, "Disable random shape in last N iter.")
add_arg('valid_thresh', float, 0.005, "Valid confidence score for NMS.")
add_arg('nms_thresh', float, 0.45, "NMS threshold.")
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.
先完成此消息的编辑!
想要评论请 注册