@@ -46,7 +46,6 @@ - (NSDictionary *)postprocess:(NSArray *)output {
46
46
NSAssert (outputSize == numLabels * numModelPixels,
47
47
@" Model generated unexpected output size." );
48
48
49
-
50
49
// For each label extract it's matrix and rescale it to the original size
51
50
std::vector<cv::Mat> resizedLabelScores (numLabels);
52
51
for (std::size_t label = 0 ; label < numLabels; ++label) {
@@ -61,6 +60,8 @@ - (NSDictionary *)postprocess:(NSArray *)output {
61
60
cv::resize (labelMat, resizedLabelScores[label], originalSize);
62
61
}
63
62
63
+ cv::Mat maxArg = cv::Mat (originalSize, CV_32S);
64
+
64
65
// For each pixel apply softmax across all the labels
65
66
for (std::size_t pixel = 0 ; pixel < numOriginalPixels; ++pixel) {
66
67
int row = pixel / originalSize.width ;
@@ -73,26 +74,30 @@ - (NSDictionary *)postprocess:(NSArray *)output {
73
74
74
75
std::vector<double > adjustedScores = softmax (scores);
75
76
77
+ std::size_t maxArgIndex = 0 ;
78
+ double maxArgVal = 0 ;
76
79
for (std::size_t label = 0 ; label < numLabels; ++label) {
77
80
resizedLabelScores[label].at <double >(row, col) = adjustedScores[label];
81
+ if (adjustedScores[label] > maxArgVal) {
82
+ maxArgIndex = label;
83
+ maxArgVal = adjustedScores[label];
84
+ }
78
85
}
86
+
87
+ maxArg.at <int >(row, col) = maxArgIndex;
79
88
}
80
89
81
90
NSMutableDictionary *result = [NSMutableDictionary dictionary ];
82
91
92
+ // Convert to NSArray and populate the final dictionary
83
93
for (std::size_t label = 0 ; label < numLabels; ++label) {
84
94
NSString *labelString = @(deeplabv3_resnet50_labels[label].c_str ());
85
- NSMutableArray *arr = [[NSMutableArray alloc ] initWithCapacity: numOriginalPixels];
86
-
87
- for (std::size_t x = 0 ; x < originalSize.height ; ++x) {
88
- for (std::size_t y = 0 ; y < originalSize.width ; ++y) {
89
- arr[x * originalSize.width + y] = @(resizedLabelScores[label].at <double >(x, y));
90
- }
91
- }
92
-
95
+ NSMutableArray *arr = matToNSArray<double >(resizedLabelScores[label]);
93
96
result[labelString] = arr;
94
97
}
95
98
99
+ result[@" argmax" ] = matToNSArray<int >(maxArg);
100
+
96
101
return result;
97
102
}
98
103
0 commit comments