Skip to content

Commit

Permalink
Switch to domain named prop and fix tensorflow toString issue
Browse files Browse the repository at this point in the history
  • Loading branch information
alanocallaghan committed Apr 30, 2024
1 parent e73df2f commit 3355caa
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 87 deletions.
2 changes: 1 addition & 1 deletion src/main/java/qupath/ext/djl/DjlExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class DjlExtension implements QuPathExtension, GitHubProject {

static {
// Prevent downloading engines automatically
System.setProperty("offline", "true");
System.setProperty("ai.djl.offline", "true");
// Opt out of tracking, see https://github.com/deepjavalibrary/djl/pull/2178/files
System.setProperty("OPT_OUT_TRACKING", "true");
}
Expand Down
67 changes: 32 additions & 35 deletions src/main/java/qupath/ext/djl/DjlTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,7 @@

package qupath.ext.djl;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import ai.djl.Device;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacpp.PointerScope;
import org.bytedeco.javacpp.indexer.BooleanIndexer;
import org.bytedeco.javacpp.indexer.ByteIndexer;
import org.bytedeco.javacpp.indexer.DoubleIndexer;
import org.bytedeco.javacpp.indexer.FloatIndexer;
import org.bytedeco.javacpp.indexer.HalfIndexer;
import org.bytedeco.javacpp.indexer.IntIndexer;
import org.bytedeco.javacpp.indexer.LongIndexer;
import org.bytedeco.javacpp.indexer.UByteIndexer;
import org.bytedeco.javacpp.indexer.UShortIndexer;
import org.bytedeco.opencv.global.opencv_core;
import org.bytedeco.opencv.opencv_core.Mat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ai.djl.MalformedModelException;
import ai.djl.Model;
import ai.djl.engine.Engine;
Expand All @@ -64,10 +36,36 @@
import ai.djl.translate.TranslatorContext;
import ai.djl.util.Pair;
import ai.djl.util.PairList;
import org.bytedeco.javacpp.PointerScope;
import org.bytedeco.javacpp.indexer.BooleanIndexer;
import org.bytedeco.javacpp.indexer.ByteIndexer;
import org.bytedeco.javacpp.indexer.DoubleIndexer;
import org.bytedeco.javacpp.indexer.FloatIndexer;
import org.bytedeco.javacpp.indexer.HalfIndexer;
import org.bytedeco.javacpp.indexer.IntIndexer;
import org.bytedeco.javacpp.indexer.LongIndexer;
import org.bytedeco.javacpp.indexer.UByteIndexer;
import org.bytedeco.javacpp.indexer.UShortIndexer;
import org.bytedeco.opencv.global.opencv_core;
import org.bytedeco.opencv.opencv_core.Mat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qupath.opencv.dnn.DnnModel;
import qupath.opencv.dnn.DnnShape;
import qupath.opencv.tools.OpenCVTools;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Tools to help work with Deep Java Library within QuPath.
*
Expand Down Expand Up @@ -245,21 +243,20 @@ public static Engine getEngine(String name, boolean downloadIfNeeded) throws Ill
}

synchronized (lock) {
var offlineStatus = System.getProperty("offline");
var offlineStatus = System.getProperty("ai.djl.offline");
try {
if (downloadIfNeeded)
System.setProperty("offline", "false");
System.setProperty("ai.djl.offline", "false");
else
System.setProperty("offline", "true");

System.setProperty("ai.djl.offline", "true");
var engine = Engine.getEngine(name);
if (engine != null)
loadedEngines.add(name);
return engine;
} catch (Exception e) {
if (downloadIfNeeded)
if (downloadIfNeeded) {
logger.error("Unable to get engine " + name + ": " + e.getMessage(), e);
else {
} else {
var msg = e.getLocalizedMessage();
if (msg == null)
logger.warn("Unable to get engine {}", name);
Expand All @@ -268,7 +265,7 @@ public static Engine getEngine(String name, boolean downloadIfNeeded) throws Ill
}
return null;
} finally {
System.setProperty("offline", offlineStatus);
System.setProperty("ai.djl.offline", offlineStatus);
}
}
}
Expand Down
61 changes: 30 additions & 31 deletions src/main/java/qupath/ext/djl/DjlZoo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,6 @@

package qupath.ext.djl;

import java.awt.image.BandedSampleModel;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferFloat;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.locationtech.jts.geom.util.AffineTransformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ai.djl.Application;
import ai.djl.MalformedModelException;
import ai.djl.Model;
Expand All @@ -54,12 +29,12 @@
import ai.djl.modality.cv.output.DetectedObjects;
import ai.djl.modality.cv.output.DetectedObjects.DetectedObject;
import ai.djl.modality.cv.output.Joints;
import ai.djl.ndarray.NDList;
import ai.djl.ndarray.types.LayoutType;
import ai.djl.ndarray.types.Shape;
import ai.djl.modality.cv.output.Landmark;
import ai.djl.modality.cv.output.Mask;
import ai.djl.modality.cv.translator.BigGANTranslator;
import ai.djl.ndarray.NDList;
import ai.djl.ndarray.types.LayoutType;
import ai.djl.ndarray.types.Shape;
import ai.djl.repository.Artifact;
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ModelNotFoundException;
Expand All @@ -71,6 +46,9 @@
import ai.djl.util.ClassLoaderUtils;
import ai.djl.util.Pair;
import ai.djl.util.PairList;
import org.locationtech.jts.geom.util.AffineTransformation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qupath.lib.analysis.images.ContourTracing;
import qupath.lib.analysis.images.SimpleImage;
import qupath.lib.geom.Point2;
Expand All @@ -94,6 +72,27 @@
import qupath.lib.roi.RoiTools;
import qupath.lib.roi.interfaces.ROI;

import java.awt.image.BandedSampleModel;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferFloat;
import java.awt.image.WritableRaster;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* Helper class for working with DeepJavaLibrary model zoos.
*
Expand Down Expand Up @@ -244,10 +243,10 @@ public static List<Artifact> listInstanceSegmentationModels() throws ModelNotFou
* @throws IOException
*/
public static Criteria<?, ?> buildCriteria(Artifact artifact, boolean allowDownload) throws ModelNotFoundException, MalformedModelException, IOException {
var before = System.getProperty("offline");
var before = System.getProperty("ai.djl.offline");
try {
if (allowDownload)
System.setProperty("offline", "false");
System.setProperty("ai.djl.offline", "false");


var application = artifact.getMetadata().getApplication();
Expand Down Expand Up @@ -300,7 +299,7 @@ public static List<Artifact> listInstanceSegmentationModels() throws ModelNotFou
}
return builder.build();
} finally {
System.setProperty("offline", before);
System.setProperty("ai.djl.offline", before);
}
}

Expand Down
39 changes: 19 additions & 20 deletions src/main/java/qupath/ext/djl/ui/DjlEngineCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,10 @@

package qupath.ext.djl.ui;

import java.nio.file.Files;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import ai.djl.util.cuda.CudaUtils;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.layout.StackPane;
import javafx.scene.text.TextAlignment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ai.djl.engine.Engine;
import ai.djl.engine.EngineException;
import ai.djl.util.Utils;
import ai.djl.util.cuda.CudaUtils;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
Expand All @@ -45,6 +28,9 @@
import javafx.collections.ObservableMap;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
Expand All @@ -54,8 +40,12 @@
import javafx.scene.control.Separator;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Circle;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import qupath.ext.djl.DjlTools;
import qupath.fx.dialogs.Dialogs;
import qupath.fx.utils.GridPaneUtils;
Expand All @@ -64,6 +54,15 @@
import qupath.lib.gui.QuPathGUI;
import qupath.lib.gui.tools.GuiTools;

import java.nio.file.Files;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
* Command to display Deep Java Library engines.
*
Expand Down Expand Up @@ -268,7 +267,8 @@ private static void updateVersionFromStatus(EngineStatus status, String engineNa
try {
var engine = Engine.getEngine(engineName);
labelVersion.setText(engine.getVersion());
labelVersion.setTooltip(new Tooltip(engine.toString()));
// this is toString, but TF ends up trying to download again.
labelVersion.setTooltip(new Tooltip(engine.getEngineName() + ':' + engine.getVersion()));
return;
} catch (Exception e) {
logger.error("Error updating engine version: {}", e.getMessage(), e);
Expand Down Expand Up @@ -392,7 +392,6 @@ private Boolean checkEngineAvailability(String name, ObjectProperty<EngineStatus
return Boolean.FALSE;
}
}

try {
// Allow engine downloads if not doQuietly
updateStatus(status, EngineStatus.PENDING);
Expand Down

0 comments on commit 3355caa

Please sign in to comment.