use of com.spotify.zoltar.IrisFeaturesSpec.Iris in project zoltar by spotify.
the class TensorFlowModelTest method getTFIrisPredictor.
public static Predictor<Iris, Long> getTFIrisPredictor() throws Exception {
final TensorFlowPredictFn<Iris, Long> predictFn = (model, vectors) -> {
final List<CompletableFuture<Prediction<Iris, Long>>> predictions = vectors.stream().map(vector -> {
return CompletableFuture.supplyAsync(() -> predict(model, vector.value())).thenApply(value -> Prediction.create(vector.input(), value));
}).collect(Collectors.toList());
return CompletableFutures.allAsList(predictions);
};
final URI trainedModelUri = TensorFlowModelTest.class.getResource("/trained_model").toURI();
final URI settingsUri = TensorFlowModelTest.class.getResource("/settings.json").toURI();
final String settings = new String(Files.readAllBytes(Paths.get(settingsUri)), StandardCharsets.UTF_8);
final ModelLoader<TensorFlowModel> model = TensorFlowLoader.create(trainedModelUri.toString());
final ExtractFn<Iris, Example> extractFn = FeatranExtractFns.example(IrisFeaturesSpec.irisFeaturesSpec(), settings);
return PredictorsTest.newBuilder(model, extractFn, predictFn).predictor();
}
use of com.spotify.zoltar.IrisFeaturesSpec.Iris in project zoltar by spotify.
the class XGBoostModelTest method getXGBoostIrisPredictor.
public static Predictor<Iris, Long> getXGBoostIrisPredictor() throws Exception {
final URI trainedModelUri = XGBoostModelTest.class.getResource("/iris.model").toURI();
final URI settingsUri = XGBoostModelTest.class.getResource("/settings.json").toURI();
final XGBoostPredictFn<Iris, Long> predictFn = (model, vectors) -> {
final List<CompletableFuture<Prediction<Iris, Long>>> predictions = vectors.stream().map(vector -> {
return CompletableFuture.supplyAsync(() -> {
try {
final Iterator<LabeledPoint> iterator = Collections.singletonList(vector.value()).iterator();
final DMatrix dMatrix = new DMatrix(iterator, null);
final float[] score = model.instance().predict(dMatrix)[0];
int idx = IntStream.range(0, score.length).reduce((i, j) -> score[i] >= score[j] ? i : j).getAsInt();
return Prediction.create(vector.input(), (long) idx);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}).collect(Collectors.toList());
return CompletableFutures.allAsList(predictions);
};
final String settings = new String(Files.readAllBytes(Paths.get(settingsUri)), StandardCharsets.UTF_8);
final XGBoostLoader model = XGBoostLoader.create(trainedModelUri.toString());
final ExtractFn<Iris, LabeledPoint> extractFn = FeatranExtractFns.labeledPoints(IrisFeaturesSpec.irisFeaturesSpec(), settings);
return PredictorsTest.newBuilder(model, extractFn, predictFn).predictor();
}
use of com.spotify.zoltar.IrisFeaturesSpec.Iris in project zoltar by spotify.
the class XGBoostModelTest method testModelPrediction.
@Test
public void testModelPrediction() throws Exception {
final Iris[] irisStream = IrisHelper.getIrisTestData();
final Map<Integer, String> classToId = ImmutableMap.of(0, "Iris-setosa", 1, "Iris-versicolor", 2, "Iris-virginica");
final CompletableFuture<Integer> sum = getXGBoostIrisPredictor().predict(Duration.ofSeconds(10), irisStream).thenApply(predictions -> {
return predictions.stream().mapToInt(prediction -> {
String className = prediction.input().className().get();
int score = prediction.value().intValue();
return classToId.get(score).equals(className) ? 1 : 0;
}).sum();
}).toCompletableFuture();
assertTrue("Should be more the 0.8", sum.get() / (float) irisStream.length > .8);
}
use of com.spotify.zoltar.IrisFeaturesSpec.Iris in project zoltar by spotify.
the class IrisPrediction method predict.
/**
* Prediction endpoint. Takes a request in a from of a String containing iris features `-`
* separated, and returns a response in a form of a predicted iris class.
*/
public static Response<String> predict(final String requestFeatures) {
if (requestFeatures == null) {
return Response.forStatus(Status.BAD_REQUEST);
}
final String[] features = requestFeatures.split("-");
if (features.length != 4) {
return Response.forStatus(Status.BAD_REQUEST);
}
final Iris featureData = new Iris(Option.apply(Double.parseDouble(features[0])), Option.apply(Double.parseDouble(features[1])), Option.apply(Double.parseDouble(features[2])), Option.apply(Double.parseDouble(features[3])), Option.empty());
int[] predictions = new int[0];
try {
predictions = predictor.predict(featureData).thenApply(p -> p.stream().mapToInt(prediction -> prediction.value().intValue()).toArray()).toCompletableFuture().get();
} catch (final Exception e) {
e.printStackTrace();
// TODO: what to return in case of failure here?
}
final String predictedClass = idToClass.get(predictions[0]);
return Response.forPayload(predictedClass);
}
Aggregations