From 37eae2cd237fd493fd26cfc5863804f8fb5a50f7 Mon Sep 17 00:00:00 2001 From: carlosuc3m <100329787@alumnos.uc3m.es> Date: Tue, 3 Dec 2024 19:18:59 +0100 Subject: [PATCH] come back to sam2 because sam2.1 requires code changes --- .../ai/nets/samj/communication/model/SAM2Large.java | 13 ++++++++----- .../ai/nets/samj/communication/model/SAM2Small.java | 9 ++++++--- .../ai/nets/samj/communication/model/SAM2Tiny.java | 11 +++++++---- .../java/ai/nets/samj/install/Sam2EnvManager.java | 12 ++++++++++-- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/main/java/ai/nets/samj/communication/model/SAM2Large.java b/src/main/java/ai/nets/samj/communication/model/SAM2Large.java index 02e2cf0..ac28c68 100644 --- a/src/main/java/ai/nets/samj/communication/model/SAM2Large.java +++ b/src/main/java/ai/nets/samj/communication/model/SAM2Large.java @@ -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"; /** @@ -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 @@ -97,7 +100,7 @@ public & NativeType> 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) { diff --git a/src/main/java/ai/nets/samj/communication/model/SAM2Small.java b/src/main/java/ai/nets/samj/communication/model/SAM2Small.java index a09b347..8c10cb1 100644 --- a/src/main/java/ai/nets/samj/communication/model/SAM2Small.java +++ b/src/main/java/ai/nets/samj/communication/model/SAM2Small.java @@ -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"; /** @@ -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 @@ -84,7 +87,7 @@ public & NativeType> 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\""); diff --git a/src/main/java/ai/nets/samj/communication/model/SAM2Tiny.java b/src/main/java/ai/nets/samj/communication/model/SAM2Tiny.java index e084ddd..0697d6e 100644 --- a/src/main/java/ai/nets/samj/communication/model/SAM2Tiny.java +++ b/src/main/java/ai/nets/samj/communication/model/SAM2Tiny.java @@ -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"; /** @@ -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 @@ -96,7 +99,7 @@ public & NativeType> 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) { diff --git a/src/main/java/ai/nets/samj/install/Sam2EnvManager.java b/src/main/java/ai/nets/samj/install/Sam2EnvManager.java index 1679a2a..ac9991e 100644 --- a/src/main/java/ai/nets/samj/install/Sam2EnvManager.java +++ b/src/main/java/ai/nets/samj/install/Sam2EnvManager.java @@ -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 modelTypes = SAM2_BYTE_SIZES_MAP.keySet().stream().collect(Collectors.toList());