Updated object detector test

This commit is contained in:
Prianka Liz Kariat 2023-04-06 21:35:03 +05:30
parent 1c0e17ea17
commit 21551a846f

View File

@ -20,10 +20,12 @@
static NSString *const kModelName = @"coco_ssd_mobilenet_v1_1.0_quant_2018_06_29"; static NSString *const kModelName = @"coco_ssd_mobilenet_v1_1.0_quant_2018_06_29";
static NSDictionary *const kCatsAndDogsImage = @{@"name" : @"cats_and_dogs", @"type" : @"jpg"}; static NSDictionary *const kCatsAndDogsImage = @{@"name" : @"cats_and_dogs", @"type" : @"jpg"};
static NSDictionary *const kCatsAndDogsRotatedImage = @{@"name" : @"cats_and_dogs_rotated", @"type" : @"jpg"}; static NSDictionary *const kCatsAndDogsRotatedImage =
@{@"name" : @"cats_and_dogs_rotated", @"type" : @"jpg"};
static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks"; static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
#define PixelDifferenceTolerance 5.0f #define PixelDifferenceTolerance 5.0f
#define ScoreDifferenceTolerance 1e-2f
#define AssertEqualErrors(error, expectedError) \ #define AssertEqualErrors(error, expectedError) \
XCTAssertNotNil(error); \ XCTAssertNotNil(error); \
@ -33,40 +35,51 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
[error.localizedDescription rangeOfString:expectedError.localizedDescription].location, \ [error.localizedDescription rangeOfString:expectedError.localizedDescription].location, \
NSNotFound) NSNotFound)
#define AssertEqualCategoryArrays(categories, expectedCategories) \ #define AssertEqualCategoryArrays(categories, expectedCategories, detectionIndex) \
XCTAssertEqual(categories.count, expectedCategories.count); \ XCTAssertEqual(categories.count, expectedCategories.count); \
for (int i = 0; i < categories.count; i++) { \ for (int j = 0; j < categories.count; j++) { \
XCTAssertEqual(categories[i].index, expectedCategories[i].index, @"categories index i = %d", i); \ XCTAssertEqual(categories[j].index, expectedCategories[j].index, \
XCTAssertEqualWithAccuracy(categories[i].score, expectedCategories[i].score, 1e-3, \ @"detection Index = %d category array index j = %d", detectionIndex, j); \
@"categories index i = %d", i); \ XCTAssertEqualWithAccuracy( \
XCTAssertEqualObjects(categories[i].categoryName, expectedCategories[i].categoryName, \ categories[j].score, expectedCategories[j].score, ScoreDifferenceTolerance, \
@"categories index i = %d", i); \ @"detection Index = %d, category array index j = %d", detectionIndex, j); \
XCTAssertEqualObjects(categories[i].displayName, expectedCategories[i].displayName, \ XCTAssertEqualObjects(categories[j].categoryName, expectedCategories[j].categoryName, \
@"categories index i = %d", i); \ @"detection Index = %d, category array index j = %d", detectionIndex, \
j); \
XCTAssertEqualObjects(categories[j].displayName, expectedCategories[j].displayName, \
@"detection Index = %d, category array index j = %d", detectionIndex, \
j); \
\
\
} }
#define AssertApproximatelyEqualBoundingBoxes(boundingBox, expectedBoundingBox, idx) \ #define AssertApproximatelyEqualBoundingBoxes(boundingBox, expectedBoundingBox, idx) \
XCTAssertEqualWithAccuracy(boundingBox.origin.x, expectedBoundingBox.origin.x, PixelDifferenceTolerance, @"index i = %d", idx); \ XCTAssertEqualWithAccuracy(boundingBox.origin.x, expectedBoundingBox.origin.x, \
XCTAssertEqualWithAccuracy(boundingBox.origin.y, expectedBoundingBox.origin.y, PixelDifferenceTolerance, @"index i = %d", idx); \ PixelDifferenceTolerance, @"index i = %d", idx); \
XCTAssertEqualWithAccuracy(boundingBox.size.width, expectedBoundingBox.size.width, PixelDifferenceTolerance, @"index i = %d", idx); \ XCTAssertEqualWithAccuracy(boundingBox.origin.y, expectedBoundingBox.origin.y, \
XCTAssertEqualWithAccuracy(boundingBox.size.height, expectedBoundingBox.size.height, PixelDifferenceTolerance, @"index i = %d", idx); PixelDifferenceTolerance, @"index i = %d", idx); \
XCTAssertEqualWithAccuracy(boundingBox.size.width, expectedBoundingBox.size.width, \
PixelDifferenceTolerance, @"index i = %d", idx); \
XCTAssertEqualWithAccuracy(boundingBox.size.height, expectedBoundingBox.size.height, \
PixelDifferenceTolerance, @"index i = %d", idx);
#define AssertEqualDetections(detection, expectedDetection, idx) \ #define AssertEqualDetections(detection, expectedDetection, idx) \
XCTAssertNotNil(detection); \ XCTAssertNotNil(detection); \
XCTAssertNil(detection.keypoints, @"index i = %d", idx) \ AssertEqualCategoryArrays(detection.categories, expectedDetection.categories, idx); \
AssertEqualCategoryArrays(detection.categories, expectedDetection.categories) \
AssertApproximatelyEqualBoundingBoxes(detection.boundingBox, expectedDetection.boundingBox, idx); AssertApproximatelyEqualBoundingBoxes(detection.boundingBox, expectedDetection.boundingBox, idx);
#define AssertEqualDetectionArrays(detections, expectedDetections) \ #define AssertEqualDetectionArrays(detections, expectedDetections) \
XCTAssertEqual(detections.count, expectedDetections.count); \ XCTAssertEqual(detections.count, expectedDetections.count); \
for (int i = 0; i < detections.count; i++) { \ for (int i = 0; i < detections.count; i++) { \
AssertEqualDetections(detections[i], expectedDetections[i], i) \ AssertEqualDetections(detections[i], expectedDetections[i], i); \
} }
#define AssertEqualObjectDetectionResults(objectDetectionResult, expectedObjectDetectionResult) \ #define AssertEqualObjectDetectionResults(objectDetectionResult, expectedObjectDetectionResult) \
XCTAssertNotNil(objectDetectionResult); XCTAssertNotNil(objectDetectionResult); \
// AssertEqualDetectionArrays(objectDetectionResult.detections, expectedObjectDetectionResult.detections) \ \
// XCTAssertEqual(objectDetectionResult.timestampMs, expectedObjectDetectionResult.timestampMs) AssertEqualDetectionArrays(objectDetectionResult.detections, \
expectedObjectDetectionResult.detections); \
XCTAssertEqual(objectDetectionResult.timestampMs, expectedObjectDetectionResult.timestampMs);
@interface MPPObjectDetectorTests : XCTestCase @interface MPPObjectDetectorTests : XCTestCase
@end @end
@ -75,37 +88,32 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
#pragma mark Results #pragma mark Results
+ (MPPObjectDetectionResult*)expectedDetectionResultForCatsAndDogsImageWithTimestampMs:(NSInteger)timestampMs { + (MPPObjectDetectionResult *)expectedDetectionResultForCatsAndDogsImageWithTimestampMs:
(NSInteger)timestampMs {
NSArray<MPPDetection *> * detections = @[ NSArray<MPPDetection *> *detections = @[
[[MPPDetection alloc] initWithCategories:@[ [[MPPDetection alloc] initWithCategories:@[
[[MPPCategory alloc] initWithIndex:-1 [[MPPCategory alloc] initWithIndex:-1 score:0.69921875f categoryName:@"cat" displayName:nil],
score:0.69921875f ]
categoryName:@"cat" boundingBox:CGRectMake(608, 161, 381, 439)
displayName:nil], keypoints:nil],
] boundingBox:CGRectMake(608, 161, 381, 439) keypoints:nil],
[[MPPDetection alloc] initWithCategories:@[ [[MPPDetection alloc] initWithCategories:@[
[[MPPCategory alloc] initWithIndex:-1 [[MPPCategory alloc] initWithIndex:-1 score:0.656250f categoryName:@"cat" displayName:nil],
score:0.64453125f ]
categoryName:@"cat" boundingBox:CGRectMake(57, 398, 392, 196)
displayName:nil], keypoints:nil],
] boundingBox:CGRectMake(60, 398, 386, 196) keypoints:nil],
[[MPPDetection alloc] initWithCategories:@[ [[MPPDetection alloc] initWithCategories:@[
[[MPPCategory alloc] initWithIndex:-1 [[MPPCategory alloc] initWithIndex:-1 score:0.51171875f categoryName:@"cat" displayName:nil],
score:0.51171875f ]
categoryName:@"cat" boundingBox:CGRectMake(257, 395, 173, 202)
displayName:nil], keypoints:nil],
] boundingBox:CGRectMake(256, 395, 173, 202) keypoints:nil],
[[MPPDetection alloc] initWithCategories:@[ [[MPPDetection alloc] initWithCategories:@[
[[MPPCategory alloc] initWithIndex:-1 [[MPPCategory alloc] initWithIndex:-1 score:0.48828125f categoryName:@"cat" displayName:nil],
score:0.48828125f ]
categoryName:@"cat" boundingBox:CGRectMake(363, 195, 330, 412)
displayName:nil], keypoints:nil],
] boundingBox:CGRectMake(362, 191, 325, 419) keypoints:nil],
]; ];
MPPObjectDetectionResult *objectDetectionResult = [[MPPObjectDetectionResult alloc] initWithDetections:detections timestampMs:timestampMs]; return [[MPPObjectDetectionResult alloc] initWithDetections:detections timestampMs:timestampMs];
} }
#pragma mark File #pragma mark File
@ -158,22 +166,25 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
} }
- (void)assertResultsOfDetectInImage:(MPPImage *)mppImage - (void)assertResultsOfDetectInImage:(MPPImage *)mppImage
usingObjectDetector:(MPPObjectDetector *)objectDetector usingObjectDetector:(MPPObjectDetector *)objectDetector
equalsObjectDetectionResult:(MPPObjectDetectionResult *)expectedObjectDetectionResult { equalsObjectDetectionResult:(MPPObjectDetectionResult *)expectedObjectDetectionResult {
MPPObjectDetectionResult *objectDetectionResult = [objectDetector detectInImage:mppImage MPPObjectDetectionResult *objectDetectionResult = [objectDetector detectInImage:mppImage
error:nil]; error:nil];
AssertEqualObjectDetectionResults(objectDetectionResult, [MPPObjectDetectorTests expectedDetectionResultForCatsAndDogsImageWithTimestampMs:0]); AssertEqualObjectDetectionResults(
objectDetectionResult,
[MPPObjectDetectorTests expectedDetectionResultForCatsAndDogsImageWithTimestampMs:0]);
} }
- (void)assertResultsOfDetectInImageWithFileInfo:(NSDictionary *)fileInfo - (void)assertResultsOfDetectInImageWithFileInfo:(NSDictionary *)fileInfo
usingObjectDetector:(MPPObjectDetector *)objectDetector usingObjectDetector:(MPPObjectDetector *)objectDetector
equalsObjectDetectionResult:(MPPObjectDetectionResult *)expectedObjectDetectionResult { equalsObjectDetectionResult:
(MPPObjectDetectionResult *)expectedObjectDetectionResult {
MPPImage *mppImage = [self imageWithFileInfo:fileInfo]; MPPImage *mppImage = [self imageWithFileInfo:fileInfo];
[self assertResultsOfDetectInImage:mppImage [self assertResultsOfDetectInImage:mppImage
usingObjectDetector:objectDetector usingObjectDetector:objectDetector
equalsObjectDetectionResult:expectedObjectDetectionResult]; equalsObjectDetectionResult:expectedObjectDetectionResult];
} }
#pragma mark General Tests #pragma mark General Tests
@ -186,12 +197,11 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
MPPObjectDetector *objectDetector = [self objectDetectorWithOptionsSucceeds:options]; MPPObjectDetector *objectDetector = [self objectDetectorWithOptionsSucceeds:options];
[self [self assertResultsOfDetectInImageWithFileInfo:kCatsAndDogsImage
assertResultsOfDetectInImageWithFileInfo:kCatsAndDogsImage usingObjectDetector:objectDetector
usingObjectDetector:objectDetector equalsObjectDetectionResult:
equalsObjectDetectionResult: [MPPObjectDetectorTests
[MPPObjectDetectorTests expectedDetectionResultForCatsAndDogsImageWithTimestampMs:0]];
expectedDetectionResultForCatsAndDogsImageWithTimestampMs:0]];
} }
@end @end