Skip to content

Commit

Permalink
come back to sam2 because sam2.1 requires code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Dec 3, 2024
1 parent c71a97f commit 37eae2c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
13 changes: 8 additions & 5 deletions src/main/java/ai/nets/samj/communication/model/SAM2Large.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ public class SAM2Large extends SAMModel {
/**
* Name of the model
*/
public static final String FULL_NAME = "SAM2.1 Large";
public static final String FULL_NAME = "SAM2 Large";
/**
* Axes order required for the input image by the model
*/
public static final String INPUT_IMAGE_AXES = "xyc";
public static final String INPUT_IMAGE_AXES = "xyc";

private static String ID = "large";


/**
Expand All @@ -63,8 +65,9 @@ public SAM2Large() {
this.paperName = "SAM 2: Segment Anything in Images and Videos";
this.speedRank = 4;
this.performanceRank = 1;
this.size = 898.1;
this.manager = Sam2EnvManager.create(Sam2EnvManager.DEFAULT_DIR, "large");
//this.size = Math.round(10 * Sam2EnvManager.SAM2_1_BYTE_SIZES_MAP.get(ID) / ((double) ( 1024 * 1024))) / 10.0;
this.size = Math.round(10 * Sam2EnvManager.SAM2_BYTE_SIZES_MAP.get(ID) / ((double) ( 1024 * 1024))) / 10.0;
this.manager = Sam2EnvManager.create(Sam2EnvManager.DEFAULT_DIR, ID);
}

@Override
Expand Down Expand Up @@ -97,7 +100,7 @@ public <T extends RealType<T> & NativeType<T>> void setImage(final RandomAccessi
this.log.info( text );
};
if (this.samj == null)
samj = Sam2.initializeSam("large", manager, filteringLogger, false);
samj = Sam2.initializeSam(ID, manager, filteringLogger, false);
try {
this.samj.setImage(Cast.unchecked(image));;
} catch (IOException | InterruptedException | RuntimeException e) {
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/ai/nets/samj/communication/model/SAM2Small.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class SAM2Small extends SAMModel {
* Axes order required for the input image by the model
*/
public static final String INPUT_IMAGE_AXES = "xyc";

private static String ID = "small";


/**
Expand All @@ -62,8 +64,9 @@ public SAM2Small() {
this.paperName = "SAM 2: Segment Anything in Images and Videos";
this.speedRank = 3;
this.performanceRank = 3;
this.size = 184.4;
this.manager = Sam2EnvManager.create(Sam2EnvManager.DEFAULT_DIR, "small");
//this.size = Math.round(10 * Sam2EnvManager.SAM2_1_BYTE_SIZES_MAP.get(ID) / ((double) ( 1024 * 1024))) / 10.0;
this.size = Math.round(10 * Sam2EnvManager.SAM2_BYTE_SIZES_MAP.get(ID) / ((double) ( 1024 * 1024))) / 10.0;
this.manager = Sam2EnvManager.create(Sam2EnvManager.DEFAULT_DIR, ID);
}

@Override
Expand All @@ -84,7 +87,7 @@ public <T extends RealType<T> & NativeType<T>> void setImage(final RandomAccessi
if (useThisLoggerForIt != null)
this.log = useThisLoggerForIt;
if (this.samj == null)
samj = Sam2.initializeSam("small", manager);
samj = Sam2.initializeSam(ID, manager);
try {
AbstractSamJ.DebugTextPrinter filteringLogger = text -> {
int idx = text.indexOf("\"responseType\": \"COMPLETION\"");
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/ai/nets/samj/communication/model/SAM2Tiny.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public class SAM2Tiny extends SAMModel {
/**
* Axes order required for the input image by the model
*/
public static final String INPUT_IMAGE_AXES = "xyc";
public static final String INPUT_IMAGE_AXES = "xyc";

private static String ID = "tiny";


/**
Expand All @@ -62,8 +64,9 @@ public SAM2Tiny() {
this.paperName = "SAM 2: Segment Anything in Images and Videos";
this.speedRank = 2;
this.performanceRank = 4;
this.size = 156.0;
this.manager = Sam2EnvManager.create(Sam2EnvManager.DEFAULT_DIR, "tiny");
//this.size = Math.round(10 * Sam2EnvManager.SAM2_1_BYTE_SIZES_MAP.get(ID) / ((double) ( 1024 * 1024))) / 10.0;
this.size = Math.round(10 * Sam2EnvManager.SAM2_BYTE_SIZES_MAP.get(ID) / ((double) ( 1024 * 1024))) / 10.0;
this.manager = Sam2EnvManager.create(Sam2EnvManager.DEFAULT_DIR, ID);
}

@Override
Expand Down Expand Up @@ -96,7 +99,7 @@ public <T extends RealType<T> & NativeType<T>> void setImage(final RandomAccessi
this.log.info( text );
};
if (this.samj == null)
samj = Sam2.initializeSam("tiny", manager, filteringLogger, false);
samj = Sam2.initializeSam(ID, manager, filteringLogger, false);
try {
this.samj.setImage(Cast.unchecked(image));;
} catch (IOException | InterruptedException | RuntimeException e) {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/ai/nets/samj/install/Sam2EnvManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,19 @@ else if (!PlatformDetection.getArch().equals(PlatformDetection.ARCH_ARM64) && !P
/**
* URL to download the SAM2 model
*/
final static private String SAM2_URL = "https://dl.fbaipublicfiles.com/segment_anything_2/092824/sam2.1_hiera_%s.pt";
final static private String SAM2_URL = "https://dl.fbaipublicfiles.com/segment_anything_2/072824/sam2_hiera_%s.pt";
/**
* URL to download the SAM2 model
*/
final static private String SAM2_FNAME = "sam2.1_hiera_%s.pt";
final static private String SAM2_FNAME = "sam2_hiera_%s.pt";
/**
* URL to download the SAM2 model
*/
final static private String SAM2_1_URL = "https://dl.fbaipublicfiles.com/segment_anything_2/092824/sam2.1_hiera_%s.pt";
/**
* URL to download the SAM2 model
*/
final static private String SAM2_1_FNAME = "sam2.1_hiera_%s.pt";

private Sam2EnvManager(String modelType) {
List<String> modelTypes = SAM2_BYTE_SIZES_MAP.keySet().stream().collect(Collectors.toList());
Expand Down

0 comments on commit 37eae2c

Please sign in to comment.