Skip to content

Commit

Permalink
adds new logic for filtering objects
Browse files Browse the repository at this point in the history
fixes training example script to remove diameter as it is not useful
  • Loading branch information
lacan committed Nov 4, 2021
1 parent 62515e4 commit 96c2383
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
3 changes: 1 addition & 2 deletions Scripts/Train-Cellpose-Model.groovy
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import qupath.ext.biop.cellpose.Cellpose2DTraining

// Example for training cellpose
// Requries a project, where there are annotations (usually rectangles) of class "Training" and "Validation" in which there are objects inside.
// Requires a project, where there are annotations (usually rectangles) of class "Training" and "Validation" in which there are objects inside.
// The objects that you have annotated which will be exported as labeled images should have no PathClass at all.


def cellposeTrainer = new Cellpose2DTraining.Builder('cyto2')
.channels("my", "channels") // Up to two channels for training.
.pixelSize(1.2976)
.epochs(500)
.diameter(90) // Diameter for cellpose to further downsample your data to the desired model
.modelDirectory("/where/my/model/should/be/saved")
.build()

Expand Down
11 changes: 9 additions & 2 deletions src/main/java/qupath/ext/biop/cellpose/Cellpose2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,15 @@ private List<PathObject> filterDetections(List<PathObject> rawDetections) {
Geometry intersection = detection.getROI().getGeometry().intersection(nuc2.getROI().getGeometry());
Geometry union = detection.getROI().getGeometry().union(nuc2.getROI().getGeometry());
double iou = intersection.getArea() / union.getArea();
if (envelope.intersects(env) && detection.getROI().getGeometry().intersects(nuc2.getROI().getGeometry()) && iou > this.iouThreshold) {
skippedDetections.add(nuc2);

// Get the difference between the two. In case the result is smaller than half the area of the largest object, remove it
// Or if it exceeds the allowed IoU threshold
Geometry difference = nuc2.getROI().getGeometry().difference(detection.getROI().getGeometry());

if (envelope.intersects(env) && detection.getROI().getGeometry().intersects(nuc2.getROI().getGeometry())) {
if( iou > this.iouThreshold || difference.getArea() < detection.getROI().getGeometry().getArea() / 2.0 ) {
skippedDetections.add(nuc2);
}
}
} catch (Exception e) {
skippedDetections.add(nuc2);
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/qupath/ext/biop/cellpose/Cellpose2DTraining.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void saveImagePairs(List<PathObject> annotations, String imageName, Imag
int finalDownsample = downsample;

logger.info("Saving Images...");
annotations.parallelStream().forEach(a -> {
annotations.stream().forEach(a -> {
int i = idx.getAndIncrement();

RegionRequest request = RegionRequest.createInstance(originalServer.getPath(), finalDownsample, a.getROI());
Expand Down Expand Up @@ -303,6 +303,27 @@ public Builder pixelSize(double pixelSize) {
return this;
}

/**
* Apply percentile normalization to the input image channels.
* <p>
* Note that this can be used in combination with {@link #preprocess(ImageOp...)},
* in which case the order in which the operations are applied depends upon the order
* in which the methods of the builder are called.
* <p>
* Warning! This is applied on a per-tile basis. This can result in artifacts and false detections
* without background/constant regions.
* Consider using {@link #inputAdd(double...)} and {@link #inputScale(double...)} as alternative
* normalization strategies, if appropriate constants can be determined to apply globally.
*
* @param min minimum percentile
* @param max maximum percentile
* @return this builder
*/
public Builder normalizePercentiles(double min, double max) {
this.ops.add(ImageOps.Normalize.percentile(min, max));
return this;
}

/**
* Add an offset as a preprocessing step.
* Usually the value will be negative. Along with {@link #inputScale(double...)} this can be used as an alternative (global) normalization.
Expand Down

0 comments on commit 96c2383

Please sign in to comment.