Added iOS result containers for classification tasks

This commit is contained in:
Prianka Liz Kariat 2023-01-06 16:07:11 +05:30
parent 667fd81ddc
commit 33df6c042f
5 changed files with 300 additions and 0 deletions

View File

@ -0,0 +1,32 @@
# Copyright 2023 The MediaPipe Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
package(default_visibility = ["//mediapipe/tasks:internal"])
licenses(["notice"])
objc_library(
name = "MPPCategory",
srcs = ["sources/MPPCategory.m"],
hdrs = ["sources/MPPCategory.h"],
)
objc_library(
name = "MPPClassificationResult",
srcs = ["sources/MPPClassificationResult.m"],
hdrs = ["sources/MPPClassificationResult.h"],
deps = [
":MPPCategory",
],
)

View File

@ -0,0 +1,68 @@
// Copyright 2023 The MediaPipe Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
/**
* Category is a util class that contains a label, its display name, a float value as score, and the
* index of the label in the corresponding label file. Typically it's used as the result of
* classification tasks.
**/
NS_SWIFT_NAME(ClassificationCategory)
@interface MPPCategory : NSObject
/**
* The index of the label in the corresponding label file. Set to -1 if the index is
* not set.
**/
@property(nonatomic, readonly) NSInteger index;
/** Confidence score for this class . **/
@property(nonatomic, readonly) float score;
/** The label of this category object. **/
@property(nonatomic, readonly, nullable) NSString *categoryName;
/**
* The display name of the label, which may be translated for different locales. For example, a
* label, "apple", may be translated into Spanish for display purpose, so that the display name is
* "manzana".
**/
@property(nonatomic, readonly, nullable) NSString *displayName;
/**
* Initializes a new `MPPCategory` with the given index, score, category name and display name.
*
* @param index The index of the label in the corresponding label file.
* @param score The probability score of this label category.
* @param categoryName The label of this category object.
* @param displayName The display name of the label.
*
* @return An instance of `MPPCategory` initialized with the given index, score, category name and
* display name.
**/
- (instancetype)initWithIndex:(NSInteger)index
score:(float)score
categoryName:(nullable NSString *)categoryName
displayName:(nullable NSString *)displayName NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,33 @@
// Copyright 2023 The MediaPipe Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "mediapipe/tasks/ios/components/containers/sources/MPPCategory.h"
@implementation MPPCategory
- (instancetype)initWithIndex:(NSInteger)index
score:(float)score
categoryName:(nullable NSString *)categoryName
displayName:(nullable NSString *)displayName {
self = [super init];
if (self) {
_index = index;
_score = score;
_categoryName = categoryName;
_displayName = displayName;
}
return self;
}
@end

View File

@ -0,0 +1,116 @@
// Copyright 2023 The MediaPipe Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import <Foundation/Foundation.h>
#import "mediapipe/tasks/ios/components/containers/sources/MPPCategory.h"
NS_ASSUME_NONNULL_BEGIN
/**
* Represents the list of classification for a given classifier head. Typically used as a result
* for classification tasks.
**/
NS_SWIFT_NAME(Classifications)
@interface MPPClassifications : NSObject
/**
* The index of the classifier head these entries refer to. This is useful for multi-head models.
**/
@property(nonatomic, readonly) NSInteger headIndex;
/** The optional name of the classifier head, which is the corresponding tensor metadata name. **/
@property(nonatomic, readonly, nullable) NSString *headName;
/** An array of `MPPCategory` objects containing the predicted categories. **/
@property(nonatomic, readonly) NSArray<MPPCategory *> *categories;
/**
* Initializes a new `MPPClassifications` object with the given head index and array of categories.
* Head name is initialized to `nil`.
*
* @param headIndex The index of the classifier head.
* @param categories An array of `MPPCategory` objects containing the predicted categories.
*
* @return An instance of `MPPClassifications` initialized with the given head index and
* array of categories.
**/
- (instancetype)initWithHeadIndex:(NSInteger)headIndex
categories:(NSArray<MPPCategory *> *)categories;
/**
* Initializes a new `MPPClassifications` with the given head index, head name and array of
* categories.
*
* @param headIndex The index of the classifier head.
* @param headName The name of the classifier head, which is the corresponding tensor metadata
* name.
* @param categories An array of `MPPCategory` objects containing the predicted categories.
*
* @return An object of `MPPClassifications` initialized with the given head index, head name and
* array of categories.
**/
- (instancetype)initWithHeadIndex:(NSInteger)headIndex
headName:(nullable NSString *)headName
categories:(NSArray<MPPCategory *> *)categories NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
@end
/**
* Represents the classification results of a model. Typically used as a result for classification
* tasks.
**/
NS_SWIFT_NAME(ClassificationResult)
@interface MPPClassificationResult : NSObject
/**
* An Array of `MPPClassifications` objects containing the predicted categories for each head of
* the model.
**/
@property(nonatomic, readonly) NSArray<MPPClassifications *> *classifications;
/**
* The optional timestamp (in milliseconds) of the start of the chunk of data corresponding to
* these results. If it is set to the value -1, it signifies the absence of a timestamp. This is
* only used for classification on time series (e.g. audio classification). In these use cases, the
* amount of data to process might exceed the maximum size that the model can process: to solve
* this, the input data is split into multiple chunks starting at different timestamps.
**/
@property(nonatomic, readonly) NSInteger timestampMs;
/**
* Initializes a new `MPPClassificationResult` with the given array of classifications and time
* stamp (in milliseconds).
*
* @param classifications An Array of `MPPClassifications` objects containing the predicted
* categories for each head of the model.
* @param timestampMs The timestamp (in milliseconds) of the start of the chunk of data
* corresponding to these results.
*
* @return An instance of `MPPClassificationResult` initialized with the given array of
* classifications and timestampMs.
**/
- (instancetype)initWithClassifications:(NSArray<MPPClassifications *> *)classifications
timestampMs:(NSInteger)timestampMs NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
@end
NS_ASSUME_NONNULL_END

View File

@ -0,0 +1,51 @@
// Copyright 2023 The MediaPipe Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "mediapipe/tasks/ios/components/containers/sources/MPPClassificationResult.h"
@implementation MPPClassifications
- (instancetype)initWithHeadIndex:(NSInteger)headIndex
headName:(nullable NSString *)headName
categories:(NSArray<MPPCategory *> *)categories {
self = [super init];
if (self) {
_headIndex = headIndex;
_headName = headName;
_categories = categories;
}
return self;
}
- (instancetype)initWithHeadIndex:(NSInteger)headIndex
categories:(NSArray<MPPCategory *> *)categories {
return [self initWithHeadIndex:headIndex headName:nil categories:categories];
}
@end
@implementation MPPClassificationResult
- (instancetype)initWithClassifications:(NSArray<MPPClassifications *> *)classifications
timestampMs:(NSInteger)timestampMs {
self = [super init];
if (self) {
_classifications = classifications;
_timestampMs = timestampMs;
}
return self;
}
@end