Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "TfLiteMotionPredictor" |
| 18 | #include <input/TfLiteMotionPredictor.h> |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <cmath> |
| 22 | #include <cstddef> |
| 23 | #include <cstdint> |
| 24 | #include <fstream> |
| 25 | #include <ios> |
| 26 | #include <iterator> |
| 27 | #include <memory> |
| 28 | #include <span> |
| 29 | #include <string> |
| 30 | #include <type_traits> |
| 31 | #include <utility> |
| 32 | |
| 33 | #define ATRACE_TAG ATRACE_TAG_INPUT |
| 34 | #include <cutils/trace.h> |
| 35 | #include <log/log.h> |
| 36 | |
| 37 | #include "tensorflow/lite/core/api/error_reporter.h" |
| 38 | #include "tensorflow/lite/interpreter.h" |
| 39 | #include "tensorflow/lite/kernels/register.h" |
| 40 | #include "tensorflow/lite/model.h" |
| 41 | |
| 42 | namespace android { |
| 43 | namespace { |
| 44 | |
| 45 | constexpr char SIGNATURE_KEY[] = "serving_default"; |
| 46 | |
| 47 | // Input tensor names. |
| 48 | constexpr char INPUT_R[] = "r"; |
| 49 | constexpr char INPUT_PHI[] = "phi"; |
| 50 | constexpr char INPUT_PRESSURE[] = "pressure"; |
| 51 | constexpr char INPUT_TILT[] = "tilt"; |
| 52 | constexpr char INPUT_ORIENTATION[] = "orientation"; |
| 53 | |
| 54 | // Output tensor names. |
| 55 | constexpr char OUTPUT_R[] = "r"; |
| 56 | constexpr char OUTPUT_PHI[] = "phi"; |
| 57 | constexpr char OUTPUT_PRESSURE[] = "pressure"; |
| 58 | |
| 59 | // A TFLite ErrorReporter that logs to logcat. |
| 60 | class LoggingErrorReporter : public tflite::ErrorReporter { |
| 61 | public: |
| 62 | int Report(const char* format, va_list args) override { |
| 63 | return LOG_PRI_VA(ANDROID_LOG_ERROR, LOG_TAG, format, args); |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | // Searches a runner for an input tensor. |
| 68 | TfLiteTensor* findInputTensor(const char* name, tflite::SignatureRunner* runner) { |
| 69 | TfLiteTensor* tensor = runner->input_tensor(name); |
| 70 | LOG_ALWAYS_FATAL_IF(!tensor, "Failed to find input tensor '%s'", name); |
| 71 | return tensor; |
| 72 | } |
| 73 | |
| 74 | // Searches a runner for an output tensor. |
| 75 | const TfLiteTensor* findOutputTensor(const char* name, tflite::SignatureRunner* runner) { |
| 76 | const TfLiteTensor* tensor = runner->output_tensor(name); |
| 77 | LOG_ALWAYS_FATAL_IF(!tensor, "Failed to find output tensor '%s'", name); |
| 78 | return tensor; |
| 79 | } |
| 80 | |
| 81 | // Returns the buffer for a tensor of type T. |
| 82 | template <typename T> |
| 83 | std::span<T> getTensorBuffer(typename std::conditional<std::is_const<T>::value, const TfLiteTensor*, |
| 84 | TfLiteTensor*>::type tensor) { |
| 85 | LOG_ALWAYS_FATAL_IF(!tensor); |
| 86 | |
| 87 | const TfLiteType type = tflite::typeToTfLiteType<typename std::remove_cv<T>::type>(); |
| 88 | LOG_ALWAYS_FATAL_IF(tensor->type != type, "Unexpected type for '%s' tensor: %s (expected %s)", |
| 89 | tensor->name, TfLiteTypeGetName(tensor->type), TfLiteTypeGetName(type)); |
| 90 | |
| 91 | LOG_ALWAYS_FATAL_IF(!tensor->data.data); |
| 92 | return {reinterpret_cast<T*>(tensor->data.data), |
| 93 | static_cast<typename std::span<T>::index_type>(tensor->bytes / sizeof(T))}; |
| 94 | } |
| 95 | |
| 96 | // Verifies that a tensor exists and has an underlying buffer of type T. |
| 97 | template <typename T> |
| 98 | void checkTensor(const TfLiteTensor* tensor) { |
| 99 | LOG_ALWAYS_FATAL_IF(!tensor); |
| 100 | |
| 101 | const auto buffer = getTensorBuffer<const T>(tensor); |
| 102 | LOG_ALWAYS_FATAL_IF(buffer.empty(), "No buffer for tensor '%s'", tensor->name); |
| 103 | } |
| 104 | |
| 105 | } // namespace |
| 106 | |
Philip Quinn | 9b8926e | 2023-01-31 14:50:02 -0800 | [diff] [blame] | 107 | TfLiteMotionPredictorBuffers::TfLiteMotionPredictorBuffers(size_t inputLength) |
| 108 | : mInputR(inputLength, 0), |
| 109 | mInputPhi(inputLength, 0), |
| 110 | mInputPressure(inputLength, 0), |
| 111 | mInputTilt(inputLength, 0), |
| 112 | mInputOrientation(inputLength, 0) { |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 113 | LOG_ALWAYS_FATAL_IF(inputLength == 0, "Buffer input size must be greater than 0"); |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void TfLiteMotionPredictorBuffers::reset() { |
| 117 | std::fill(mInputR.begin(), mInputR.end(), 0); |
| 118 | std::fill(mInputPhi.begin(), mInputPhi.end(), 0); |
| 119 | std::fill(mInputPressure.begin(), mInputPressure.end(), 0); |
| 120 | std::fill(mInputTilt.begin(), mInputTilt.end(), 0); |
| 121 | std::fill(mInputOrientation.begin(), mInputOrientation.end(), 0); |
| 122 | mAxisFrom.reset(); |
| 123 | mAxisTo.reset(); |
| 124 | } |
| 125 | |
| 126 | void TfLiteMotionPredictorBuffers::copyTo(TfLiteMotionPredictorModel& model) const { |
| 127 | LOG_ALWAYS_FATAL_IF(mInputR.size() != model.inputLength(), |
| 128 | "Buffer length %zu doesn't match model input length %zu", mInputR.size(), |
| 129 | model.inputLength()); |
| 130 | LOG_ALWAYS_FATAL_IF(!isReady(), "Buffers are incomplete"); |
| 131 | |
| 132 | std::copy(mInputR.begin(), mInputR.end(), model.inputR().begin()); |
| 133 | std::copy(mInputPhi.begin(), mInputPhi.end(), model.inputPhi().begin()); |
| 134 | std::copy(mInputPressure.begin(), mInputPressure.end(), model.inputPressure().begin()); |
| 135 | std::copy(mInputTilt.begin(), mInputTilt.end(), model.inputTilt().begin()); |
| 136 | std::copy(mInputOrientation.begin(), mInputOrientation.end(), model.inputOrientation().begin()); |
| 137 | } |
| 138 | |
| 139 | void TfLiteMotionPredictorBuffers::pushSample(int64_t timestamp, |
| 140 | const TfLiteMotionPredictorSample sample) { |
| 141 | // Convert the sample (x, y) into polar (r, φ) based on a reference axis |
| 142 | // from the preceding two points (mAxisFrom/mAxisTo). |
| 143 | |
| 144 | mTimestamp = timestamp; |
| 145 | |
| 146 | if (!mAxisTo) { // First point. |
| 147 | mAxisTo = sample; |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | // Vector from the last point to the current sample point. |
| 152 | const TfLiteMotionPredictorSample::Point v = sample.position - mAxisTo->position; |
| 153 | |
| 154 | const float r = std::hypot(v.x, v.y); |
| 155 | float phi = 0; |
| 156 | float orientation = 0; |
| 157 | |
| 158 | // Ignore the sample if there is no movement. These samples can occur when there's change to a |
| 159 | // property other than the coordinates and pollute the input to the model. |
| 160 | if (r == 0) { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | if (!mAxisFrom) { // Second point. |
| 165 | // We can only determine the distance from the first point, and not any |
| 166 | // angle. However, if the second point forms an axis, the orientation can |
| 167 | // be transformed relative to that axis. |
| 168 | const float axisPhi = std::atan2(v.y, v.x); |
| 169 | // A MotionEvent's orientation is measured clockwise from the vertical |
| 170 | // axis, but axisPhi is measured counter-clockwise from the horizontal |
| 171 | // axis. |
| 172 | orientation = M_PI_2 - sample.orientation - axisPhi; |
| 173 | } else { |
| 174 | const TfLiteMotionPredictorSample::Point axis = mAxisTo->position - mAxisFrom->position; |
| 175 | const float axisPhi = std::atan2(axis.y, axis.x); |
| 176 | phi = std::atan2(v.y, v.x) - axisPhi; |
| 177 | |
| 178 | if (std::hypot(axis.x, axis.y) > 0) { |
| 179 | // See note above. |
| 180 | orientation = M_PI_2 - sample.orientation - axisPhi; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // Update the axis for the next point. |
| 185 | mAxisFrom = mAxisTo; |
| 186 | mAxisTo = sample; |
| 187 | |
| 188 | // Push the current sample onto the end of the input buffers. |
Philip Quinn | 9b8926e | 2023-01-31 14:50:02 -0800 | [diff] [blame] | 189 | mInputR.pushBack(r); |
| 190 | mInputPhi.pushBack(phi); |
| 191 | mInputPressure.pushBack(sample.pressure); |
| 192 | mInputTilt.pushBack(sample.tilt); |
| 193 | mInputOrientation.pushBack(orientation); |
Philip Quinn | 8f953ab | 2022-12-06 15:37:07 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | std::unique_ptr<TfLiteMotionPredictorModel> TfLiteMotionPredictorModel::create( |
| 197 | const char* modelPath) { |
| 198 | std::ifstream f(modelPath, std::ios::binary); |
| 199 | LOG_ALWAYS_FATAL_IF(!f, "Could not read model from %s", modelPath); |
| 200 | |
| 201 | std::string data; |
| 202 | data.assign(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>()); |
| 203 | |
| 204 | return std::unique_ptr<TfLiteMotionPredictorModel>( |
| 205 | new TfLiteMotionPredictorModel(std::move(data))); |
| 206 | } |
| 207 | |
| 208 | TfLiteMotionPredictorModel::TfLiteMotionPredictorModel(std::string model) |
| 209 | : mFlatBuffer(std::move(model)) { |
| 210 | mErrorReporter = std::make_unique<LoggingErrorReporter>(); |
| 211 | mModel = tflite::FlatBufferModel::VerifyAndBuildFromBuffer(mFlatBuffer.data(), |
| 212 | mFlatBuffer.length(), |
| 213 | /*extra_verifier=*/nullptr, |
| 214 | mErrorReporter.get()); |
| 215 | LOG_ALWAYS_FATAL_IF(!mModel); |
| 216 | |
| 217 | tflite::ops::builtin::BuiltinOpResolver resolver; |
| 218 | tflite::InterpreterBuilder builder(*mModel, resolver); |
| 219 | |
| 220 | if (builder(&mInterpreter) != kTfLiteOk || !mInterpreter) { |
| 221 | LOG_ALWAYS_FATAL("Failed to build interpreter"); |
| 222 | } |
| 223 | |
| 224 | mRunner = mInterpreter->GetSignatureRunner(SIGNATURE_KEY); |
| 225 | LOG_ALWAYS_FATAL_IF(!mRunner, "Failed to find runner for signature '%s'", SIGNATURE_KEY); |
| 226 | |
| 227 | allocateTensors(); |
| 228 | } |
| 229 | |
| 230 | void TfLiteMotionPredictorModel::allocateTensors() { |
| 231 | if (mRunner->AllocateTensors() != kTfLiteOk) { |
| 232 | LOG_ALWAYS_FATAL("Failed to allocate tensors"); |
| 233 | } |
| 234 | |
| 235 | attachInputTensors(); |
| 236 | attachOutputTensors(); |
| 237 | |
| 238 | checkTensor<float>(mInputR); |
| 239 | checkTensor<float>(mInputPhi); |
| 240 | checkTensor<float>(mInputPressure); |
| 241 | checkTensor<float>(mInputTilt); |
| 242 | checkTensor<float>(mInputOrientation); |
| 243 | checkTensor<float>(mOutputR); |
| 244 | checkTensor<float>(mOutputPhi); |
| 245 | checkTensor<float>(mOutputPressure); |
| 246 | |
| 247 | const auto checkInputTensorSize = [this](const TfLiteTensor* tensor) { |
| 248 | const size_t size = getTensorBuffer<const float>(tensor).size(); |
| 249 | LOG_ALWAYS_FATAL_IF(size != inputLength(), |
| 250 | "Tensor '%s' length %zu does not match input length %zu", tensor->name, |
| 251 | size, inputLength()); |
| 252 | }; |
| 253 | |
| 254 | checkInputTensorSize(mInputR); |
| 255 | checkInputTensorSize(mInputPhi); |
| 256 | checkInputTensorSize(mInputPressure); |
| 257 | checkInputTensorSize(mInputTilt); |
| 258 | checkInputTensorSize(mInputOrientation); |
| 259 | } |
| 260 | |
| 261 | void TfLiteMotionPredictorModel::attachInputTensors() { |
| 262 | mInputR = findInputTensor(INPUT_R, mRunner); |
| 263 | mInputPhi = findInputTensor(INPUT_PHI, mRunner); |
| 264 | mInputPressure = findInputTensor(INPUT_PRESSURE, mRunner); |
| 265 | mInputTilt = findInputTensor(INPUT_TILT, mRunner); |
| 266 | mInputOrientation = findInputTensor(INPUT_ORIENTATION, mRunner); |
| 267 | } |
| 268 | |
| 269 | void TfLiteMotionPredictorModel::attachOutputTensors() { |
| 270 | mOutputR = findOutputTensor(OUTPUT_R, mRunner); |
| 271 | mOutputPhi = findOutputTensor(OUTPUT_PHI, mRunner); |
| 272 | mOutputPressure = findOutputTensor(OUTPUT_PRESSURE, mRunner); |
| 273 | } |
| 274 | |
| 275 | bool TfLiteMotionPredictorModel::invoke() { |
| 276 | ATRACE_BEGIN("TfLiteMotionPredictorModel::invoke"); |
| 277 | TfLiteStatus result = mRunner->Invoke(); |
| 278 | ATRACE_END(); |
| 279 | |
| 280 | if (result != kTfLiteOk) { |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | // Invoke() might reallocate tensors, so they need to be reattached. |
| 285 | attachInputTensors(); |
| 286 | attachOutputTensors(); |
| 287 | |
| 288 | if (outputR().size() != outputPhi().size() || outputR().size() != outputPressure().size()) { |
| 289 | LOG_ALWAYS_FATAL("Output size mismatch: (r: %zu, phi: %zu, pressure: %zu)", |
| 290 | outputR().size(), outputPhi().size(), outputPressure().size()); |
| 291 | } |
| 292 | |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | size_t TfLiteMotionPredictorModel::inputLength() const { |
| 297 | return getTensorBuffer<const float>(mInputR).size(); |
| 298 | } |
| 299 | |
| 300 | std::span<float> TfLiteMotionPredictorModel::inputR() { |
| 301 | return getTensorBuffer<float>(mInputR); |
| 302 | } |
| 303 | |
| 304 | std::span<float> TfLiteMotionPredictorModel::inputPhi() { |
| 305 | return getTensorBuffer<float>(mInputPhi); |
| 306 | } |
| 307 | |
| 308 | std::span<float> TfLiteMotionPredictorModel::inputPressure() { |
| 309 | return getTensorBuffer<float>(mInputPressure); |
| 310 | } |
| 311 | |
| 312 | std::span<float> TfLiteMotionPredictorModel::inputTilt() { |
| 313 | return getTensorBuffer<float>(mInputTilt); |
| 314 | } |
| 315 | |
| 316 | std::span<float> TfLiteMotionPredictorModel::inputOrientation() { |
| 317 | return getTensorBuffer<float>(mInputOrientation); |
| 318 | } |
| 319 | |
| 320 | std::span<const float> TfLiteMotionPredictorModel::outputR() const { |
| 321 | return getTensorBuffer<const float>(mOutputR); |
| 322 | } |
| 323 | |
| 324 | std::span<const float> TfLiteMotionPredictorModel::outputPhi() const { |
| 325 | return getTensorBuffer<const float>(mOutputPhi); |
| 326 | } |
| 327 | |
| 328 | std::span<const float> TfLiteMotionPredictorModel::outputPressure() const { |
| 329 | return getTensorBuffer<const float>(mOutputPressure); |
| 330 | } |
| 331 | |
| 332 | } // namespace android |