def main(): parser = argparse.ArgumentParser() parser.add_argument('data_dir', type=str, help='Path to the dirctory of COCO dataset.') parser.add_argument('dataset_type', type=str, choices=['COCO', 'VOC']) parser.add_argument('--split', type=str, default='val', choices=['train', 'val']) args = parser.parse_args() if args.dataset_type == 'COCO': dataset = COCOBboxDataset(args.data_dir, args.split) elif args.dataset_type == 'VOC': dataset = VOCBboxDataset(args.data_dir, split=args.split) else: raise ValueError() visualizer = Visualizer(args.dataset_type) for img, bbox, label in dataset: result = visualizer.visualize(img, ([bbox], [label])) cv2.imshow('output', result) key = cv2.waitKey(0) & 0xff if key == ord('q'): break cv2.destroyAllWindows()
def setup(dataset, model, pretrained_model, batchsize): dataset_name = dataset if dataset_name == 'voc': dataset = VOCBboxDataset(year='2007', split='test', use_difficult=True, return_difficult=True) label_names = voc_bbox_label_names def eval_(out_values, rest_values): pred_bboxes, pred_labels, pred_scores = out_values gt_bboxes, gt_labels, gt_difficults = rest_values result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) print() print('mAP: {:f}'.format(result['map'])) for l, name in enumerate(voc_bbox_label_names): if result['ap'][l]: print('{:s}: {:f}'.format(name, result['ap'][l])) else: print('{:s}: -'.format(name)) elif dataset_name == 'coco': dataset = COCOBboxDataset(year='2017', split='val', use_crowded=True, return_area=True, return_crowded=True) label_names = coco_bbox_label_names def eval_(out_values, rest_values): pred_bboxes, pred_labels, pred_scores = out_values gt_bboxes, gt_labels, gt_area, gt_crowded = rest_values result = eval_detection_coco(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_area, gt_crowded) print() for area in ('all', 'large', 'medium', 'small'): print( 'mmAP ({}):'.format(area), result[ 'map/iou=0.50:0.95/area={}/max_dets=100'.format(area)]) cls, pretrained_models, default_batchsize = models[model] if pretrained_model is None: pretrained_model = pretrained_models.get(dataset_name, dataset_name) model = cls(n_fg_class=len(label_names), pretrained_model=pretrained_model) if batchsize is None: batchsize = default_batchsize return dataset, eval_, model, batchsize
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--model', choices=('resnet50', 'resnet101')) parser.add_argument( '--mean', choices=('chainercv', 'detectron'), default='chainercv') parser.add_argument('--batchsize', type=int, default=1) group = parser.add_mutually_exclusive_group() group.add_argument('--pretrained-model') group.add_argument('--snapshot') args = parser.parse_args() if args.model == 'resnet50': model = FasterRCNNFPNResNet50(n_fg_class=len(coco_bbox_label_names), mean=args.mean) elif args.model == 'resnet101': model = FasterRCNNFPNResNet101(n_fg_class=len(coco_bbox_label_names), mean=args.mean) if args.pretrained_model: chainer.serializers.load_npz(args.pretrained_model, model) elif args.snapshot: chainer.serializers.load_npz( args.snapshot, model, path='updater/model:main/model/') if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() model.use_preset('evaluate') dataset = COCOBboxDataset( split='minival', use_crowded=True, return_area=True, return_crowded=True) iterator = iterators.MultithreadIterator( dataset, args.batchsize, repeat=False, shuffle=False) in_values, out_values, rest_values = apply_to_iterator( model.predict, iterator, hook=ProgressHook(len(dataset))) # delete unused iterators explicitly del in_values pred_bboxes, pred_labels, pred_scores = out_values gt_bboxes, gt_labels, gt_area, gt_crowded = rest_values result = eval_detection_coco( pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_area, gt_crowded) print() for area in ('all', 'large', 'medium', 'small'): print('mmAP ({}):'.format(area), result['map/iou=0.50:0.95/area={}/max_dets=100'.format(area)])
def __init__(self, conf, mode='train'): assert mode == 'train' or mode == 'val', 'mode shoulde be train or val' if mode == 'train': self.training = True self.orig_dataset = ConcatenatedDataset( COCOBboxDataset(data_dir=conf.coco_path, split='train'), COCOBboxDataset(data_dir=conf.coco_path, split='valminusminival')) print('train dataset imported') else: self.training = False self.orig_dataset = COCOBboxDataset(data_dir=conf.coco_path, split='minival', use_crowded=True, return_crowded=True, return_area=True) print('minival dataset imported') self.pair = namedtuple('pair', ['img', 'bboxes', 'labels', 'scale']) self.maps = conf.maps self.min_sizes = conf.min_sizes self.max_size = conf.max_size self.mean = conf.mean
def main(): parser = argparse.ArgumentParser() parser.add_argument('--pretrained-model', default='coco') parser.add_argument('--gpu', type=int, default=-1) args = parser.parse_args() model = LightHeadRCNNResNet101( n_fg_class=len(coco_bbox_label_names), pretrained_model=args.pretrained_model) model.use_preset('evaluate') if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() dataset = COCOBboxDataset( split='minival', use_crowded=True, return_crowded=True, return_area=True) iterator = iterators.SerialIterator( dataset, 1, repeat=False, shuffle=False) in_values, out_values, rest_values = apply_to_iterator( model.predict, iterator, hook=ProgressHook(len(dataset))) # delete unused iterators explicitly del in_values pred_bboxes, pred_labels, pred_scores = out_values gt_bboxes, gt_labels, gt_areas, gt_crowdeds = rest_values result = eval_detection_coco( pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_areas, gt_crowdeds) keys = [ 'map/iou=0.50:0.95/area=all/max_dets=100', 'map/iou=0.50/area=all/max_dets=100', 'map/iou=0.75/area=all/max_dets=100', 'map/iou=0.50:0.95/area=small/max_dets=100', 'map/iou=0.50:0.95/area=medium/max_dets=100', 'map/iou=0.50:0.95/area=large/max_dets=100', 'mar/iou=0.50:0.95/area=all/max_dets=1', 'mar/iou=0.50:0.95/area=all/max_dets=10', 'mar/iou=0.50:0.95/area=all/max_dets=100', 'mar/iou=0.50:0.95/area=small/max_dets=100', 'mar/iou=0.50:0.95/area=medium/max_dets=100', 'mar/iou=0.50:0.95/area=large/max_dets=100', ] print('') for key in keys: print('{:s}: {:f}'.format(key, result[key]))
def main(): parser = argparse.ArgumentParser() parser.add_argument( '--model', choices=('resnet50', 'resnet101')) parser.add_argument('--batchsize', type=int, default=16) parser.add_argument('--out', default='result') parser.add_argument('--resume') args = parser.parse_args() comm = chainermn.create_communicator() device = comm.intra_rank if args.model == 'resnet50': model = FasterRCNNFPNResNet50( n_fg_class=len(coco_bbox_label_names), mean='chainercv') copyparams(model.extractor.base, ResNet50(pretrained_model='imagenet', arch='he')) elif args.model == 'resnet101': model = FasterRCNNFPNResNet101( n_fg_class=len(coco_bbox_label_names), mean='chainercv') copyparams(model.extractor.base, ResNet101(pretrained_model='imagenet', arch='he')) model.use_preset('evaluate') train_chain = TrainChain(model) chainer.cuda.get_device_from_id(device).use() train_chain.to_gpu() train = TransformDataset( ConcatenatedDataset( COCOBboxDataset(split='train'), COCOBboxDataset(split='valminusminival'), ), ('img', 'bbox', 'label'), transform) if comm.rank == 0: indices = np.arange(len(train)) else: indices = None indices = chainermn.scatter_dataset(indices, comm, shuffle=True) train = train.slice[indices] train_iter = chainer.iterators.MultithreadIterator( train, args.batchsize // comm.size) optimizer = chainermn.create_multi_node_optimizer( chainer.optimizers.MomentumSGD(), comm) optimizer.setup(train_chain) optimizer.add_hook(WeightDecay(0.0001)) model.extractor.base.conv1.disable_update() model.extractor.base.res2.disable_update() for link in model.links(): if isinstance(link, L.BatchNormalization): link.disable_update() updater = training.updaters.StandardUpdater( train_iter, optimizer, converter=converter, device=device) trainer = training.Trainer( updater, (90000 * 16 / args.batchsize, 'iteration'), args.out) def lr_schedule(updater): base_lr = 0.02 * args.batchsize / 16 warm_up_duration = 500 warm_up_rate = 1 / 3 iteration = updater.iteration if iteration < warm_up_duration: rate = warm_up_rate \ + (1 - warm_up_rate) * iteration / warm_up_duration elif iteration < 60000 * 16 / args.batchsize: rate = 1 elif iteration < 80000 * 16 / args.batchsize: rate = 0.1 else: rate = 0.01 return base_lr * rate trainer.extend(ManualScheduler('lr', lr_schedule)) if comm.rank == 0: log_interval = 10, 'iteration' trainer.extend(extensions.LogReport(trigger=log_interval)) trainer.extend(extensions.observe_lr(), trigger=log_interval) trainer.extend(extensions.PrintReport( ['epoch', 'iteration', 'lr', 'main/loss', 'main/loss/rpn/loc', 'main/loss/rpn/conf', 'main/loss/head/loc', 'main/loss/head/conf']), trigger=log_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend(extensions.snapshot(), trigger=(10000, 'iteration')) trainer.extend( extensions.snapshot_object( model, 'model_iter_{.updater.iteration}'), trigger=(90000 * 16 / args.batchsize, 'iteration')) if args.resume: serializers.load_npz(args.resume, trainer, strict=False) trainer.run()
def main(): parser = argparse.ArgumentParser() parser.add_argument( '--model', choices=('faster_rcnn_fpn_resnet50', 'faster_rcnn_fpn_resnet101'), default='faster_rcnn_fpn_resnet50') parser.add_argument('--batchsize', type=int, default=16) parser.add_argument('--iteration', type=int, default=90000) parser.add_argument('--step', type=int, nargs='*', default=[60000, 80000]) parser.add_argument('--out', default='result') parser.add_argument('--resume') args = parser.parse_args() # https://docs.chainer.org/en/stable/chainermn/tutorial/tips_faqs.html#using-multiprocessiterator if hasattr(multiprocessing, 'set_start_method'): multiprocessing.set_start_method('forkserver') p = multiprocessing.Process() p.start() p.join() comm = chainermn.create_communicator() device = comm.intra_rank if args.model == 'faster_rcnn_fpn_resnet50': model = FasterRCNNFPNResNet50( n_fg_class=len(coco_bbox_label_names), pretrained_model='imagenet') elif args.model == 'faster_rcnn_fpn_resnet101': model = FasterRCNNFPNResNet101( n_fg_class=len(coco_bbox_label_names), pretrained_model='imagenet') model.use_preset('evaluate') train_chain = TrainChain(model) chainer.cuda.get_device_from_id(device).use() train_chain.to_gpu() train = TransformDataset( COCOBboxDataset(year='2017', split='train'), ('img', 'bbox', 'label'), transform) if comm.rank == 0: indices = np.arange(len(train)) else: indices = None indices = chainermn.scatter_dataset(indices, comm, shuffle=True) train = train.slice[indices] train_iter = chainer.iterators.MultithreadIterator( train, args.batchsize // comm.size) optimizer = chainermn.create_multi_node_optimizer( chainer.optimizers.MomentumSGD(), comm) optimizer.setup(train_chain) optimizer.add_hook(WeightDecay(0.0001)) model.extractor.base.conv1.disable_update() model.extractor.base.res2.disable_update() for link in model.links(): if isinstance(link, L.BatchNormalization): link.disable_update() updater = training.updaters.StandardUpdater( train_iter, optimizer, converter=converter, device=device) trainer = training.Trainer( updater, (args.iteration * 16 / args.batchsize, 'iteration'), args.out) @make_shift('lr') def lr_schedule(trainer): base_lr = 0.02 * args.batchsize / 16 warm_up_duration = 500 warm_up_rate = 1 / 3 iteration = trainer.updater.iteration if iteration < warm_up_duration: rate = warm_up_rate \ + (1 - warm_up_rate) * iteration / warm_up_duration else: rate = 1 for step in args.step: if iteration >= step * 16 / args.batchsize: rate *= 0.1 return base_lr * rate trainer.extend(lr_schedule) if comm.rank == 0: log_interval = 10, 'iteration' trainer.extend(extensions.LogReport(trigger=log_interval)) trainer.extend(extensions.observe_lr(), trigger=log_interval) trainer.extend(extensions.PrintReport( ['epoch', 'iteration', 'lr', 'main/loss', 'main/loss/rpn/loc', 'main/loss/rpn/conf', 'main/loss/head/loc', 'main/loss/head/conf']), trigger=log_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend(extensions.snapshot(), trigger=(10000, 'iteration')) trainer.extend( extensions.snapshot_object( model, 'model_iter_{.updater.iteration}'), trigger=(90000 * 16 / args.batchsize, 'iteration')) if args.resume: serializers.load_npz(args.resume, trainer, strict=False) trainer.run()
labels = list() scores = list() keys = sorted(data.keys()) print keys for key in keys: data_i = data[key] bboxes.append(np.array([d['bbox'] for d in data_i], dtype=np.float32)) labels.append(np.array([d['label'] for d in data_i], dtype=np.int32)) scores.append(np.array([d['score'] for d in data_i], dtype=np.float32)) with open('data/fake.pkl', 'wb') as f: pickle.dump((bboxes, labels, scores, keys), f) ############################################################################## dataset = COCOBboxDataset(split='val', return_crowded=True, return_area=True, use_crowded=True) indices = [dataset.ids.index(key) for key in keys] gts = dataset[indices] gt_bboxes = [gt[1] for gt in gts] gt_labels = [gt[2] for gt in gts] gt_crowded = [gt[3] for gt in gts] gt_areas = [gt[4] for gt in gts] with open('data/fake_gt.pkl', 'wb') as f: pickle.dump((gt_bboxes, gt_labels, gt_crowded, gt_areas), f) import matplotlib.pyplot as plt from chainercv.visualizations import vis_bbox img, bbox, label, crowded, _ = dataset[indices[12]]
def main(): parser = argparse.ArgumentParser( description='ChainerCV training example: LightHeadRCNN') parser.add_argument('--out', '-o', default='result', help='Output directory') parser.add_argument('--seed', '-s', type=int, default=1234) parser.add_argument('--batchsize', '-b', type=int, default=8) parser.add_argument('--epoch', type=int, default=30) parser.add_argument('--step-epoch', type=int, nargs='*', default=[19, 25]) args = parser.parse_args() # https://docs.chainer.org/en/stable/chainermn/tutorial/tips_faqs.html#using-multiprocessiterator if hasattr(multiprocessing, 'set_start_method'): multiprocessing.set_start_method('forkserver') p = multiprocessing.Process() p.start() p.join() # chainermn comm = chainermn.create_communicator('pure_nccl') device = comm.intra_rank np.random.seed(args.seed) random.seed(args.seed) # model light_head_rcnn = LightHeadRCNNResNet101( pretrained_model='imagenet', n_fg_class=len(coco_bbox_label_names)) light_head_rcnn.use_preset('evaluate') model = LightHeadRCNNTrainChain(light_head_rcnn) chainer.cuda.get_device_from_id(device).use() model.to_gpu() # train dataset train_dataset = COCOBboxDataset(year='2017', split='train') # filter non-annotated data train_indices = np.array([ i for i, label in enumerate(train_dataset.slice[:, ['label']]) if len(label[0]) > 0 ], dtype=np.int32) train_dataset = train_dataset.slice[train_indices] train_dataset = TransformDataset(train_dataset, ('img', 'bbox', 'label', 'scale'), Transform(model.light_head_rcnn)) if comm.rank == 0: indices = np.arange(len(train_dataset)) else: indices = None indices = chainermn.scatter_dataset(indices, comm, shuffle=True) train_dataset = train_dataset.slice[indices] train_iter = chainer.iterators.SerialIterator(train_dataset, batch_size=args.batchsize // comm.size) optimizer = chainermn.create_multi_node_optimizer( chainer.optimizers.MomentumSGD(momentum=0.9), comm) optimizer.setup(model) global_context_module = model.light_head_rcnn.head.global_context_module global_context_module.col_max.W.update_rule.add_hook(GradientScaling(3.0)) global_context_module.col_max.b.update_rule.add_hook(GradientScaling(3.0)) global_context_module.col.W.update_rule.add_hook(GradientScaling(3.0)) global_context_module.col.b.update_rule.add_hook(GradientScaling(3.0)) global_context_module.row_max.W.update_rule.add_hook(GradientScaling(3.0)) global_context_module.row_max.b.update_rule.add_hook(GradientScaling(3.0)) global_context_module.row.W.update_rule.add_hook(GradientScaling(3.0)) global_context_module.row.b.update_rule.add_hook(GradientScaling(3.0)) optimizer.add_hook(chainer.optimizer.WeightDecay(rate=0.0001)) model.light_head_rcnn.extractor.conv1.disable_update() model.light_head_rcnn.extractor.res2.disable_update() for link in model.links(): if isinstance(link, L.BatchNormalization): link.disable_update() converter = functools.partial( concat_examples, padding=0, # img, bboxes, labels, scales indices_concat=[0, 2, 3], # img, _, labels, scales indices_to_device=[0], # img ) updater = chainer.training.updater.StandardUpdater(train_iter, optimizer, converter=converter, device=device) trainer = chainer.training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) @make_shift('lr') def lr_scheduler(trainer): base_lr = 0.0005 * 1.25 * args.batchsize warm_up_duration = 500 warm_up_rate = 1 / 3 iteration = trainer.updater.iteration epoch = trainer.updater.epoch if iteration < warm_up_duration: rate = warm_up_rate \ + (1 - warm_up_rate) * iteration / warm_up_duration else: for step in args.step_epoch: if epoch > step: rate *= 0.1 return rate * base_lr trainer.extend(lr_scheduler) if comm.rank == 0: # interval log_interval = 100, 'iteration' plot_interval = 3000, 'iteration' print_interval = 20, 'iteration' # training extensions model_name = model.light_head_rcnn.__class__.__name__ trainer.extend(chainer.training.extensions.snapshot_object( model.light_head_rcnn, filename='%s_model_iter_{.updater.iteration}.npz' % model_name), trigger=(1, 'epoch')) trainer.extend(extensions.observe_lr(), trigger=log_interval) trainer.extend( extensions.LogReport(log_name='log.json', trigger=log_interval)) report_items = [ 'iteration', 'epoch', 'elapsed_time', 'lr', 'main/loss', 'main/rpn_loc_loss', 'main/rpn_cls_loss', 'main/roi_loc_loss', 'main/roi_cls_loss', 'validation/main/map/iou=0.50:0.95/area=all/max_dets=100', ] trainer.extend(extensions.PrintReport(report_items), trigger=print_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) if extensions.PlotReport.available(): trainer.extend(extensions.PlotReport(['main/loss'], file_name='loss.png', trigger=plot_interval), trigger=plot_interval) trainer.extend(extensions.dump_graph('main/loss')) trainer.run()
def setUp(self): self.dataset = COCOBboxDataset( split=self.split, use_crowded=self.use_crowded, return_area=self.return_area, return_crowded=self.return_crowded)
def main(): parser = argparse.ArgumentParser( description='ChainerCV training example: LightHeadRCNN') parser.add_argument('--out', '-o', default='result', help='Output directory') parser.add_argument('--seed', '-s', type=int, default=1234) parser.add_argument('--batch-size', '-b', type=int, default=2) args = parser.parse_args() # chainermn comm = chainermn.create_communicator('pure_nccl') device = comm.intra_rank np.random.seed(args.seed) random.seed(args.seed) # model light_head_rcnn = LightHeadRCNNResNet101( pretrained_model='imagenet', n_fg_class=len(coco_bbox_label_names)) light_head_rcnn.use_preset('evaluate') model = LightHeadRCNNTrainChain(light_head_rcnn) chainer.cuda.get_device_from_id(device).use() model.to_gpu() # train dataset train_dataset = COCOBboxDataset(year='2014', split='train') vmml_dataset = COCOBboxDataset(year='2014', split='valminusminival') # filter non-annotated data train_indices = np.array([ i for i, label in enumerate(train_dataset.slice[:, ['label']]) if len(label[0]) > 0 ], dtype=np.int32) train_dataset = train_dataset.slice[train_indices] vmml_indices = np.array([ i for i, label in enumerate(vmml_dataset.slice[:, ['label']]) if len(label[0]) > 0 ], dtype=np.int32) vmml_dataset = vmml_dataset.slice[vmml_indices] train_dataset = TransformDataset( ConcatenatedDataset(train_dataset, vmml_dataset), ('img', 'bbox', 'label', 'scale'), Transform(model.light_head_rcnn)) if comm.rank == 0: indices = np.arange(len(train_dataset)) else: indices = None indices = chainermn.scatter_dataset(indices, comm, shuffle=True) train_dataset = train_dataset.slice[indices] train_iter = chainer.iterators.SerialIterator(train_dataset, batch_size=args.batch_size) if comm.rank == 0: test_dataset = COCOBboxDataset(year='2014', split='minival', use_crowded=True, return_crowded=True, return_area=True) test_iter = chainer.iterators.SerialIterator(test_dataset, batch_size=1, repeat=False, shuffle=False) optimizer = chainermn.create_multi_node_optimizer( chainer.optimizers.MomentumSGD(momentum=0.9), comm) optimizer.setup(model) global_context_module = model.light_head_rcnn.head.global_context_module global_context_module.col_max.W.update_rule.add_hook(GradientScaling(3.0)) global_context_module.col_max.b.update_rule.add_hook(GradientScaling(3.0)) global_context_module.col.W.update_rule.add_hook(GradientScaling(3.0)) global_context_module.col.b.update_rule.add_hook(GradientScaling(3.0)) global_context_module.row_max.W.update_rule.add_hook(GradientScaling(3.0)) global_context_module.row_max.b.update_rule.add_hook(GradientScaling(3.0)) global_context_module.row.W.update_rule.add_hook(GradientScaling(3.0)) global_context_module.row.b.update_rule.add_hook(GradientScaling(3.0)) optimizer.add_hook(chainer.optimizer.WeightDecay(rate=0.0001)) for param in model.params(): if param.name in ['beta', 'gamma']: param.update_rule.enabled = False model.light_head_rcnn.extractor.conv1.disable_update() model.light_head_rcnn.extractor.res2.disable_update() converter = functools.partial( concat_examples, padding=0, # img, bboxes, labels, scales indices_concat=[0, 2, 3], # img, _, labels, scales indices_to_device=[0], # img ) updater = chainer.training.updater.StandardUpdater(train_iter, optimizer, converter=converter, device=device) trainer = chainer.training.Trainer(updater, (30, 'epoch'), out=args.out) @make_shift('lr') def lr_scheduler(trainer): base_lr = 0.0005 * 1.25 * args.batch_size * comm.size warm_up_duration = 500 warm_up_rate = 1 / 3 iteration = trainer.updater.iteration epoch = trainer.updater.epoch if iteration < warm_up_duration: rate = warm_up_rate \ + (1 - warm_up_rate) * iteration / warm_up_duration elif epoch < 20: rate = 1 elif epoch < 26: rate = 0.1 else: rate = 0.01 return rate * base_lr trainer.extend(lr_scheduler) if comm.rank == 0: # interval log_interval = 100, 'iteration' plot_interval = 3000, 'iteration' print_interval = 20, 'iteration' # training extensions model_name = model.light_head_rcnn.__class__.__name__ trainer.extend(chainer.training.extensions.snapshot_object( model.light_head_rcnn, savefun=chainer.serializers.save_npz, filename='%s_model_iter_{.updater.iteration}.npz' % model_name), trigger=(1, 'epoch')) trainer.extend(extensions.observe_lr(), trigger=log_interval) trainer.extend( extensions.LogReport(log_name='log.json', trigger=log_interval)) report_items = [ 'iteration', 'epoch', 'elapsed_time', 'lr', 'main/loss', 'main/rpn_loc_loss', 'main/rpn_cls_loss', 'main/roi_loc_loss', 'main/roi_cls_loss', 'validation/main/map/iou=0.50:0.95/area=all/max_dets=100', ] trainer.extend(extensions.PrintReport(report_items), trigger=print_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) if extensions.PlotReport.available(): trainer.extend(extensions.PlotReport(['main/loss'], file_name='loss.png', trigger=plot_interval), trigger=plot_interval) trainer.extend(DetectionCOCOEvaluator( test_iter, model.light_head_rcnn, label_names=coco_bbox_label_names), trigger=ManualScheduleTrigger([20, 26], 'epoch')) trainer.extend(extensions.dump_graph('main/loss')) trainer.run()
def main(): parser = argparse.ArgumentParser() parser.add_argument( '--model', choices=('fpn', 'ssd300', 'ssd512'), default='fpn') parser.add_argument('--batchsize', type=int, default=32) parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--out', default='result') parser.add_argument('--data_dir', type=str, default='auto') parser.add_argument('--dataset', choices=['voc', 'coco'], default='voc') parser.add_argument('--lr', type=float, default=1e-3) parser.add_argument('--init_scale', type=float, default=1e-2) parser.add_argument('--resume') args = parser.parse_args() if args.dataset == 'voc': train = ConcatenatedDataset( VOCBboxDataset( year='2007', split='trainval', data_dir=join(args.data_dir, 'VOCdevkit/VOC2007') if args.data_dir != 'auto' else args.data_dir), VOCBboxDataset( year='2012', split='trainval', data_dir=join(args.data_dir, 'VOCdevkit/VOC2012') if args.data_dir != 'auto' else args.data_dir)) test = VOCBboxDataset( year='2007', split='test', use_difficult=True, return_difficult=True, data_dir=join(args.data_dir, 'VOCdevkit/VOC2007') if args.data_dir != 'auto' else args.data_dir) label_names = voc_bbox_label_names elif args.dataset == 'coco': # todo: use train+valminusminival(=coco2017train) # https://github.com/chainer/chainercv/issues/651 train = COCOBboxDataset(data_dir=args.data_dir, split='train') test = COCOBboxDataset(data_dir=args.data_dir, split='val') label_names = coco_bbox_label_names if args.model == 'ssd300': model = SSD300( n_fg_class=len(label_names), pretrained_model='imagenet') elif args.model == 'ssd512': model = SSD512( n_fg_class=len(label_names), pretrained_model='imagenet') elif args.model == 'fpn': model = FPNSSD( n_fg_class=len(label_names), pretrained_model='imagenet', init_scale=args.init_scale) model.use_preset('evaluate') train_chain = MultiboxTrainChain(model) if args.gpu >= 0: chainer.backends.cuda.get_device_from_id(args.gpu).use() model.to_gpu() train = TransformDataset( train, Transform(model.coder, model.insize, model.mean)) train_iter = chainer.iterators.MultithreadIterator(train, args.batchsize) test_iter = chainer.iterators.SerialIterator( test, args.batchsize, repeat=False, shuffle=False) # initial lr is set to 1e-3 by ExponentialShift optimizer = chainer.optimizers.MomentumSGD() optimizer.setup(train_chain) for param in train_chain.params(): if param.name == 'b': param.update_rule.add_hook(GradientScaling(2)) else: param.update_rule.add_hook(WeightDecay(0.0005)) updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu) trainer = training.Trainer(updater, (120000, 'iteration'), args.out) trainer.extend( extensions.ExponentialShift('lr', 0.1, init=args.lr), trigger=triggers.ManualScheduleTrigger([80000, 100000], 'iteration')) trainer.extend( DetectionVOCEvaluator( test_iter, model, use_07_metric=True, label_names=label_names), trigger=(10000, 'iteration')) log_interval = 100, 'iteration' trainer.extend(extensions.LogReport(trigger=log_interval)) trainer.extend(extensions.observe_lr(), trigger=log_interval) trainer.extend( extensions.PrintReport([ 'epoch', 'iteration', 'lr', 'main/loss', 'main/loss/loc', 'main/loss/conf', 'validation/main/map' ]), trigger=log_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend(extensions.snapshot(), trigger=(10000, 'iteration')) trainer.extend( extensions.snapshot_object(model, 'model_iter_{.updater.iteration}'), trigger=(120000, 'iteration')) if args.resume: serializers.load_npz(args.resume, trainer) trainer.run()