currently, only float is supported as model input, actually, there are other data types, this patch adds uint8.
Showing the changes to libavfilter (4 of 4 files). Show all
Unified · Split
| @@ -24,8 +24,9 @@ · 23 unmodified lines | |||
| 24 | */ | 24 | */ |
| 25 | 25 | ||
| 26 | #include "dnn_backend_native.h" | 26 | #include "dnn_backend_native.h" |
| 27 | #include "libavutil/avassert.h" | ||
| 27 | 28 | ||
| 28 | static DNNReturnType set_input_output_native(void *model, DNNData *input, const char *input_name, const char **output_names, uint32_t nb_output) | 29 | static DNNReturnType set_input_output_native(void *model, DNNInputData *input, const char *input_name, const char **output_names, uint32_t nb_output) |
| 29 | { | 30 | { |
| 30 | ConvolutionalNetwork *network = (ConvolutionalNetwork *)model; | 31 | ConvolutionalNetwork *network = (ConvolutionalNetwork *)model; |
| 31 | InputParams *input_params; | 32 | InputParams *input_params; |
| @@ -45,6 +46,7 @@ static DNNReturnType set_input_output_native(void *model, DNNData *input, const · 13 unmodified lines | |||
| 45 | if (input->data){ | 46 | if (input->data){ |
| 46 | av_freep(&input->data); | 47 | av_freep(&input->data); |
| 47 | } | 48 | } |
| 49 | av_assert0(input->dt == DNN_FLOAT); | ||
| 48 | network->layers[0].output = input->data = av_malloc(cur_height * cur_width * cur_channels * sizeof(float)); | 50 | network->layers[0].output = input->data = av_malloc(cur_height * cur_width * cur_channels * sizeof(float)); |
| 49 | if (!network->layers[0].output){ | 51 | if (!network->layers[0].output){ |
| 50 | return DNN_ERROR; | 52 | return DNN_ERROR; |
| @@ -79,10 +79,31 @@ static TF_Buffer *read_graph(const char *model_filename) · 78 unmodified lines | |||
| 79 | return graph_buf; | 79 | return graph_buf; |
| 80 | } | 80 | } |
| 81 | 81 | ||
| 82 | static DNNReturnType set_input_output_tf(void *model, DNNData *input, const char *input_name, const char **output_names, uint32_t nb_output) | 82 | static TF_Tensor *allocate_input_tensor(const DNNInputData *input) |
| 83 | { | 83 | { |
| 84 | TFModel *tf_model = (TFModel *)model; | 84 | TF_DataType dt; |
| 85 | size_t size; | ||
| 85 | int64_t input_dims[] = {1, input->height, input->width, input->channels}; | 86 | int64_t input_dims[] = {1, input->height, input->width, input->channels}; |
| 87 | switch (input->dt) { | ||
| 88 | case DNN_FLOAT: | ||
| 89 | dt = TF_FLOAT; | ||
| 90 | size = sizeof(float); | ||
| 91 | break; | ||
| 92 | case DNN_UINT8: | ||
| 93 | dt = TF_UINT8; | ||
| 94 | size = sizeof(char); | ||
| 95 | break; | ||
| 96 | default: | ||
| 97 | av_assert0(!"should not reach here"); | ||
| 98 | } | ||
| 99 | |||
| 100 | return TF_AllocateTensor(dt, input_dims, 4, | ||
| 101 | input_dims[1] * input_dims[2] * input_dims[3] * size); | ||
| 102 | } | ||
| 103 | |||
| 104 | static DNNReturnType set_input_output_tf(void *model, DNNInputData *input, const char *input_name, const char **output_names, uint32_t nb_output) | ||
| 105 | { | ||
| 106 | TFModel *tf_model = (TFModel *)model; | ||
| 86 | TF_SessionOptions *sess_opts; | 107 | TF_SessionOptions *sess_opts; |
| 87 | const TF_Operation *init_op = TF_GraphOperationByName(tf_model->graph, "init"); | 108 | const TF_Operation *init_op = TF_GraphOperationByName(tf_model->graph, "init"); |
| 88 | 109 | ||
| @@ -95,8 +116,7 @@ static DNNReturnType set_input_output_tf(void *model, DNNData *input, const char · 6 unmodified lines | |||
| 95 | if (tf_model->input_tensor){ | 116 | if (tf_model->input_tensor){ |
| 96 | TF_DeleteTensor(tf_model->input_tensor); | 117 | TF_DeleteTensor(tf_model->input_tensor); |
| 97 | } | 118 | } |
| 98 | tf_model->input_tensor = TF_AllocateTensor(TF_FLOAT, input_dims, 4, | 119 | tf_model->input_tensor = allocate_input_tensor(input); |
| 99 | input_dims[1] * input_dims[2] * input_dims[3] * sizeof(float)); | ||
| 100 | if (!tf_model->input_tensor){ | 120 | if (!tf_model->input_tensor){ |
| 101 | return DNN_ERROR; | 121 | return DNN_ERROR; |
| 102 | } | 122 | } |
| @@ -32,6 +32,14 @@ typedef enum {DNN_SUCCESS, DNN_ERROR} DNNReturnType; · 31 unmodified lines | |||
| 32 | 32 | ||
| 33 | typedef enum {DNN_NATIVE, DNN_TF} DNNBackendType; | 33 | typedef enum {DNN_NATIVE, DNN_TF} DNNBackendType; |
| 34 | 34 | ||
| 35 | typedef enum {DNN_FLOAT, DNN_UINT8} DNNDataType; | ||
| 36 | |||
| 37 | typedef struct DNNInputData{ | ||
| 38 | void *data; | ||
| 39 | DNNDataType dt; | ||
| 40 | int width, height, channels; | ||
| 41 | } DNNInputData; | ||
| 42 | |||
| 35 | typedef struct DNNData{ | 43 | typedef struct DNNData{ |
| 36 | float *data; | 44 | float *data; |
| 37 | int width, height, channels; | 45 | int width, height, channels; |
| @@ -42,7 +50,7 @@ typedef struct DNNModel{ · 4 unmodified lines | |||
| 42 | void *model; | 50 | void *model; |
| 43 | // Sets model input and output. | 51 | // Sets model input and output. |
| 44 | // Should be called at least once before model execution. | 52 | // Should be called at least once before model execution. |
| 45 | DNNReturnType (*set_input_output)(void *model, DNNData *input, const char *input_name, const char **output_names, uint32_t nb_output); | 53 | DNNReturnType (*set_input_output)(void *model, DNNInputData *input, const char *input_name, const char **output_names, uint32_t nb_output); |
| 46 | } DNNModel; | 54 | } DNNModel; |
| 47 | 55 | ||
| 48 | // Stores pointers to functions for loading, executing, freeing DNN models for one of the backends. | 56 | // Stores pointers to functions for loading, executing, freeing DNN models for one of the backends. |
| @@ -40,7 +40,8 @@ typedef struct SRContext { · 39 unmodified lines | |||
| 40 | DNNBackendType backend_type; | 40 | DNNBackendType backend_type; |
| 41 | DNNModule *dnn_module; | 41 | DNNModule *dnn_module; |
| 42 | DNNModel *model; | 42 | DNNModel *model; |
| 43 | DNNData input, output; | 43 | DNNInputData input; |
| 44 | DNNData output; | ||
| 44 | int scale_factor; | 45 | int scale_factor; |
| 45 | struct SwsContext *sws_contexts[3]; | 46 | struct SwsContext *sws_contexts[3]; |
| 46 | int sws_slice_h, sws_input_linesize, sws_output_linesize; | 47 | int sws_slice_h, sws_input_linesize, sws_output_linesize; |
| @@ -86,6 +87,7 @@ static av_cold int init(AVFilterContext *context) · 39 unmodified lines | |||
| 86 | return AVERROR(EIO); | 87 | return AVERROR(EIO); |
| 87 | } | 88 | } |
| 88 | 89 | ||
| 90 | sr_context->input.dt = DNN_FLOAT; | ||
| 89 | sr_context->sws_contexts[0] = NULL; | 91 | sr_context->sws_contexts[0] = NULL; |
| 90 | sr_context->sws_contexts[1] = NULL; | 92 | sr_context->sws_contexts[1] = NULL; |
| 91 | sr_context->sws_contexts[2] = NULL; | 93 | sr_context->sws_contexts[2] = NULL; |