Skip to content

Commit

Permalink
CREATE SAMMODEL ABSTRACT AND DELETE CODE
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Nov 30, 2024
1 parent 5dca5cb commit c919644
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 1,358 deletions.
175 changes: 4 additions & 171 deletions src/main/java/ai/nets/samj/communication/model/EfficientViTSAML0.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,17 @@
*/
package ai.nets.samj.communication.model;

import net.imglib2.Interval;
import net.imglib2.Localizable;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.util.Cast;

import java.awt.Rectangle;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

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

/**
Expand All @@ -46,28 +39,6 @@
*/
public class EfficientViTSAML0 extends SAMModel {

private EfficientViTSamJ efficientSamJ;
private final SamEnvManagerAbstract manager;
private SAMJLogger log = new SAMJLogger() {

@Override
public void info(String text) {
System.out.println(text);
}

@Override
public void warn(String text) {
System.err.println("[WARNING] -- " + text);
}

@Override
public void error(String text) {
System.err.println(text);
}

};
private Boolean installed = false;
private boolean onlyBiggest = false;
/**
* Official name of the model
*/
Expand Down Expand Up @@ -103,14 +74,6 @@ public String getName() {
return FULL_NAME;
}

@Override
/**
* {@inheritDoc}
*/
public boolean isInstalled() {
return installed;
}

@Override
/**
* {@inheritDoc}
Expand All @@ -120,157 +83,27 @@ public <T extends RealType<T> & NativeType<T>> void setImage(final RandomAccessi
Objects.requireNonNull(image, "The image cannot be null.");
if (useThisLoggerForIt != null)
this.log = useThisLoggerForIt;
if (this.efficientSamJ == null)
this.efficientSamJ = EfficientViTSamJ.initializeSam("l0", manager);
if (this.samj == null)
this.samj = EfficientViTSamJ.initializeSam("l0", manager);
try {
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));;
this.samj.setDebugPrinter(filteringLogger);
this.samj.setImage(Cast.unchecked(image));;
} catch (IOException | InterruptedException | RuntimeException e) {
log.error(FULL_NAME + " experienced an error: " + e.getMessage());
throw e;
}
}

@Override
/**
* {@inheritDoc}
*/
public List<Mask> fetch2dSegmentation(List<Localizable> listOfPoints2D, List<Localizable> listOfNegPoints2D)
throws IOException, InterruptedException, RuntimeException {
try {
List<int[]> list = listOfPoints2D.stream()
.map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList());
List<int[]> negList = listOfNegPoints2D.stream()
.map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList());
if (negList.size() == 0) return efficientSamJ.processPoints(list, !onlyBiggest);
else return efficientSamJ.processPoints(list, negList, !onlyBiggest);
} catch (IOException | RuntimeException | InterruptedException e) {
log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage());
throw e;
}
}

@Override
public List<Mask> fetch2dSegmentation(List<Localizable> listOfPoints2D, List<Localizable> listOfNegPoints2D,
Rectangle zoomedRectangle) throws IOException, RuntimeException, InterruptedException {
try {
List<int[]> list = listOfPoints2D.stream()
.map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList());
List<int[]> negList = listOfNegPoints2D.stream()
.map(i -> new int[] {(int) i.positionAsDoubleArray()[0], (int) i.positionAsDoubleArray()[1]}).collect(Collectors.toList());
if (negList.size() == 0) return efficientSamJ.processPoints(list, zoomedRectangle, !onlyBiggest);
else return efficientSamJ.processPoints(list, negList, zoomedRectangle, !onlyBiggest);
} catch (IOException | RuntimeException | InterruptedException e) {
log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage());
throw e;
}
}

@Override
/**
* {@inheritDoc}
*/
public List<Mask> fetch2dSegmentation(Interval boundingBox2D)
throws IOException, InterruptedException, RuntimeException {
try {
//order to processBox() should be: x0,y0, x1,y1
final int bbox[] = {
(int)boundingBox2D.min(0),
(int)boundingBox2D.min(1),
(int)boundingBox2D.max(0),
(int)boundingBox2D.max(1)
};
return efficientSamJ.processBox(bbox, !onlyBiggest);
} catch (IOException | InterruptedException | RuntimeException e) {
log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage());
throw e;
}
}

@Override
/**
* {@inheritDoc}
*/
public <T extends RealType<T> & NativeType<T>> List<Mask> fetch2dSegmentationFromMask(RandomAccessibleInterval<T> rai)
throws IOException, InterruptedException, RuntimeException {
try {
return efficientSamJ.processMask(rai, !onlyBiggest);
} catch (IOException | InterruptedException | RuntimeException e) {
log.error(FULL_NAME+", providing empty result because of some trouble: "+e.getMessage());
throw e;
}
}

@Override
/**
* {@inheritDoc}
*/
public void notifyUiHasBeenClosed() {
if (log != null)
log.info(FULL_NAME+": OKAY, I'm closing myself...");
closeProcess();
}

@Override
/**
* {@inheritDoc}
*/
public void closeProcess() {
if (efficientSamJ != null)
efficientSamJ.close();
efficientSamJ = null;
}

@Override
/**
* {@inheritDoc}
*/
public String getInputImageAxes() {
return INPUT_IMAGE_AXES;
}

@Override
public void setReturnOnlyBiggest(boolean onlyBiggest) {
this.onlyBiggest = onlyBiggest;
}

@Override
public String persistEncoding() throws IOException, InterruptedException {
try {
return efficientSamJ.persistEncoding();
} catch (IOException | InterruptedException | RuntimeException e) {
log.error(FULL_NAME+", unable to persist the encoding: "+e.getMessage());
throw e;
}
}

@Override
public void selectEncoding(String encodingName) throws IOException, InterruptedException {
try {
efficientSamJ.selectEncoding(encodingName);
} catch (IOException | InterruptedException | RuntimeException e) {
log.error(FULL_NAME+", unable to persist the encoding named '" + encodingName + "': "+e.getMessage());
throw e;
}
}

@Override
public void deleteEncoding(String encodingName) throws IOException, InterruptedException {
try {
efficientSamJ.deleteEncoding(encodingName);
} catch (IOException | InterruptedException | RuntimeException e) {
log.error(FULL_NAME+", unable to delete the encoding named '" + encodingName + "': "+e.getMessage());
throw e;
}
}

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

0 comments on commit c919644

Please sign in to comment.