@@ -37,50 +37,43 @@ - (NSArray *)preprocess:(cv::Mat &)input {
37
37
return modelInput;
38
38
}
39
39
40
- - (NSDictionary *)postprocess : (NSArray *)output
41
- returnClasses : (NSArray *)classesOfInterest {
42
- cv::Size modelImageSize = [self getModelImageSize ];
43
-
44
- std::size_t numLabels = deeplabv3_resnet50_labels.size ();
40
+ std::vector<cv::Mat> rescaleResults (NSArray *result, std::size_t numLabels,
41
+ cv::Size modelImageSize, cv::Size originalSize) {
45
42
std::size_t numModelPixels = modelImageSize.height * modelImageSize.width ;
46
- std::size_t numOriginalPixels = originalSize.height * originalSize.width ;
47
- std::size_t outputSize = (std::size_t )output.count ;
48
-
49
- NSAssert (outputSize == numLabels * numModelPixels,
50
- @" Model generated unexpected output size." );
51
43
52
- // For each label extract it's matrix and rescale it to the original size
53
44
std::vector<cv::Mat> resizedLabelScores (numLabels);
54
45
for (std::size_t label = 0 ; label < numLabels; ++label) {
55
46
cv::Mat labelMat = cv::Mat (modelImageSize, CV_64F);
56
47
57
48
for (std::size_t pixel = 0 ; pixel < numModelPixels; ++pixel){
58
49
int row = pixel / modelImageSize.width ;
59
50
int col = pixel % modelImageSize.width ;
60
- labelMat.at <double >(row, col) = [output [label * numModelPixels + pixel] doubleValue ];
51
+ labelMat.at <double >(row, col) = [result [label * numModelPixels + pixel] doubleValue ];
61
52
}
62
53
63
54
cv::resize (labelMat, resizedLabelScores[label], originalSize);
64
55
}
56
+ return resizedLabelScores;
57
+ }
65
58
66
- cv::Mat maxArg = cv::Mat (originalSize, CV_32S);
67
-
68
- // For each pixel apply softmax across all the labels
59
+ void adjustScoresPerPixel (std::vector< cv::Mat>& labelScores, cv::Mat& maxArg,
60
+ cv:: Size originalSize, std:: size_t numLabels) {
61
+ std:: size_t numOriginalPixels = originalSize. height * originalSize. width ;
69
62
for (std::size_t pixel = 0 ; pixel < numOriginalPixels; ++pixel) {
70
63
int row = pixel / originalSize.width ;
71
64
int col = pixel % originalSize.width ;
72
65
std::vector<double > scores;
73
66
scores.reserve (numLabels);
74
- for (const cv::Mat& mat : resizedLabelScores ) {
67
+ for (const cv::Mat& mat : labelScores ) {
75
68
scores.push_back (mat.at <double >(row, col));
76
69
}
77
-
70
+
78
71
std::vector<double > adjustedScores = softmax (scores);
79
-
72
+
80
73
std::size_t maxArgIndex = 0 ;
81
74
double maxArgVal = 0 ;
82
75
for (std::size_t label = 0 ; label < numLabels; ++label) {
83
- resizedLabelScores [label].at <double >(row, col) = adjustedScores[label];
76
+ labelScores [label].at <double >(row, col) = adjustedScores[label];
84
77
if (adjustedScores[label] > maxArgVal) {
85
78
maxArgIndex = label;
86
79
maxArgVal = adjustedScores[label];
@@ -89,6 +82,25 @@ - (NSDictionary *)postprocess:(NSArray *)output
89
82
90
83
maxArg.at <int >(row, col) = maxArgIndex;
91
84
}
85
+ }
86
+
87
+ - (NSDictionary *)postprocess : (NSArray *)output
88
+ returnClasses : (NSArray *)classesOfInterest {
89
+ cv::Size modelImageSize = [self getModelImageSize ];
90
+
91
+ std::size_t numLabels = deeplabv3_resnet50_labels.size ();
92
+
93
+ NSAssert ((std::size_t )output.count == numLabels * modelImageSize.height * modelImageSize.width,
94
+ @"Model generated unexpected output size.");
95
+
96
+ // For each label extract it's matrix and rescale it to the original size
97
+ std::vector<cv::Mat> resizedLabelScores =
98
+ rescaleResults (output, numLabels, modelImageSize, originalSize);
99
+
100
+ cv::Mat maxArg = cv::Mat (originalSize, CV_32S);
101
+
102
+ // For each pixel apply softmax across all the labels and calculate the maxArg
103
+ adjustScoresPerPixel (resizedLabelScores, maxArg, originalSize, numLabels);
92
104
93
105
std::unordered_set<std::string> labelSet;
94
106
0 commit comments