Skip to content

Commit

Permalink
completely restore the Bioengine
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Oct 16, 2023
1 parent caa3e38 commit 3c1696b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,10 @@
<groupId>net.imglib2</groupId>
<artifactId>imglib2</artifactId>
</dependency>
<dependency>
<groupId>org.msgpack</groupId>
<artifactId>jackson-dataformat-msgpack</artifactId>
<version>0.9.5</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
Expand All @@ -39,6 +38,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import io.bioimage.modelrunner.bioimageio.bioengine.BioEngineAvailableModels;
import io.bioimage.modelrunner.bioimageio.description.ModelDescriptor;
import io.bioimage.modelrunner.bioimageio.download.DownloadModel;
import io.bioimage.modelrunner.bioimageio.download.DownloadTracker;
Expand Down Expand Up @@ -340,6 +340,18 @@ public static List<String> getModelNicknames(){
return modelNicknames;
}

/**
* Check whether a model is available on the Bioengine or not by providing its id
* @param id
* id of the model of interest
* @return true if it is available or false otherwise
* @throws IOException if there is no connection to the internet or the
* JSOn with the information cannot be accessed: https://raw.githubusercontent.com/bioimage-io/bioengine-model-runner/gh-pages/manifest.bioengine.json
*/
public static boolean isModelOnTheBioengineById(String id) throws IOException {
return BioEngineAvailableModels.isModelSupportedInBioengine(id);
}

/**
* Return the {@link ModelDescriptor} for the model defined by the modelID
* (field 'id' in the rdf.yaml) introduced as a parameter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import javax.xml.bind.ValidationException;

import io.bioimage.modelrunner.bioimageio.BioimageioRepo;
import io.bioimage.modelrunner.bioimageio.description.weights.ModelWeight;
import io.bioimage.modelrunner.utils.Log;
import io.bioimage.modelrunner.utils.YAMLUtils;
Expand Down Expand Up @@ -326,7 +327,7 @@ private void addBioEngine() throws ValidationException {
return;
}
try {
supportBioengine = false;// BioimageioRepo.isModelOnTheBioengineById(modelID);
supportBioengine = BioimageioRepo.isModelOnTheBioengineById(modelID);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import io.bioimage.modelrunner.bioimageio.bioengine.BioengineInterface;
import io.bioimage.modelrunner.exceptions.LoadEngineException;
import io.bioimage.modelrunner.versionmanagement.DeepLearningVersion;

Expand Down Expand Up @@ -127,6 +128,7 @@ private EngineLoader( ClassLoader classloader, EngineInfo engineInfo ) throws Lo
this.engine = engineInfo.getFramework();
this.bioengine = engineInfo.isBioengine();
if (engineInfo.isBioengine()) {
this.engineInstance = new BioengineInterface();
return;
}
this.enginePath = engineInfo.getDeepLearningVersionJarsDirectory();
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/io/bioimage/modelrunner/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

import javax.xml.bind.ValidationException;

import io.bioimage.modelrunner.bioimageio.bioengine.BioEngineAvailableModels;
import io.bioimage.modelrunner.bioimageio.bioengine.BioengineInterface;
import io.bioimage.modelrunner.bioimageio.description.ModelDescriptor;
import io.bioimage.modelrunner.bioimageio.description.weights.WeightFormat;
import io.bioimage.modelrunner.engine.DeepLearningEngineInterface;
Expand Down Expand Up @@ -381,6 +383,35 @@ public static Model createDeepLearningModel( String modelFolder, String modelSou
return new Model( engineInfo, modelFolder, modelSource, classLoader );
}

/**
* Load a model from the bioimage.io directly on the Bioengine.
* Only the path to the model folder that contains the rdf.yaml is needed.
* To load a model on the bioengine we need to specify the server where our instance
* of the Bioengine is hosted.
* @param bmzModelFolder
* folder where the bioimage.io model is located (parent folder of the rdf.yaml file)
* @param serverURL
* url where the wanted insance of the bioengine is hosted
* @return a model ready to be loaded
* @throws Exception if there is any error creating the model (no rdf.yaml file,
* or the url does not exist) or if the model is not supported on the Bioengine.
* To check the models supported on the Bioengine, visit: https://raw.githubusercontent.com/bioimage-io/bioengine-model-runner/gh-pages/manifest.bioengine.yaml
*/
public static Model createBioimageioModelForBioengine(String bmzModelFolder, String serverURL) throws Exception {
if (new File(bmzModelFolder, Constants.RDF_FNAME).isFile() == false)
throw new IOException("A Bioimage.io model folder should contain its corresponding rdf.yaml file.");
ModelDescriptor descriptor =
ModelDescriptor.readFromLocalFile(bmzModelFolder + File.separator + Constants.RDF_FNAME, false);
boolean valid = BioEngineAvailableModels.isModelSupportedInBioengine(descriptor.getModelID());
if (!valid)
throw new IllegalArgumentException("The selected model is currently not supported by the Bioegine. "
+ "To check the list of supported models please visit: " + BioEngineAvailableModels.getBioengineJson());
EngineInfo info = EngineInfo.defineBioengine(serverURL);
Model model = Model.createDeepLearningModel(bmzModelFolder, null, info);
model.bioengine = true;
return model;
}

/**
* Sets the classloader containing the Deep Learning engine
*
Expand Down Expand Up @@ -411,6 +442,8 @@ public void loadModel() throws LoadModelException
DeepLearningEngineInterface engineInstance = engineClassLoader.getEngineInstance();
engineClassLoader.setEngineClassLoader();
engineInstance.loadModel( modelFolder, modelSource );
if (engineClassLoader.isBioengine())
((BioengineInterface) engineInstance).addServer(engineInfo.getServer());
engineClassLoader.setBaseClassLoader();
}

Expand Down

0 comments on commit 3c1696b

Please sign in to comment.