Skip to content

Commit

Permalink
keep adapting samj
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Aug 7, 2024
1 parent 7cf0d9d commit b8ef3e6
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 127 deletions.
36 changes: 21 additions & 15 deletions src/main/java/ai/nets/samj/communication/model/EfficientSAM.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@

import ai.nets.samj.models.AbstractSamJ;
import ai.nets.samj.models.EfficientSamJ;
import ai.nets.samj.install.SamEnvManager;
import ai.nets.samj.install.EfficientSamEnvManager;
import ai.nets.samj.install.SamEnvManagerAbstract;
import ai.nets.samj.ui.SAMJLogger;

/**
Expand All @@ -45,6 +46,7 @@
public class EfficientSAM implements SAMModel {

private EfficientSamJ efficientSamJ;
private final SamEnvManagerAbstract manager;
private SAMJLogger log;
private Boolean installed = false;
private boolean onlyBiggest = false;
Expand All @@ -67,7 +69,6 @@ public class EfficientSAM implements SAMModel {

private static final String CAUTION_STRING = "<br><p style=\"color: green;\">CAUTION: This model is computationally heavy. It is not recommended to use it on lower-end computers.</p>";

public EfficientSAM() {}

/**
* Create an instance of the model that loads the model and encodes an image
Expand All @@ -79,17 +80,10 @@ public EfficientSAM() {}
* @throws RuntimeException if there is any error running the Python code
* @throws InterruptedException if the process is interrupted
*/
public EfficientSAM(final RandomAccessibleInterval<?> image, final SAMJLogger log)
throws IOException, RuntimeException, InterruptedException {
this.log = log;
AbstractSamJ.DebugTextPrinter filteringLogger = text -> {
int idx = text.indexOf("contours_x");
if (idx > 0) this.log.info( text.substring(0,idx) );
else this.log.info( text );
};
public EfficientSAM() throws IOException, RuntimeException, InterruptedException {
this.manager = EfficientSamEnvManager.create();
efficientSamJ = EfficientSamJ.initializeSam(
SamEnvManager.create(), Cast.unchecked(image),
filteringLogger, false);
manager);
}

@Override
Expand Down Expand Up @@ -120,12 +114,19 @@ public boolean isInstalled() {
/**
* {@inheritDoc}
*/
public SAMModel instantiate(final RandomAccessibleInterval<?> image, final SAMJLogger useThisLoggerForIt)
public void setImage(final RandomAccessibleInterval<?> image, final SAMJLogger useThisLoggerForIt)
throws IOException, InterruptedException, RuntimeException {
try {
return new EfficientSAM(image,useThisLoggerForIt);
this.log = useThisLoggerForIt;
AbstractSamJ.DebugTextPrinter filteringLogger = text -> {
int idx = text.indexOf("contours_x");
if (idx > 0) this.log.info( text.substring(0,idx) );
else this.log.info( text );
};
this.efficientSamJ.setDebugPrinter(filteringLogger);
this.efficientSamJ.setImage(Cast.unchecked(image));;
} catch (IOException | InterruptedException | RuntimeException e) {
useThisLoggerForIt.error(FULL_NAME + " experienced an error: " + e.getMessage());
log.error(FULL_NAME + " experienced an error: " + e.getMessage());
throw e;
}
}
Expand Down Expand Up @@ -270,4 +271,9 @@ public void deleteEncoding(String encodingName) throws IOException, InterruptedE
throw e;
}
}

@Override
public SamEnvManagerAbstract getInstallationManger() {
return this.manager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@

import ai.nets.samj.models.AbstractSamJ;
import ai.nets.samj.models.EfficientViTSamJ;
import ai.nets.samj.install.SamEnvManager;
import ai.nets.samj.install.EfficientViTSamEnvManager;
import ai.nets.samj.install.SamEnvManagerAbstract;
import ai.nets.samj.ui.SAMJLogger;

/**
Expand All @@ -45,6 +46,7 @@
public class EfficientViTSAML0 implements SAMModel {

private EfficientViTSamJ efficientSamJ;
private final SamEnvManagerAbstract manager;
private SAMJLogger log;
private Boolean installed = false;
private boolean onlyBiggest = false;
Expand All @@ -66,10 +68,6 @@ public class EfficientViTSAML0 implements SAMModel {
+ "<strong>Paper:</strong> <a href=\"https://arxiv.org/pdf/2402.05008.pdf\">EfficientViT-SAM: Accelerated "
+ "Segment Anything Model Without Performance Loss</a>";

/**
* Create an empty instance of the model
*/
public EfficientViTSAML0() {}

@Override
/**
Expand Down Expand Up @@ -99,12 +97,19 @@ public boolean isInstalled() {
/**
* {@inheritDoc}
*/
public SAMModel instantiate(final RandomAccessibleInterval<?> image, final SAMJLogger useThisLoggerForIt)
public void setImage(final RandomAccessibleInterval<?> image, final SAMJLogger useThisLoggerForIt)
throws IOException, InterruptedException, RuntimeException {
try {
return new EfficientViTSAML0(image,useThisLoggerForIt);
this.log = useThisLoggerForIt;
AbstractSamJ.DebugTextPrinter filteringLogger = text -> {
int idx = text.indexOf("contours_x");
if (idx > 0) this.log.info( text.substring(0,idx) );
else this.log.info( text );
};
this.efficientSamJ.setDebugPrinter(filteringLogger);
this.efficientSamJ.setImage(Cast.unchecked(image));;
} catch (IOException | InterruptedException | RuntimeException e) {
useThisLoggerForIt.error(FULL_NAME + " experienced an error: " + e.getMessage());
log.error(FULL_NAME + " experienced an error: " + e.getMessage());
throw e;
}
}
Expand All @@ -127,17 +132,9 @@ public void setInstalled(boolean installed) {
* @throws RuntimeException if there is any error running the Python code
* @throws InterruptedException if the process is interrupted
*/
public EfficientViTSAML0(final RandomAccessibleInterval<?> image, final SAMJLogger log)
throws IOException, RuntimeException, InterruptedException {
this.log = log;
AbstractSamJ.DebugTextPrinter filteringLogger = text -> {
int idx = text.indexOf("contours_x");
if (idx > 0) this.log.info( text.substring(0,idx) );
else this.log.info( text );
};
efficientSamJ = EfficientViTSamJ.initializeSam("l0",
SamEnvManager.create(), Cast.unchecked(image),
filteringLogger, false);
public EfficientViTSAML0() throws IOException, RuntimeException, InterruptedException {
this.manager = EfficientViTSamEnvManager.create(EfficientViTSamEnvManager.DEFAULT_DIR, "l0");
efficientSamJ = EfficientViTSamJ.initializeSam("l0", manager);
}

@Override
Expand Down Expand Up @@ -269,4 +266,9 @@ public void deleteEncoding(String encodingName) throws IOException, InterruptedE
throw e;
}
}

@Override
public SamEnvManagerAbstract getInstallationManger() {
return this.manager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
import java.util.List;
import java.util.stream.Collectors;

import ai.nets.samj.install.EfficientViTSamEnvManager;
import ai.nets.samj.install.SamEnvManagerAbstract;
import ai.nets.samj.models.AbstractSamJ;
import ai.nets.samj.models.EfficientViTSamJ;
import ai.nets.samj.install.SamEnvManager;
import ai.nets.samj.ui.SAMJLogger;

/**
Expand All @@ -45,6 +46,7 @@
public class EfficientViTSAML1 implements SAMModel {

private EfficientViTSamJ efficientSamJ;
private final SamEnvManagerAbstract manager;
private SAMJLogger log;
private Boolean installed = false;
private boolean onlyBiggest = false;
Expand All @@ -66,12 +68,6 @@ public class EfficientViTSAML1 implements SAMModel {
+ "<strong>Paper:</strong> <a href=\"https://arxiv.org/pdf/2402.05008.pdf\">EfficientViT-SAM: Accelerated "
+ "Segment Anything Model Without Performance Loss</a>";

/**
* Create an empty instance of the model
*/
public EfficientViTSAML1() {

}

@Override
/**
Expand Down Expand Up @@ -101,12 +97,19 @@ public boolean isInstalled() {
/**
* {@inheritDoc}
*/
public SAMModel instantiate(final RandomAccessibleInterval<?> image, final SAMJLogger useThisLoggerForIt)
public void setImage(final RandomAccessibleInterval<?> image, final SAMJLogger useThisLoggerForIt)
throws IOException, InterruptedException, RuntimeException {
try {
return new EfficientViTSAML1(image,useThisLoggerForIt);
this.log = useThisLoggerForIt;
AbstractSamJ.DebugTextPrinter filteringLogger = text -> {
int idx = text.indexOf("contours_x");
if (idx > 0) this.log.info( text.substring(0,idx) );
else this.log.info( text );
};
this.efficientSamJ.setDebugPrinter(filteringLogger);
this.efficientSamJ.setImage(Cast.unchecked(image));;
} catch (IOException | InterruptedException | RuntimeException e) {
useThisLoggerForIt.error(FULL_NAME + " experienced an error: " + e.getMessage());
log.error(FULL_NAME + " experienced an error: " + e.getMessage());
throw e;
}
}
Expand All @@ -129,17 +132,9 @@ public void setInstalled(boolean installed) {
* @throws RuntimeException if there is any error running the Python code
* @throws InterruptedException if the process is interrupted
*/
public EfficientViTSAML1(final RandomAccessibleInterval<?> image, final SAMJLogger log)
throws IOException, RuntimeException, InterruptedException {
this.log = log;
AbstractSamJ.DebugTextPrinter filteringLogger = text -> {
int idx = text.indexOf("contours_x");
if (idx > 0) this.log.info( text.substring(0,idx) );
else this.log.info( text );
};
efficientSamJ = EfficientViTSamJ.initializeSam("l1",
SamEnvManager.create(), Cast.unchecked(image),
filteringLogger, false);
public EfficientViTSAML1() throws IOException, RuntimeException, InterruptedException {
this.manager = EfficientViTSamEnvManager.create(EfficientViTSamEnvManager.DEFAULT_DIR, "l1");
efficientSamJ = EfficientViTSamJ.initializeSam("l1", manager);
}

@Override
Expand Down Expand Up @@ -271,4 +266,9 @@ public void deleteEncoding(String encodingName) throws IOException, InterruptedE
throw e;
}
}

@Override
public SamEnvManagerAbstract getInstallationManger() {
return this.manager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@

import ai.nets.samj.models.AbstractSamJ;
import ai.nets.samj.models.EfficientViTSamJ;
import ai.nets.samj.install.SamEnvManager;
import ai.nets.samj.install.EfficientViTSamEnvManager;
import ai.nets.samj.install.SamEnvManagerAbstract;
import ai.nets.samj.ui.SAMJLogger;

/**
Expand All @@ -45,6 +46,7 @@
public class EfficientViTSAML2 implements SAMModel {

private EfficientViTSamJ efficientSamJ;
private final SamEnvManagerAbstract manager;
private SAMJLogger log;
private Boolean installed = false;
private boolean onlyBiggest = false;
Expand All @@ -66,13 +68,6 @@ public class EfficientViTSAML2 implements SAMModel {
+ "<strong>Paper:</strong> <a href=\"https://arxiv.org/pdf/2402.05008.pdf\">EfficientViT-SAM: Accelerated "
+ "Segment Anything Model Without Performance Loss</a>";

/**
* Create an empty instance of the model
*/
public EfficientViTSAML2() {

}

@Override
/**
* {@inheritDoc}
Expand Down Expand Up @@ -101,12 +96,19 @@ public boolean isInstalled() {
/**
* {@inheritDoc}
*/
public SAMModel instantiate(final RandomAccessibleInterval<?> image, final SAMJLogger useThisLoggerForIt)
public void setImage(final RandomAccessibleInterval<?> image, final SAMJLogger useThisLoggerForIt)
throws IOException, InterruptedException, RuntimeException {
try {
return new EfficientViTSAML2(image,useThisLoggerForIt);
this.log = useThisLoggerForIt;
AbstractSamJ.DebugTextPrinter filteringLogger = text -> {
int idx = text.indexOf("contours_x");
if (idx > 0) this.log.info( text.substring(0,idx) );
else this.log.info( text );
};
this.efficientSamJ.setDebugPrinter(filteringLogger);
this.efficientSamJ.setImage(Cast.unchecked(image));;
} catch (IOException | InterruptedException | RuntimeException e) {
useThisLoggerForIt.error(FULL_NAME + " experienced an error: " + e.getMessage());
log.error(FULL_NAME + " experienced an error: " + e.getMessage());
throw e;
}
}
Expand All @@ -129,17 +131,9 @@ public void setInstalled(boolean installed) {
* @throws RuntimeException if there is any error running the Python code
* @throws InterruptedException if the process is interrupted
*/
public EfficientViTSAML2(final RandomAccessibleInterval<?> image, final SAMJLogger log)
throws IOException, RuntimeException, InterruptedException {
this.log = log;
AbstractSamJ.DebugTextPrinter filteringLogger = text -> {
int idx = text.indexOf("contours_x");
if (idx > 0) this.log.info( text.substring(0,idx) );
else this.log.info( text );
};
efficientSamJ = EfficientViTSamJ.initializeSam("l2",
SamEnvManager.create(), Cast.unchecked(image),
filteringLogger, false);
public EfficientViTSAML2() throws IOException, RuntimeException, InterruptedException {
this.manager = EfficientViTSamEnvManager.create(EfficientViTSamEnvManager.DEFAULT_DIR, "l2");
efficientSamJ = EfficientViTSamJ.initializeSam("l2", manager);
}

@Override
Expand Down Expand Up @@ -271,4 +265,9 @@ public void deleteEncoding(String encodingName) throws IOException, InterruptedE
throw e;
}
}

@Override
public SamEnvManagerAbstract getInstallationManger() {
return this.manager;
}
}
Loading

0 comments on commit b8ef3e6

Please sign in to comment.