About model parameters sharing between multithreads for image prediction
I trained a image classification model, and do prediction by the c-api according to the example in "mxnet/example/cpp/image-classification". The problem is: when I launch multithreads to do the prediction, I must create a new model for each thread, and that's very memory consuming. So how can I share one model between all the predict thread, is there any simple way to do that? The following are the main codes for prediction:
...... // Models path for the model BufferFile json_data(symbol_json); BufferFile param_data(model_params);
const mx_uint input_shape_indptr[2] = { 0, 4 };
// ( trained_width, trained_height, channel, num)
const mx_uint input_shape_data[4] = { 1,
static_cast<mx_uint>(channels),
static_cast<mx_uint>(width),
static_cast<mx_uint>(height) };
PredictorHandle out = 0; // alias for void *
//-- Create Predictor
MXPredCreate((const char*)json_data.GetBuffer(),
(const char*)param_data.GetBuffer(),
static_cast<size_t>(param_data.GetLength()),
dev_type,
dev_id,
num_input_nodes,
input_keys,
input_shape_indptr,
input_shape_data,
&out);
......
MXPredSetInput(out, "data", image_data.data(), image_size);
//-- Do Predict Forward
MXPredForward(out);
...... //-- Get Output Result MXPredGetOutputShape(out, output_index, &shape, &shape_len);
MXPredGetOutput(out, output_index, &(data[0]), size);
anybody help me??
Are you sure you need to run multiple threads running inference in parallel? MXNet already does parallel computing. Check this: https://github.com/dmlc/mxnet/issues/5437
This issue is closed due to lack of activity in the last 90 days. Feel free to reopen if this is still an active issue. Thanks!