Exemplo n.º 1
0
    def train_stratum(self, stratum):
        features_shapefile = self.get_output_path("features-{}.shp",
                                                  stratum.id)

        split_features(stratum, self.args.refp, self.args.outdir)

        area_training_polygons = self.get_output_path(
            "training_polygons-{}.shp", stratum.id)
        area_validation_polygons = self.get_output_path(
            "validation_polygons-{}.shp", stratum.id)
        area_statistics = self.get_output_path("statistics-{}.xml", stratum.id)
        area_model = self.get_output_path("model-{}.txt", stratum.id)
        area_confmatout = self.get_output_path(
            "confusion-matrix-training-{}.csv", stratum.id)
        area_days = self.get_output_path("days-{}.txt", stratum.id)

        area_descriptors = []
        area_prodpertile = []
        for tile in stratum.tiles:
            area_descriptors += tile.get_descriptor_paths()
            area_prodpertile.append(len(tile.descriptors))

        run_step(
            Step("SampleSelection", [
                "otbcli", "SampleSelection", self.args.buildfolder, "-ref",
                features_shapefile, "-ratio", self.args.ratio, "-seed",
                self.args.rseed, "-tp", area_training_polygons, "-vp",
                area_validation_polygons
            ]))
        step_args = [
            "otbcli", "CropTypeTrainImagesClassifier", self.args.buildfolder,
            "-mission", self.args.mission.name, "-nodatalabel", -10000,
            "-pixsize", self.args.pixsize, "-outdays", area_days, "-mode",
            self.args.trm, "-io.vd", area_training_polygons, "-rand",
            self.args.rseed, "-sample.bm", 0, "-io.confmatout",
            area_confmatout, "-io.out", area_model, "-sample.mt",
            self.args.nbtrsample, "-sample.mv", 10, "-sample.vfn", "CODE",
            "-sample.vtr", 0.01, "-classifier", self.args.classifier
        ]
        if self.args.red_edge:
            step_args += ["-rededge", "true"]
        step_args += ["-sp"] + self.args.sp
        step_args += ["-prodpertile"] + area_prodpertile
        step_args += ["-il"] + area_descriptors
        if self.args.classifier == "rf":
            step_args += [
                "-classifier.rf.nbtrees", self.args.rfnbtrees,
                "-classifier.rf.min", self.args.rfmin, "-classifier.rf.max",
                self.args.rfmax
            ]
        else:
            step_args += [
                "-classifier.svm.k", "rbf", "-classifier.svm.opt", 1,
                "-imstat", area_statistics
            ]
        run_step(Step("TrainImagesClassifier", step_args))
Exemplo n.º 2
0
    def train_stratum(self, stratum):
        area_model = self.get_output_path("model-{}.txt", stratum.id)
        area_confmatout = self.get_output_path(
            "confusion-matrix-training-{}.csv", stratum.id)
        if self.args.refp is not None:
            features_shapefile = self.get_output_path("features-{}.shp",
                                                      stratum.id)

            split_features(stratum, self.args.refp, self.args.outdir)

            area_training_polygons = self.get_output_path(
                "training_polygons-{}.shp", stratum.id)
            area_validation_polygons = self.get_output_path(
                "validation_polygons-{}.shp", stratum.id)
            area_statistics = self.get_output_path("statistics-{}.xml",
                                                   stratum.id)
            area_days = self.get_output_path("days-{}.txt", stratum.id)

            area_descriptors = []
            area_prodpertile = []
            for tile in stratum.tiles:
                area_descriptors += tile.get_descriptor_paths()
                area_prodpertile.append(len(tile.descriptors))

            run_step(
                Step("SampleSelection", [
                    "otbcli", "SampleSelection", self.args.buildfolder, "-ref",
                    features_shapefile, "-ratio", self.args.ratio, "-seed",
                    self.args.rseed, "-nofilter", "true", "-tp",
                    area_training_polygons, "-vp", area_validation_polygons
                ]))
            step_args = [
                "otbcli", "CropMaskTrainImagesClassifier",
                self.args.buildfolder, "-progress", "false", "-mission",
                self.args.mission.name, "-nodatalabel", -10000, "-pixsize",
                self.args.pixsize, "-outdays", area_days, "-mode",
                self.args.trm, "-io.vd", area_training_polygons, "-rand",
                self.args.rseed, "-sample.bm", 0, "-io.confmatout",
                area_confmatout, "-io.out", area_model, "-sample.mt",
                self.args.nbtrsample, "-sample.mv", 10, "-sample.vfn", "CROP",
                "-sample.vtr", 0.01, "-window", self.args.window, "-bm",
                "true" if self.args.bm else "false", "-classifier",
                self.args.classifier
            ]
            if self.args.red_edge:
                step_args += ["-rededge", "true"]
            step_args += ["-sp"] + self.args.sp
            step_args += ["-prodpertile"] + area_prodpertile
            step_args += ["-il"] + area_descriptors
            if self.args.classifier == "rf":
                step_args += [
                    "-classifier.rf.nbtrees", self.args.rfnbtrees,
                    "-classifier.rf.min", self.args.rfmin,
                    "-classifier.rf.max", self.args.rfmax
                ]
            else:
                step_args += [
                    "-classifier.svm.k", "rbf", "-classifier.svm.opt", 1,
                    "-imstat", area_statistics
                ]

            run_step(Step("TrainImagesClassifier", step_args))
        else:
            for tile in stratum.tiles:
                tile_reference_trimmed = self.get_output_path(
                    "reference-trimmed-{}.tif", tile.id)
                tile_stratum_reference_trimmed = self.get_output_path(
                    "reference-trimmed-{}-{}.tif", stratum.id, tile.id)

                self.rasterize_tile_mask(stratum, tile)

                stratum_tile_mask = self.get_stratum_tile_mask(stratum, tile)

                step_args = [
                    "otbcli_BandMath", "-progress", "false", "-exp",
                    "im1b1 > 0 ? im2b1 : -10000", "-il", stratum_tile_mask,
                    tile_reference_trimmed, "-out",
                    format_otb_filename(tile_stratum_reference_trimmed,
                                        compression='DEFLATE'), "int16"
                ]

                run_step(Step("BandMath_" + str(tile.id), step_args))

            area_model = self.get_output_path("model-{}.txt", stratum.id)
            area_confmatout = self.get_output_path(
                "confusion-matrix-training-{}.csv", stratum.id)

            if self.args.classifier == "svm":
                files = []
                for tile in stratum.tiles:
                    tile_spectral_features = self.get_output_path(
                        "spectral-features-{}.tif", tile.id)
                    tile_stratum_spectral_features = self.get_output_path(
                        "spectral-features-{}-{}.tif", stratum.id, tile.id)

                    stratum_tile_mask = self.get_stratum_tile_mask(
                        stratum, tile)

                    step_args = [
                        "otbcli_BandMath", "-progress", "false", "-exp",
                        "im1b1 > 0 ? im2b1 : -10000", "-il", stratum_tile_mask,
                        tile_spectral_features, "-out",
                        format_otb_filename(tile_stratum_spectral_features,
                                            compression='DEFLATE'), "int16"
                    ]

                    run_step(Step("BandMath_" + str(tile.id), step_args))

                    files.append(tile_stratum_spectral_features)

                step_args = [
                    "otbcli_ComputeImagesStatistics", "-bv", -10000, "-out",
                    area_statistics, "-il"
                ] + files

            step_args = [
                "otbcli", "TrainImagesClassifierNew", self.args.buildfolder,
                "-nodatalabel", -10000, "-rand", self.args.rseed, "-sample.bm",
                0, "-io.confmatout", area_confmatout, "-io.out", area_model,
                "-sample.mt", self.args.nbtrsample, "-sample.mv", 1000,
                "-sample.vfn", "CROP", "-sample.vtr", 0.01, "-classifier",
                self.args.classifier
            ]
            if self.args.classifier == "rf":
                step_args += [
                    "-classifier.rf.nbtrees", self.args.rfnbtrees,
                    "-classifier.rf.min", self.args.rfmin,
                    "-classifier.rf.max", self.args.rfmax
                ]
            else:
                step_args += [
                    "-classifier.svm.k", "rbf", "-classifier.svm.opt", 1,
                    "-imstat", area_statistics
                ]

            step_args.append("-io.rs")
            for tile in stratum.tiles:
                tile_stratum_reference_trimmed = self.get_output_path(
                    "reference-trimmed-{}-{}.tif", stratum.id, tile.id)

                step_args.append(tile_stratum_reference_trimmed)

            step_args.append("-io.il")
            for tile in stratum.tiles:
                tile_spectral_features = self.get_output_path(
                    "spectral-features-{}.tif", tile.id)

                step_args.append(tile_spectral_features)

            run_step(Step("TrainImagesClassifier", step_args))