public void runForMultipleInputsOutputs(
@NonNull Object[] inputs, @NonNull Map<Integer, Object> outputs) {
if (wrapper == null) {
- throw new IllegalStateException("The Interpreter has already been closed.");
+ throw new IllegalStateException("Internal error: The Interpreter has already been closed.");
}
Tensor[] tensors = wrapper.run(inputs);
if (outputs == null || tensors == null || outputs.size() > tensors.length) {
- throw new IllegalArgumentException("Outputs do not match with model outputs.");
+ throw new IllegalArgumentException("Output error: Outputs do not match with model outputs.");
}
final int size = tensors.length;
for (Integer idx : outputs.keySet()) {
if (idx == null || idx < 0 || idx >= size) {
throw new IllegalArgumentException(
- String.format("Invalid index of output %d (should be in range [0, %d))", idx, size));
+ String.format(
+ "Output error: Invalid index of output %d (should be in range [0, %d))",
+ idx, size));
}
tensors[idx].copyTo(outputs.get(idx));
}
*/
public void resizeInput(int idx, @NonNull int[] dims) {
if (wrapper == null) {
- throw new IllegalStateException("The Interpreter has already been closed.");
+ throw new IllegalStateException("Internal error: The Interpreter has already been closed.");
}
wrapper.resizeInput(idx, dims);
}
*/
public int getInputIndex(String opName) {
if (wrapper == null) {
- throw new IllegalStateException("The Interpreter has already been closed.");
+ throw new IllegalStateException("Internal error: The Interpreter has already been closed.");
}
return wrapper.getInputIndex(opName);
}
*/
public int getOutputIndex(String opName) {
if (wrapper == null) {
- throw new IllegalStateException("The Interpreter has already been closed.");
+ throw new IllegalStateException("Internal error: The Interpreter has already been closed.");
}
return wrapper.getOutputIndex(opName);
}
*/
public Long getLastNativeInferenceDurationNanoseconds() {
if (wrapper == null) {
- throw new IllegalStateException("The interpreter has already been closed.");
+ throw new IllegalStateException("Internal error: The interpreter has already been closed.");
}
return wrapper.getLastNativeInferenceDurationNanoseconds();
}
if (wrapper != null) {
wrapper.setUseNNAPI(useNNAPI);
} else {
- throw new IllegalStateException("NativeInterpreterWrapper has already been closed.");
+ throw new IllegalStateException(
+ "Internal error: NativeInterpreterWrapper has already been closed.");
}
}
/** Sets inputs, runs model inference and returns outputs. */
Tensor[] run(Object[] inputs) {
if (inputs == null || inputs.length == 0) {
- throw new IllegalArgumentException("Invalid inputs. Inputs should not be null or empty.");
+ throw new IllegalArgumentException("Input error: Inputs should not be null or empty.");
}
int[] dataTypes = new int[inputs.length];
Object[] sizes = new Object[inputs.length];
ByteBuffer buffer = (ByteBuffer) inputs[i];
if (buffer.order() != ByteOrder.nativeOrder()) {
throw new IllegalArgumentException(
- "Invalid ByteBuffer. It shoud use ByteOrder.nativeOrder().");
+ "Input error: ByteBuffer shoud use ByteOrder.nativeOrder().");
}
numsOfBytes[i] = buffer.limit();
sizes[i] = getInputDims(interpreterHandle, i, numsOfBytes[i]);
} else {
throw new IllegalArgumentException(
String.format(
- "%d-th element of the %d inputs is not an array or a ByteBuffer.",
+ "Input error: %d-th element of the %d inputs is not an array or a ByteBuffer.",
i, inputs.length));
}
}
this,
isMemoryAllocated);
if (outputsHandles == null || outputsHandles.length == 0) {
- throw new IllegalStateException("Interpreter has no outputs.");
+ throw new IllegalStateException("Internal error: Interpreter has no outputs.");
}
isMemoryAllocated = true;
Tensor[] outputs = new Tensor[outputsHandles.length];
} else {
throw new IllegalArgumentException(
String.format(
- "%s is not a valid name for any input. The indexes of the inputs are %s",
+ "Input error: %s is not a valid name for any input. "
+ + "The indexes of the inputs are %s",
name, inputsIndexes.toString()));
}
}
} else {
throw new IllegalArgumentException(
String.format(
- "%s is not a valid name for any output. The indexes of the outputs are %s",
+ "Input error: %s is not a valid name for any output. "
+ + "The indexes of the outputs are %s",
name, outputsIndexes.toString()));
}
}
return DataType.BYTEBUFFER;
}
}
- throw new IllegalArgumentException("cannot resolve DataType of " + o.getClass().getName());
+ throw new IllegalArgumentException(
+ "DataType error: cannot resolve DataType of " + o.getClass().getName());
}
/** Returns the shape of an object as an int array. */
return 0;
}
if (Array.getLength(o) == 0) {
- throw new IllegalArgumentException("array lengths cannot be 0.");
+ throw new IllegalArgumentException("Array lengths cannot be 0.");
}
return 1 + numDimensions(Array.get(o, 0));
}
shape[dim] = len;
} else if (shape[dim] != len) {
throw new IllegalArgumentException(
- String.format("mismatched lengths (%d and %d) in dimension %d", shape[dim], len, dim));
+ String.format("Mismatched lengths (%d and %d) in dimension %d", shape[dim], len, dim));
}
for (int i = 0; i < len; ++i) {
fillShape(Array.get(o, i), dim + 1, shape);
tflite::Interpreter* convertLongToInterpreter(JNIEnv* env, jlong handle) {
if (handle == 0) {
throwException(env, kIllegalArgumentException,
- "Invalid handle to Interpreter.");
+ "Internal error: Invalid handle to Interpreter.");
return nullptr;
}
return reinterpret_cast<tflite::Interpreter*>(handle);
tflite::FlatBufferModel* convertLongToModel(JNIEnv* env, jlong handle) {
if (handle == 0) {
- throwException(env, kIllegalArgumentException, "Invalid handle to model.");
+ throwException(env, kIllegalArgumentException,
+ "Internal error: Invalid handle to model.");
return nullptr;
}
return reinterpret_cast<tflite::FlatBufferModel*>(handle);
BufferErrorReporter* convertLongToErrorReporter(JNIEnv* env, jlong handle) {
if (handle == 0) {
throwException(env, kIllegalArgumentException,
- "Invalid handle to ErrorReporter.");
+ "Internal error: Invalid handle to ErrorReporter.");
return nullptr;
}
return reinterpret_cast<BufferErrorReporter*>(handle);
jint* ptr = env->GetIntArrayElements(inputs, nullptr);
if (ptr == nullptr) {
throwException(env, kIllegalArgumentException,
- "Empty dimensions of input array.");
+ "Array has empty dimensions.");
return {};
}
for (int i = 0; i < size; ++i) {
jobjectArray sizes) {
if (input_size != interpreter->inputs().size()) {
throwException(env, kIllegalArgumentException,
- "Expected num of inputs is %d but got %d",
+ "Input error: Expected num of inputs is %d but got %d",
interpreter->inputs().size(), input_size);
return kTfLiteError;
}
input_size != env->GetArrayLength(nums_of_bytes) ||
input_size != env->GetArrayLength(values)) {
throwException(env, kIllegalArgumentException,
- "Arrays in arguments should be of the same length, but got "
- "%d sizes, %d data_types, %d nums_of_bytes, and %d values",
+ "Internal error: Arrays in arguments should be of the same "
+ "length, but got %d sizes, %d data_types, %d nums_of_bytes, "
+ "and %d values",
input_size, env->GetArrayLength(data_types),
env->GetArrayLength(nums_of_bytes),
env->GetArrayLength(values));
int num_dims = static_cast<int>(env->GetArrayLength(dims));
if (target->dims->size != num_dims) {
throwException(env, kIllegalArgumentException,
- "%d-th input should have %d dimensions, but found %d "
- "dimensions",
+ "Input error: %d-th input should have %d dimensions, but "
+ "found %d dimensions",
i, target->dims->size, num_dims);
return kTfLiteError;
}
num_dims);
printDims(obtained_dims.get(), kBufferSize, ptr, num_dims);
throwException(env, kIllegalArgumentException,
- "%d-th input dimension should be [%s], but found [%s]",
+ "Input error: %d-th input dimension should be [%s], but "
+ "found [%s]",
i, expected_dims.get(), obtained_dims.get());
env->ReleaseIntArrayElements(dims, ptr, JNI_ABORT);
return kTfLiteError;
TfLiteType type = resolveDataType(data_type[i]);
if (type != target->type) {
throwException(env, kIllegalArgumentException,
- "DataType (%d) of input data does not match with the "
- "DataType (%d) of model inputs.",
+ "Input error: DataType (%d) of input data does not "
+ "match with the DataType (%d) of model inputs.",
type, target->type);
return kTfLiteError;
}
jclass string_class = env->FindClass("java/lang/String");
if (string_class == nullptr) {
throwException(env, kUnsupportedOperationException,
- "Can not find java/lang/String class to get input names.");
+ "Internal error: Can not find java/lang/String class to get "
+ "input names.");
return nullptr;
}
size_t size = interpreter->inputs().size();
jclass string_class = env->FindClass("java/lang/String");
if (string_class == nullptr) {
throwException(env, kUnsupportedOperationException,
- "Can not find java/lang/String class to get output names.");
+ "Internal error: Can not find java/lang/String class to get "
+ "output names.");
return nullptr;
}
size_t size = interpreter->outputs().size();
path, verifier.get(), error_reporter);
if (!model) {
throwException(env, kIllegalArgumentException,
- "Contents of %s does not encode a valid TensorFlowLite "
- "model: %s",
+ "Contents of %s does not encode a valid "
+ "TensorFlowLite model: %s",
path, error_reporter->CachedErrorMessage());
env->ReleaseStringUTFChars(model_file, path);
return 0;
buf, static_cast<size_t>(capacity), error_reporter);
if (!model) {
throwException(env, kIllegalArgumentException,
- "MappedByteBuffer does not encode a valid TensorFlowLite "
- "model: %s",
+ "MappedByteBuffer does not encode a valid "
+ "TensorFlowLite model: %s",
error_reporter->CachedErrorMessage());
return 0;
}
&interpreter, static_cast<int>(num_threads));
if (status != kTfLiteOk) {
throwException(env, kIllegalArgumentException,
- "Cannot create interpreter: %s",
+ "Internal error: Cannot create interpreter: %s",
error_reporter->CachedErrorMessage());
return 0;
}
status = interpreter->AllocateTensors();
if (status != kTfLiteOk) {
throwException(env, kNullPointerException,
- "Can not allocate memory for the interpreter",
+ "Internal error: Cannot allocate memory for the interpreter",
error_reporter->CachedErrorMessage());
return 0;
}
// resizes inputs
status = resizeInputs(env, interpreter, input_size, sizes);
if (status != kTfLiteOk) {
- throwException(env, kNullPointerException, "Can not resize the input: %s",
+ throwException(env, kNullPointerException,
+ "Internal error: Can not resize the input: %s",
error_reporter->CachedErrorMessage());
return nullptr;
}
status = interpreter->AllocateTensors();
if (status != kTfLiteOk) {
throwException(env, kNullPointerException,
- "Can not allocate memory for the given inputs: %s",
+ "Internal error: Can not allocate memory for the given "
+ "inputs: %s",
error_reporter->CachedErrorMessage());
return nullptr;
}
// runs inference
if (interpreter->Invoke() != kTfLiteOk) {
throwException(env, kIllegalArgumentException,
- "Failed to run on the given Interpreter: %s",
+ "Internal error: Failed to run on the given Interpreter: %s",
error_reporter->CachedErrorMessage());
return nullptr;
}
// returns outputs
const std::vector<int>& results = interpreter->outputs();
if (results.empty()) {
- throwException(env, kIllegalArgumentException,
- "The Interpreter does not have any outputs.");
+ throwException(
+ env, kIllegalArgumentException,
+ "Internal error: The Interpreter does not have any outputs.");
return nullptr;
}
jlongArray outputs = env->NewLongArray(results.size());
const int idx = static_cast<int>(input_idx);
if (input_idx < 0 || input_idx >= interpreter->inputs().size()) {
throwException(env, kIllegalArgumentException,
- "Out of range: Failed to get %d-th input out of %d inputs",
+ "Input error: Out of range: Failed to get %d-th input out of"
+ " %d inputs",
input_idx, interpreter->inputs().size());
return nullptr;
}
}
if (num_bytes != expected_num_bytes) {
throwException(env, kIllegalArgumentException,
- "Failed to get input dimensions. %d-th input should have"
- " %d bytes, but found %d bytes.",
+ "Input error: Failed to get input dimensions. %d-th input "
+ "should have %d bytes, but found %d bytes.",
idx, expected_num_bytes, num_bytes);
return nullptr;
}
const int idx = static_cast<int>(output_idx);
if (output_idx < 0 || output_idx >= interpreter->outputs().size()) {
throwException(env, kIllegalArgumentException,
- "Out of range: Failed to get %d-th output out of %d outputs",
- output_idx, interpreter->outputs().size());
+ "Failed to get %d-th output out of %d outputs", output_idx,
+ interpreter->outputs().size());
return -1;
}
TfLiteTensor* target = interpreter->tensor(interpreter->outputs()[idx]);
const int idx = static_cast<int>(input_idx);
if (idx < 0 || idx >= interpreter->inputs().size()) {
throwException(env, kIllegalArgumentException,
- "Can not resize %d-th input for a model having %d inputs.",
+ "Input error: Can not resize %d-th input for a model having "
+ "%d inputs.",
idx, interpreter->inputs().size());
return JNI_FALSE;
}
interpreter->inputs()[idx], convertJIntArrayToVector(env, dims));
if (status != kTfLiteOk) {
throwException(env, kIllegalArgumentException,
- "Failed to resize %d-th input: %s", idx,
+ "Internal error: Failed to resize %d-th input: %s", idx,
error_reporter->CachedErrorMessage());
return JNI_FALSE;
}
TfLiteTensor* convertLongToTensor(JNIEnv* env, jlong handle) {
if (handle == 0) {
throwException(env, kIllegalArgumentException,
- "Invalid handle to TfLiteTensor.");
+ "Internal error: Invalid handle to TfLiteTensor.");
return nullptr;
}
return reinterpret_cast<TfLiteTensor*>(handle);
size_t to_copy = num_elements * elementByteSize(type);
if (to_copy > dst_size) {
throwException(env, kIllegalStateException,
- "cannot write Java array of %d bytes to Tensor of %d bytes",
+ "Internal error: cannot write Java array of %d bytes to "
+ "Tensor of %d bytes",
to_copy, dst_size);
return 0;
}
}
default: {
throwException(env, kUnsupportedOperationException,
- "TensorFlowLite currently supports float (32 bits), "
- "int (32 bits), byte (8 bits), and long (64 bits), "
- "support for other types (DataType %d in this case) will "
- "be added in the future",
+ "DataType error: TensorFlowLite currently supports float "
+ "(32 bits), int (32 bits), byte (8 bits), and long "
+ "(64 bits), support for other types (DataType %d in this "
+ "case) will be added in the future",
kTfLiteFloat32, type);
return 0;
}
if (size > src_size) {
throwException(
env, kIllegalStateException,
- "cannot fill a Java array of %d bytes with a Tensor of %d bytes", size,
- src_size);
+ "Internal error: cannot fill a Java array of %d bytes with a Tensor of "
+ "%d bytes",
+ size, src_size);
return 0;
}
switch (data_type) {
return size;
}
default: {
- throwException(env, kIllegalStateException, "invalid DataType(%d)",
- data_type);
+ throwException(env, kIllegalStateException,
+ "DataType error: invalid DataType(%d)", data_type);
}
}
return 0;
switch (data_type) {
case kTfLiteFloat32:
static_assert(sizeof(jfloat) == 4,
- "Java float not compatible with kTfLiteFloat");
+ "Interal error: Java float not compatible with "
+ "kTfLiteFloat");
return 4;
case kTfLiteInt32:
static_assert(sizeof(jint) == 4,
- "Java int not compatible with kTfLiteInt");
+ "Interal error: Java int not compatible with kTfLiteInt");
return 4;
case kTfLiteUInt8:
static_assert(sizeof(jbyte) == 1,
- "Java byte not compatible with kTfLiteUInt8");
+ "Interal error: Java byte not compatible with "
+ "kTfLiteUInt8");
return 1;
case kTfLiteInt64:
static_assert(sizeof(jlong) == 8,
- "Java long not compatible with kTfLiteInt64");
+ "Interal error: Java long not compatible with "
+ "kTfLiteInt64");
return 8;
default:
return 0;
int num_dims = tensor->dims->size;
if (num_dims == 0) {
throwException(env, kIllegalArgumentException,
- "copyTo() is not meant for scalar Tensors.");
+ "Internal error: Cannot copy empty/scalar Tensors.");
return;
}
readMultiDimensionalArray(env, tensor->type, tensor->data.raw, tensor->bytes,