Ejemplo n.º 1
0
def calc_bn_statistics(model, gpu):
    model.to_gpu(gpu)

    d = CamVidDataset(split='train')
    it = chainer.iterators.SerialIterator(d, 24, repeat=False, shuffle=False)
    bn_params = {}
    num_iterations = 0
    for batch in it:
        imgs, labels = concat_examples(batch, device=gpu)
        model(imgs)
        for name, link in model.namedlinks():
            if name.endswith('_bn'):
                if name not in bn_params:
                    bn_params[name] = [
                        cuda.to_cpu(link.avg_mean),
                        cuda.to_cpu(link.avg_var)
                    ]
                else:
                    bn_params[name][0] += cuda.to_cpu(link.avg_mean)
                    bn_params[name][1] += cuda.to_cpu(link.avg_var)
        num_iterations += 1

    for name, params in bn_params.items():
        bn_params[name][0] /= num_iterations
        bn_params[name][1] /= num_iterations

    for name, link in model.namedlinks():
        if name.endswith('_bn'):
            link.avg_mean = bn_params[name][0]
            link.avg_var = bn_params[name][1]

    model.to_cpu()
    return model
Ejemplo n.º 2
0
def recalculate_bn_statistics(model, batchsize, dtype='float32'):
    print(
        '==> Recalculating BN statistics (batchsize={}) ...'.format(batchsize))
    train = CamVidDataset(split='train')
    it = chainer.iterators.SerialIterator(train,
                                          batchsize,
                                          repeat=False,
                                          shuffle=False)
    bn_avg_mean = defaultdict(np.float32)
    bn_avg_var = defaultdict(np.float32)

    if dtype == 'mixed16':
        dtype = 'float16'

    n_iter = 0
    for batch in it:
        imgs, _ = concat_examples(batch)

        model(F.cast(model.xp.array(imgs), dtype))
        for name, link in model.namedlinks():
            if name.endswith('_bn'):
                bn_avg_mean[name] += link.avg_mean
                bn_avg_var[name] += link.avg_var
        n_iter += 1

    for name, link in model.namedlinks():
        if name.endswith('_bn'):
            link.avg_mean = bn_avg_mean[name] / n_iter
            link.avg_var = bn_avg_var[name] / n_iter

    return model
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
Ejemplo n.º 4
0
def calc_bn_statistics(model, batchsize):
    train = CamVidDataset(split="train")
    it = chainer.iterators.SerialIterator(train,
                                          batchsize,
                                          repeat=False,
                                          shuffle=False)
    bn_avg_mean = defaultdict(np.float32)
    bn_avg_var = defaultdict(np.float32)

    n_iter = 0
    for batch in it:
        imgs, _ = concat_examples(batch)
        model(model.xp.array(imgs))
        for name, link in model.namedlinks():
            if name.endwith("_bn"):
                bn_avg_mean[name] += link.avg_mean
                bn_avg_var[name] += link.avg_var
        n_iter += 1

    for name, link in model.namedlinks():
        if name.endwith("_bn"):
            link.avg_mean = bn_avg_mean[name] / n_iter
            link.avg_var = bn_avg_var[name] / n_iter

    return model
Ejemplo n.º 5
0
def CamVid_loader(dataset_dir=root_dir):

    # Dataset
    train = CamVidDataset(dataset_dir, split='train')
    test = CamVidDataset(dataset_dir, split='val')

    train = concat_examples(train)
    test = concat_examples(test)

    train_images = train[:][0]
    train_labels = train[:][1]
    test_images = test[:][0]
    test_labels = test[:][1]

    train_images /= 255.0
    test_images /= 255.0

    return train_images, test_images, train_labels, test_labels
Ejemplo n.º 6
0
def setup(dataset, model, pretrained_model, batchsize, input_size):
    dataset_name = dataset
    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
    elif dataset_name == 'voc':
        dataset = VOCSemanticSegmentationDataset(split='val')
        label_names = voc_semantic_segmentation_label_names

    def eval_(out_values, rest_values):
        pred_labels, = out_values
        gt_labels, = rest_values

        result = eval_semantic_segmentation(pred_labels, gt_labels)

        for iu, label_name in zip(result['iou'], 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']))

    cls, pretrained_models, default_batchsize = models[model]
    if pretrained_model is None:
        pretrained_model = pretrained_models.get(dataset_name, dataset_name)
    if input_size is None:
        input_size = None
    else:
        input_size = (input_size, input_size)

    kwargs = {
        'n_class': len(label_names),
        'pretrained_model': pretrained_model,
    }
    if model in ['pspnet_resnet50', 'pspnet_resnet101']:
        kwargs.update({'input_size': input_size})
    elif model == 'deeplab_v3plus_xception65':
        kwargs.update({'min_input_size': input_size})
    model = cls(**kwargs)

    if batchsize is None:
        batchsize = default_batchsize

    return dataset, eval_, model, batchsize
Ejemplo n.º 7
0
def main():
    n_class = 11
    dataset = CamVidDataset(split="train")

    n_cls_pixels = np.zeros((n_class, ))
    n_img_pizels = np.zeros((n_class, ))

    for img, label in dataset:
        for cls_i in np.unique(label):
            if cls_i == -1:
                continue
            n_cls_pixels[cls_i] += np.sum(label == cls_i)
            n_img_pizels[cls_i] += label.size

    freq = n_cls_pixels / n_img_pizels
    median_freq = np.median(freq)
    np.save("class_weight", median_freq / freq)
Ejemplo n.º 8
0
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))
Ejemplo n.º 9
0
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():
    args = arg()
    model = CamVidAutoEncoder(F.mean_squared_error)
    chainer.serializers.load_npz(args.model, model)

    train = CamVidDataset(split='test')
    img = train[0][0]
    img /= 255.0
    ch, w, h = img.shape
    example_input = img.reshape(1, ch, w, h)
    x = chainer.Variable(example_input)
    y = model.predict(x)

    import matplotlib.pyplot as plt
    img1 = img.transpose(1, 2, 0)
    plt.subplot(1,2,1)
    plt.imshow(img1)
    y = y.data.reshape(ch, w, h)
    y = y.transpose(1, 2, 0).clip(0, 1.0)
    plt.subplot(1,2,2)
    plt.imshow(y)
    plt.show()
Ejemplo n.º 11
0
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(dataset=train, transform=transform)
    val = CamVidDataset(split="val")

    # Iterator
    train_iter = iterators.MultiprocessIterator(train, args.batchsize)
    val_iter = iterators.MultiprocessIterator(val, args.batchsize,
                                              repeat=False,
                                              shuffle=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()
        # Copy the model to the GPU
        model.to_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=updater, stop_trigger=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/loss"], 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()
Ejemplo n.º 12
0
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))
Ejemplo n.º 13
0
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('--val-iter', type=int, default=2000)
    parser.add_argument('--out', type=str, default='result')
    parser.add_argument('--iter', type=int, default=16000)
    parser.add_argument('--dtype', type=str, default='float32')
    parser.add_argument('--init-scale', type=float, default=1.0)
    parser.add_argument('--n-uf', type=float, default=1e-5)
    parser.add_argument('--dynamic-interval', type=int, default=None)
    parser.add_argument('--loss-scale-method',
                        type=str,
                        default='approx_range')
    parser.add_argument('--verbose', action='store_true', default=False)
    args = parser.parse_args()

    # Data type
    print('==> Using data type: {}'.format(args.dtype))
    chainer.global_config.dtype = dtypes[args.dtype]

    # Triggers
    log_trigger = (50, 'iteration')
    validation_trigger = (args.val_iter, 'iteration')
    end_trigger = (args.iter, '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),
                        dtype=dtypes[args.dtype])

    # adaptive loss scaling
    if args.dtype != 'float32':
        recorder = AdaLossRecorder(sample_per_n_iter=100)
        model = AdaLossScaled(model,
                              init_scale=args.init_scale,
                              transforms=[
                                  AdaLossTransformLinear(),
                                  AdaLossTransformConvolution2D(),
                                  AdaLossTransformBatchNormalization(),
                              ],
                              cfg={
                                  'loss_scale_method': args.loss_scale_method,
                                  'scale_upper_bound': 65504,
                                  'accum_upper_bound': 65504,
                                  'update_per_n_iteration': 100,
                                  'recorder': recorder,
                                  'n_uf_threshold': args.n_uf,
                              },
                              verbose=args.verbose)

    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))

    if args.dtype == 'mixed16':
        optimizer.use_fp32_update()

    # Updater
    updater = training.updaters.StandardUpdater(train_iter,
                                                optimizer,
                                                device=args.gpu)

    if args.dtype == 'mixed16':
        if args.dynamic_interval is not None:
            print('==> Using dynamic loss scaling (interval={}) ...'.format(
                args.dynamic_interval))
            optimizer.loss_scaling(interval=args.dynamic_interval, scale=None)
            optimizer._loss_scale_max = 32770.0
        else:
            if args.loss_scale_method == 'approx_range':
                print('==> Using adaptive loss scaling ...')
            else:
                print('==> Using default fixed loss scaling (scale={}) ...'.
                      format(args.init_scale))

            optimizer.loss_scaling(interval=float('inf'), scale=None)
            optimizer._loss_scale_max = 1.0  # to prevent actual loss scaling

    # Trainer
    trainer = training.Trainer(updater, end_trigger, out=args.out)

    # warmup_attr_ratio = 0.1 if args.dtype != 'float32' else None
    # # NOTE: this is confusing but it means n_iter
    # warmup_n_epoch = 1000 if args.dtype != 'float32' else None
    # lr_shift = chainerlp.extensions.ExponentialShift(
    #     'lr',
    #     0.1,
    #     init=0.1 * warmup_attr_ratio,
    #     warmup_attr_ratio=warmup_attr_ratio,
    #     warmup_n_epoch=warmup_n_epoch)
    # trainer.extend(lr_shift, trigger=(1, 'iteration'))

    trainer.extend(extensions.LogReport(trigger=log_trigger))
    trainer.extend(extensions.observe_lr(), trigger=log_trigger)
    if args.dtype != 'float32':
        trainer.extend(extensions.observe_value(
            'loss_scale',
            lambda trainer: trainer.updater.get_optimizer('main')._loss_scale),
                       trigger=log_trigger)
    trainer.extend(extensions.dump_graph('main/loss'))
    # snapshot the trainer after each
    trainer.extend(extensions.snapshot(), trigger=validation_trigger)
    # snapshot the model itself
    trainer.extend(extensions.snapshot_object(
        get_snapshot_model(model), 'model_iter_{.updater.iteration}'),
                   trigger=validation_trigger)

    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'))

    metrics = [
        'epoch', 'iteration', 'elapsed_time', 'lr', 'main/loss',
        'validation/main/miou', 'validation/main/mean_class_accuracy',
        'validation/main/pixel_accuracy'
    ]
    if args.dtype != 'float32':
        metrics.append('loss_scale')
    trainer.extend(extensions.PrintReport(metrics), trigger=log_trigger)
    trainer.extend(extensions.ProgressBar(update_interval=10))

    trainer.extend(SemanticSegmentationEvaluator(val_iter, model.predictor,
                                                 camvid_label_names),
                   trigger=validation_trigger)
    # snapshot the best validation result
    trainer.extend(extensions.snapshot_object(get_snapshot_model(model),
                                              'model_best'),
                   trigger=chainer.training.triggers.MaxValueTrigger(
                       'validation/main/miou', trigger=validation_trigger))

    hooks = []
    if args.dtype != 'float32':
        hook = AdaLossMonitor(sample_per_n_iter=100,
                              verbose=args.verbose,
                              includes=['Grad', 'Deconvolution'])
        recorder.trainer = trainer
        hook.trainer = trainer

        hooks.append(hook)

    with ExitStack() as stack:
        for hook in hooks:
            stack.enter_context(hook)
        trainer.run()

    chainer.serializers.save_npz(
        os.path.join(args.out, 'snapshot_model.npz'),
        recalculate_bn_statistics(model.predictor,
                                  args.batchsize,
                                  dtype=args.dtype))

    if args.dtype != 'float32':
        recorder.export().to_csv(os.path.join(args.out, 'loss_scale.csv'))
 def setUp(self):
     self.dataset = CamVidDataset(split=self.split)
Ejemplo n.º 15
0
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()
Ejemplo n.º 16
0
from __future__ import division

import numpy as np

from chainercv.datasets import CamVidDataset

n_class = 11
dataset = CamVidDataset(split='train')

n_cls_pixels = np.zeros((n_class, ))
n_img_pixels = np.zeros((n_class, ))

for img, label in dataset:
    for cls_i in np.unique(label):
        if cls_i == -1:
            continue
        n_cls_pixels[cls_i] += np.sum(label == cls_i)
        n_img_pixels[cls_i] += label.size
freq = n_cls_pixels / n_img_pixels
median_freq = np.median(freq)

np.save('class_weight', median_freq / freq)
Ejemplo n.º 17
0
def CamVid_loader(dataset_dir=root_dir):

    # Dataset
    train = CamVidDataset(dataset_dir, split='train')
    train = TransformDataset(train, transform)
    test = CamVidDataset(dataset_dir, split='val')