From 18ff53fed434eb33084016158c9d6de5154c06ce Mon Sep 17 00:00:00 2001 From: "A. Unique TensorFlower" Date: Wed, 15 May 2019 13:23:05 -0700 Subject: [PATCH] Removes the `isErrorLoggingEnabled` property from the `InterpreterOptions` struct. Error logging is enabled by default to match ObjC behavior. PiperOrigin-RevId: 248392591 --- .../swift/Sources/Interpreter.swift | 34 +++++++++---------- .../swift/Sources/InterpreterError.swift | 2 +- .../swift/Sources/InterpreterOptions.swift | 5 +-- .../TensorFlowLiteApp/ViewController.swift | 4 +-- .../swift/Tests/InterpreterOptionsTests.swift | 11 ------ 5 files changed, 19 insertions(+), 37 deletions(-) diff --git a/tensorflow/lite/experimental/swift/Sources/Interpreter.swift b/tensorflow/lite/experimental/swift/Sources/Interpreter.swift index c1aea0a067e..c32f67c8b52 100644 --- a/tensorflow/lite/experimental/swift/Sources/Interpreter.swift +++ b/tensorflow/lite/experimental/swift/Sources/Interpreter.swift @@ -39,7 +39,7 @@ public final class Interpreter { /// - Parameters: /// - modelPath: Local file path to a TensorFlow Lite model. /// - options: Custom configurations for the interpreter. The default is `nil` indicating that - /// interpreter will determine the configuration options. + /// the interpreter will determine the configuration options. /// - Throws: An error if the model could not be loaded or the interpreter could not be created. public init(modelPath: String, options: InterpreterOptions? = nil) throws { guard let model = Model(filePath: modelPath) else { throw InterpreterError.failedToLoadModel } @@ -51,23 +51,21 @@ public final class Interpreter { if let threadCount = options.threadCount, threadCount > 0 { TFL_InterpreterOptionsSetNumThreads(cOptions, Int32(threadCount)) } - if options.isErrorLoggingEnabled { - TFL_InterpreterOptionsSetErrorReporter( - cOptions, - { (_, format, args) -> Void in - // Workaround for Swift optionality bug: https://bugs.swift.org/browse/SR-3429. - let optionalArgs: CVaListPointer? = args - guard let cFormat = format, - let arguments = optionalArgs, - let message = String(cFormat: cFormat, arguments: arguments) - else { - return - } - print(String(describing: InterpreterError.tensorFlowLiteError(message))) - }, - nil - ) - } + TFL_InterpreterOptionsSetErrorReporter( + cOptions, + { (_, format, args) -> Void in + // Workaround for Swift optionality bug: https://bugs.swift.org/browse/SR-3429. + let optionalArgs: CVaListPointer? = args + guard let cFormat = format, + let arguments = optionalArgs, + let message = String(cFormat: cFormat, arguments: arguments) + else { + return + } + print(String(describing: InterpreterError.tensorFlowLiteError(message))) + }, + nil + ) return cOptions } defer { TFL_DeleteInterpreterOptions(cInterpreterOptions) } diff --git a/tensorflow/lite/experimental/swift/Sources/InterpreterError.swift b/tensorflow/lite/experimental/swift/Sources/InterpreterError.swift index b9dc01c3a9a..a07f8575b52 100644 --- a/tensorflow/lite/experimental/swift/Sources/InterpreterError.swift +++ b/tensorflow/lite/experimental/swift/Sources/InterpreterError.swift @@ -40,7 +40,7 @@ extension InterpreterError: LocalizedError { case .invalidTensorDataCount(let providedCount, let requiredCount): return "Provided data count \(providedCount) must match the required count \(requiredCount)." case .invalidTensorDataType: - return "Tensor data type is unsupported or could not be determined because of a model error." + return "Tensor data type is unsupported or could not be determined due to a model error." case .failedToLoadModel: return "Failed to load the given model." case .failedToCreateInterpreter: diff --git a/tensorflow/lite/experimental/swift/Sources/InterpreterOptions.swift b/tensorflow/lite/experimental/swift/Sources/InterpreterOptions.swift index 44c51aedee8..ae2bbc4b5e6 100644 --- a/tensorflow/lite/experimental/swift/Sources/InterpreterOptions.swift +++ b/tensorflow/lite/experimental/swift/Sources/InterpreterOptions.swift @@ -12,16 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. -/// Custom configuration options for a TensorFlow Lite interpreter. +/// Custom configuration options for a TensorFlow Lite `Interpreter`. public struct InterpreterOptions: Equatable { /// Maximum number of CPU threads that the interpreter should run on. Default is `nil` which /// indicates that the `Interpreter` will decide the number of threads to use. public var threadCount: Int? = nil - /// Whether error logging to the console is enabled. The default is `false`. - public var isErrorLoggingEnabled = false - /// Creates a new instance of interpreter options. public init() {} } diff --git a/tensorflow/lite/experimental/swift/TestApps/TensorFlowLiteApp/TensorFlowLiteApp/ViewController.swift b/tensorflow/lite/experimental/swift/TestApps/TensorFlowLiteApp/TensorFlowLiteApp/ViewController.swift index 73c74fd19c9..b7641744a31 100644 --- a/tensorflow/lite/experimental/swift/TestApps/TensorFlowLiteApp/TensorFlowLiteApp/ViewController.swift +++ b/tensorflow/lite/experimental/swift/TestApps/TensorFlowLiteApp/TensorFlowLiteApp/ViewController.swift @@ -103,9 +103,7 @@ class ViewController: UIViewController { private func setUpInterpreter(withModelPath modelPath: String) { interpreterQueue.async { do { - var options = InterpreterOptions() - options.isErrorLoggingEnabled = true - self.interpreter = try Interpreter(modelPath: modelPath, options: options) + self.interpreter = try Interpreter(modelPath: modelPath) } catch let error { self.updateResultsText( "Failed to create the interpreter with error: \(error.localizedDescription)" diff --git a/tensorflow/lite/experimental/swift/Tests/InterpreterOptionsTests.swift b/tensorflow/lite/experimental/swift/Tests/InterpreterOptionsTests.swift index 54b4f59b289..2113f86b47a 100644 --- a/tensorflow/lite/experimental/swift/Tests/InterpreterOptionsTests.swift +++ b/tensorflow/lite/experimental/swift/Tests/InterpreterOptionsTests.swift @@ -20,15 +20,12 @@ class InterpreterOptionsTests: XCTestCase { func testInterpreterOptions_InitWithDefaultValues() { let options = InterpreterOptions() XCTAssertNil(options.threadCount) - XCTAssertFalse(options.isErrorLoggingEnabled) } func testInterpreterOptions_InitWithCustomValues() { var options = InterpreterOptions() options.threadCount = 2 XCTAssertEqual(options.threadCount, 2) - options.isErrorLoggingEnabled = true - XCTAssertTrue(options.isErrorLoggingEnabled) } func testInterpreterOptions_Equatable() { @@ -42,13 +39,5 @@ class InterpreterOptionsTests: XCTestCase { options2.threadCount = 3 XCTAssertNotEqual(options1, options2) - options2.threadCount = 2 - - options1.isErrorLoggingEnabled = true - options2.isErrorLoggingEnabled = true - XCTAssertEqual(options1, options2) - - options2.isErrorLoggingEnabled = false - XCTAssertNotEqual(options1, options2) } }