mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

About model parameters sharing between multithreads for image prediction

Open sliu012 opened this issue 9 years ago • 3 comments

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);

sliu012 avatar Jan 12 '17 08:01 sliu012

anybody help me??

sliu012 avatar Jan 13 '17 05:01 sliu012

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

indhub avatar Mar 17 '17 14:03 indhub

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!

yajiedesign avatar Sep 29 '17 06:09 yajiedesign