Predict with TfLite model.
| 43 | |
| 44 | // Predict with TfLite model. |
| 45 | void ExecuteTfLite(const std::string& sentence, |
| 46 | ::tflite::Interpreter* interpreter, |
| 47 | std::map<std::string, float>* response_map) { |
| 48 | { |
| 49 | TfLiteTensor* input = interpreter->tensor(interpreter->inputs()[0]); |
| 50 | tflite::DynamicBuffer buf; |
| 51 | buf.AddString(sentence.data(), sentence.length()); |
| 52 | buf.WriteToTensorAsVector(input); |
| 53 | interpreter->AllocateTensors(); |
| 54 | |
| 55 | interpreter->Invoke(); |
| 56 | |
| 57 | TfLiteTensor* messages = interpreter->tensor(interpreter->outputs()[0]); |
| 58 | TfLiteTensor* confidence = interpreter->tensor(interpreter->outputs()[1]); |
| 59 | |
| 60 | for (int i = 0; i < confidence->dims->data[0]; i++) { |
| 61 | float weight = confidence->data.f[i]; |
| 62 | auto response_text = tflite::GetString(messages, i); |
| 63 | if (response_text.len > 0) { |
| 64 | (*response_map)[string(response_text.str, response_text.len)] += weight; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void GetSegmentPredictions( |
| 71 | const std::vector<std::string>& input, |
no test coverage detected