class TestSegNetBasic(unittest.TestCase): def setUp(self): self.n_class = 10 self.link = SegNetBasic(n_class=self.n_class) def check_call(self): xp = self.link.xp x = chainer.Variable( xp.random.uniform(low=-1, high=1, size=(2, 3, 128, 160)).astype(np.float32)) y = self.link(x) self.assertIsInstance(y, chainer.Variable) self.assertIsInstance(y.array, xp.ndarray) self.assertEqual(y.shape, (2, self.n_class, 128, 160)) @attr.slow def test_call_cpu(self): self.check_call() @attr.gpu @attr.slow def test_call_gpu(self): self.link.to_gpu() self.check_call() @attr.slow def test_predict_cpu(self): assert_is_semantic_segmentation_link(self.link, self.n_class) @attr.gpu @attr.slow def test_predict_gpu(self): self.link.to_gpu() assert_is_semantic_segmentation_link(self.link, self.n_class)
class TestSegNetBasic(unittest.TestCase): def setUp(self): self.n_class = 10 self.link = SegNetBasic(n_class=self.n_class) def check_call(self): xp = self.link.xp x = chainer.Variable(xp.random.uniform( low=-1, high=1, size=(2, 3, 128, 160)).astype(np.float32)) y = self.link(x) self.assertIsInstance(y, chainer.Variable) self.assertIsInstance(y.array, xp.ndarray) self.assertEqual(y.shape, (2, self.n_class, 128, 160)) @attr.slow def test_call_cpu(self): self.check_call() @attr.gpu @attr.slow def test_call_gpu(self): self.link.to_gpu() self.check_call() @attr.slow def test_predict_cpu(self): assert_is_semantic_segmentation_link(self.link, self.n_class) @attr.gpu @attr.slow def test_predict_gpu(self): self.link.to_gpu() assert_is_semantic_segmentation_link(self.link, self.n_class)
def main(): chainer.config.train = False parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained_model', default='camvid') parser.add_argument('image') args = parser.parse_args() model = SegNetBasic( n_class=len(camvid_label_names), pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) labels = model.predict([img]) label = labels[0] fig = plot.figure() ax1 = fig.add_subplot(1, 2, 1) vis_image(img, ax=ax1) ax2 = fig.add_subplot(1, 2, 2) vis_label(label, camvid_label_names, camvid_label_colors, ax=ax2) plot.show()
def main(): chainer.config.train = False parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained_model', default='camvid') parser.add_argument('image') args = parser.parse_args() model = SegNetBasic( n_class=len(camvid_label_names), pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) labels = model.predict([img]) label = labels[0] fig = plot.figure() ax1 = fig.add_subplot(1, 2, 1) vis_image(img, ax=ax1) ax2 = fig.add_subplot(1, 2, 2) vis_semantic_segmentation( label, camvid_label_names, camvid_label_colors, ax=ax2) plot.show()
def main(): chainer.config.train = False parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model') parser.add_argument('--dataset', choices=('camvid', ), default='camvid') parser.add_argument('image') args = parser.parse_args() if args.dataset == 'camvid': if args.pretrained_model is None: args.pretrained_model = 'camvid' label_names = camvid_label_names colors = camvid_label_colors model = SegNetBasic(n_class=len(label_names), pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) labels = model.predict([img]) label = labels[0] fig = plt.figure() ax1 = fig.add_subplot(1, 2, 1) vis_image(img, ax=ax1) ax2 = fig.add_subplot(1, 2, 2) # Do not overlay the label image on the color image vis_semantic_segmentation(None, label, label_names, colors, ax=ax2) plt.show()
def test_pretrained(self): kwargs = { 'n_class': self.n_class, 'pretrained_model': self.pretrained_model, } if self.pretrained_model == 'camvid': valid = self.n_class in {None, 11} if valid: SegNetBasic(**kwargs) else: with self.assertRaises(ValueError): SegNetBasic(**kwargs)
def get_dataset_and_model(dataset_name, model_name, pretrained_model, input_size): if dataset_name == 'cityscapes': dataset = CityscapesSemanticSegmentationDataset( split='val', label_resolution='fine') label_names = cityscapes_semantic_segmentation_label_names elif dataset_name == 'ade20k': dataset = ADE20KSemanticSegmentationDataset(split='val') label_names = ade20k_semantic_segmentation_label_names elif dataset_name == 'camvid': dataset = CamVidDataset(split='test') label_names = camvid_label_names n_class = len(label_names) if pretrained_model: pretrained_model = pretrained_model else: pretrained_model = dataset_name if model_name == 'pspnet_resnet101': model = PSPNetResNet101(n_class=n_class, pretrained_model=pretrained_model, input_size=input_size) elif model_name == 'pspnet_resnet50': model = PSPNetResNet50(n_class=n_class, pretrained_model=pretrained_model, input_size=input_size) elif model_name == 'segnet': model = SegNetBasic(n_class=n_class, pretrained_model=pretrained_model) return dataset, label_names, model
class TestSegNetBasic(unittest.TestCase): def setUp(self): self.n_class = 10 self.link = SegNetBasic(n_class=self.n_class) def check_call(self): xp = self.link.xp x = chainer.Variable( xp.random.uniform(low=-1, high=1, size=(2, 3, 128, 160)).astype(np.float32)) y = self.link(x) self.assertIsInstance(y, chainer.Variable) self.assertIsInstance(y.data, xp.ndarray) self.assertEqual(y.shape, (2, self.n_class, 128, 160)) def test_call_cpu(self): self.check_call() @attr.gpu def test_call_gpu(self): self.link.to_gpu() self.check_call() def check_predict(self): hs = np.random.randint(128, 160, size=(2, )) ws = np.random.randint(128, 160, size=(2, )) imgs = [ np.random.uniform(size=(3, hs[0], ws[0])).astype(np.float32), np.random.uniform(size=(3, hs[1], ws[1])).astype(np.float32), ] labels = self.link.predict(imgs) self.assertEqual(len(labels), 2) for i in range(2): self.assertIsInstance(labels[i], np.ndarray) self.assertEqual(labels[i].shape, (hs[i], ws[i])) self.assertEqual(labels[i].dtype, np.int64) def test_predict_cpu(self): self.check_predict() @attr.gpu def test_predict_gpu(self): self.link.to_gpu() self.check_predict()
def get_segnet(batchsize): model = SegNetBasic(n_class=17) model = PixelwiseSoftmaxClassifier(model, class_weight=np.ones(17)) x = np.random.uniform(size=(batchsize, 3, 1024, 1024)).astype('f') x = chainer.as_variable(x) t = np.random.randint(size=(batchsize, 1024, 1024), low=0, high=10)\ .astype(np.int32) t = chainer.as_variable(t) return [x, t], model
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained_model', type=str, default='camvid') parser.add_argument('--batchsize', type=int, default=24) args = parser.parse_args() model = SegNetBasic(n_class=len(camvid_label_names), pretrained_model=args.pretrained_model) if args.gpu >= 0: model.to_gpu(args.gpu) model = calc_bn_statistics(model, args.batchsize) chainer.config.train = False test = CamVidDataset(split='test') it = chainer.iterators.SerialIterator(test, batch_size=args.batchsize, repeat=False, shuffle=False) imgs, pred_values, gt_values = apply_prediction_to_iterator( model.predict, it) # Delete an iterator of images to save memory usage. del imgs pred_labels, = pred_values gt_labels, = gt_values confusion = calc_semantic_segmentation_confusion(pred_labels, gt_labels) ious = calc_semantic_segmentation_iou(confusion) pixel_accuracy = np.diag(confusion).sum() / confusion.sum() mean_pixel_accuracy = np.mean( np.diag(confusion) / np.sum(confusion, axis=1)) for iou, label_name in zip(ious, camvid_label_names): print('{:>23} : {:.4f}'.format(label_name, iou)) print('=' * 34) print('{:>23} : {:.4f}'.format('mean IoU', np.nanmean(ious))) print('{:>23} : {:.4f}'.format('Class average accuracy', mean_pixel_accuracy)) print('{:>23} : {:.4f}'.format('Global average accuracy', pixel_accuracy))
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained_model', type=str, default='camvid') parser.add_argument('--batchsize', type=int, default=24) args = parser.parse_args() model = SegNetBasic(n_class=len(camvid_label_names), pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() model = calc_bn_statistics(model, args.batchsize) chainer.config.train = False test = CamVidDataset(split='test') it = chainer.iterators.SerialIterator(test, batch_size=args.batchsize, repeat=False, shuffle=False) imgs, pred_values, gt_values = apply_prediction_to_iterator( model.predict, it) # Delete an iterator of images to save memory usage. del imgs pred_labels, = pred_values gt_labels, = gt_values result = eval_semantic_segmentation(pred_labels, gt_labels) for iu, label_name in zip(result['iou'], camvid_label_names): print('{:>23} : {:.4f}'.format(label_name, iu)) print('=' * 34) print('{:>23} : {:.4f}'.format('mean IoU', result['miou'])) print('{:>23} : {:.4f}'.format('Class average accuracy', result['mean_class_accuracy'])) print('{:>23} : {:.4f}'.format('Global average accuracy', result['pixel_accuracy']))
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--batchsize', type=int, default=12) parser.add_argument('--class_weight', type=str, default='class_weight.npy') parser.add_argument('--out', type=str, default='result') args = parser.parse_args() # Triggers log_trigger = (50, 'iteration') validation_trigger = (2000, 'iteration') end_trigger = (16000, 'iteration') # Dataset train = CamVidDataset(split='train') train = TransformDataset(train, transform) val = CamVidDataset(split='val') # Iterator train_iter = iterators.MultiprocessIterator(train, args.batchsize) val_iter = iterators.MultiprocessIterator( val, args.batchsize, shuffle=False, repeat=False) # Model class_weight = np.load(args.class_weight) model = SegNetBasic(n_class=11) model = PixelwiseSoftmaxClassifier( model, class_weight=class_weight) if args.gpu >= 0: # Make a specified GPU current chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() # Copy the model to the GPU # Optimizer optimizer = optimizers.MomentumSGD(lr=0.1, momentum=0.9) optimizer.setup(model) optimizer.add_hook(chainer.optimizer.WeightDecay(rate=0.0005)) # Updater updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu) # Trainer trainer = training.Trainer(updater, end_trigger, out=args.out) trainer.extend(extensions.LogReport(trigger=log_trigger)) trainer.extend(extensions.observe_lr(), trigger=log_trigger) trainer.extend(extensions.dump_graph('main/loss')) if extensions.PlotReport.available(): trainer.extend(extensions.PlotReport( ['main/loss'], x_key='iteration', file_name='loss.png')) trainer.extend(extensions.PlotReport( ['validation/main/miou'], x_key='iteration', file_name='miou.png')) trainer.extend(extensions.snapshot_object( model.predictor, filename='model_iteration-{.updater.iteration}'), trigger=end_trigger) trainer.extend(extensions.PrintReport( ['epoch', 'iteration', 'elapsed_time', 'lr', 'main/loss', 'validation/main/miou', 'validation/main/mean_class_accuracy', 'validation/main/pixel_accuracy']), trigger=log_trigger) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend( SemanticSegmentationEvaluator( val_iter, model.predictor, camvid_label_names), trigger=validation_trigger) trainer.run()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--batchsize', type=int, default=12) parser.add_argument('--class_weight', type=str, default='class_weight.npy') parser.add_argument('--out', type=str, default='result') args = parser.parse_args() # Triggers log_trigger = (50, 'iteration') validation_trigger = (2000, 'iteration') end_trigger = (16000, 'iteration') # Dataset train = CamVidDataset(split='train') train = TransformDataset(train, transform) val = CamVidDataset(split='val') # Iterator train_iter = iterators.MultiprocessIterator(train, args.batchsize) val_iter = iterators.MultiprocessIterator(val, args.batchsize, shuffle=False, repeat=False) # Model class_weight = np.load(args.class_weight) model = SegNetBasic(n_class=len(camvid_label_names)) model = PixelwiseSoftmaxClassifier(model, class_weight=class_weight) if args.gpu >= 0: # Make a specified GPU current chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() # Copy the model to the GPU # Optimizer optimizer = optimizers.MomentumSGD(lr=0.1, momentum=0.9) optimizer.setup(model) optimizer.add_hook(chainer.optimizer_hooks.WeightDecay(rate=0.0005)) # Updater updater = training.updaters.StandardUpdater(train_iter, optimizer, device=args.gpu) # Trainer trainer = training.Trainer(updater, end_trigger, out=args.out) trainer.extend(extensions.LogReport(trigger=log_trigger)) trainer.extend(extensions.observe_lr(), trigger=log_trigger) trainer.extend(extensions.dump_graph('main/loss')) if extensions.PlotReport.available(): trainer.extend( extensions.PlotReport(['main/loss'], x_key='iteration', file_name='loss.png')) trainer.extend( extensions.PlotReport(['validation/main/miou'], x_key='iteration', file_name='miou.png')) trainer.extend(extensions.snapshot_object( model.predictor, filename='model_iteration-{.updater.iteration}'), trigger=end_trigger) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'elapsed_time', 'lr', 'main/loss', 'validation/main/miou', 'validation/main/mean_class_accuracy', 'validation/main/pixel_accuracy' ]), trigger=log_trigger) trainer.extend(extensions.ProgressBar(update_interval=10)) trainer.extend(SemanticSegmentationEvaluator(val_iter, model.predictor, camvid_label_names), trigger=validation_trigger) trainer.run()
def main(): # This follows evaluation code used in SegNet. # https://github.com/alexgkendall/SegNet-Tutorial/blob/master/ # # Scripts/compute_test_results.m parser = argparse.ArgumentParser() parser.add_argument('gpu', type=int, default=-1) parser.add_argument('snapshot', type=str) parser.add_argument('--batchsize', type=int, default=24) args = parser.parse_args() n_class = 11 ignore_labels = [-1] model = SegNetBasic(n_class=n_class) serializers.load_npz(args.snapshot, model) model = calc_bn_statistics(model, args.gpu) model.train = False if args.gpu >= 0: model.to_gpu(args.gpu) test = CamVidDataset(split='test') it = chainer.iterators.SerialIterator(test, batch_size=args.batchsize, repeat=False, shuffle=False) n_positive = [0 for _ in range(n_class)] n_true = [0 for _ in range(n_class)] n_true_positive = [0 for _ in range(n_class)] for batch in it: img, gt_label = concat_examples(batch, args.gpu) img = chainer.Variable(img, volatile=True) pred_label = F.argmax(F.softmax(model(img)), axis=1) pred_label = cuda.to_cpu(pred_label.data) gt_label = cuda.to_cpu(gt_label) for cls_i in range(n_class): if cls_i in ignore_labels: continue n_positive[cls_i] += np.sum(pred_label == cls_i) n_true[cls_i] += np.sum(gt_label == cls_i) n_true_positive[cls_i] += np.sum( (pred_label == cls_i) * (gt_label == cls_i)) ious = [] mean_accs = [] pixel_accs = [] for cls_i in range(n_class): if cls_i in ignore_labels: continue deno = n_positive[cls_i] + n_true[cls_i] - n_true_positive[cls_i] iou = n_true_positive[cls_i] / deno ious.append(iou) print('{:>23} : {:.4f}'.format(camvid_label_names[cls_i], iou)) mean_accs.append(n_true_positive[cls_i] / n_true[cls_i]) pixel_accs.append([n_true_positive[cls_i], n_true[cls_i]]) print('=' * 34) print('{:>23} : {:.4f}'.format('mean IoU', np.mean(ious))) print('{:>23} : {:.4f}'.format('Class average accuracy', np.mean(mean_accs))) pixel_accs = np.asarray(pixel_accs) pixel_accs = pixel_accs[:, 0].sum() / pixel_accs[:, 1].sum() print('{:>23} : {:.4f}'.format('Global average accuracy', pixel_accs))
def setUp(self): self.n_class = 10 self.link = SegNetBasic(n_class=self.n_class)
def handler(context): # Triggers log_trigger = (50, 'iteration') validation_trigger = (2000, 'iteration') end_trigger = (nb_iterations, 'iteration') # Dataset dataset_alias = context.datasets train_dataset_id = dataset_alias['train'] val_dataset_id = dataset_alias['val'] train = SegmentationDatasetFromAPI(train_dataset_id) val = SegmentationDatasetFromAPI(val_dataset_id) class_weight = calc_weight(train) print(class_weight) train = TransformDataset(train, transform) # Iterator train_iter = iterators.SerialIterator(train, BATCHSIZE) val_iter = iterators.SerialIterator(val, BATCHSIZE, shuffle=False, repeat=False) # Model model = SegNetBasic(n_class=len(camvid_label_names)) model = PixelwiseSoftmaxClassifier(model, class_weight=class_weight) if USE_GPU >= 0: # Make a specified GPU current chainer.cuda.get_device_from_id(USE_GPU).use() model.to_gpu() # Copy the model to the GPU # Optimizer optimizer = optimizers.MomentumSGD(lr=0.1, momentum=0.9) optimizer.setup(model) optimizer.add_hook(chainer.optimizer_hooks.WeightDecay(rate=0.0005)) # Updater updater = training.updaters.StandardUpdater(train_iter, optimizer, device=USE_GPU) # Trainer trainer = training.Trainer(updater, end_trigger, out=ABEJA_TRAINING_RESULT_DIR) trainer.extend(extensions.LogReport(trigger=log_trigger)) trainer.extend(extensions.observe_lr(), trigger=log_trigger) trainer.extend(extensions.dump_graph('main/loss')) trainer.extend(extensions.snapshot_object( model.predictor, filename='model_iteration-{.updater.iteration}'), trigger=end_trigger) print_entries = [ 'iteration', 'main/loss', 'validation/main/miou', 'validation/main/mean_class_accuracy', 'validation/main/pixel_accuracy' ] report_entries = [ 'epoch', 'iteration', 'lr', 'main/loss', 'validation/main/miou', 'validation/main/mean_class_accuracy', 'validation/main/pixel_accuracy' ] trainer.extend(Statistics(report_entries, nb_iterations, obs_key='iteration'), trigger=log_trigger) trainer.extend(Tensorboard(report_entries, out_dir=log_path)) trainer.extend(extensions.PrintReport(print_entries), trigger=log_trigger) trainer.extend(SemanticSegmentationEvaluator(val_iter, model.predictor, camvid_label_names), trigger=validation_trigger) trainer.run()