Exemple #1
0
def test_cnn_trainset_builder():
    builder = CnnTrainsetBuilder(
        ['tests/fixtures/sen2_20161215_clipped.tif'],
        'tests/fixtures/settlements.geojson',
        size=128,
        step_size=64)

    with tempfile.TemporaryDirectory(
            prefix='aplatam_test_build_trainset') as tmpdir:
        builder.build(tmpdir)

        metadata_path = os.path.join(tmpdir, 'metadata.json')
        assert os.path.exists(metadata_path)
        with open(metadata_path) as f:
            metadata = json.load(f)
            assert isinstance(metadata, dict)
            assert metadata["version"] == __version__
            list_argument = [
                'size', 'step_size', 'buffer_size', 'rescale_intensity',
                'lower_cut', 'upper_cut'
            ]
            for name_argument in list_argument:
                assert metadata[name_argument] == getattr(
                    builder, name_argument)

        for dir_a, dir_b in zip(('train', 'validation', 'test'), ('t', 'f')):
            dirname = os.path.join(tmpdir, dir_a, dir_b)
            assert os.path.exists(dirname), '{} exists'.format(dirname)
            assert glob.glob(os.path.join(
                dirname, '*.jpg')), '{} contains at least one jpg file'
Exemple #2
0
def main(args):
    """
    Main entry point allowing external calls

    Args:
      args ([str]): command line parameter list

    """
    args = parse_args(args)
    setup_logging(args.loglevel)

    opts = dict(version=__version__,
                size=args.size,
                step_size=args.step_size,
                buffer_size=args.buffer_size,
                rescale_intensity=args.rescale_intensity,
                lower_cut=args.lower_cut,
                upper_cut=args.upper_cut)
    _logger.info('Options: %s', opts)

    _logger.info('Collect all rasters from %s', args.rasters_dir)
    rasters = all_raster_files(args.rasters_dir)

    validate_rasters_band_count(rasters)

    builder = CnnTrainsetBuilder(rasters, args.vector, **opts)
    builder.build(args.output_dir)

    _logger.info('Done')
def test_cnn_trainset_builder():
    builder = CnnTrainsetBuilder(['tests/fixtures/sen2_20161215_clipped.tif'],
                                 'tests/fixtures/settlements.geojson',
                                 size=128,
                                 step_size=64)

    with tempfile.TemporaryDirectory() as tmpdir:
        builder.build(tmpdir)
        assert_trainset(tmpdir, builder)
def main(args):
    """
    Main entry point allowing external calls

    Args:
      args ([str]): command line parameter list

    """
    args = parse_args(args)
    setup_logging(args.loglevel)

    # Set default output model path, if not set
    if args.output_model:
        output_model = args.output_model
    else:
        output_model = os.path.join(args.output_dir, DEFAULT_MODEL_FILENAME)

    opts = dict(
        size=args.size,
        step_size=args.step_size,
        buffer_size=args.buffer_size,
        rescale_intensity=args.rescale_intensity,
        lower_cut=args.lower_cut,
        upper_cut=args.upper_cut,
        block_size=args.block_size,
        test_size=args.test_size,
        balancing_multiplier=args.balancing_multiplier,
        rasters_contour=args.rasters_contour)
    _logger.info('Options: %s', opts)

    # Set seed number
    if args.seed:
        _logger.info('Seed: %d', args.seed)
        random.seed(args.seed)

    _logger.info('Collect all rasters from %s', args.rasters_dir)
    rasters = all_raster_files(args.rasters_dir)

    validate_rasters_band_count(rasters)

    if not os.path.exists(args.output_dir):
        builder = CnnTrainsetBuilder(rasters, args.vector, **opts)
        builder.build(args.output_dir)

    # Train and save model
    train(
        output_model,
        args.output_dir,
        trainable_layers=args.trainable_layers,
        batch_size=args.batch_size,
        epochs=args.epochs,
        size=args.size)

    _logger.info('Done')
def test_cnn_trainset_builder_with_no_rescale_intensity():
    builder = CnnTrainsetBuilder(['tests/fixtures/sen2_20161215_clipped.tif'],
                                 'tests/fixtures/settlements.geojson',
                                 size=128,
                                 step_size=64,
                                 rescale_intensity=False)

    with tempfile.TemporaryDirectory(
            prefix='aplatam_test_build_trainset') as tmpdir:
        builder.build(tmpdir)
        assert_trainset(tmpdir, builder, empty_dirs=True)
def test_cnn_trainset_builder():
    builder = CnnTrainsetBuilder(['tests/fixtures/sen2_20161215_clipped.tif'],
                                 'tests/fixtures/settlements.geojson',
                                 size=128,
                                 step_size=64)

    with tempfile.TemporaryDirectory(prefix='aplatam_test_') as tmpdir:
        builder.build(tmpdir)

        metadata_path = os.path.join(tmpdir, 'metadata.json')
        with open(metadata_path) as f:
            metadata = json.load(f)
            assert isinstance(metadata, dict)
            assert metadata['version'] == __version__