Skip to content

Commit

Permalink
update initalizers
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Aug 7, 2024
1 parent 26330ab commit 7cf0d9d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/main/java/ai/nets/samj/models/AbstractSamJ.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public void setDebugging(boolean newState) {
* @throws InterruptedException if the process is interrupted
*/
public <T extends RealType<T> & NativeType<T>>
void updateImage(RandomAccessibleInterval<T> rai) throws IOException, RuntimeException, InterruptedException {
void setImage(RandomAccessibleInterval<T> rai) throws IOException, RuntimeException, InterruptedException {
setImageOfInterest(rai);
if (img.dimensionsAsLongArray()[0] * img.dimensionsAsLongArray()[1] > MAX_ENCODED_AREA_RS * MAX_ENCODED_AREA_RS
|| img.dimensionsAsLongArray()[0] > MAX_ENCODED_SIDE || img.dimensionsAsLongArray()[1] > MAX_ENCODED_SIDE) {
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/ai/nets/samj/models/EfficientSamJ.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ else if (task.status == TaskStatus.CRASHED)
* the ImgLib2 data type of the image provided
* @param manager
* environment manager that contians all the paths to the environments needed, Python executables and model weights
* @param image
* the image where SAM is going to be run on
* @return an instance of {@link EfficientSamJ} that allows running EfficientSAM on an image
* @param debugPrinter
* functional interface to redirect the Python process Appose text log and ouptut to be redirected anywhere
Expand All @@ -157,14 +155,12 @@ else if (task.status == TaskStatus.CRASHED)
*/
public static <T extends RealType<T> & NativeType<T>> EfficientSamJ
initializeSam(SamEnvManagerAbstract manager,
RandomAccessibleInterval<T> image,
final DebugTextPrinter debugPrinter,
final boolean printPythonCode) throws IOException, RuntimeException, InterruptedException {
EfficientSamJ sam = null;
try{
sam = new EfficientSamJ(manager, debugPrinter, printPythonCode);
sam.encodeCoords = new long[] {0, 0};
sam.updateImage(image);
} catch (IOException | RuntimeException | InterruptedException ex) {
if (sam != null) sam.close();
throw ex;
Expand All @@ -191,13 +187,11 @@ else if (task.status == TaskStatus.CRASHED)
* @throws InterruptedException if the process is interrupted
*/
public static <T extends RealType<T> & NativeType<T>> EfficientSamJ
initializeSam(SamEnvManagerAbstract manager, RandomAccessibleInterval<T> image) throws IOException, RuntimeException, InterruptedException {
initializeSam(SamEnvManagerAbstract manager) throws IOException, RuntimeException, InterruptedException {
EfficientSamJ sam = null;
try{
sam = new EfficientSamJ(manager);
sam.encodeCoords = new long[] {0, 0};
sam.updateImage(image);
sam.img = image;
} catch (IOException | RuntimeException | InterruptedException ex) {
if (sam != null) sam.close();
throw ex;
Expand Down Expand Up @@ -399,7 +393,8 @@ void adaptImageToModel(final RandomAccessibleInterval<T> ogImg, RandomAccessible
public static void main(String[] args) throws IOException, RuntimeException, InterruptedException {
RandomAccessibleInterval<UnsignedByteType> img = ArrayImgs.unsignedBytes(new long[] {50, 50, 3});
img = Views.addDimension(img, 1, 2);
try (EfficientSamJ sam = initializeSam(EfficientSamEnvManager.create(), img)) {
try (EfficientSamJ sam = initializeSam(EfficientSamEnvManager.create())) {
sam.setImage(img);
sam.processBox(new int[] {0, 5, 10, 26});
}
}
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/ai/nets/samj/models/EfficientViTSamJ.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,12 @@ else if (task.status == TaskStatus.CRASHED)
*/
public static <T extends RealType<T> & NativeType<T>> EfficientViTSamJ
initializeSam(String modelType, SamEnvManagerAbstract manager,
RandomAccessibleInterval<T> image,
final DebugTextPrinter debugPrinter,
final boolean printPythonCode) throws IOException, RuntimeException, InterruptedException {
EfficientViTSamJ sam = null;
try{
sam = new EfficientViTSamJ(manager, modelType, debugPrinter, printPythonCode);
sam.encodeCoords = new long[] {0, 0};
sam.updateImage(image);
} catch (IOException | RuntimeException | InterruptedException ex) {
if (sam != null) sam.close();
throw ex;
Expand All @@ -235,13 +233,12 @@ else if (task.status == TaskStatus.CRASHED)
* @throws InterruptedException if the process is interrupted
*/
public static <T extends RealType<T> & NativeType<T>> EfficientViTSamJ
initializeSam(String modelType, SamEnvManagerAbstract manager, RandomAccessibleInterval<T> image)
initializeSam(String modelType, SamEnvManagerAbstract manager)
throws IOException, RuntimeException, InterruptedException {
EfficientViTSamJ sam = null;
try{
sam = new EfficientViTSamJ(manager, modelType);
sam.encodeCoords = new long[] {0, 0};
sam.updateImage(image);
} catch (IOException | RuntimeException | InterruptedException ex) {
if (sam != null) sam.close();
throw ex;
Expand Down Expand Up @@ -274,10 +271,9 @@ else if (task.status == TaskStatus.CRASHED)
*/
public static <T extends RealType<T> & NativeType<T>> EfficientViTSamJ
initializeSam(SamEnvManagerAbstract manager,
RandomAccessibleInterval<T> image,
final DebugTextPrinter debugPrinter,
final boolean printPythonCode) throws IOException, RuntimeException, InterruptedException {
return initializeSam(EfficientViTSamEnvManager.DEFAULT_EVITSAM, manager, image, debugPrinter, printPythonCode);
return initializeSam(EfficientViTSamEnvManager.DEFAULT_EVITSAM, manager, debugPrinter, printPythonCode);
}

/**
Expand All @@ -300,8 +296,8 @@ else if (task.status == TaskStatus.CRASHED)
* @throws InterruptedException if the process is interrupted
*/
public static <T extends RealType<T> & NativeType<T>> EfficientViTSamJ
initializeSam(SamEnvManagerAbstract manager, RandomAccessibleInterval<T> image) throws IOException, RuntimeException, InterruptedException {
return initializeSam(EfficientViTSamEnvManager.DEFAULT_EVITSAM, manager, image);
initializeSam(SamEnvManagerAbstract manager) throws IOException, RuntimeException, InterruptedException {
return initializeSam(EfficientViTSamEnvManager.DEFAULT_EVITSAM, manager);
}

@Override
Expand Down Expand Up @@ -497,7 +493,8 @@ void adaptImageToModel(RandomAccessibleInterval<T> ogImg, RandomAccessibleInterv
public static void main(String[] args) throws IOException, RuntimeException, InterruptedException {
RandomAccessibleInterval<UnsignedByteType> img = ArrayImgs.unsignedBytes(new long[] {50, 50, 3});
img = Views.addDimension(img, 1, 2);
try (EfficientViTSamJ sam = initializeSam(EfficientViTSamEnvManager.create(), img)) {
try (EfficientViTSamJ sam = initializeSam(EfficientViTSamEnvManager.create())) {
sam.setImage(img);
sam.processBox(new int[] {0, 5, 10, 26});
}
}
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/ai/nets/samj/models/Sam2.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import ai.nets.samj.install.Sam2EnvManager;
import ai.nets.samj.install.SamEnvManagerAbstract;

import java.io.File;
import java.io.IOException;

import io.bioimage.modelrunner.apposed.appose.Environment;
Expand Down Expand Up @@ -186,14 +185,12 @@ else if (task.status == TaskStatus.CRASHED)
*/
public static <T extends RealType<T> & NativeType<T>> Sam2
initializeSam(String modelType, SamEnvManagerAbstract manager,
RandomAccessibleInterval<T> image,
final DebugTextPrinter debugPrinter,
final boolean printPythonCode) throws IOException, RuntimeException, InterruptedException {
Sam2 sam = null;
try{
sam = new Sam2(manager, modelType, debugPrinter, printPythonCode);
sam.encodeCoords = new long[] {0, 0};
sam.updateImage(image);
} catch (IOException | RuntimeException | InterruptedException ex) {
if (sam != null) sam.close();
throw ex;
Expand All @@ -220,13 +217,12 @@ else if (task.status == TaskStatus.CRASHED)
* @throws InterruptedException if the process is interrupted
*/
public static <T extends RealType<T> & NativeType<T>> Sam2
initializeSam(String modelType, SamEnvManagerAbstract manager, RandomAccessibleInterval<T> image)
initializeSam(String modelType, SamEnvManagerAbstract manager)
throws IOException, RuntimeException, InterruptedException {
Sam2 sam = null;
try{
sam = new Sam2(manager, modelType);
sam.encodeCoords = new long[] {0, 0};
sam.updateImage(image);
} catch (IOException | RuntimeException | InterruptedException ex) {
if (sam != null) sam.close();
throw ex;
Expand Down Expand Up @@ -259,10 +255,9 @@ else if (task.status == TaskStatus.CRASHED)
*/
public static <T extends RealType<T> & NativeType<T>> Sam2
initializeSam(SamEnvManagerAbstract manager,
RandomAccessibleInterval<T> image,
final DebugTextPrinter debugPrinter,
final boolean printPythonCode) throws IOException, RuntimeException, InterruptedException {
return initializeSam(Sam2EnvManager.DEFAULT_SAM2, manager, image, debugPrinter, printPythonCode);
return initializeSam(Sam2EnvManager.DEFAULT_SAM2, manager, debugPrinter, printPythonCode);
}

/**
Expand All @@ -285,8 +280,8 @@ else if (task.status == TaskStatus.CRASHED)
* @throws InterruptedException if the process is interrupted
*/
public static <T extends RealType<T> & NativeType<T>> Sam2
initializeSam(SamEnvManagerAbstract manager, RandomAccessibleInterval<T> image) throws IOException, RuntimeException, InterruptedException {
return initializeSam(Sam2EnvManager.DEFAULT_SAM2, manager, image);
initializeSam(SamEnvManagerAbstract manager) throws IOException, RuntimeException, InterruptedException {
return initializeSam(Sam2EnvManager.DEFAULT_SAM2, manager);
}

@Override
Expand Down Expand Up @@ -482,7 +477,8 @@ void adaptImageToModel(RandomAccessibleInterval<T> ogImg, RandomAccessibleInterv
public static void main(String[] args) throws IOException, RuntimeException, InterruptedException {
RandomAccessibleInterval<UnsignedByteType> img = ArrayImgs.unsignedBytes(new long[] {50, 50, 3});
img = Views.addDimension(img, 1, 2);
try (Sam2 sam = initializeSam(Sam2EnvManager.create(), img)) {
try (Sam2 sam = initializeSam(Sam2EnvManager.create())) {
sam.setImage(img);
sam.processBox(new int[] {0, 5, 10, 26});
}
}
Expand Down

0 comments on commit 7cf0d9d

Please sign in to comment.