def main(): parser = argparse.ArgumentParser() parser.add_argument('--model', choices=('fcis_resnet101'), default='fcis_resnet101') parser.add_argument('--pretrained_model') parser.add_argument('--iou-thresh', type=float, default=0.5) parser.add_argument('--gpu', type=int, default=-1) args = parser.parse_args() if args.model == 'fcis_resnet101': if args.pretrained_model: model = FCISResNet101( n_fg_class=len(sbd_instance_segmentation_label_names), pretrained_model=args.pretrained_model) else: model = FCISResNet101(pretrained_model='sbd') model.use_preset('evaluate') if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() dataset = SBDInstanceSegmentationDataset(split='val') 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_masks, pred_labels, pred_scores = out_values gt_masks, gt_labels = rest_values result = eval_instance_segmentation_voc(pred_masks, pred_labels, pred_scores, gt_masks, gt_labels, args.iou_thresh, use_07_metric=True) print('') print('mAP: {:f}'.format(result['map'])) for l, name in enumerate(sbd_instance_segmentation_label_names): if result['ap'][l]: print('{:s}: {:f}'.format(name, result['ap'][l])) else: print('{:s}: -'.format(name))
def test_pretrained(self): kwargs = { 'n_fg_class': self.n_fg_class, 'pretrained_model': self.pretrained_model, } if self.pretrained_model == 'sbd': valid = self.n_fg_class in {None, 20} if valid: FCISResNet101(**kwargs) else: with self.assertRaises(ValueError): FCISResNet101(**kwargs)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model', default='sbd') parser.add_argument('image') args = parser.parse_args() model = FCISResNet101( n_fg_class=20, pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = read_image(args.image, color=True) masks, labels, scores = model.predict([img]) mask, label, score = masks[0], labels[0], scores[0] bbox = mask_to_bbox(mask) colors = voc_colormap(list(range(1, len(mask) + 1))) ax = vis_bbox( img, bbox, instance_colors=colors, alpha=0.5, linewidth=1.5) vis_instance_segmentation( None, mask, label, score, label_names=sbd_instance_segmentation_label_names, instance_colors=colors, alpha=0.7, ax=ax) plt.show()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model', default=None) parser.add_argument('--dataset', choices=('sbd', 'coco'), default='sbd') parser.add_argument('image') args = parser.parse_args() if args.dataset == 'sbd': if args.pretrained_model is None: args.pretrained_model = 'sbd' label_names = sbd_instance_segmentation_label_names model = FCISResNet101(n_fg_class=len(label_names), pretrained_model=args.pretrained_model) elif args.dataset == 'coco': if args.pretrained_model is None: args.pretrained_model = 'coco' label_names = coco_instance_segmentation_label_names proposal_creator_params = FCISResNet101.proposal_creator_params proposal_creator_params['min_size'] = 2 model = FCISResNet101(n_fg_class=len(label_names), anchor_scales=(4, 8, 16, 32), pretrained_model=args.pretrained_model, proposal_creator_params=proposal_creator_params) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = read_image(args.image, color=True) masks, labels, scores = model.predict([img]) mask, label, score = masks[0], labels[0], scores[0] bbox = mask_to_bbox(mask) colors = voc_colormap(list(range(1, len(mask) + 1))) ax = vis_bbox(img, bbox, instance_colors=colors, alpha=0.5, linewidth=1.5) vis_instance_segmentation(None, mask, label, score, label_names=label_names, instance_colors=colors, alpha=0.7, ax=ax) plt.show()
def test_pretrained(self): kwargs = { 'n_fg_class': self.n_fg_class, 'anchor_scales': self.anchor_scales, 'pretrained_model': self.pretrained_model, } if self.pretrained_model.startswith('sbd'): valid = self.n_fg_class in [None, 20] valid = valid and self.anchor_scales == (8, 16, 32) elif self.pretrained_model.startswith('coco'): valid = self.n_fg_class in [None, 80] valid = valid and self.anchor_scales == (4, 8, 16, 32) if valid: FCISResNet101(**kwargs) else: with self.assertRaises(ValueError): FCISResNet101(**kwargs)
def main(): parser = argparse.ArgumentParser( description='Script to convert mxnet params to chainer npz') parser.add_argument('mxnet_param_file', metavar='mxnet-param-file', help='Mxnet param file i.e. fcis_coco-0000.params') parser.add_argument('--process', action='store_true') parser.add_argument('--dataset', choices=('sbd', 'coco'), type=str, default='sbd') parser.add_argument('--out', '-o', type=str, default=None) args = parser.parse_args() if args.dataset == 'sbd': model = FCISResNet101(n_fg_class=20, pretrained_model=None) elif args.dataset == 'coco': model = FCISResNet101(n_fg_class=80, pretrained_model=None, anchor_scales=[4, 8, 16, 32], proposal_creator_params={ 'nms_thresh': 0.7, 'n_train_pre_nms': 6000, 'n_train_post_nms': 300, 'n_test_pre_nms': 6000, 'n_test_post_nms': 300, 'force_cpu_nms': False, 'min_size': 2 }) params = mx.nd.load(args.mxnet_param_file) print('mxnet param is loaded: {}'.format(args.mxnet_param_file)) print('start conversion') if args.process: tests = [k for k in params.keys() if k.endswith('_test')] for test in tests: params[test.replace('_test', '')] = params.pop(test) model = convert(model, params) print('finish conversion') if args.out is None: out = 'fcis_resnet101_{}_converted.npz'.format(args.dataset) print('saving to {}'.format(out)) chainer.serializers.save_npz(out, model)
def setUp(self): proposal_creator_params = { 'n_train_post_nms': self.n_train_post_nms, 'n_test_post_nms': self.n_test_post_nms, } self.link = FCISResNet101( self.n_fg_class, pretrained_model=None, iter2=self.iter2, proposal_creator_params=proposal_creator_params) chainer.config.train = self.train
def setUp(self): proposal_creator_params = { 'n_train_post_nms': self.n_train_post_nms, 'n_test_post_nms': self.n_test_post_nms, } self.model = FCISTrainChain( FCISResNet101(self.n_fg_class, pretrained_model=None, iter2=False, proposal_creator_params=proposal_creator_params)) self.masks = np.random.randint(0, 2, size=(1, self.n_bbox, 600, 800)).astype(np.bool) self.labels = np.random.randint(0, self.n_fg_class, size=(1, self.n_bbox)).astype(np.int32) self.imgs = _random_array(np, (1, 3, 600, 800)) self.scale = np.array(1.)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--model', choices=('fcis_resnet101', ), default='fcis_resnet101') parser.add_argument('--pretrained-model', default=None) parser.add_argument('--gpu', type=int, default=-1) args = parser.parse_args() if args.model == 'fcis_resnet101': if args.pretrained_model is None: args.pretrained_model = 'coco' proposal_creator_params = FCISResNet101.proposal_creator_params proposal_creator_params['min_size'] = 2 model = FCISResNet101( n_fg_class=len(coco_instance_segmentation_label_names), anchor_scales=(4, 8, 16, 32), pretrained_model=args.pretrained_model, proposal_creator_params=proposal_creator_params) model.use_preset('coco_evaluate') if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() dataset = COCOInstanceSegmentationDataset(split='minival', year='2014', 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_masks, pred_labels, pred_scores = out_values gt_masks, gt_labels, gt_areas, gt_crowdeds = rest_values result = eval_instance_segmentation_coco(pred_masks, pred_labels, pred_scores, gt_masks, 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( description='ChainerCV training example: FCIS') parser.add_argument('--gpu', '-g', type=int, default=-1) parser.add_argument('--out', '-o', default='result', help='Output directory') parser.add_argument('--seed', '-s', type=int, default=0) parser.add_argument('--lr', '-l', type=float, default=0.0005) parser.add_argument('--lr-cooldown-factor', '-lcf', type=float, default=0.1) parser.add_argument('--epoch', '-e', type=int, default=42) parser.add_argument('--cooldown-epoch', '-ce', type=int, default=28) args = parser.parse_args() np.random.seed(args.seed) # dataset train_dataset = SBDInstanceSegmentationDataset(split='train') test_dataset = SBDInstanceSegmentationDataset(split='val') # model fcis = FCISResNet101(n_fg_class=len(sbd_instance_segmentation_label_names), pretrained_model='imagenet', iter2=False) fcis.use_preset('evaluate') model = FCISTrainChain(fcis) # gpu if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() # optimizer optimizer = chainer.optimizers.MomentumSGD(lr=args.lr, momentum=0.9) optimizer.setup(model) model.fcis.head.conv1.W.update_rule.add_hook(GradientScaling(3.0)) model.fcis.head.conv1.b.update_rule.add_hook(GradientScaling(3.0)) optimizer.add_hook(chainer.optimizer.WeightDecay(rate=0.0005)) for param in model.params(): if param.name in ['beta', 'gamma']: param.update_rule.enabled = False model.fcis.extractor.conv1.disable_update() model.fcis.extractor.res2.disable_update() train_dataset = TransformDataset(train_dataset, Transform(model.fcis)) # iterator train_iter = chainer.iterators.SerialIterator(train_dataset, batch_size=1) test_iter = chainer.iterators.SerialIterator(test_dataset, batch_size=1, repeat=False, shuffle=False) updater = chainer.training.updater.StandardUpdater( train_iter, optimizer, converter=concat_examples, device=args.gpu) trainer = chainer.training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) # lr scheduler trainer.extend(chainer.training.extensions.ExponentialShift( 'lr', args.lr_cooldown_factor, init=args.lr), trigger=(args.cooldown_epoch, 'epoch')) # interval log_interval = 100, 'iteration' plot_interval = 3000, 'iteration' print_interval = 20, 'iteration' # training extensions trainer.extend(extensions.snapshot_object(model.fcis, filename='snapshot_model.npz'), trigger=(args.epoch, 'epoch')) trainer.extend(extensions.observe_lr(), trigger=log_interval) trainer.extend( extensions.LogReport(log_name='log.json', trigger=log_interval)) trainer.extend(extensions.PrintReport([ 'iteration', 'epoch', 'elapsed_time', 'lr', 'main/loss', 'main/rpn_loc_loss', 'main/rpn_cls_loss', 'main/roi_loc_loss', 'main/roi_cls_loss', 'main/roi_mask_loss', 'validation/main/map', ]), 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(InstanceSegmentationVOCEvaluator( test_iter, model.fcis, iou_thresh=0.5, use_07_metric=True, label_names=sbd_instance_segmentation_label_names), trigger=ManualScheduleTrigger([ len(train_dataset) * args.cooldown_epoch, len(train_dataset) * args.epoch ], 'iteration')) trainer.extend(extensions.dump_graph('main/loss')) trainer.run()
def main(): parser = argparse.ArgumentParser( description='ChainerCV training example: FCIS') parser.add_argument('--out', '-o', default='result', help='Output directory') parser.add_argument('--seed', '-s', type=int, default=0) parser.add_argument( '--lr', '-l', type=float, default=0.0005, help='Default value is for 1 GPU.\n' 'The learning rate should be multiplied by the number of gpu') parser.add_argument('--epoch', '-e', type=int, default=18) parser.add_argument('--cooldown-epoch', '-ce', type=int, default=12) args = parser.parse_args() # chainermn comm = chainermn.create_communicator() device = comm.intra_rank np.random.seed(args.seed) # model proposal_creator_params = FCISResNet101.proposal_creator_params proposal_creator_params['min_size'] = 2 fcis = FCISResNet101( n_fg_class=len(coco_instance_segmentation_label_names), anchor_scales=(4, 8, 16, 32), pretrained_model='imagenet', iter2=False, proposal_creator_params=proposal_creator_params) fcis.use_preset('coco_evaluate') proposal_target_creator = ProposalTargetCreator() proposal_target_creator.neg_iou_thresh_lo = 0.0 model = FCISTrainChain(fcis, proposal_target_creator=proposal_target_creator) chainer.cuda.get_device_from_id(device).use() model.to_gpu() # train dataset train_dataset = COCOInstanceSegmentationDataset(year='2014', split='train') vmml_dataset = COCOInstanceSegmentationDataset(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', 'mask', 'label', 'bbox', 'scale'), Transform(model.fcis)) 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=1) # test dataset if comm.rank == 0: test_dataset = COCOInstanceSegmentationDataset(year='2014', split='minival', use_crowded=True, return_crowded=True, return_area=True) indices = np.arange(len(test_dataset)) test_dataset = test_dataset.slice[indices] test_iter = chainer.iterators.SerialIterator(test_dataset, batch_size=1, repeat=False, shuffle=False) # optimizer optimizer = chainermn.create_multi_node_optimizer( chainer.optimizers.MomentumSGD(momentum=0.9), comm) optimizer.setup(model) model.fcis.head.conv1.W.update_rule.add_hook(GradientScaling(3.0)) model.fcis.head.conv1.b.update_rule.add_hook(GradientScaling(3.0)) optimizer.add_hook(chainer.optimizer.WeightDecay(rate=0.0005)) for param in model.params(): if param.name in ['beta', 'gamma']: param.update_rule.enabled = False model.fcis.extractor.conv1.disable_update() model.fcis.extractor.res2.disable_update() updater = chainer.training.updater.StandardUpdater( train_iter, optimizer, converter=concat_examples, device=device) trainer = chainer.training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) # lr scheduler @make_shift('lr') def lr_scheduler(trainer): base_lr = args.lr iteration = trainer.updater.iteration epoch = trainer.updater.epoch if (iteration * comm.size) < 2000: rate = 0.1 elif epoch < args.cooldown_epoch: rate = 1 else: 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 trainer.extend(extensions.snapshot_object( model.fcis, filename='snapshot_model.npz'), trigger=(args.epoch, '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', 'main/roi_mask_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(InstanceSegmentationCOCOEvaluator( test_iter, model.fcis, label_names=coco_instance_segmentation_label_names), trigger=ManualScheduleTrigger([ len(train_dataset) * args.cooldown_epoch, len(train_dataset) * args.epoch ], 'iteration')) trainer.extend(extensions.dump_graph('main/loss')) trainer.run()
def test_pretrained_n_fg_class(self): link = FCISResNet101(n_fg_class=20, pretrained_model='sbd') self.assertIsInstance(link, FCIS)
def test_pretrained(self): link = FCISResNet101(pretrained_model='sbd') self.assertIsInstance(link, FCIS)
def test_pretrained_wrong_n_fg_class(self): with self.assertRaises(ValueError): FCISResNet101(n_fg_class=10, pretrained_model='sbd')
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model', default='sbd') parser.add_argument('video') args = parser.parse_args() model = FCISResNet101( n_fg_class=20, pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() if args.video == "0": vid = cv2.VideoCapture(0) else: vid = cv2.VideoCapture(args.video) if not vid.isOpened(): raise ImportError("Couldn't open video file or webcam.") # Compute aspect ratio of video vidw = vid.get(cv2.CAP_PROP_FRAME_WIDTH) vidh = vid.get(cv2.CAP_PROP_FRAME_HEIGHT) # vidar = vidw / vidh print(vidw) print(vidh) accum_time = 0 curr_fps = 0 fps = "FPS: ??" prev_time = timer() frame_count = 1 while True: ret, frame = vid.read() if ret == False: print("Done!") return # BGR -> RGB rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Result image result = frame.copy() # (H, W, C) -> (C, H, W) img = np.asarray(rgb, dtype=np.float32).transpose((2, 0, 1)) # Object Detection masks, labels, scores = model.predict([img]) mask, label, score = masks[0], labels[0], scores[0] bbox = mask_to_bbox(mask) colors = voc_colormap(list(range(1, len(mask) + 1))) # For Colors n_inst = len(bbox) instance_colors = voc_colormap(list(range(1, n_inst + 1))) instance_colors = np.array(instance_colors) # For Mask _, H, W = mask.shape canvas_img = np.zeros((H, W, 4), dtype=np.uint8) alf_img = np.zeros((H, W, 1), dtype=np.uint8) if len(bbox) != 0: # for i, bb in enumerate(bbox): for i, (bb, msk) in enumerate(zip(bbox, mask)): # print(i) lb = label[i] conf = score[i].tolist() ymin = int(bb[0]) xmin = int(bb[1]) ymax = int(bb[2]) xmax = int(bb[3]) class_num = int(lb) # Draw box # cv2.rectangle(result, (xmin, ymin), (xmax, ymax), (0,255,0), 2) text = sbd_instance_segmentation_label_names[ class_num] + " " + ('%.2f' % conf) print(text) # text_pos 1 test_x = round(xmax - xmin / 2) - 30 test_y = round(ymax - ymin / 2) - 30 text_top = (test_x, test_y - 10) text_bot = (test_x + 80, test_y + 5) text_pos = (test_x + 5, test_y) # text_pos 2 # text_top = (xmin, ymin - 10) # text_bot = (xmin + 80, ymin + 5) # text_pos = (xmin + 5, ymin) # Draw label cv2.rectangle(result, text_top, text_bot, (255, 255, 255), -1) cv2.putText(result, text, text_pos, cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 0), 1) # Draw msk 1 color = instance_colors[i % len(instance_colors)] rgba = np.append(color, 0.7 * 255) # alpha=0.7 if ymax > ymin and xmax > xmin: canvas_img[msk] = rgba mask_img = np.asarray(canvas_img) tmp_bgr = cv2.split(result) mask_result = cv2.merge(tmp_bgr + [alf_img]) mask_result = cv2.addWeighted(mask_result, 1, mask_img, 0.5, 0) # Draw msk 2 # rgba = np.append((0,255,0), 0.7 * 255) # alpha=0.7 # if ymax > ymin and xmax > xmin: # canvas_img[msk] = rgba # mask_img = np.asarray(canvas_img) # tmp_bgr = cv2.split(result) # mask_result = cv2.merge(tmp_bgr + [alf_img]) # mask_result = cv2.addWeighted(mask_result, 1, mask_img, 0.5, 0) # Calculate FPS curr_time = timer() exec_time = curr_time - prev_time prev_time = curr_time accum_time = accum_time + exec_time curr_fps = curr_fps + 1 if accum_time > 1: accum_time = accum_time - 1 fps = "FPS:" + str(curr_fps) curr_fps = 0 # Draw FPS in top right corner cv2.rectangle(result, (590, 0), (640, 17), (0, 0, 0), -1) cv2.putText(result, fps, (595, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (255, 255, 255), 1) # Draw Frame Number cv2.rectangle(result, (0, 0), (50, 17), (0, 0, 0), -1) cv2.putText(result, str(frame_count), (0, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (255, 255, 255), 1) # Output Result # cv2.imshow("BBOX Result", result) # cv2.imshow("Mask img", mask_img) cv2.imshow("Fcis Result", mask_result) # For Debug print("===== BBOX Result =====") print(type(result)) print(result.shape) print(type(result.shape)) print("===== Mask img =====") print(type(mask_img)) print(mask_img.shape) print(type(mask_img.shape)) # Stop Processing if cv2.waitKey(1) & 0xFF == ord('q'): break frame_count += 1
def main(): parser = argparse.ArgumentParser( description='ChainerCV training example: FCIS') parser.add_argument('--out', '-o', default='result', help='Output directory') parser.add_argument('--seed', '-s', type=int, default=0) parser.add_argument('--lr', '-l', type=float, default=None, help='Learning rate for multi GPUs') parser.add_argument('--batchsize', type=int, default=8) parser.add_argument('--epoch', '-e', type=int, default=42) parser.add_argument('--cooldown-epoch', '-ce', type=int, default=28) 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) # model fcis = FCISResNet101(n_fg_class=len(sbd_instance_segmentation_label_names), pretrained_model='imagenet', iter2=False) fcis.use_preset('evaluate') model = FCISTrainChain(fcis) chainer.cuda.get_device_from_id(device).use() model.to_gpu() # dataset train_dataset = TransformDataset( SBDInstanceSegmentationDataset(split='train'), ('img', 'mask', 'label', 'bbox', 'scale'), Transform(model.fcis)) 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) if comm.rank == 0: test_dataset = SBDInstanceSegmentationDataset(split='val') test_iter = chainer.iterators.SerialIterator(test_dataset, batch_size=1, repeat=False, shuffle=False) # optimizer optimizer = chainermn.create_multi_node_optimizer( chainer.optimizers.MomentumSGD(lr=args.lr, momentum=0.9), comm) optimizer.setup(model) model.fcis.head.conv1.W.update_rule.add_hook(GradientScaling(3.0)) model.fcis.head.conv1.b.update_rule.add_hook(GradientScaling(3.0)) optimizer.add_hook(chainer.optimizer.WeightDecay(rate=0.0005)) for param in model.params(): if param.name in ['beta', 'gamma']: param.update_rule.enabled = False model.fcis.extractor.conv1.disable_update() model.fcis.extractor.res2.disable_update() updater = chainer.training.updater.StandardUpdater( train_iter, optimizer, converter=concat_examples, device=device) trainer = chainer.training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) @make_shift('lr') def lr_scheduler(trainer): if args.lr is None: base_lr = 0.0005 * args.batchsize else: base_lr = args.lr epoch = trainer.updater.epoch if epoch < args.cooldown_epoch: rate = 1 else: 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 trainer.extend(extensions.snapshot_object( model.fcis, filename='snapshot_model.npz'), trigger=(args.epoch, 'epoch')) trainer.extend(extensions.observe_lr(), trigger=log_interval) trainer.extend( extensions.LogReport(log_name='log.json', trigger=log_interval)) trainer.extend(extensions.PrintReport([ 'iteration', 'epoch', 'elapsed_time', 'lr', 'main/loss', 'main/rpn_loc_loss', 'main/rpn_cls_loss', 'main/roi_loc_loss', 'main/roi_cls_loss', 'main/roi_mask_loss', 'validation/main/map', ]), 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(InstanceSegmentationVOCEvaluator( test_iter, model.fcis, iou_thresh=0.5, use_07_metric=True, label_names=sbd_instance_segmentation_label_names), trigger=ManualScheduleTrigger([ len(train_dataset) * args.cooldown_epoch, len(train_dataset) * args.epoch ], 'iteration')) trainer.extend(extensions.dump_graph('main/loss')) trainer.run()