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

[examples] Fix ImageClassification invalid probability #1575

Merged
merged 3 commits into from
Apr 16, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static Classifications predict() throws IOException, ModelException, Tran
ImageClassificationTranslator.builder()
.addTransform(new ToTensor())
.optSynset(classes)
.optApplySoftmax(true)
.build();

try (Predictor<Image, Classifications> predictor = model.newPredictor(translator)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class TrainMnistTest {
public void testTrainMnist() throws ModelException, TranslateException, IOException {
TestRequirements.engine("MXNet", "PyTorch");

double expectedProb;
if (Boolean.getBoolean("nightly")) {
String[] args = new String[] {"-g", "1"};

Expand All @@ -39,14 +40,18 @@ public void testTrainMnist() throws ModelException, TranslateException, IOExcept
Assert.assertTrue(accuracy > 0.9f, "Accuracy: " + accuracy);
Assert.assertTrue(loss < 0.35f, "Loss: " + loss);

Classifications classifications = ImageClassification.predict();
Classifications.Classification best = classifications.best();
Assert.assertEquals(best.getClassName(), "0");
Assert.assertTrue(Double.compare(best.getProbability(), 0.9) > 0);
expectedProb = 0.9;
} else {
String[] args = new String[] {"-g", "1", "-m", "2"};

TrainMnist.runExample(args);
expectedProb = 0;
}

Classifications classifications = ImageClassification.predict();
Classifications.Classification best = classifications.best();
Assert.assertEquals(best.getClassName(), "0");
double probability = best.getProbability();
Assert.assertTrue(probability > expectedProb && probability <= 1);
}
}