From f3a251c5b1e42241d6a21fed480daf6319c287a1 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Mon, 14 Oct 2024 18:41:27 +0530 Subject: [PATCH 1/4] Added streams mode tests for iOS audio embedder --- .../audio_embedder/MPPAudioEmbedderTests.mm | 152 +++++++++++++++++- 1 file changed, 150 insertions(+), 2 deletions(-) diff --git a/mediapipe/tasks/ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm b/mediapipe/tasks/ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm index 3e17169c8b..14897c8399 100644 --- a/mediapipe/tasks/ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm +++ b/mediapipe/tasks/ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm @@ -22,6 +22,8 @@ #import "mediapipe/tasks/ios/test/audio/core/utils/sources/MPPAudioData+TestUtils.h" #import "mediapipe/tasks/ios/test/utils/sources/MPPFileInfo.h" +#include + static MPPFileInfo *const kYamnetModelFileInfo = [[MPPFileInfo alloc] initWithName:@"yamnet_embedding_metadata" type:@"tflite"]; static MPPFileInfo *const kSpeech16KHzMonoFileInfo = @@ -62,7 +64,11 @@ XCTAssertEqual(embedding.floatEmbedding.count, expectedLength); \ } -@interface MPPAudioEmbedderTests : XCTestCase +@interface MPPAudioEmbedderTests : XCTestCase { + NSDictionary *_16kHZAudioStreamSucceedsTestDict; + NSDictionary *_48kHZAudioStreamSucceedsTestDict; + NSDictionary *_outOfOrderTimestampTestDict; +} @end @implementation MPPAudioEmbedderTests @@ -185,6 +191,14 @@ - (void)testEmbedWithSilenceSucceeds { isQuantized:options.quantize expectedEmbeddingResultsCount:expectedEmbedderResultsCount expectedEmbeddingLength:kExpectedEmbeddingLength]; + std::array expectedEmbeddingValuesSubset = {2.07613f, 0.392721f, 0.543622f}; + constexpr float valueDifferenceTolerance = 3e-6; + + NSArray *floatEmbedding = result.embeddingResults[0].embeddings[0].floatEmbedding; + for (int i; i < expectedEmbeddingValuesSubset.size(); i++) { + XCTAssertEqualWithAccuracy(expectedEmbeddingValuesSubset[i], floatEmbedding[i].floatValue, + valueDifferenceTolerance); + } } - (void)testEmbedAfterCloseFailsInAudioClipsMode { @@ -337,17 +351,125 @@ - (void)testCreateAudioRecordWithInvalidChannelCountFails { AssertEqualErrors(error, expectedError); } +- (void)testEmbedWithAudioStreamModeAndOutOfOrderTimestampsFails { + MPPAudioEmbedder *audioEmbedder = + [self audioEmbedderInStreamModeWithModelFileInfo:kYamnetModelFileInfo]; + NSArray *streamedAudioDataList = + [MPPAudioEmbedderTests streamedAudioDataListforYamnet]; + + XCTestExpectation *expectation = + [[XCTestExpectation alloc] initWithDescription:@"embedWithOutOfOrderTimestampsAndLiveStream"]; + expectation.expectedFulfillmentCount = 1; + + _outOfOrderTimestampTestDict = @{ + kAudioStreamTestsDictEmbedderKey : audioEmbedder, + kAudioStreamTestsDictExpectationKey : expectation + }; + + // Can safely access indices 1 and 0 `streamedAudioDataList` count is already asserted. + XCTAssertTrue([audioEmbedder embedAsyncAudioBlock:streamedAudioDataList[1].audioData + timestampInMilliseconds:streamedAudioDataList[1].timestampInMilliseconds + error:nil]); + + NSError *error; + XCTAssertFalse([audioEmbedder + embedAsyncAudioBlock:streamedAudioDataList[0].audioData + timestampInMilliseconds:streamedAudioDataList[0].timestampInMilliseconds + error:&error]); + + NSError *expectedError = + [NSError errorWithDomain:kExpectedErrorDomain + code:MPPTasksErrorCodeInvalidArgumentError + userInfo:@{ + NSLocalizedDescriptionKey : + @"INVALID_ARGUMENT: Input timestamp must be monotonically increasing." + }]; + AssertEqualErrors(error, expectedError); + + [audioEmbedder closeWithError:nil]; + + NSTimeInterval timeout = 1.0f; +} + +- (void)testClassifyWithAudioStreamModeSucceeds { + [self embedUsingYamnetAsyncAudioFileWithInfo:kSpeech16KHzMonoFileInfo + info:&_16kHZAudioStreamSucceedsTestDict]; + [self embedUsingYamnetAsyncAudioFileWithInfo:kSpeech48KHzMonoFileInfo + info:&_48kHZAudioStreamSucceedsTestDict]; +} + #pragma mark MPPAudioEmbedderStreamDelegate - (void)audioEmbedder:(MPPAudioEmbedder *)audioEmbedder didFinishEmbeddingWithResult:(MPPAudioEmbedderResult *)result timestampInMilliseconds:(NSInteger)timestampInMilliseconds error:(NSError *)error { - // TODO: Add assertion for the result when stream mode inference tests are added. + // Can safely test for yamnet results before `audioEmbedder` object tests since only yamnet with + // 16khz and 48khz speech files are used for async tests. + + // Returns a `nil` `expectedResult` for the last timestamp to prevent the result from being + // tested. + [MPPAudioEmbedderTests + assertNonQuantizedAudioEmbedderYamnetStreamModeResult:result + timestampInMilliseconds:timestampInMilliseconds + expectedEmbeddingLength:kExpectedEmbeddingLength]; + + if (audioEmbedder == _outOfOrderTimestampTestDict[kAudioStreamTestsDictEmbedderKey]) { + [_outOfOrderTimestampTestDict[kAudioStreamTestsDictExpectationKey] fulfill]; + } else if (audioEmbedder == _16kHZAudioStreamSucceedsTestDict[kAudioStreamTestsDictEmbedderKey]) { + [_16kHZAudioStreamSucceedsTestDict[kAudioStreamTestsDictExpectationKey] fulfill]; + } else if (audioEmbedder == _48kHZAudioStreamSucceedsTestDict[kAudioStreamTestsDictEmbedderKey]) { + [_48kHZAudioStreamSucceedsTestDict[kAudioStreamTestsDictExpectationKey] fulfill]; + } +} + +#pragma mark Audio Stream Mode Test Helpers + +// info is strong here since address of global variables will be passed to this function. By default +// `NSDictionary **` will be `NSDictionary * __autoreleasing *. +- (void)embedUsingYamnetAsyncAudioFileWithInfo:(MPPFileInfo *)audioFileInfo + info:(NSDictionary *__strong *)info { + MPPAudioEmbedder *audioEmbedder = + [self audioEmbedderInStreamModeWithModelFileInfo:kYamnetModelFileInfo]; + + NSArray *streamedAudioDataList = + [MPPAudioEmbedderTests streamedAudioDataListforYamnet]; + + XCTestExpectation *expectation = [[XCTestExpectation alloc] + initWithDescription:[NSString + stringWithFormat:@"embedWithStreamMode_%@", audioFileInfo.name]]; + expectation.expectedFulfillmentCount = streamedAudioDataList.count; + + *info = @{ + kAudioStreamTestsDictEmbedderKey : audioEmbedder, + kAudioStreamTestsDictExpectationKey : expectation + }; + + for (MPPTimestampedAudioData *timestampedAudioData in streamedAudioDataList) { + XCTAssertTrue([audioEmbedder embedAsyncAudioBlock:timestampedAudioData.audioData + timestampInMilliseconds:timestampedAudioData.timestampInMilliseconds + error:nil]); + } + + [audioEmbedder closeWithError:nil]; + + NSTimeInterval timeout = 1.0f; + [self waitForExpectations:@[ expectation ] timeout:timeout]; } #pragma mark Audio Embedder Initializers +- (MPPAudioEmbedder *)audioEmbedderInStreamModeWithModelFileInfo:(MPPFileInfo *)fileInfo { + MPPAudioEmbedderOptions *options = + [MPPAudioEmbedderTests audioEmbedderOptionsWithModelFileInfo:fileInfo]; + options.runningMode = MPPAudioRunningModeAudioStream; + options.audioEmbedderStreamDelegate = self; + + MPPAudioEmbedder *audioEmbedder = [MPPAudioEmbedderTests audioEmbedderWithOptions:options]; + + return audioEmbedder; +} + + (MPPAudioEmbedderOptions *)audioEmbedderOptionsWithModelFileInfo:(MPPFileInfo *)modelFileInfo { MPPAudioEmbedderOptions *options = [[MPPAudioEmbedderOptions alloc] init]; options.baseOptions.modelAssetPath = modelFileInfo.path; @@ -421,6 +543,21 @@ + (void)assertAudioEmbedderResult:(MPPAudioEmbedderResult *)result } } ++ (void)assertNonQuantizedAudioEmbedderYamnetStreamModeResult:(MPPAudioEmbedderResult *)result + timestampInMilliseconds:(NSInteger)timestampInMilliseconds + expectedEmbeddingLength:(NSInteger)expectedEmbeddingLength { + // In stream mode result will only have one embedding result corresponding to the timestamp at + // which it was sent for inference. + XCTAssertEqual(result.embeddingResults.count, 1); + + MPPEmbeddingResult *embeddingResult = result.embeddingResults[0]; + AssertEmbeddingResultHasOneEmbedding(embeddingResult); + AssertEmbeddingHasCorrectTypeAndDimension(embeddingResult.embeddings[0], NO, + expectedEmbeddingLength); + + XCTAssertEqual(result.timestampInMilliseconds, timestampInMilliseconds); +} + + (MPPAudioEmbedderResult *)embedAudioClipWithFileInfo:(MPPFileInfo *)fileInfo usingAudioEmbedder:(MPPAudioEmbedder *)audioEmbedder { MPPAudioData *audioData = [[MPPAudioData alloc] initWithFileInfo:fileInfo]; @@ -430,4 +567,15 @@ + (MPPAudioEmbedderResult *)embedAudioClipWithFileInfo:(MPPFileInfo *)fileInfo return result; } ++ (NSArray *)streamedAudioDataListforYamnet { + NSArray *streamedAudioDataList = + [AVAudioFile streamedAudioBlocksFromAudioFileWithInfo:kSpeech16KHzMonoFileInfo + modelSampleCount:kYamnetSampleCount + modelSampleRate:kYamnetSampleRate]; + + XCTAssertEqual(streamedAudioDataList.count, 5); + + return streamedAudioDataList; +} + @end From 8972b29dc35e339aa193d2f5b83bf6c1569df500 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Mon, 14 Oct 2024 21:21:10 +0530 Subject: [PATCH 2/4] Added iOS audio embedder to cocoapods build --- mediapipe/tasks/ios/BUILD | 10 ++++++++++ .../audio/audio_embedder/sources/MPPAudioEmbedder.h | 2 +- .../audio_embedder/sources/MPPAudioEmbedderResult.h | 2 +- .../utils/sources/MPPAudioEmbedderResult+Helpers.mm | 2 +- .../test/audio/audio_embedder/MPPAudioEmbedderTests.mm | 5 +---- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/mediapipe/tasks/ios/BUILD b/mediapipe/tasks/ios/BUILD index 806a52a729..360097ea73 100644 --- a/mediapipe/tasks/ios/BUILD +++ b/mediapipe/tasks/ios/BUILD @@ -71,6 +71,7 @@ TENSORFLOW_LITE_C_DEPS = [ CALCULATORS_AND_GRAPHS = [ "//mediapipe/calculators/core:flow_limiter_calculator", "//mediapipe/tasks/cc/audio/audio_classifier:audio_classifier_graph", + "//mediapipe/tasks/cc/audio/audio_embedder:audio_embedder_graph", "//mediapipe/tasks/cc/text/text_classifier:text_classifier_graph", "//mediapipe/tasks/cc/text/text_embedder:text_embedder_graph", "//mediapipe/tasks/cc/vision/face_detector:face_detector_graph", @@ -116,6 +117,9 @@ strip_api_include_path_prefix( "//mediapipe/tasks/ios/audio/audio_classifier:sources/MPPAudioClassifier.h", "//mediapipe/tasks/ios/audio/audio_classifier:sources/MPPAudioClassifierOptions.h", "//mediapipe/tasks/ios/audio/audio_classifier:sources/MPPAudioClassifierResult.h", + "//mediapipe/tasks/ios/audio/audio_embedder:sources/MPPAudioEmbedder.h", + "//mediapipe/tasks/ios/audio/audio_embedder:sources/MPPAudioEmbedderOptions.h", + "//mediapipe/tasks/ios/audio/audio_embedder:sources/MPPAudioEmbedderResult.h", "//mediapipe/tasks/ios/audio/core:sources/MPPAudioData.h", "//mediapipe/tasks/ios/audio/core:sources/MPPAudioDataFormat.h", "//mediapipe/tasks/ios/audio/core:sources/MPPAudioRecord.h", @@ -194,6 +198,9 @@ apple_static_xcframework( ":MPPAudioClassifier.h", ":MPPAudioClassifierOptions.h", ":MPPAudioClassifierResult.h", + ":MPPAudioEmbedder.h", + ":MPPAudioEmbedderOptions.h", + ":MPPAudioEmbedderResult.h", ":MPPAudioData.h", ":MPPAudioDataFormat.h", ":MPPAudioRecord.h", @@ -202,12 +209,15 @@ apple_static_xcframework( ":MPPCategory.h", ":MPPClassificationResult.h", ":MPPCommon.h", + ":MPPEmbedding.h", + ":MPPEmbeddingResult.h", ":MPPFloatBuffer.h", ":MPPTaskOptions.h", ":MPPTaskResult.h", ], deps = [ "//mediapipe/tasks/ios/audio/audio_classifier:MPPAudioClassifier", + "//mediapipe/tasks/ios/audio/audio_embedder:MPPAudioEmbedder", ], ) diff --git a/mediapipe/tasks/ios/audio/audio_embedder/sources/MPPAudioEmbedder.h b/mediapipe/tasks/ios/audio/audio_embedder/sources/MPPAudioEmbedder.h index 5ba7aa8d67..ec1cacc317 100644 --- a/mediapipe/tasks/ios/audio/audio_embedder/sources/MPPAudioEmbedder.h +++ b/mediapipe/tasks/ios/audio/audio_embedder/sources/MPPAudioEmbedder.h @@ -124,7 +124,7 @@ NS_SWIFT_NAME(AudioEmbedder) - (BOOL)embedAsyncAudioBlock:(MPPAudioData *)audioBlock timestampInMilliseconds:(NSInteger)timestampInMilliseconds error:(NSError **)error - NS_SWIFT_NAME(classifyAsync(audioBlock:timestampInMilliseconds:)); + NS_SWIFT_NAME(embedAsync(audioBlock:timestampInMilliseconds:)); /** * Closes and cleans up the MediaPipe audio embedder. diff --git a/mediapipe/tasks/ios/audio/audio_embedder/sources/MPPAudioEmbedderResult.h b/mediapipe/tasks/ios/audio/audio_embedder/sources/MPPAudioEmbedderResult.h index 497705225a..6fa433ebf4 100644 --- a/mediapipe/tasks/ios/audio/audio_embedder/sources/MPPAudioEmbedderResult.h +++ b/mediapipe/tasks/ios/audio/audio_embedder/sources/MPPAudioEmbedderResult.h @@ -43,7 +43,7 @@ NS_SWIFT_NAME(AudioEmbedderResult) * @return An instance of `AudioEmbedderResult` initialized with the given array of * `EmbeddingResult` objects and timestamp (in milliseconds). */ -- (instancetype)initWithEmbeddingResults:(nullable NSArray *)embeddingResults +- (instancetype)initWithEmbeddingResults:(NSArray *)embeddingResults timestampInMilliseconds:(NSInteger)timestampInMilliseconds; - (instancetype)init NS_UNAVAILABLE; diff --git a/mediapipe/tasks/ios/audio/audio_embedder/utils/sources/MPPAudioEmbedderResult+Helpers.mm b/mediapipe/tasks/ios/audio/audio_embedder/utils/sources/MPPAudioEmbedderResult+Helpers.mm index 7cf565ec02..92d0f7f9f7 100644 --- a/mediapipe/tasks/ios/audio/audio_embedder/utils/sources/MPPAudioEmbedderResult+Helpers.mm +++ b/mediapipe/tasks/ios/audio/audio_embedder/utils/sources/MPPAudioEmbedderResult+Helpers.mm @@ -42,7 +42,7 @@ + (MPPAudioEmbedderResult *)audioEmbedderResultWithEmbeddingResultPacket:(const cppEmbeddingResults = packet.Get>(); } else { // If packet does not contain protobuf of a type expected by the audio embedder. - return [[MPPAudioEmbedderResult alloc] initWithEmbeddingResults:nil + return [[MPPAudioEmbedderResult alloc] initWithEmbeddingResults:@[] timestampInMilliseconds:timestampInMilliseconds]; } diff --git a/mediapipe/tasks/ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm b/mediapipe/tasks/ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm index 6e75e0e0d1..3f6e38b251 100644 --- a/mediapipe/tasks/ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm +++ b/mediapipe/tasks/ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm @@ -406,9 +406,6 @@ - (void)audioEmbedder:(MPPAudioEmbedder *)audioEmbedder error:(NSError *)error { // Can safely test for yamnet results before `audioEmbedder` object tests since only yamnet with // 16khz and 48khz speech files are used for async tests. - - // Returns a `nil` `expectedResult` for the last timestamp to prevent the result from being - // tested. [MPPAudioEmbedderTests assertNonQuantizedAudioEmbedderYamnetStreamModeResult:result timestampInMilliseconds:timestampInMilliseconds @@ -546,7 +543,7 @@ + (void)assertAudioEmbedderResult:(MPPAudioEmbedderResult *)result + (void)assertNonQuantizedAudioEmbedderYamnetStreamModeResult:(MPPAudioEmbedderResult *)result timestampInMilliseconds:(NSInteger)timestampInMilliseconds expectedEmbeddingLength:(NSInteger)expectedEmbeddingLength { - // In stream mode result will only have one embedding result corresponding to the timestamp at + // In stream mode, `result` will only have one embedding result corresponding to the timestamp at // which it was sent for inference. XCTAssertEqual(result.embeddingResults.count, 1); From 6aa6fb5562c93f7c8cbd3cf265f18de5d72ff821 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Mon, 14 Oct 2024 21:32:29 +0530 Subject: [PATCH 3/4] Fixed typos in MPPAudioEmbedderTests --- .../ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mediapipe/tasks/ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm b/mediapipe/tasks/ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm index 3f6e38b251..0dc59df4ae 100644 --- a/mediapipe/tasks/ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm +++ b/mediapipe/tasks/ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm @@ -191,8 +191,8 @@ - (void)testEmbedWithSilenceSucceeds { isQuantized:options.quantize expectedEmbeddingResultsCount:expectedEmbedderResultsCount expectedEmbeddingLength:kExpectedEmbeddingLength]; - std::array expectedEmbeddingValuesSubset = {2.07613f, 0.392721f, 0.543622f}; - constexpr float valueDifferenceTolerance = 3e-6; + const std::array expectedEmbeddingValuesSubset = {2.07613f, 0.392721f, 0.543622f}; + const float valueDifferenceTolerance = 3e-6f; NSArray *floatEmbedding = result.embeddingResults[0].embeddings[0].floatEmbedding; for (int i; i < expectedEmbeddingValuesSubset.size(); i++) { From 4e6a57f1b7dd964be9aa7ff2684fcfa2c927d619 Mon Sep 17 00:00:00 2001 From: Prianka Liz Kariat Date: Mon, 14 Oct 2024 21:34:23 +0530 Subject: [PATCH 4/4] Fixed typos in MPPAudioEmbedderTests --- .../ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediapipe/tasks/ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm b/mediapipe/tasks/ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm index 0dc59df4ae..1665b348df 100644 --- a/mediapipe/tasks/ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm +++ b/mediapipe/tasks/ios/test/audio/audio_embedder/MPPAudioEmbedderTests.mm @@ -195,7 +195,7 @@ - (void)testEmbedWithSilenceSucceeds { const float valueDifferenceTolerance = 3e-6f; NSArray *floatEmbedding = result.embeddingResults[0].embeddings[0].floatEmbedding; - for (int i; i < expectedEmbeddingValuesSubset.size(); i++) { + for (int i = 0; i < expectedEmbeddingValuesSubset.size(); i++) { XCTAssertEqualWithAccuracy(expectedEmbeddingValuesSubset[i], floatEmbedding[i].floatValue, valueDifferenceTolerance); }