so, we can make a filter more general to accept different network models, by adding a data type convertion after getting data from network. After we add dt field into struct DNNData, it becomes the same as DNNInputData, so merge them with one struct: DNNData.
Unified · Split
| @@ -28,7 +28,7 @@ · 27 unmodified lines | |||
| 28 | #include "dnn_backend_native_layer_conv2d.h" | 28 | #include "dnn_backend_native_layer_conv2d.h" |
| 29 | #include "dnn_backend_native_layers.h" | 29 | #include "dnn_backend_native_layers.h" |
| 30 | 30 | ||
| 31 | static DNNReturnType set_input_output_native(void *model, DNNInputData *input, const char *input_name, const char **output_names, uint32_t nb_output) | 31 | static DNNReturnType set_input_output_native(void *model, DNNData *input, const char *input_name, const char **output_names, uint32_t nb_output) |
| 32 | { | 32 | { |
| 33 | ConvolutionalNetwork *network = (ConvolutionalNetwork *)model; | 33 | ConvolutionalNetwork *network = (ConvolutionalNetwork *)model; |
| 34 | DnnOperand *oprd = NULL; | 34 | DnnOperand *oprd = NULL; |
| @@ -263,6 +263,7 @@ DNNReturnType ff_dnn_execute_model_native(const DNNModel *model, DNNData *output · 228 unmodified lines | |||
| 263 | outputs[i].height = oprd->dims[1]; | 263 | outputs[i].height = oprd->dims[1]; |
| 264 | outputs[i].width = oprd->dims[2]; | 264 | outputs[i].width = oprd->dims[2]; |
| 265 | outputs[i].channels = oprd->dims[3]; | 265 | outputs[i].channels = oprd->dims[3]; |
| 266 | outputs[i].dt = oprd->data_type; | ||
| 266 | } | 267 | } |
| 267 | 268 | ||
| 268 | return DNN_SUCCESS; | 269 | return DNN_SUCCESS; |
| @@ -106,6 +106,7 @@ int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_ · 105 unmodified lines | |||
| 106 | output_operand->dims[1] = height - pad_size * 2; | 106 | output_operand->dims[1] = height - pad_size * 2; |
| 107 | output_operand->dims[2] = width - pad_size * 2; | 107 | output_operand->dims[2] = width - pad_size * 2; |
| 108 | output_operand->dims[3] = conv_params->output_num; | 108 | output_operand->dims[3] = conv_params->output_num; |
| 109 | output_operand->data_type = operands[input_operand_index].data_type; | ||
| 109 | output_operand->length = calculate_operand_data_length(output_operand); | 110 | output_operand->length = calculate_operand_data_length(output_operand); |
| 110 | output_operand->data = av_realloc(output_operand->data, output_operand->length); | 111 | output_operand->data = av_realloc(output_operand->data, output_operand->length); |
| 111 | if (!output_operand->data) | 112 | if (!output_operand->data) |
| @@ -69,6 +69,7 @@ int dnn_execute_layer_depth2space(DnnOperand *operands, const int32_t *input_ope · 68 unmodified lines | |||
| 69 | output_operand->dims[1] = height * block_size; | 69 | output_operand->dims[1] = height * block_size; |
| 70 | output_operand->dims[2] = width * block_size; | 70 | output_operand->dims[2] = width * block_size; |
| 71 | output_operand->dims[3] = new_channels; | 71 | output_operand->dims[3] = new_channels; |
| 72 | output_operand->data_type = operands[input_operand_index].data_type; | ||
| 72 | output_operand->length = calculate_operand_data_length(output_operand); | 73 | output_operand->length = calculate_operand_data_length(output_operand); |
| 73 | output_operand->data = av_realloc(output_operand->data, output_operand->length); | 74 | output_operand->data = av_realloc(output_operand->data, output_operand->length); |
| 74 | if (!output_operand->data) | 75 | if (!output_operand->data) |
| @@ -105,6 +105,7 @@ int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_ind · 104 unmodified lines | |||
| 105 | output_operand->dims[1] = new_height; | 105 | output_operand->dims[1] = new_height; |
| 106 | output_operand->dims[2] = new_width; | 106 | output_operand->dims[2] = new_width; |
| 107 | output_operand->dims[3] = new_channel; | 107 | output_operand->dims[3] = new_channel; |
| 108 | output_operand->data_type = operands[input_operand_index].data_type; | ||
| 108 | output_operand->length = calculate_operand_data_length(output_operand); | 109 | output_operand->length = calculate_operand_data_length(output_operand); |
| 109 | output_operand->data = av_realloc(output_operand->data, output_operand->length); | 110 | output_operand->data = av_realloc(output_operand->data, output_operand->length); |
| 110 | if (!output_operand->data) | 111 | if (!output_operand->data) |
| @@ -83,7 +83,7 @@ static TF_Buffer *read_graph(const char *model_filename) · 82 unmodified lines | |||
| 83 | return graph_buf; | 83 | return graph_buf; |
| 84 | } | 84 | } |
| 85 | 85 | ||
| 86 | static TF_Tensor *allocate_input_tensor(const DNNInputData *input) | 86 | static TF_Tensor *allocate_input_tensor(const DNNData *input) |
| 87 | { | 87 | { |
| 88 | TF_DataType dt; | 88 | TF_DataType dt; |
| 89 | size_t size; | 89 | size_t size; |
| @@ -105,7 +105,7 @@ static TF_Tensor *allocate_input_tensor(const DNNInputData *input) · 15 unmodified lines | |||
| 105 | input_dims[1] * input_dims[2] * input_dims[3] * size); | 105 | input_dims[1] * input_dims[2] * input_dims[3] * size); |
| 106 | } | 106 | } |
| 107 | 107 | ||
| 108 | static DNNReturnType set_input_output_tf(void *model, DNNInputData *input, const char *input_name, const char **output_names, uint32_t nb_output) | 108 | static DNNReturnType set_input_output_tf(void *model, DNNData *input, const char *input_name, const char **output_names, uint32_t nb_output) |
| 109 | { | 109 | { |
| 110 | TFModel *tf_model = (TFModel *)model; | 110 | TFModel *tf_model = (TFModel *)model; |
| 111 | TF_SessionOptions *sess_opts; | 111 | TF_SessionOptions *sess_opts; |
| @@ -603,6 +603,7 @@ DNNReturnType ff_dnn_execute_model_tf(const DNNModel *model, DNNData *outputs, u · 491 unmodified lines | |||
| 603 | outputs[i].width = TF_Dim(tf_model->output_tensors[i], 2); | 603 | outputs[i].width = TF_Dim(tf_model->output_tensors[i], 2); |
| 604 | outputs[i].channels = TF_Dim(tf_model->output_tensors[i], 3); | 604 | outputs[i].channels = TF_Dim(tf_model->output_tensors[i], 3); |
| 605 | outputs[i].data = TF_TensorData(tf_model->output_tensors[i]); | 605 | outputs[i].data = TF_TensorData(tf_model->output_tensors[i]); |
| 606 | outputs[i].dt = TF_TensorType(tf_model->output_tensors[i]); | ||
| 606 | } | 607 | } |
| 607 | 608 | ||
| 608 | return DNN_SUCCESS; | 609 | return DNN_SUCCESS; |
| @@ -34,15 +34,10 @@ typedef enum {DNN_NATIVE, DNN_TF} DNNBackendType; · 33 unmodified lines | |||
| 34 | 34 | ||
| 35 | typedef enum {DNN_FLOAT = 1, DNN_UINT8 = 4} DNNDataType; | 35 | typedef enum {DNN_FLOAT = 1, DNN_UINT8 = 4} DNNDataType; |
| 36 | 36 | ||
| 37 | typedef struct DNNInputData{ | 37 | typedef struct DNNData{ |
| 38 | void *data; | 38 | void *data; |
| 39 | DNNDataType dt; | 39 | DNNDataType dt; |
| 40 | int width, height, channels; | 40 | int width, height, channels; |
| 41 | } DNNInputData; | ||
| 42 | |||
| 43 | typedef struct DNNData{ | ||
| 44 | float *data; | ||
| 45 | int width, height, channels; | ||
| 46 | } DNNData; | 41 | } DNNData; |
| 47 | 42 | ||
| 48 | typedef struct DNNModel{ | 43 | typedef struct DNNModel{ |
| @@ -50,7 +45,7 @@ typedef struct DNNModel{ · 1 unmodified line | |||
| 50 | void *model; | 45 | void *model; |
| 51 | // Sets model input and output. | 46 | // Sets model input and output. |
| 52 | // Should be called at least once before model execution. | 47 | // Should be called at least once before model execution. |
| 53 | DNNReturnType (*set_input_output)(void *model, DNNInputData *input, const char *input_name, const char **output_names, uint32_t nb_output); | 48 | DNNReturnType (*set_input_output)(void *model, DNNData *input, const char *input_name, const char **output_names, uint32_t nb_output); |
| 54 | } DNNModel; | 49 | } DNNModel; |
| 55 | 50 | ||
| 56 | // Stores pointers to functions for loading, executing, freeing DNN models for one of the backends. | 51 | // Stores pointers to functions for loading, executing, freeing DNN models for one of the backends. |
| @@ -39,7 +39,7 @@ typedef struct DRContext { · 38 unmodified lines | |||
| 39 | DNNBackendType backend_type; | 39 | DNNBackendType backend_type; |
| 40 | DNNModule *dnn_module; | 40 | DNNModule *dnn_module; |
| 41 | DNNModel *model; | 41 | DNNModel *model; |
| 42 | DNNInputData input; | 42 | DNNData input; |
| 43 | DNNData output; | 43 | DNNData output; |
| 44 | } DRContext; | 44 | } DRContext; |
| 45 | 45 | ||
| @@ -137,7 +137,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in) · 91 unmodified lines | |||
| 137 | int t = i * out->width * 3 + j; | 137 | int t = i * out->width * 3 + j; |
| 138 | 138 | ||
| 139 | int t_in = (i + pad_size) * in->width * 3 + j + pad_size * 3; | 139 | int t_in = (i + pad_size) * in->width * 3 + j + pad_size * 3; |
| 140 | out->data[0][k] = CLIP((int)((((float *)dr_context->input.data)[t_in] - dr_context->output.data[t]) * 255), 0, 255); | 140 | out->data[0][k] = CLIP((int)((((float *)dr_context->input.data)[t_in] - ((float *)dr_context->output.data)[t]) * 255), 0, 255); |
| 141 | } | 141 | } |
| 142 | } | 142 | } |
| 143 | 143 | ||
| @@ -41,7 +41,7 @@ typedef struct SRContext { · 40 unmodified lines | |||
| 41 | DNNBackendType backend_type; | 41 | DNNBackendType backend_type; |
| 42 | DNNModule *dnn_module; | 42 | DNNModule *dnn_module; |
| 43 | DNNModel *model; | 43 | DNNModel *model; |
| 44 | DNNInputData input; | 44 | DNNData input; |
| 45 | DNNData output; | 45 | DNNData output; |
| 46 | int scale_factor; | 46 | int scale_factor; |
| 47 | struct SwsContext *sws_contexts[3]; | 47 | struct SwsContext *sws_contexts[3]; |