-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Speed up the inference of the saved_model(s). Fixes #5847 #5848
Speed up the inference of the saved_model(s). Fixes #5847 #5848
Conversation
Signed-off-by: darth-vader-lg <luigi.generale@gmail.com>
- Fixed the exception while fitting data with more than one input tensor. Followed the OnnxTransformer schema for the data view getters creation. Signed-off-by: darth-vader-lg <luigi.generale@gmail.com>
Codecov Report
@@ Coverage Diff @@
## main #5848 +/- ##
==========================================
- Coverage 68.35% 68.33% -0.02%
==========================================
Files 1134 1134
Lines 241910 241932 +22
Branches 25289 25293 +4
==========================================
- Hits 165347 165330 -17
- Misses 69919 69954 +35
- Partials 6644 6648 +4
Flags with carried forward coverage won't be shown. Click here to find out more.
|
Unluckily it wasn't as easy as expected and it wasn't completely solvable with just the first little changes. machinelearning/src/Microsoft.ML.OnnxTransformer/OnnxTransform.cs Lines 486 to 531 in ff01708
Now it's all ok and drastically more faster than before 👍. |
- The cached tensors are disposed at the end of inference operations. Signed-off-by: darth-vader-lg <luigi.generale@gmail.com>
As mentioned in the issues #5847, these changes improve the tensorflow inference speed a lot; mostly if used for object detection. [TensorFlowFact]
public void TensorFlowTransformObjectDetectionTest()
{
// Saved model
var modelLocation = @"D:\ObjectDetection\carp\TensorFlow\exported-model-SSD-MobileNET-v2-320x320\saved_model";
// Create the estimators pipe
var pipe =
_mlContext.Transforms.LoadImages(
inputColumnName: "ImagePath",
outputColumnName: "Image",
imageFolder: "")
.Append(_mlContext.Transforms.ResizeImages(
inputColumnName: "Image",
outputColumnName: "ResizedImage",
imageWidth: 300,
imageHeight: 300,
resizing: ImageResizingEstimator.ResizingKind.Fill))
.Append(_mlContext.Transforms.ExtractPixels(
inputColumnName: "ResizedImage",
outputColumnName: "serving_default_input_tensor:0",
interleavePixelColors: true,
outputAsFloatArray: false))
.Append(_mlContext.Model.LoadTensorFlowModel(modelLocation).ScoreTensorFlowModel(
inputColumnNames: new[] { "serving_default_input_tensor:0" },
outputColumnNames: new[]
{
"StatefulPartitionedCall:1" /* detection_boxes */,
"StatefulPartitionedCall:2" /* detection_classes */,
"StatefulPartitionedCall:4" /* detection_scores */
}));
// Collect all the path of the images in the test directory
var imagesLocation = @"D:\ObjectDetection\carp\TensorFlow\images\test";
var images =
Directory.GetFiles(imagesLocation).Where(file => new[] { ".jpg", ".jfif" }
.Any(ext => Path.GetExtension(file).ToLower() == ext))
.Select(file => new { ImagePath = file })
.ToArray();
// Create the transformer
var data = _mlContext.Data.LoadFromEnumerable(images.Take(0));
var model = pipe.Fit(data);
// Test n times the inference on the collected images
for (int i = 0, nImage = 0; i < 1000; i++, nImage = (nImage + 1) % images.Length)
model.Transform(_mlContext.Data.LoadFromEnumerable(new[] { images[nImage] })).Preview();
} Here there are the results of the tests: Without optimizations (current)With only the TF cache optimization in the Microsoft.ML.TensorFlowTransform.cs (Issue #5847 / PR #5848)With TensorFlow and image raw access optimization (Issue #5847 / PR #5848 and Issue #5856 / PR #5857) |
@darth-vader-lg thanks for submitting this! That is a pretty drastic speedup which is awesome. I really appreciate you making the PR and the issue really detailed. It makes understanding all the changes so much easier. Let me run a couple of tests on it but it looks good to me. |
@michaelgsharp, I was just glad to give to this awesome project my little contribution and repay for the big help it gave me on my work. 👍 Anyway, have a good merge. P.S. (🔔 promotional spot 🔔) If also MS. and .NET teams will need services from my company in the future, I’m always here. 😉 Kind Regards |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* remotes/official/main: Update lgbm to v2.3.1 (dotnet#5851) Speed-up bitmap operations on images. Fixes dotnet#5856 (dotnet#5857) Onnx recursion limit (dotnet#5840) Speed up the inference of the saved_model(s). Fixes dotnet#5847 (dotnet#5848) Signed-off-by: darth-vader-lg <luigi.generale@gmail.com>
This little commit fixes #5847 issue about the inference speed of the TensorFlow models.
All information are well explained in the issue #5847.