Search in sources :

Example 51 with ParameterList

use of qupath.lib.plugins.parameters.ParameterList in project qupath by qupath.

the class NeuralNetworksClassifier method createAndTrainClassifier.

@Override
protected void createAndTrainClassifier() {
    // Create the required Mats
    int nMeasurements = measurements.size();
    Mat matTraining = new Mat(arrayTraining.length / nMeasurements, nMeasurements, CV_32FC1);
    ((FloatBuffer) matTraining.createBuffer()).put(arrayTraining);
    // Parse parameters
    ParameterList params = getParameterList();
    int nHidden = Math.max(2, params.getIntParameterValue("nHidden"));
    int termIterations = params.getIntParameterValue("termCritMaxIterations");
    double termEPS = params.getDoubleParameterValue("termCritEPS");
    TermCriteria crit = createTerminationCriteria(termIterations, termEPS);
    // Create & train the classifier
    classifier = createClassifier();
    ANN_MLP nnet = (ANN_MLP) classifier;
    System.out.println(nnet.getLayerSizes());
    Mat layers = new Mat(3, 1, CV_32F);
    int n = arrayTraining.length / nMeasurements;
    // layers.put(0, 0, new float[]{nMeasurements, nHidden, pathClasses.size()});
    FloatBuffer bufLayers = layers.createBuffer();
    bufLayers.clear();
    bufLayers.put(nMeasurements);
    // Number of hidden layers
    bufLayers.put(nHidden);
    bufLayers.put(pathClasses.size());
    if (crit != null)
        nnet.setTermCriteria(crit);
    else
        crit = nnet.getTermCriteria();
    nnet.setLayerSizes(layers);
    // matResponses.convertTo(matResponses, CV_32F);
    Mat matResponses = new Mat(n, pathClasses.size(), CV_32F, Scalar.ZERO);
    FloatIndexer indexerResponses = matResponses.createIndexer();
    for (int i = 0; i < n; i++) {
        indexerResponses.put(i, arrayResponses[i], 1);
        indexerResponses.release();
    // matResponses.put(i, arrayResponses[i], 1);
    }
    nnet.setActivationFunction(ANN_MLP.SIGMOID_SYM, 1, 1);
    nnet.train(matTraining, opencv_ml.ROW_SAMPLE, matResponses);
// lastDescription = getName() + "\n\nMain parameters:\n  " + DefaultPluginWorkflowStep.getParameterListJSON(params, "\n  ") + "\n\nTermination criteria:\n  " + crit.toString();
}
Also used : ParameterList(qupath.lib.plugins.parameters.ParameterList) FloatBuffer(java.nio.FloatBuffer) FloatIndexer(org.bytedeco.javacpp.indexer.FloatIndexer)

Example 52 with ParameterList

use of qupath.lib.plugins.parameters.ParameterList in project qupath by qupath.

the class SVMClassifier method createClassifier.

@Override
protected SVM createClassifier() {
    SVM svm = SVM.create();
    ParameterList params = getParameterList();
    if (params != null) {
        String kernel = (String) params.getChoiceParameterValue("kernel");
        kernel = kernel.toLowerCase();
        if (kernel.equals("linear"))
            svm.setKernel(SVM.LINEAR);
        else if (kernel.equals("polynomial"))
            svm.setKernel(SVM.POLY);
        else if (kernel.equals("rbf"))
            svm.setKernel(SVM.RBF);
        else if (kernel.equals("sigmoid"))
            svm.setKernel(SVM.SIGMOID);
        else if (kernel.equals("chi2"))
            svm.setKernel(SVM.CHI2);
        else if (kernel.equals("histogram intersection"))
            svm.setKernel(SVM.INTER);
        double c = params.getDoubleParameterValue("c");
        if (c > 0)
            svm.setC(c);
        svm.setGamma(params.getDoubleParameterValue("gamma"));
        svm.setDegree(params.getIntParameterValue("degree"));
    }
    return svm;
}
Also used : ParameterList(qupath.lib.plugins.parameters.ParameterList) SVM(org.bytedeco.opencv.opencv_ml.SVM)

Example 53 with ParameterList

use of qupath.lib.plugins.parameters.ParameterList in project qupath by qupath.

the class GuiTools method promptForParentObjects.

/**
 * Get the parent objects to use when running the plugin, or null if no suitable parent objects are found.
 * This involves prompting the user if multiple options are possible, and logging an appropriate command
 * in the workflow history of the {@link ImageData} if possible.
 *
 * @param name command name, to include in dialog messages
 * @param imageData imageData containing potential parent objects
 * @param includeSelected if true, provide 'selected objects' as an option
 * @param supportedParents collection of valid parent objects
 * @return
 */
public static <T> boolean promptForParentObjects(final String name, final ImageData<T> imageData, final boolean includeSelected, final Collection<Class<? extends PathObject>> supportedParents) {
    PathObjectHierarchy hierarchy = imageData == null ? null : imageData.getHierarchy();
    if (hierarchy == null)
        return false;
    // Check what possible parent types are available
    Collection<PathObject> possibleParents = null;
    int nParents = 0;
    List<Class<? extends PathObject>> availableTypes = new ArrayList<>();
    for (Class<? extends PathObject> cls : supportedParents) {
        if (cls.equals(PathRootObject.class))
            continue;
        possibleParents = hierarchy.getObjects(possibleParents, cls);
        if (possibleParents.size() > nParents)
            availableTypes.add(cls);
        nParents = possibleParents.size();
    }
    // Create a map of potential choices
    LinkedHashMap<String, Class<? extends PathObject>> choices = new LinkedHashMap<>();
    for (Class<? extends PathObject> cls : availableTypes) choices.put(PathObjectTools.getSuitableName(cls, true), cls);
    if (supportedParents.contains(PathRootObject.class))
        choices.put("Entire image", PathRootObject.class);
    ArrayList<String> choiceList = new ArrayList<>(choices.keySet());
    // Add selected objects option, if required
    if (includeSelected)
        choiceList.add(0, "Selected objects");
    // Determine the currently-selected object
    PathObject pathObjectSelected = hierarchy.getSelectionModel().getSelectedObject();
    // If the currently-selected object is supported, use it as the parent
    if (!includeSelected && pathObjectSelected != null && !pathObjectSelected.isRootObject()) {
        if (supportedParents.contains(pathObjectSelected.getClass()))
            return true;
    // else {
    // String message = name + " does not support parent objects of type " + pathObjectSelected.getClass().getSimpleName();
    // DisplayHelpers.showErrorMessage(name + " error", message);
    // return false;
    // }
    }
    // If the root object is supported, and we don't have any of the other types, just run for the root object
    if (!includeSelected && availableTypes.isEmpty()) {
        if (supportedParents.contains(PathRootObject.class))
            return true;
        else {
            String message = name + " requires parent objects of one of the following types:";
            for (Class<? extends PathObject> cls : supportedParents) message += ("\n" + PathObjectTools.getSuitableName(cls, false));
            Dialogs.showErrorMessage(name + " error", message);
            return false;
        }
    }
    // Prepare to prompt
    ParameterList paramsParents = new ParameterList();
    paramsParents.addChoiceParameter(KEY_REGIONS, "Process all", choiceList.get(0), choiceList);
    if (!Dialogs.showParameterDialog("Process regions", paramsParents))
        return false;
    String choiceString = (String) paramsParents.getChoiceParameterValue(KEY_REGIONS);
    if (!"Selected objects".equals(choiceString))
        Commands.selectObjectsByClass(imageData, choices.get(choiceString));
    // Success!  Probably...
    return !hierarchy.getSelectionModel().noSelection();
}
Also used : PathObjectHierarchy(qupath.lib.objects.hierarchy.PathObjectHierarchy) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) PathRootObject(qupath.lib.objects.PathRootObject) PathObject(qupath.lib.objects.PathObject) ParameterList(qupath.lib.plugins.parameters.ParameterList)

Aggregations

ParameterList (qupath.lib.plugins.parameters.ParameterList)53 BufferedImage (java.awt.image.BufferedImage)15 ArrayList (java.util.ArrayList)13 IOException (java.io.IOException)11 List (java.util.List)9 Collectors (java.util.stream.Collectors)8 BorderPane (javafx.scene.layout.BorderPane)8 PathObject (qupath.lib.objects.PathObject)8 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 GeneralTools (qupath.lib.common.GeneralTools)7 Dialogs (qupath.lib.gui.dialogs.Dialogs)7 ImageServer (qupath.lib.images.servers.ImageServer)7 Insets (javafx.geometry.Insets)6 Tooltip (javafx.scene.control.Tooltip)6 Collection (java.util.Collection)5 Map (java.util.Map)5 SimpleStringProperty (javafx.beans.property.SimpleStringProperty)5 ContextMenu (javafx.scene.control.ContextMenu)5 Label (javafx.scene.control.Label)5