Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api] Adds utility method to Model for accessing properties #3007

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions api/src/main/java/ai/djl/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,36 @@ default String getProperty(String key, String defValue) {
*/
void setProperty(String key, String value);

/**
* Returns the property of the model based on property name.
*
* @param key the name of the property
* @param defValue the default value if key not found
* @return the value of the property
*/
default int intProperty(String key, int defValue) {
String value = getProperty(key);
if (value == null || value.isEmpty()) {
return defValue;
}
return Integer.parseInt(value);
}

/**
* Returns the property of the model based on property name.
*
* @param key the name of the property
* @param defValue the default value if key not found
* @return the value of the property
*/
default long longProperty(String key, long defValue) {
String value = getProperty(key);
if (value == null || value.isEmpty()) {
return defValue;
}
return Integer.parseInt(value);
}

/**
* Gets the {@link NDManager} from the model.
*
Expand Down
4 changes: 4 additions & 0 deletions api/src/test/java/ai/djl/translate/TranslatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public void testBatchTranslator() throws IOException, ModelException, TranslateE
Predictor<Image[], Classifications[]> predictor = model.newPredictor()) {
Classifications[] res = predictor.predict(inputs);
Assert.assertEquals(res.length, 2);
int intValue = model.intProperty("something", -1);
Assert.assertEquals(intValue, -1);
long longValue = model.longProperty("something", -1L);
Assert.assertEquals(longValue, -1L);
}
}
}
Loading