Skip to content

Commit 0bd82be

Browse files
lu-wang-gtflite-support-robot
authored andcommitted
Throw RuntimeExceptions instead of AssertionError in ImageSegmenter
PiperOrigin-RevId: 400353917
1 parent e25cb4f commit 0bd82be

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

tensorflow_lite_support/java/src/java/org/tensorflow/lite/task/vision/segmenter/ImageSegmenter.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ public final class ImageSegmenter extends BaseVisionTaskApi {
8787
*
8888
* @param modelPath path of the segmentation model with metadata in the assets
8989
* @throws IOException if an I/O error occurs when loading the tflite model
90-
* @throws AssertionError if error occurs when creating {@link ImageSegmenter} from the native
91-
* code
90+
* @throws IllegalArgumentException if an argument is invalid
91+
* @throws IllegalStateException if there is an internal error
92+
* @throws RuntimeException if there is an otherwise unspecified error
9293
*/
9394
public static ImageSegmenter createFromFile(Context context, String modelPath)
9495
throws IOException {
@@ -100,8 +101,9 @@ public static ImageSegmenter createFromFile(Context context, String modelPath)
100101
*
101102
* @param modelFile the segmentation model {@link File} instance
102103
* @throws IOException if an I/O error occurs when loading the tflite model
103-
* @throws AssertionError if error occurs when creating {@link ImageSegmenter} from the native
104-
* code
104+
* @throws IllegalArgumentException if an argument is invalid
105+
* @throws IllegalStateException if there is an internal error
106+
* @throws RuntimeException if there is an otherwise unspecified error
105107
*/
106108
public static ImageSegmenter createFromFile(File modelFile) throws IOException {
107109
return createFromFileAndOptions(modelFile, ImageSegmenterOptions.builder().build());
@@ -113,8 +115,8 @@ public static ImageSegmenter createFromFile(File modelFile) throws IOException {
113115
*
114116
* @param modelBuffer a direct {@link ByteBuffer} or a {@link MappedByteBuffer} of the
115117
* segmentation model
116-
* @throws AssertionError if error occurs when creating {@link ImageSegmenter} from the native
117-
* code
118+
* @throws IllegalStateException if there is an internal error
119+
* @throws RuntimeException if there is an otherwise unspecified error
118120
* @throws IllegalArgumentException if the model buffer is not a direct {@link ByteBuffer} or a
119121
* {@link MappedByteBuffer}
120122
*/
@@ -127,8 +129,9 @@ public static ImageSegmenter createFromBuffer(final ByteBuffer modelBuffer) {
127129
*
128130
* @param modelPath path of the segmentation model with metadata in the assets
129131
* @throws IOException if an I/O error occurs when loading the tflite model
130-
* @throws AssertionError if error occurs when creating {@link ImageSegmenter} from the native
131-
* code
132+
* @throws IllegalArgumentException if an argument is invalid
133+
* @throws IllegalStateException if there is an internal error
134+
* @throws RuntimeException if there is an otherwise unspecified error
132135
*/
133136
public static ImageSegmenter createFromFileAndOptions(
134137
Context context, String modelPath, final ImageSegmenterOptions options) throws IOException {
@@ -146,8 +149,9 @@ public static ImageSegmenter createFromFileAndOptions(
146149
*
147150
* @param modelFile the segmentation model {@link File} instance
148151
* @throws IOException if an I/O error occurs when loading the tflite model
149-
* @throws AssertionError if error occurs when creating {@link ImageSegmenter} from the native
150-
* code
152+
* @throws IllegalArgumentException if an argument is invalid
153+
* @throws IllegalStateException if there is an internal error
154+
* @throws RuntimeException if there is an otherwise unspecified error
151155
*/
152156
public static ImageSegmenter createFromFileAndOptions(
153157
File modelFile, final ImageSegmenterOptions options) throws IOException {
@@ -167,8 +171,8 @@ public static ImageSegmenter createFromFileAndOptions(
167171
*
168172
* @param modelBuffer a direct {@link ByteBuffer} or a {@link MappedByteBuffer} of the
169173
* segmentation model
170-
* @throws AssertionError if error occurs when creating {@link ImageSegmenter} from the native
171-
* code
174+
* @throws IllegalStateException if there is an internal error
175+
* @throws RuntimeException if there is an otherwise unspecified error
172176
* @throws IllegalArgumentException if the model buffer is not a direct {@link ByteBuffer} or a
173177
* {@link MappedByteBuffer}
174178
*/
@@ -282,7 +286,8 @@ public abstract static class Builder {
282286
* Segmentation} element is expected to be returned. The result is stored in a {@link List}
283287
* for later extension to e.g. instance segmentation models, which may return one segmentation
284288
* per object.
285-
* @throws AssertionError if error occurs when segmenting the image from the native code
289+
* @throws IllegalStateException if there is an internal error
290+
* @throws RuntimeException if there is an otherwise unspecified error
286291
* @throws IllegalArgumentException if the color space type of image is unsupported
287292
*/
288293
public List<Segmentation> segment(TensorImage image) {
@@ -315,7 +320,8 @@ public List<Segmentation> segment(TensorImage image) {
315320
* Segmentation} element is expected to be returned. The result is stored in a {@link List}
316321
* for later extension to e.g. instance segmentation models, which may return one segmentation
317322
* per object.
318-
* @throws AssertionError if error occurs when segmenting the image from the native code
323+
* @throws IllegalStateException if there is an internal error
324+
* @throws RuntimeException if there is an otherwise unspecified error
319325
* @throws IllegalArgumentException if the color space type of image is unsupported
320326
*/
321327
public List<Segmentation> segment(TensorImage image, ImageProcessingOptions options) {
@@ -339,7 +345,8 @@ public List<Segmentation> run(
339345
* Segmentation} element is expected to be returned. The result is stored in a {@link List}
340346
* for later extension to e.g. instance segmentation models, which may return one segmentation
341347
* per object.
342-
* @throws AssertionError if error occurs when segmenting the image from the native code
348+
* @throws IllegalStateException if there is an internal error
349+
* @throws RuntimeException if there is an otherwise unspecified error
343350
* @throws IllegalArgumentException if the storage type or format of the image is unsupported
344351
*/
345352
public List<Segmentation> segment(MlImage image) {
@@ -364,7 +371,8 @@ public List<Segmentation> segment(MlImage image) {
364371
* Segmentation} element is expected to be returned. The result is stored in a {@link List}
365372
* for later extension to e.g. instance segmentation models, which may return one segmentation
366373
* per object.
367-
* @throws AssertionError if error occurs when segmenting the image from the native code
374+
* @throws IllegalStateException if there is an internal error
375+
* @throws RuntimeException if there is an otherwise unspecified error
368376
* @throws IllegalArgumentException if the color space type of image is unsupported
369377
*/
370378
public List<Segmentation> segment(MlImage image, ImageProcessingOptions options) {

tensorflow_lite_support/java/src/native/task/vision/segmenter/image_segmenter_jni.cc

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ namespace {
3333

3434
using ::tflite::support::StatusOr;
3535
using ::tflite::support::utils::CreateByteArray;
36-
using ::tflite::support::utils::kAssertionError;
36+
using ::tflite::support::utils::GetExceptionClassNameForStatusCode;
3737
using ::tflite::support::utils::kIllegalArgumentException;
38+
using ::tflite::support::utils::kIllegalStateException;
3839
using ::tflite::support::utils::kInvalidPointer;
3940
using ::tflite::support::utils::ThrowException;
4041
using ::tflite::task::core::BaseOptions;
@@ -97,7 +98,7 @@ void ConvertFromSegmentationResults(JNIEnv* env,
9798
if (results.segmentation_size() != 1) {
9899
// Should never happen.
99100
ThrowException(
100-
env, kAssertionError,
101+
env, kIllegalStateException,
101102
"ImageSegmenter only supports one segmentation result, getting %d",
102103
results.segmentation_size());
103104
}
@@ -171,13 +172,17 @@ jlong CreateImageSegmenterFromOptions(JNIEnv* env,
171172
if (image_segmenter_or.ok()) {
172173
return reinterpret_cast<jlong>(image_segmenter_or->release());
173174
} else {
174-
ThrowException(env, kAssertionError,
175-
"Error occurred when initializing ImageSegmenter: %s",
176-
image_segmenter_or.status().message().data());
175+
ThrowException(
176+
env,
177+
GetExceptionClassNameForStatusCode(image_segmenter_or.status().code()),
178+
"Error occurred when initializing ImageSegmenter: %s",
179+
image_segmenter_or.status().message().data());
177180
return kInvalidPointer;
178181
}
179182
}
180183

184+
} // namespace
185+
181186
extern "C" JNIEXPORT void JNICALL
182187
Java_org_tensorflow_lite_task_vision_segmenter_ImageSegmenter_deinitJni(
183188
JNIEnv* env, jobject thiz, jlong native_handle) {
@@ -233,10 +238,9 @@ Java_org_tensorflow_lite_task_vision_segmenter_ImageSegmenter_segmentNative(
233238
ConvertFromSegmentationResults(env, results_or.value(), jmask_buffers,
234239
jmask_shape, jcolored_labels);
235240
} else {
236-
ThrowException(env, kAssertionError,
237-
"Error occurred when segmenting the image: %s",
238-
results_or.status().message().data());
241+
ThrowException(
242+
env, GetExceptionClassNameForStatusCode(results_or.status().code()),
243+
"Error occurred when segmenting the image: %s",
244+
results_or.status().message().data());
239245
}
240246
}
241-
242-
} // namespace

0 commit comments

Comments
 (0)