-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented TFLite inference processor
- Loading branch information
Showing
6 changed files
with
85 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include "TFLiteProcessor.h" | ||
|
||
TFLiteProcessor::TFLiteProcessor() | ||
{ | ||
model = TfLiteModelCreateFromFile(modelpath.c_str()); | ||
options = TfLiteInterpreterOptionsCreate(); | ||
interpreter = TfLiteInterpreterCreate(model, options); | ||
} | ||
|
||
TFLiteProcessor::~TFLiteProcessor() | ||
{ | ||
TfLiteInterpreterDelete(interpreter); | ||
TfLiteInterpreterOptionsDelete(options); | ||
TfLiteModelDelete(model); | ||
} | ||
|
||
void TFLiteProcessor::prepareToPlay() { | ||
TfLiteInterpreterAllocateTensors(interpreter); | ||
inputTensor = TfLiteInterpreterGetInputTensor(interpreter, 0); | ||
outputTensor = TfLiteInterpreterGetOutputTensor(interpreter, 0); | ||
std::array<float, BATCH_SIZE * MODEL_INPUT_SIZE_BACKEND> input; | ||
std::array<float, BATCH_SIZE * MODEL_OUTPUT_SIZE_BACKEND> output; | ||
processBlock(input, output); | ||
} | ||
|
||
void TFLiteProcessor::processBlock(std::array<float, BATCH_SIZE * MODEL_INPUT_SIZE_BACKEND>& input, std::array<float, BATCH_SIZE * MODEL_OUTPUT_SIZE_BACKEND>& output) { | ||
TfLiteTensorCopyFromBuffer(inputTensor, input.data(), input.size() * sizeof(float)); | ||
TfLiteInterpreterInvoke(interpreter); | ||
TfLiteTensorCopyToBuffer(outputTensor, output.data(), output.size() * sizeof(float)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef NN_INFERENCE_TEMPLATE_TFLITEPROCESSOR_H | ||
#define NN_INFERENCE_TEMPLATE_TFLITEPROCESSOR_H | ||
|
||
#include <JuceHeader.h> | ||
#include "../InferenceConfig.h" | ||
#include "tensorflow/lite/c_api.h" | ||
|
||
class TFLiteProcessor { | ||
public: | ||
TFLiteProcessor(); | ||
~TFLiteProcessor(); | ||
|
||
void prepareToPlay(); | ||
void processBlock(std::array<float, BATCH_SIZE * MODEL_INPUT_SIZE_BACKEND>& input, std::array<float, BATCH_SIZE * MODEL_OUTPUT_SIZE_BACKEND>& output); | ||
|
||
private: | ||
std::string filepath = MODELS_PATH_TENSORFLOW; | ||
std::string modelname = MODEL_TFLITE; | ||
#ifdef _WIN32 | ||
std::string modelpathStr = filepath + modelname; | ||
std::wstring modelpath = std::wstring(modelpathStr.begin(), modelpathStr.end()); | ||
#else | ||
std::string modelpath = filepath + modelname; | ||
#endif | ||
|
||
TfLiteModel* model; | ||
TfLiteInterpreterOptions* options; | ||
TfLiteInterpreter* interpreter; | ||
|
||
TfLiteTensor* inputTensor; | ||
const TfLiteTensor* outputTensor; | ||
}; | ||
|
||
#endif //NN_INFERENCE_TEMPLATE_TFLITEPROCESSOR_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters