Skip to content

Commit 203fd0c

Browse files
Merge pull request #709 from khanhlvg:ios-image-classifier-impl
PiperOrigin-RevId: 411099262
2 parents aed9982 + 1e9e188 commit 203fd0c

28 files changed

+1257
-86
lines changed

tensorflow_lite_support/c/task/processor/classification_options.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ typedef struct TfLiteStringArrayOption {
3030
int length;
3131

3232
// Array of C strings.
33-
const char** list;
33+
char** list;
3434
} TfLiteStringArrayOption;
3535

3636
// Holds settings for any single classification task.
@@ -47,7 +47,7 @@ typedef struct TfLiteClassificationOptions {
4747

4848
// The locale to use for display names specified through the TFLite Model
4949
// Metadata, if any. Defaults to English.
50-
const char* display_names_local;
50+
char* display_names_local;
5151

5252
// The maximum number of top-scored classification results to return. If < 0,
5353
// all available results will be returned. If 0, an invalid argument error is

tensorflow_lite_support/c/test/task/vision/image_classifier_test.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ TEST_F(ImageClassifierFromOptionsTest,
133133
TfLiteImageClassifierOptions options = TfLiteImageClassifierOptionsCreate();
134134
options.base_options.model_file.file_path = model_path.data();
135135

136-
const char* label_denylist[] = {"brambling"};
136+
char* label_denylist[9] = {(char*)"brambling"};
137137
options.classification_options.label_denylist.list = label_denylist;
138138
options.classification_options.label_denylist.length = 1;
139139

140-
const char* label_allowlist[] = {"cheeseburger"};
140+
char* label_allowlist[12] = {(char*)"cheeseburger"};
141141
options.classification_options.label_allowlist.list = label_allowlist;
142142
options.classification_options.label_allowlist.length = 1;
143143

@@ -329,15 +329,15 @@ TEST_F(ImageClassifierClassifyTest, FailsWithRoiOutsideImageBoundsAndError) {
329329

330330
TEST(ImageClassifierWithUserDefinedOptionsClassifyTest,
331331
SucceedsWithClassNameDenyList) {
332-
const char* denylisted_label_name = "cheeseburger";
332+
char* denylisted_label_name = (char*)"cheeseburger";
333333
std::string model_path =
334334
JoinPath("./" /*test src dir*/, kTestDataDirectory,
335335
kMobileNetQuantizedWithMetadata);
336336

337337
TfLiteImageClassifierOptions options = TfLiteImageClassifierOptionsCreate();
338338
options.base_options.model_file.file_path = model_path.data();
339339

340-
const char* label_denylist[] = {denylisted_label_name};
340+
char* label_denylist[12] = {denylisted_label_name};
341341
options.classification_options.label_denylist.list = label_denylist;
342342
options.classification_options.label_denylist.length = 1;
343343

@@ -373,7 +373,7 @@ TEST(ImageClassifierWithUserDefinedOptionsClassifyTest,
373373

374374
TEST(ImageClassifierWithUserDefinedOptionsClassifyTest,
375375
SucceedsWithClassNameAllowList) {
376-
const char* allowlisted_label_name = "cheeseburger";
376+
char* allowlisted_label_name = (char*)"cheeseburger";
377377
std::string model_path =
378378
JoinPath("./" /*test src dir*/, kTestDataDirectory,
379379
kMobileNetQuantizedWithMetadata)
@@ -382,7 +382,7 @@ TEST(ImageClassifierWithUserDefinedOptionsClassifyTest,
382382
TfLiteImageClassifierOptions options = TfLiteImageClassifierOptionsCreate();
383383
options.base_options.model_file.file_path = model_path.data();
384384

385-
const char* label_allowlist[] = {allowlisted_label_name};
385+
char* label_allowlist[12] = {allowlisted_label_name};
386386
options.classification_options.label_allowlist.list = label_allowlist;
387387
options.classification_options.label_allowlist.length = 1;
388388

187 KB
Loading

tensorflow_lite_support/ios/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ objc_library(
9393

9494
objc_library(
9595
name = "TFLCommonUtils",
96+
srcs = [
97+
"sources/TFLCommonUtils.m",
98+
],
9699
hdrs = [
97100
"sources/TFLCommonUtils.h",
98101
],
@@ -102,5 +105,6 @@ objc_library(
102105
],
103106
deps = [
104107
"//tensorflow_lite_support/c:common",
108+
"//tensorflow_lite_support/ios:TFLCommon",
105109
],
106110
)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#import "tensorflow_lite_support/ios/sources/TFLCommonUtils.h"
16+
#import "tensorflow_lite_support/ios/sources/TFLCommon.h"
17+
18+
/** Error domain of TensorFlow Lite Support related errors. */
19+
static NSString *const TFLSupportTaskErrorDomain = @"org.tensorflow.lite.tasks";
20+
21+
@implementation TFLCommonUtils
22+
23+
+ (void)customErrorWithCode:(NSInteger)code
24+
description:(NSString *)description
25+
error:(NSError **)error {
26+
if (error)
27+
*error = [NSError errorWithDomain:TFLSupportTaskErrorDomain
28+
code:code
29+
userInfo:@{NSLocalizedDescriptionKey : description}];
30+
}
31+
32+
+ (void)errorFromTfLiteSupportError:(TfLiteSupportError *)supportError error:(NSError **)error {
33+
if (supportError && error)
34+
*error = [NSError
35+
errorWithDomain:TFLSupportTaskErrorDomain
36+
code:supportError->code
37+
userInfo:@{
38+
NSLocalizedDescriptionKey : [NSString stringWithCString:supportError->message
39+
encoding:NSUTF8StringEncoding]
40+
}];
41+
}
42+
43+
+ (void *)mallocWithSize:(size_t)memSize error:(NSError **)error {
44+
if (!memSize) {
45+
[TFLCommonUtils customErrorWithCode:TFLSupportErrorCodeInvalidArgumentError
46+
description:@"Invalid memory size passed for allocation of object."
47+
error:error];
48+
return NULL;
49+
}
50+
51+
void *allocedMemory = malloc(memSize);
52+
if (!allocedMemory && memSize) {
53+
exit(-1);
54+
}
55+
56+
return allocedMemory;
57+
}
58+
59+
@end

tensorflow_lite_support/ios/task/core/BUILD

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,26 @@ package(
55

66
objc_library(
77
name = "TFLBaseOptions",
8+
srcs = [
9+
"sources/TFLBaseOptions.m",
10+
],
811
hdrs = [
9-
"apis/TFLBaseOptions.h",
12+
"sources/TFLBaseOptions.h",
1013
],
1114
module_name = "TFLBaseOptions",
1215
)
16+
17+
objc_library(
18+
name = "TFLBaseOptionsHelpers",
19+
srcs = [
20+
"sources/TFLBaseOptions+Helpers.m",
21+
],
22+
hdrs = [
23+
"sources/TFLBaseOptions+Helpers.h",
24+
],
25+
module_name = "TFLBaseOptionsHelpers",
26+
deps = [
27+
"//tensorflow_lite_support/c/task/core:base_options",
28+
"//tensorflow_lite_support/ios/task/core:TFLBaseOptions",
29+
],
30+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
#include "tensorflow_lite_support/c/task/core/base_options.h"
16+
#import "third_party/tensorflow_lite_support/ios/task/core/sources/TFLBaseOptions.h"
17+
18+
NS_ASSUME_NONNULL_BEGIN
19+
20+
@interface TFLBaseOptions (Helpers)
21+
- (void)copyBaseOptionsToCBaseOptions:(TfLiteBaseOptions *)cBaseOptions;
22+
@end
23+
24+
NS_ASSUME_NONNULL_END
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
#import "tensorflow_lite_support/ios/task/core/sources/TFLBaseOptions+Helpers.h"
16+
17+
@implementation TFLBaseOptions (Helpers)
18+
19+
- (void)copyBaseOptionsToCBaseOptions:(TfLiteBaseOptions *)cBaseOptions {
20+
if (self.modelFile.filePath) {
21+
cBaseOptions->model_file.file_path = self.modelFile.filePath.UTF8String;
22+
}
23+
}
24+
25+
@end

tensorflow_lite_support/ios/task/core/sources/TFLBaseOptions.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ NS_ASSUME_NONNULL_BEGIN
1717
* Holds cpu settings.
1818
*/
1919
NS_SWIFT_NAME(CpuSettings)
20-
@interface TFLCpuSettings : NSObject
20+
@interface TFLCpuSettings : NSObject <NSCopying>
2121

2222
/** Specifies the number of threads to be used for TFLite ops that support multi-threadingwhen
2323
* running inference with CPU.
@@ -32,7 +32,7 @@ NS_SWIFT_NAME(CpuSettings)
3232
* Holds settings for one possible acceleration configuration.
3333
*/
3434
NS_SWIFT_NAME(ComputeSettings)
35-
@interface TFLComputeSettings : NSObject
35+
@interface TFLComputeSettings : NSObject <NSCopying>
3636

3737
/** Holds cpu settings. */
3838
@property(nonatomic, copy) TFLCpuSettings *cpuSettings;
@@ -43,7 +43,7 @@ NS_SWIFT_NAME(ComputeSettings)
4343
* Holds settings for one possible acceleration configuration.
4444
*/
4545
NS_SWIFT_NAME(ExternalFile)
46-
@interface TFLExternalFile : NSObject
46+
@interface TFLExternalFile : NSObject <NSCopying>
4747

4848
/** Path to the file in bundle. */
4949
@property(nonatomic, copy) NSString *filePath;
@@ -56,7 +56,7 @@ NS_SWIFT_NAME(ExternalFile)
5656
* important information acceleration configuration, tflite model source etc.
5757
*/
5858
NS_SWIFT_NAME(BaseOptions)
59-
@interface TFLBaseOptions : NSObject
59+
@interface TFLBaseOptions : NSObject <NSCopying>
6060

6161
/**
6262
* The external model file, as a single standalone TFLite file. It could be packed with TFLite Model
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
#import "tensorflow_lite_support/ios/task/core/sources/TFLBaseOptions.h"
16+
17+
@implementation TFLCpuSettings
18+
@synthesize numThreads;
19+
20+
- (instancetype)init {
21+
self = [super init];
22+
if (self) {
23+
self.numThreads = -1;
24+
}
25+
return self;
26+
}
27+
28+
- (id)copyWithZone:(NSZone *)zone {
29+
TFLCpuSettings *cpuSettings = [[TFLCpuSettings alloc] init];
30+
31+
[cpuSettings setNumThreads:self.numThreads];
32+
33+
return cpuSettings;
34+
}
35+
36+
@end
37+
38+
@implementation TFLComputeSettings
39+
@synthesize cpuSettings;
40+
41+
- (instancetype)init {
42+
self = [super init];
43+
if (self) {
44+
self.cpuSettings = [[TFLCpuSettings alloc] init];
45+
}
46+
return self;
47+
}
48+
49+
- (id)copyWithZone:(NSZone *)zone {
50+
TFLComputeSettings *computeSettings = [[TFLComputeSettings alloc] init];
51+
52+
[computeSettings setCpuSettings:self.cpuSettings];
53+
54+
return computeSettings;
55+
}
56+
57+
@end
58+
59+
@implementation TFLExternalFile
60+
@synthesize filePath;
61+
62+
- (id)copyWithZone:(NSZone *)zone {
63+
TFLExternalFile *externalFile = [[TFLExternalFile alloc] init];
64+
65+
[externalFile setFilePath:self.filePath];
66+
67+
return externalFile;
68+
}
69+
70+
@end
71+
72+
@implementation TFLBaseOptions
73+
@synthesize modelFile;
74+
@synthesize computeSettings;
75+
76+
- (instancetype)init {
77+
self = [super init];
78+
if (self) {
79+
self.computeSettings = [[TFLComputeSettings alloc] init];
80+
self.modelFile = [[TFLExternalFile alloc] init];
81+
}
82+
return self;
83+
}
84+
85+
- (id)copyWithZone:(NSZone *)zone {
86+
TFLBaseOptions *baseOptions = [[TFLBaseOptions alloc] init];
87+
88+
[baseOptions setModelFile:self.modelFile];
89+
[baseOptions setComputeSettings:self.computeSettings];
90+
91+
return baseOptions;
92+
}
93+
94+
@end

0 commit comments

Comments
 (0)