-
-
Notifications
You must be signed in to change notification settings - Fork 465
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
matrix classification via tflite model #656
matrix classification via tflite model #656
Conversation
- "matrix_frame" as name in order to avoid confusion with matrix.cc - matrix_frame is a 2D input data modality that gets converted to Eigen::MatrixXf internally - suited for non-image input to tflite models
- "float_vector_frame" as name in order to avoid confusion with FloatArrayPacket - float_vector_frame is a 1D output data modality that accepts std::vector<float> outputs from a mediapipe / tflite graph - suited for tflite classification results packaged as vector of floats
- send MatrixData to C++ as byte array
- return std::vector<float> from C++ to Unity as List<float>
- driver code for the newly added MatrixFramePacket and FloatVectorFramePacket - feeds an example matrix of size [ 2 x 3 ] into a mediapipe graph - the graph runs a simple tflite model (adds +1 to every input) - then the graph returns the result back to Unity as List<float> - only tested on Unity-Editor-Mode on Windows 10 Pro
just testing around a bit (didn't excessively test after rebasing onto current master). I guess I somehow lost the
dependency needed for the example graph somewhere along the way. |
ok so the problem was related to building only the pose solution
it works when I build the full solution ( omit the using |
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.
Thank you!
Will you add tests for MatrixPacket
and FrameVectorPacket
?
Please check out https://github.com/homuler/MediaPipeUnityPlugin/blob/6efa5e1b75b7c6aa487254ade15fe7cd49fc6842/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/FloatArrayPacketTest.cs for more details.
@@ -0,0 +1,200 @@ | |||
// TODO: |
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.
Sorry, since our policy is to add sample code only for official solutions, can you please remove this scene (files under Assets/MediaPipeUnity/Samples
)?
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.
Could move it to Assets/MediaPipeUnity/Tutorial
!? I'm not sure where else to put it. I assume MatrixClassification.cs is quite illustrative in showcasing how to use
- matrix data as input
-> tflite model
-> receive float vector as output 🤔
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.
since our policy is to add sample code only for official solutions
I don't want to maintain this sample scene, so please remove the related files.
...ages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/FloatVectorFramePacket.cs
Outdated
Show resolved
Hide resolved
/// </summary> | ||
/// | ||
|
||
private int _vectorLength; |
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.
I think we should not store the length of the internal std::vector
since its length is variable.
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.
- I had to introduce this variable in order to make the
Get ()
method working in the same class - I borrowed the structure from "FloatArrayPacket.cs" which also has a "lenght" variable for the same reason apparantly
- should not be a problem as tflite models don't allow for "dynamic output sizes" -> a single tflite model will always have a fixed size output (vector)
public FloatVectorFramePacket(float[] value) : base() | ||
{ | ||
UnsafeNativeMethods.mp__MakeFloatVectorFramePacket__PA_i(value, value.Length, out var ptr).Assert(); | ||
this.ptr = ptr; | ||
_vectorLength = value.Length; | ||
} | ||
|
||
public FloatVectorFramePacket(float[] value, Timestamp timestamp) : base() | ||
{ | ||
UnsafeNativeMethods.mp__MakeFloatVectorFramePacket_At__PA_i_Rt(value, value.Length, timestamp.mpPtr, out var ptr).Assert(); | ||
GC.KeepAlive(timestamp); | ||
this.ptr = ptr; | ||
} | ||
|
||
public FloatVectorFramePacket At(Timestamp timestamp) | ||
{ | ||
return At<FloatVectorFramePacket>(timestamp); | ||
} |
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.
I think these constructors are unnecessary for now since FloatVectorPacket
is only used to receive output from a CalculatorGraph
.
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.
- I didn't have the constructors in the beginning until I realized that I needed them
- Probably due to the fact that one needs to create a mediapipe packet in order to give it to the OutputStreamPoller (at least that's where those constructor are being called)
...ages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/FloatVectorFramePacket.cs
Outdated
Show resolved
Hide resolved
...muler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/Format/MatrixFrame_Unsafe.cs
Outdated
Show resolved
Hide resolved
@@ -0,0 +1,20 @@ | |||
|
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.
Can you add a license header to the top of each file?
// Copyright (c) 2021 homuler | |
// | |
// Use of this source code is governed by an MIT-style | |
// license that can be found in the LICENSE file or at | |
// https://opensource.org/licenses/MIT. |
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.
yes
- what exactly should I put there?
- I'm not familiar with licensing
- should I add
// Copyright (c) 2021 homuler
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
to the top of every newly added file?
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.
should I add ... to the top of every newly added file?
Yes, exactly.
That's because if the license is not clear, it will be difficult for me to modify the contents.
....mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/Format/FloatVectorFrame_Unsafe.cs
Outdated
Show resolved
Hide resolved
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/MatrixFramePacket.cs
Outdated
Show resolved
Hide resolved
thx for feedback. will look into this asap (couple of days) |
- rename variables - rename cs files - rename cc files
- rename variables - rename cs files - rename cc files matrix_frame -> matrix_data -> avoid matrix.cc as it is already used as a name in mediapipe
- does not represent an official solution - not sure where else to place this - MatrixClassification.cs is an important example for showcasing the usage of a tflite model with a matrix data input
- before it was byte[]
- delete FloatVector_Unsafe.cs
TODO: - implement GetFloatVector with vector size as argument
- using format file ".clang-format" in project root
- build similar to FloatArrayPacketTest - not yet tested
fix: FloatVectorPacket [skip actions] (+1 squashed commits) Squashed commits: [69302b1] FloatVectorPacket - replace list by array - list is slow
a90a2b5
to
7e5e5f1
Compare
[skip actions]
7e5e5f1
to
8e7771d
Compare
as per request: - deleted demo / tutorial scene that showcasts a simple tflite graph [skip actions]
I force pushed some more changes:
all your requested changes should still be inside the current HEAD of "matrix_packet_rebased_v3" |
@mgarbade Will you fix the failed tests? |
I'll look into it when I have time. (about 1-2 weeks) |
I'm sorry to bother you. But I cannot figure out from the reports what is the problem with the tests. has the following log output at the end:
This tells me nothing. I don't know what "##utp" means and any of the words following that keyword seem not to relate to my changes. "type":"MemoryLeaks" might hint to the problem, however without any more context, I'm not sure what to look for. I'll try to run the tests on my local machine and see if there is something failing either... |
See https://github.com/homuler/MediaPipeUnityPlugin/actions/runs/3135556925/jobs/5092743904. On Linux or macOS, it seems that the result.xml is not printed to the console, so I'll fix it. |
I agree that the output can be improved, but at least you can know which test cases are failing. xmllint --xpath "//test-case[@result='Failed']" results.xml <test-case id="2864" name="At_ShouldReturnNewPacketWithTimestamp" fullname="Mediapipe.Tests.MatrixPacketTest.At_ShouldReturnNewPacketWithTimestamp" methodname="At_ShouldReturnNewPacketWithTimestamp" classname="Mediapipe.Tests.MatrixPacketTest" runstate="Runnable" seed="132040465" result="Failed" label="Error" start-time="2022-10-10 10:22:45Z" end-time="2022-10-10 10:22:45Z" duration="0.010864" asserts="0">
<properties/>
<failure>
<message><![CDATA[System.NotImplementedException : The method or operation is not implemented.]]></message>
<stack-trace><![CDATA[ at Mediapipe.MatrixPacket.Get () [0x00001] in /home/runner/work/MediaPipeUnityPlugin/MediaPipeUnityPlugin/DummyProject/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/MatrixPacket.cs:65
at Mediapipe.Tests.MatrixPacketTest.At_ShouldReturnNewPacketWithTimestamp () [0x0001d] in /home/runner/work/MediaPipeUnityPlugin/MediaPipeUnityPlugin/DummyProject/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/MatrixPacketTest.cs:57
at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <8516445456524a6cb20c4b3b7de67982>:0 ]]></stack-trace>
</failure>
</test-case>
<test-case id="2865" name="Consume_ShouldThrowNotSupportedException" fullname="Mediapipe.Tests.MatrixPacketTest.Consume_ShouldThrowNotSupportedException" methodname="Consume_ShouldThrowNotSupportedException" classname="Mediapipe.Tests.MatrixPacketTest" runstate="Runnable" seed="847120028" result="Failed" start-time="2022-10-10 10:22:45Z" end-time="2022-10-10 10:22:45Z" duration="0.003217" asserts="0">
<properties/>
<failure>
<message><![CDATA[ Expected: <System.NotSupportedException>
But was: <System.NotImplementedException: The method or operation is not implemented.
at Mediapipe.MatrixPacket.Consume () [0x00001] in /home/runner/work/MediaPipeUnityPlugin/MediaPipeUnityPlugin/DummyProject/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/MatrixPacket.cs:70
at Mediapipe.Tests.MatrixPacketTest+<>c__DisplayClass4_0.<Consume_ShouldThrowNotSupportedException>b__0 () [0x00001] in /home/runner/work/MediaPipeUnityPlugin/MediaPipeUnityPlugin/DummyProject/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/MatrixPacketTest.cs:79
at NUnit.Framework.Assert.Throws (NUnit.Framework.Constraints.IResolveConstraint expression, NUnit.Framework.TestDelegate code, System.String message, System.Object[] args) [0x00004] in <10b6135e63434fdba4fc6c109928ab3b>:0 >
]]></message>
<stack-trace><![CDATA[at Mediapipe.Tests.MatrixPacketTest.Consume_ShouldThrowNotSupportedException () [0x00013] in /home/runner/work/MediaPipeUnityPlugin/MediaPipeUnityPlugin/DummyProject/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/MatrixPacketTest.cs:79
]]></stack-trace>
</failure>
</test-case>
<test-case id="2861" name="Ctor_ShouldInstantiatePacket_When_CalledWithValue" fullname="Mediapipe.Tests.MatrixPacketTest.Ctor_ShouldInstantiatePacket_When_CalledWithValue" methodname="Ctor_ShouldInstantiatePacket_When_CalledWithValue" classname="Mediapipe.Tests.MatrixPacketTest" runstate="Runnable" seed="697797710" result="Failed" label="Error" start-time="2022-10-10 10:22:45Z" end-time="2022-10-10 10:22:45Z" duration="0.000617" asserts="0">
<properties/>
<failure>
<message><![CDATA[System.NotImplementedException : The method or operation is not implemented.]]></message>
<stack-trace><![CDATA[ at Mediapipe.Packet`1[TValue].ValidateAsType () [0x00001] in /home/runner/work/MediaPipeUnityPlugin/MediaPipeUnityPlugin/DummyProject/Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/Packet.cs:74
at Mediapipe.Tests.MatrixPacketTest.Ctor_ShouldInstantiatePacket_When_CalledWithValue () [0x0000f] in /home/runner/work/MediaPipeUnityPlugin/MediaPipeUnityPlugin/DummyProject/Packages/com.github.homuler.mediapipe/Tests/EditMode/Framework/Packet/MatrixPacketTest.cs:22
at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <8516445456524a6cb20c4b3b7de67982>:0 ]]></stack-trace>
</failure>
</test-case> |
Thanks. Looks like the same tests that where failing in my local windows build. Anyway, I'll go ahead and try to fix the tests... |
You can download it from the summary page ( |
I'm not sure how to write the "packet__Get()" function for MatrixPacket.cs which is needed for one of the test functions
However I'm not sure how to do that conversion. This is my first draft:
the compilation fails saying
however I'm not sure how to convert the protobuf to serialized proto |
moving forward. This code seems more reasonable but still crashes:
The exact line where it crashes:
It throws an exception in mediapipe which I'm unable to read... this is what the console says:
the "_mp_return_code" is -327236016 (which I'm unable to google) |
nevermind. I found the problem! (some astrix to many ...)
|
- new GetMatrix function Caveat: - MatrixPacket: Consume throws NotSupportedException() -> not sure if this is a useful test, but such tests exists in similar classes as well (cherry picked from commit 707af5f454e87312a86b60deaebec18463e47ded)
all tests concerning the two new classes I introduced are running successfully on my Windows 10 Pro machine in Unity Editor mode: I'm not 100% positive how useful these tests are, but they roughly follow the pattern of similar classes. Especially questionable is the test "Consume_ShouldThrowNotSupportedException" which just makes sure that the Consume function is not implemented. (I'm not sure what the Consume function is used for anyway) |
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.
Thx!
* cc: matrix_frame as input to graph - "matrix_frame" as name in order to avoid confusion with matrix.cc - matrix_frame is a 2D input data modality that gets converted to Eigen::MatrixXf internally - suited for non-image input to tflite models * cc: float_vector_frame as input to graph - "float_vector_frame" as name in order to avoid confusion with FloatArrayPacket - float_vector_frame is a 1D output data modality that accepts std::vector<float> outputs from a mediapipe / tflite graph - suited for tflite classification results packaged as vector of floats * MatrixFramePacket - c# helper functions - send MatrixData to C++ as byte array * FloatVectorFramePacket - c# helper functions - return std::vector<float> from C++ to Unity as List<float> * Unity: Matrix Classification - Example scene * Matrix Classification.cs - driver code for the newly added MatrixFramePacket and FloatVectorFramePacket - feeds an example matrix of size [ 2 x 3 ] into a mediapipe graph - the graph runs a simple tflite model (adds +1 to every input) - then the graph returns the result back to Unity as List<float> - only tested on Unity-Editor-Mode on Windows 10 Pro * refactor: rename FloatVectorFrame -> FloatVector - rename variables - rename cs files - rename cc files * refactor: rename MatrixFrame -> Matrix - rename variables - rename cs files - rename cc files matrix_frame -> matrix_data -> avoid matrix.cc as it is already used as a name in mediapipe * move MatrixClassification example scene to Tutorials - does not represent an official solution - not sure where else to place this - MatrixClassification.cs is an important example for showcasing the usage of a tflite model with a matrix data input * GetArrayPtr() - change access to private * MatrixPacket: accept MatrixData as input - before it was byte[] * add license * move native functions to Packet_Unsafe - delete FloatVector_Unsafe.cs * float_vector.cc -> faster vector allocation * float_vector.cc remove unused function - delete(...) * float_vector.h - remove unused headers * refactor: float_vector.cc TODO: - implement GetFloatVector with vector size as argument * removed unused headers * refactor: float_vector.h * refactor: apply autoformatter on cc files - using format file ".clang-format" in project root * refactor: mp__MakeMatrixFramePacket_At__PA_i_Rt -> mp__MakeMatrixPacket_At__PKc_i_Rt * FloatVectorPacketTest added - build similar to FloatArrayPacketTest - not yet tested * fix: float_vector.cc * fix: MatrixPacket.cs * fix: Test: FloatVectorPacketTest - Consume_ShouldThrowNotSupportedException * MatrixPacketTest - add - all tests involving packet.Get() do not work - function is not yet implemented * fix: Make MatrixClassification.cs run on Android - adding StreamingAssets to ResourceManager [skip actions] * Update mediapipe_api/framework/formats/matrix_data.h Co-authored-by: Junrou Nishida <[email protected]> * Apply suggestions from code review Co-authored-by: Junrou Nishida <[email protected]> * float_vector - return vector size (+2 squashed commit) Squashed commit: [e409b05] refactor: vector_float.cc - naming aligns with files like packet.cc [bad3cd6] float_vector - return vector size * fix: matrix_data.cc - wrong func name (+3 squashed commit) Squashed commit: [9245a37] fix: Revert "Apply suggestions from code review" - the below mentioned commit is not working - return value of inline function is invalid -> probably due to inline function This reverts commit c374d61. [f597e83] fix: remove duplicate cpp func [6def10c] fix: semicolon omitted * FloatVectorPacket - replace list by array fix: FloatVectorPacket [skip actions] (+1 squashed commits) Squashed commits: [69302b1] FloatVectorPacket - replace list by array - list is slow * Add license headers [skip actions] * Remove Tutorial Scene: MatrixClassification as per request: - deleted demo / tutorial scene that showcasts a simple tflite graph [skip actions] * fix: MatrixPacket tests - new GetMatrix function Caveat: - MatrixPacket: Consume throws NotSupportedException() -> not sure if this is a useful test, but such tests exists in similar classes as well (cherry picked from commit 707af5f454e87312a86b60deaebec18463e47ded) Co-authored-by: Junrou Nishida <[email protected]>
* matrix classification via tflite model (#656) * cc: matrix_frame as input to graph - "matrix_frame" as name in order to avoid confusion with matrix.cc - matrix_frame is a 2D input data modality that gets converted to Eigen::MatrixXf internally - suited for non-image input to tflite models * cc: float_vector_frame as input to graph - "float_vector_frame" as name in order to avoid confusion with FloatArrayPacket - float_vector_frame is a 1D output data modality that accepts std::vector<float> outputs from a mediapipe / tflite graph - suited for tflite classification results packaged as vector of floats * MatrixFramePacket - c# helper functions - send MatrixData to C++ as byte array * FloatVectorFramePacket - c# helper functions - return std::vector<float> from C++ to Unity as List<float> * Unity: Matrix Classification - Example scene * Matrix Classification.cs - driver code for the newly added MatrixFramePacket and FloatVectorFramePacket - feeds an example matrix of size [ 2 x 3 ] into a mediapipe graph - the graph runs a simple tflite model (adds +1 to every input) - then the graph returns the result back to Unity as List<float> - only tested on Unity-Editor-Mode on Windows 10 Pro * refactor: rename FloatVectorFrame -> FloatVector - rename variables - rename cs files - rename cc files * refactor: rename MatrixFrame -> Matrix - rename variables - rename cs files - rename cc files matrix_frame -> matrix_data -> avoid matrix.cc as it is already used as a name in mediapipe * move MatrixClassification example scene to Tutorials - does not represent an official solution - not sure where else to place this - MatrixClassification.cs is an important example for showcasing the usage of a tflite model with a matrix data input * GetArrayPtr() - change access to private * MatrixPacket: accept MatrixData as input - before it was byte[] * add license * move native functions to Packet_Unsafe - delete FloatVector_Unsafe.cs * float_vector.cc -> faster vector allocation * float_vector.cc remove unused function - delete(...) * float_vector.h - remove unused headers * refactor: float_vector.cc TODO: - implement GetFloatVector with vector size as argument * removed unused headers * refactor: float_vector.h * refactor: apply autoformatter on cc files - using format file ".clang-format" in project root * refactor: mp__MakeMatrixFramePacket_At__PA_i_Rt -> mp__MakeMatrixPacket_At__PKc_i_Rt * FloatVectorPacketTest added - build similar to FloatArrayPacketTest - not yet tested * fix: float_vector.cc * fix: MatrixPacket.cs * fix: Test: FloatVectorPacketTest - Consume_ShouldThrowNotSupportedException * MatrixPacketTest - add - all tests involving packet.Get() do not work - function is not yet implemented * fix: Make MatrixClassification.cs run on Android - adding StreamingAssets to ResourceManager [skip actions] * Update mediapipe_api/framework/formats/matrix_data.h Co-authored-by: Junrou Nishida <[email protected]> * Apply suggestions from code review Co-authored-by: Junrou Nishida <[email protected]> * float_vector - return vector size (+2 squashed commit) Squashed commit: [e409b05] refactor: vector_float.cc - naming aligns with files like packet.cc [bad3cd6] float_vector - return vector size * fix: matrix_data.cc - wrong func name (+3 squashed commit) Squashed commit: [9245a37] fix: Revert "Apply suggestions from code review" - the below mentioned commit is not working - return value of inline function is invalid -> probably due to inline function This reverts commit c374d61. [f597e83] fix: remove duplicate cpp func [6def10c] fix: semicolon omitted * FloatVectorPacket - replace list by array fix: FloatVectorPacket [skip actions] (+1 squashed commits) Squashed commits: [69302b1] FloatVectorPacket - replace list by array - list is slow * Add license headers [skip actions] * Remove Tutorial Scene: MatrixClassification as per request: - deleted demo / tutorial scene that showcasts a simple tflite graph [skip actions] * fix: MatrixPacket tests - new GetMatrix function Caveat: - MatrixPacket: Consume throws NotSupportedException() -> not sure if this is a useful test, but such tests exists in similar classes as well (cherry picked from commit 707af5f454e87312a86b60deaebec18463e47ded) Co-authored-by: Junrou Nishida <[email protected]> * refactor: move float vector packet API * FloatVectorPacket#Get returns List<float> * copy float arrays when getting the value * refactor MatrixPacket * refactor: use ParseFromStringAsProto * test: replace DebugTypeName tests with ValidateAsType tests Co-authored-by: Martin Garbade <[email protected]>
-> the name "Frame" should resemble "ImageFramePacket" and should help to avoid confusions with similar sounding classes like
Missing: