currently, only float is supported as model input, actually, there are other data types, this patch adds uint8.
Unified · Split
| @@ -24,8 +24,9 @@ · 23 unmodified lines | ||
| 24 | 24 | */ |
| 25 | 25 | |
| 26 | 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 | 31 | ConvolutionalNetwork *network = (ConvolutionalNetwork *)model; |
| 31 | 32 | InputParams *input_params; |
| @@ -45,6 +46,7 @@ static DNNReturnType set_input_output_native(void *model, DNNData *input, const · 13 unmodified lines | ||
| 45 | 46 | if (input->data){ |
| 46 | 47 | av_freep(&input->data); |
| 47 | 48 | } |
| 49 | + av_assert0(input->dt == DNN_FLOAT); | |
| 48 | 50 | network->layers[0].output = input->data = av_malloc(cur_height * cur_width * cur_channels * sizeof(float)); |
| 49 | 51 | if (!network->layers[0].output){ |
| 50 | 52 | return DNN_ERROR; |
| @@ -79,10 +79,31 @@ static TF_Buffer *read_graph(const char *model_filename) · 78 unmodified lines | ||
| 79 | 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 | 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 | 107 | TF_SessionOptions *sess_opts; |
| 87 | 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 | 116 | if (tf_model->input_tensor){ |
| 96 | 117 | TF_DeleteTensor(tf_model->input_tensor); |
| 97 | 118 | } |
| 98 | − tf_model->input_tensor = TF_AllocateTensor(TF_FLOAT, input_dims, 4, | |
| 99 | − input_dims[1] * input_dims[2] * input_dims[3] * sizeof(float)); | |
| 119 | + tf_model->input_tensor = allocate_input_tensor(input); | |
| 100 | 120 | if (!tf_model->input_tensor){ |
| 101 | 121 | return DNN_ERROR; |
| 102 | 122 | } |
| @@ -32,6 +32,14 @@ typedef enum {DNN_SUCCESS, DNN_ERROR} DNNReturnType; · 31 unmodified lines | ||
| 32 | 32 | |
| 33 | 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 | 43 | typedef struct DNNData{ |
| 36 | 44 | float *data; |
| 37 | 45 | int width, height, channels; |
| @@ -42,7 +50,7 @@ typedef struct DNNModel{ · 4 unmodified lines | ||
| 42 | 50 | void *model; |
| 43 | 51 | // Sets model input and output. |
| 44 | 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 | 54 | } DNNModel; |
| 47 | 55 | |
| 48 | 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 | 40 | DNNBackendType backend_type; |
| 41 | 41 | DNNModule *dnn_module; |
| 42 | 42 | DNNModel *model; |
| 43 | − DNNData input, output; | |
| 43 | + DNNInputData input; | |
| 44 | + DNNData output; | |
| 44 | 45 | int scale_factor; |
| 45 | 46 | struct SwsContext *sws_contexts[3]; |
| 46 | 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 | 87 | return AVERROR(EIO); |
| 87 | 88 | } |
| 88 | 89 | |
| 90 | + sr_context->input.dt = DNN_FLOAT; | |
| 89 | 91 | sr_context->sws_contexts[0] = NULL; |
| 90 | 92 | sr_context->sws_contexts[1] = NULL; |
| 91 | 93 | sr_context->sws_contexts[2] = NULL; |