Exemple #1
0
def gen_mask_region_by_tile(field_region: str,
                            stack_ind: str,
                            working_dir: str,
                            current_tile: str,
                            all_model: str,
                            shp_rname: str,
                            path_to_img: str,
                            path_test: str,
                            path_to_config: str,
                            path_wd: Optional[str] = None) -> List[str]:
    """
    Parameters
    ----------
    field_region: string
    stack_ind: string
    working_dir: string
    current_tile: string
    all_model: string
    shp_rname: string
    path_to_image: string
    path_test: string
    path_wd: string
    path_to_config: string
        config file containing which model is associated to a given tile
    Return
    ------
    list(str)
    """
    model_tile = []
    for path in all_model:
        current_model = path.split("/")[-1].split("_")[1]
        tiles = fu.getListTileFromModel(current_model, path_to_config)
        model = path.split("/")[-1].split("_")[1]
        for tile in tiles:
            # get the model which learn the current tile
            if tile == current_tile:
                model_tile.append(model)
            path_to_feat = os.path.join(path_to_img, tile, "Final", stack_ind)
            mask_shp = os.path.join(path_test, "shapeRegion",
                                    f"{shp_rname}_region_{model}_{tile}.shp")
            mask_tif = os.path.join(
                working_dir, f"{shp_rname}_region_{model}_{tile}_NODATA.tif")
            mask_tif_f = os.path.join(
                path_test, "classif", "MASK",
                f"{shp_rname}_region_{model}_{tile}_NODATA.tif")
            # Create mask
            if not os.path.exists(mask_tif_f):
                cmd_raster = (
                    f"otbcli_Rasterization -in {mask_shp} -mode attribute "
                    f"-mode.attribute.field {field_region} -im {path_to_feat}"
                    f" -out {mask_tif}")

                run(cmd_raster)
                if path_wd is not None:
                    cmd = "cp " + mask_tif + " " + path_test + "/classif/MASK"
                    run(cmd)
    return model_tile
Exemple #2
0
def launchClassification(model, path_cfg, output_path: str,
                         classifier_name: str, classification_mode: str,
                         nomenclature_path: str, stat, pathToRT, pathToImg,
                         pathToRegion, fieldRegion, pathToCmdClassif, pathOut,
                         RAM, pathWd):
    """
    Parameters
    ----------
    output_path : str
        iota2 output directory

    """
    scriptPath = os.path.join(fu.get_iota2_project_dir(), "iota2")
    pixType = fu.getOutputPixType(nomenclature_path)
    AllCmd = []

    maskFiles = pathOut + "/MASK"
    if not os.path.exists(maskFiles):
        run("mkdir " + maskFiles)

    if pathToRegion is None:
        pathToRegion = os.path.join(output_path, "MyRegion.shp")

    shpRName = pathToRegion.split("/")[-1].replace(".shp", "")

    AllModel_tmp = fu.FileSearch_AND(model, True, "model", ".txt")
    AllModel = fu.fileSearchRegEx(model + "/*model*.txt")

    for currentFile in AllModel_tmp:
        if currentFile not in AllModel:
            os.remove(currentFile)

    for path in AllModel:
        model = path.split("/")[-1].split("_")[1]
        tiles = fu.getListTileFromModel(
            model, output_path + "/config_model/configModel.cfg")
        model_Mask = model
        if re.search('model_.*f.*_', path.split("/")[-1]):
            model_Mask = path.split("/")[-1].split("_")[1].split("f")[0]
        seed = path.split("/")[-1].split("_")[-1].replace(".txt", "")
        suffix = ""
        if "SAR.txt" in os.path.basename(path):
            seed = path.split("/")[-1].split("_")[-2]
            suffix = "_SAR"
        tilesToEvaluate = tiles

        #construction du string de sortie
        for tile in tilesToEvaluate:
            pathToFeat = fu.FileSearch_AND(pathToImg + "/" + tile + "/tmp/",
                                           True, "MaskCommunSL", ".tif")[0]
            maskSHP = pathToRT + "/" + shpRName + "_region_" + model_Mask + "_" + tile + ".shp"
            maskTif = shpRName + "_region_" + model_Mask + "_" + tile + ".tif"
            CmdConfidenceMap = ""
            confidenceMap_name = "{}_model_{}_confidence_seed_{}{}.tif".format(
                tile, model, seed, suffix)
            CmdConfidenceMap = " -confmap " + os.path.join(
                pathOut, confidenceMap_name)

            if not os.path.exists(maskFiles + "/" + maskTif):
                pathToMaskCommun = pathToImg + "/" + tile + "/tmp/" + "MaskCommunSL" + ".shp"
                #cas cluster
                if pathWd != None:
                    maskFiles = pathWd
                nameOut = fu.ClipVectorData(maskSHP, pathToMaskCommun,
                                            maskFiles,
                                            maskTif.replace(".tif", ""))
                cmdRaster = "otbcli_Rasterization -in "+nameOut+" -mode attribute -mode.attribute.field "+\
                        fieldRegion+" -im "+pathToFeat+" -out "+maskFiles+"/"+maskTif
                if "fusion" in classification_mode:
                    cmdRaster = "otbcli_Rasterization -in "+nameOut+" -mode binary -mode.binary.foreground 1 -im "+\
                                pathToFeat+" -out "+maskFiles+"/"+maskTif
                run(cmdRaster)
                if pathWd != None:
                    run("cp " + pathWd + "/" + maskTif + " " + pathOut +
                        "/MASK")
                    os.remove(pathWd + "/" + maskTif)

            out = pathOut + "/Classif_" + tile + "_model_" + model + "_seed_" + seed + suffix + ".tif"

            #hpc case
            if pathWd != None:
                out = "$TMPDIR/Classif_" + tile + "_model_" + model + "_seed_" + seed + suffix + ".tif"
                CmdConfidenceMap = " -confmap $TMPDIR/" + confidenceMap_name

            appli = "python " + scriptPath + "/Classification/ImageClassifier.py -conf " + path_cfg + " "
            pixType_cmd = " -pixType " + pixType
            if pathWd != None:
                pixType_cmd = pixType_cmd + " --wd $TMPDIR "
            cmd = appli + " -in " + pathToFeat + " -model " + path + " -mask " + pathOut + "/MASK/" + maskTif + " -out " + out + " " + pixType_cmd + " -ram " + str(
                RAM) + " " + CmdConfidenceMap

            # add stats if svm
            if "svm" in classifier_name.lower():
                model_statistics = os.path.join(
                    stat, "Model_{}_seed_{}.xml".format(model, seed))
                cmd = "{} -imstat {}".format(cmd, model_statistics)
            AllCmd.append(cmd)
    fu.writeCmds(pathToCmdClassif + "/class.txt", AllCmd)
    return AllCmd