public async Task Invokes_predict_async_call() { var expectedResultValues = new[] { 1f, 2f, 3f }; var outputTensorProto = new TensorProto { Dtype = DataType.DtFloat }; outputTensorProto.FloatVal.Add(expectedResultValues); outputTensorProto.TensorShape = new TensorShapeProto(); outputTensorProto.TensorShape.Dim.Add(new TensorShapeProto.Types.Dim()); outputTensorProto.TensorShape.Dim[0].Size = 3; var predictRequest = new PredictRequest(); var predictResponse = new PredictResponse(); predictResponse.Outputs.Add("output_alias", outputTensorProto); var predictionServiceClientMock = new Mock <IPredictionServiceClient>(); predictionServiceClientMock.Setup(x => x.PredictAsync(predictRequest)).ReturnsAsync(predictResponse).Verifiable(); var scoringRequestMock = new Mock <IScoringRequest>(); scoringRequestMock.Setup(x => x.MakePredictRequest()).Returns(() => predictRequest); var scoringClient = new ScoringClient(predictionServiceClientMock.Object); var result = await scoringClient.ScoreAsync(scoringRequestMock.Object); Assert.Equal(expectedResultValues, result); scoringRequestMock.Verify(x => x.MakePredictRequest(), Times.Exactly(1)); predictionServiceClientMock.Verify(x => x.PredictAsync(predictRequest), Times.Exactly(1)); }
private static async Task <int> MainAsync(string[] args) { if (args.Length < 2) { Console.WriteLine($"Use as {errorMessage}"); return(1); } var host = args[0]; var image = args[1]; var serviceName = args[2]; string auth = null; if (args.Length > 3) { auth = args[4]; Console.WriteLine(!string.IsNullOrEmpty(auth) ? "Using auth" : "Not using auth"); } var useSSL = false; if (args.Length > 4) { var useSslString = args[5]; var parsed = bool.TryParse(useSslString, out useSSL); Console.WriteLine(useSSL ? "Using SSL" : "Not using SSL"); } var client = new ScoringClient(host, useSSL ? 443 : 80, useSSL, auth, serviceName); using (var content = File.OpenRead(image)) { IScoringRequest request = new ImageRequest("Placeholder:0", content); //Dimensions depend on model - client.ScoreAsync<float[,,,,]>(request); for ssd-vgg var result = await client.ScoreAsync <float[, ]>(request); for (int i = 0; i < result.GetLength(0); i++) { Console.WriteLine($"Batch {i}:"); var length = result.GetLength(1); var results = new Dictionary <int, float>(); for (int j = 0; j < length; j++) { results.Add(j, result[i, j]); } foreach (var kvp in results.Where(x => x.Value > 0.001).OrderByDescending(x => x.Value).Take(5)) { Console.WriteLine( $" {GetLabel(kvp.Key)} {kvp.Value * 100}%"); } } } return(0); }
public async Task Retries_transient_exceptions(StatusCode transientStatusCode) { var exception = new RpcException(new Status(transientStatusCode, string.Empty)); var expectedResultValues = new[] { 1f, 2f, 3f }; var outputTensorProto = new TensorProto { Dtype = DataType.DtFloat }; outputTensorProto.FloatVal.Add(expectedResultValues); outputTensorProto.TensorShape = new TensorShapeProto(); outputTensorProto.TensorShape.Dim.Add(new TensorShapeProto.Types.Dim()); outputTensorProto.TensorShape.Dim[0].Size = 3; var predictRequest = new PredictRequest(); var predictResponse = new PredictResponse(); predictResponse.Outputs.Add("output_alias", outputTensorProto); var predictionServiceClientMock = new Mock <IPredictionServiceClient>(); predictionServiceClientMock.Setup(x => x.PredictAsync(predictRequest)).Returns(async() => { await Task.CompletedTask; if (exception != null) { var x = exception; exception = null; throw x; } return(predictResponse); }).Verifiable(); var scoringRequestMock = new Mock <IScoringRequest>(); scoringRequestMock.Setup(x => x.MakePredictRequest()).Returns(() => predictRequest); var scoringClient = new ScoringClient(predictionServiceClientMock.Object); var result = await scoringClient.ScoreAsync(scoringRequestMock.Object); Assert.Equal(expectedResultValues, result); scoringRequestMock.Verify(x => x.MakePredictRequest(), Times.Exactly(1)); predictionServiceClientMock.Verify(x => x.PredictAsync(predictRequest), Times.Exactly(2)); }
public async Task Retries_transient_exceptions_until_retries_exhausted(StatusCode transientStatusCode, int retryCount) { var exception = new RpcException(new Status(transientStatusCode, string.Empty)); var expectedResultValues = new[] { 1f, 2f, 3f }; var outputTensorProto = new TensorProto { Dtype = DataType.DtFloat }; outputTensorProto.FloatVal.Add(expectedResultValues); outputTensorProto.TensorShape = new TensorShapeProto(); outputTensorProto.TensorShape.Dim.Add(new TensorShapeProto.Types.Dim()); outputTensorProto.TensorShape.Dim[0].Size = 3; var predictRequest = new PredictRequest(); var predictResponse = new PredictResponse(); predictResponse.Outputs.Add("output_alias", outputTensorProto); var predictionServiceClientMock = new Mock <IPredictionServiceClient>(); predictionServiceClientMock.Setup(x => x.PredictAsync(predictRequest)).ThrowsAsync(exception).Verifiable(); var scoringRequestMock = new Mock <IScoringRequest>(); scoringRequestMock.Setup(x => x.MakePredictRequest()).Returns(() => predictRequest); var scoringClient = new ScoringClient(predictionServiceClientMock.Object); var resultException = await Assert.ThrowsAsync <RpcException>(() => scoringClient.ScoreAsync(scoringRequestMock.Object, retryCount)); Assert.Equal(exception, resultException); scoringRequestMock.Verify(x => x.MakePredictRequest(), Times.Exactly(1)); predictionServiceClientMock.Verify(x => x.PredictAsync(predictRequest), Times.Exactly(retryCount)); }
public async Task Rethrows_non_transient_exception(Exception exception) { var expectedResultValues = new[] { 1f, 2f, 3f }; var outputTensorProto = new TensorProto { Dtype = DataType.DtFloat }; outputTensorProto.FloatVal.Add(expectedResultValues); outputTensorProto.TensorShape = new TensorShapeProto(); outputTensorProto.TensorShape.Dim.Add(new TensorShapeProto.Types.Dim()); outputTensorProto.TensorShape.Dim[0].Size = 3; var predictRequest = new PredictRequest(); var predictResponse = new PredictResponse(); predictResponse.Outputs.Add("output_alias", outputTensorProto); var predictionServiceClientMock = new Mock <IPredictionServiceClient>(); predictionServiceClientMock.Setup(x => x.PredictAsync(predictRequest)).Returns(async() => { await Task.CompletedTask; throw exception; }).Verifiable(); var scoringRequestMock = new Mock <IScoringRequest>(); scoringRequestMock.Setup(x => x.MakePredictRequest()).Returns(() => predictRequest); var scoringClient = new ScoringClient(predictionServiceClientMock.Object); var actualException = await Assert.ThrowsAnyAsync <Exception>(async() => await scoringClient.ScoreAsync(scoringRequestMock.Object)); Assert.Same(exception, actualException); scoringRequestMock.Verify(x => x.MakePredictRequest(), Times.Exactly(1)); predictionServiceClientMock.Verify(x => x.PredictAsync(predictRequest), Times.Exactly(1)); }