Updated multiply function in iOS Image Segmenter tests to use C++ vectors

This commit is contained in:
Prianka Liz Kariat 2023-09-15 14:21:33 +05:30
parent fad7f9cdb4
commit b3be1418da

View File

@ -20,6 +20,9 @@
#import "mediapipe/tasks/ios/vision/image_segmenter/sources/MPPImageSegmenter.h" #import "mediapipe/tasks/ios/vision/image_segmenter/sources/MPPImageSegmenter.h"
#import "mediapipe/tasks/ios/vision/image_segmenter/sources/MPPImageSegmenterResult.h" #import "mediapipe/tasks/ios/vision/image_segmenter/sources/MPPImageSegmenterResult.h"
#include <vector>
#include <iostream>
static MPPFileInfo *const kCatImageFileInfo = [[MPPFileInfo alloc] initWithName:@"cat" static MPPFileInfo *const kCatImageFileInfo = [[MPPFileInfo alloc] initWithName:@"cat"
type:@"jpg"]; type:@"jpg"];
static MPPFileInfo *const kCatGoldenImageFileInfo = [[MPPFileInfo alloc] initWithName:@"cat_mask" static MPPFileInfo *const kCatGoldenImageFileInfo = [[MPPFileInfo alloc] initWithName:@"cat_mask"
@ -34,39 +37,36 @@ static NSString *const kExpectedErrorDomain = @"com.google.mediapipe.tasks";
constexpr float kSimilarityThreshold = 0.96f; constexpr float kSimilarityThreshold = 0.96f;
constexpr NSInteger kMagnificationFactor = 10; constexpr NSInteger kMagnificationFactor = 10;
double sum(const float *mask, size_t size) { double sum(const std::vector<float>& mask) {
double sum = 0.0; double sum = 0.0;
for (int i = 0; i < size; i++) { for (const float &maskElement : mask) {
sum += mask[i]; sum += maskElement;
} }
return sum; return sum;
} }
float *multiply(const float *mask1, const float *mask2, size_t size) { std::vector<float> multiply(const float *mask1, const float *mask2, size_t size) {
double sum = 0.0; double sum = 0.0;
float *multipliedMask = (float *)malloc(size * sizeof(float));
if (!multipliedMask) { std::vector<float> multipliedMask;
exit(-1); multipliedMask.reserve(size);
}
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
multipliedMask[i] = mask1[i] * mask2[i]; multipliedMask.push_back(mask1[i] * mask2[i]);
} }
return multipliedMask; return multipliedMask;
} }
double softIOU(const float *mask1, const float *mask2, size_t size) { double softIOU(const float *mask1, const float *mask2, size_t size) {
float *interSectionVector = multiply(mask1, mask2, size); std::vector<float> interSectionVector = multiply(mask1, mask2, size);
double interSectionSum = sum(interSectionVector, size); double interSectionSum = sum(interSectionVector);
free(interSectionVector);
float *m1m1Vector = multiply(mask1, mask1, size); std::vector<float> m1m1Vector = multiply(mask1, mask1, size);
double m1m1 = sum(m1m1Vector, size); double m1m1 = sum(m1m1Vector);
free(m1m1Vector);
float *m2m2Vector = multiply(mask2, mask2, size); std::vector<float> m2m2Vector = multiply(mask2, mask2, size);
double m2m2 = sum(m2m2Vector, size); double m2m2 = sum(m2m2Vector);
free(m2m2Vector);
double unionSum = m1m1 + m2m2 - interSectionSum; double unionSum = m1m1 + m2m2 - interSectionSum;