Skip to content

Commit 3e35d6d

Browse files
authored
Android demo resize image for llava and gemma (#95)
Use different size for llava and gemma
1 parent e7c9c84 commit 3e35d6d

File tree

3 files changed

+19
-37
lines changed

3 files changed

+19
-37
lines changed

llm/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/ETImage.java

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ public class ETImage {
2424
private final Uri uri;
2525
private final ContentResolver contentResolver;
2626

27-
ETImage(ContentResolver contentResolver, Uri uri) {
27+
ETImage(ContentResolver contentResolver, Uri uri, int sideSize) {
2828
this.contentResolver = contentResolver;
2929
this.uri = uri;
30-
bytes = getBytesFromImageURI(uri);
30+
bytes = getBytesFromImageURI(uri, sideSize);
3131
}
3232

3333
public int getWidth() {
@@ -64,10 +64,9 @@ public float[] getFloats() {
6464
return floatArray;
6565
}
6666

67-
private byte[] getBytesFromImageURI(Uri uri) {
67+
private byte[] getBytesFromImageURI(Uri uri, int sideSize) {
6868
try {
69-
int RESIZED_IMAGE_WIDTH = 336;
70-
Bitmap bitmap = resizeImage(uri, RESIZED_IMAGE_WIDTH);
69+
Bitmap bitmap = resizeImage(uri, sideSize);
7170

7271
if (bitmap == null) {
7372
ETLogging.getInstance().log("Unable to get bytes from Image URI. Bitmap is null");
@@ -102,7 +101,7 @@ private byte[] getBytesFromImageURI(Uri uri) {
102101
}
103102

104103
@Nullable
105-
private Bitmap resizeImage(Uri uri, int maxLength) throws FileNotFoundException {
104+
private Bitmap resizeImage(Uri uri, int sideSize) throws FileNotFoundException {
106105
InputStream inputStream = contentResolver.openInputStream(uri);
107106
if (inputStream == null) {
108107
ETLogging.getInstance().log("Unable to resize image, input streams is null");
@@ -114,21 +113,6 @@ private Bitmap resizeImage(Uri uri, int maxLength) throws FileNotFoundException
114113
return null;
115114
}
116115

117-
float aspectRatio;
118-
int finalWidth, finalHeight;
119-
120-
if (bitmap.getWidth() > bitmap.getHeight()) {
121-
// width > height --> width = maxLength, height scale with aspect ratio
122-
aspectRatio = bitmap.getWidth() / (float) bitmap.getHeight();
123-
finalWidth = maxLength;
124-
finalHeight = Math.round(maxLength / aspectRatio);
125-
} else {
126-
// height >= width --> height = maxLength, width scale with aspect ratio
127-
aspectRatio = bitmap.getHeight() / (float) bitmap.getWidth();
128-
finalHeight = maxLength;
129-
finalWidth = Math.round(maxLength / aspectRatio);
130-
}
131-
132-
return Bitmap.createScaledBitmap(bitmap, finalWidth, finalHeight, false);
116+
return Bitmap.createScaledBitmap(bitmap, sideSize, sideSize, false);
133117
}
134118
}

llm/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/MainActivity.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,12 +645,23 @@ private void setupGalleryPicker() {
645645
});
646646
}
647647

648+
private int getInputImageSideSize() {
649+
switch (mCurrentSettingsFields.getModelType()) {
650+
case LLAVA_1_5:
651+
return 336;
652+
case GEMMA_3:
653+
return 896;
654+
default:
655+
throw new IllegalArgumentException("Unsupported model type: " + mCurrentSettingsFields.getModelType());
656+
}
657+
}
658+
648659
private List<ETImage> getProcessedImagesForModel(List<Uri> uris) {
649660
List<ETImage> imageList = new ArrayList<>();
650661
if (uris != null) {
651662
uris.forEach(
652663
(uri) -> {
653-
imageList.add(new ETImage(this.getContentResolver(), uri));
664+
imageList.add(new ETImage(this.getContentResolver(), uri, getInputImageSideSize()));
654665
});
655666
}
656667
return imageList;

llm/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/ModelType.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,5 @@ public enum ModelType {
1414
LLAVA_1_5,
1515
LLAMA_GUARD_3,
1616
QWEN_3,
17-
VOXTRAL;
18-
19-
@Override
20-
public String toString() {
21-
// Replace underscores with spaces, capitalize words, and handle special cases
22-
String pretty = name().replace('_', ' ').toLowerCase();
23-
// Capitalize the first letter if needed
24-
if (!pretty.isEmpty()) {
25-
pretty = pretty.substring(0, 1).toUpperCase() + pretty.substring(1);
26-
}
27-
// Optionally, handle special capitalization
28-
pretty = pretty.replace("Llava 1 5", "LLaVA 1.5");
29-
return pretty;
30-
}
17+
VOXTRAL,
3118
}

0 commit comments

Comments
 (0)