Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Multithread predictions #11

Closed
vegas2ny opened this issue Nov 24, 2017 · 8 comments
Closed

Multithread predictions #11

vegas2ny opened this issue Nov 24, 2017 · 8 comments
Assignees
Labels

Comments

@vegas2ny
Copy link

Hello i am having memory access violation when i attempt to multi thread image predictions. this is happening at line

// Start evaluation on the device
modelFunc.Evaluate(inputDataMap, outputDataMap, GlobalParameters.Device);

within ImageNet.cs is there a way to multithread the functionality ?

Thanks

@deepakkumar1984
Copy link
Member

deepakkumar1984 commented Nov 24, 2017 via email

@vegas2ny
Copy link
Author

sure, do you have gmail, maybe we can google chat? and ill update here with resolution?

@deepakkumar1984
Copy link
Member

deepakkumar1984 commented Nov 24, 2017 via email

@deepakkumar1984
Copy link
Member

Or here is my gmail: deepakkumar1984@gmail.com

@deepakkumar1984
Copy link
Member

Try using lock to avoid access violation issue:

static ImageNet app;
private static Object thisLock = new Object();

static void Main(string[] args)
{
    app = new ImageNet(ImageNetModel.ResNet50);
    app.LoadModel();

    for (int i = 0; i < 5; i++)
    {
        System.Threading.ThreadPool.QueueUserWorkItem(InvokeCls);
    }
}

static void InvokeCls(Object stateInfo)
{
    Console.WriteLine("started");
    string imagePath = string.Format("{0}images\\dog_cls.jpg", AppDomain.CurrentDomain.BaseDirectory);
    lock (thisLock)
    {
        var p = app.Predict(imagePath);
        Console.WriteLine("Predicted: " + p[0].Name);
    }
}

@vegas2ny
Copy link
Author

hello i have found a resolution, by cloning the module func it makes predictions thread safe, there for no more access violations.

//clone model func for multithread
var newmodelfunc = modelFunc.Clone(ParameterCloningMethod.Share);

full method code below:

   //clone model func for multihread
            var newmodelfunc  = modelFunc.Clone(ParameterCloningMethod.Share);

            Variable inputVar = newmodelfunc.Arguments[0];

            NDShape inputShape = inputVar.Shape;
            int imageWidth = inputShape[0];
            int imageHeight = inputShape[1];

            var resized = bmp.Resize(imageWidth, imageHeight, true);
            List<float> resizedCHW = resized.ParallelExtractCHW();
            
            // Create input data map
            var inputDataMap = new Dictionary<Variable, Value>();
            var inputVal = Value.CreateBatch(inputShape, resizedCHW, GlobalParameters.Device);
            inputDataMap.Add(inputVar, inputVal);
            inputVar = newmodelfunc.Arguments[1];
            //inputDataMap.Add(inputVar, null);

            Variable outputVar = newmodelfunc.Outputs.Where(x => (x.Shape.TotalSize == 1000)).ToList()[0];

            // Create output data map. Using null as Value to indicate using system allocated memory.
            // Alternatively, create a Value object and add it to the data map.
            var outputDataMap = new Dictionary<Variable, Value>();
            outputDataMap.Add(outputVar, null);


            // Start evaluation on the device
            newmodelfunc.Evaluate(inputDataMap, outputDataMap, GlobalParameters.Device);

            // Get evaluate result as dense output
            var outputVal = outputDataMap[outputVar];
            var outputData = outputVal.GetDenseData<float>(outputVar);
            Dictionary<int, float> outputPred = new Dictionary<int, float>();

            for (int i = 0; i < outputData[0].Count; i++)
            {
                outputPred.Add(i, outputData[0][i]);
            }

            var topList = outputPred.OrderByDescending(x => (x.Value)).Take(topK).ToList();
            List<PredResult> result = new List<PredResult>();
            float sumpredresult = outputPred.Sum(x => (x.Value));
            float avgpredresult = outputPred.Average(x => (x.Value));
            float min = outputPred.Min(x=>(x.Value));
            float max = outputPred.Max(x=>(x.Value));

            foreach (var item in topList)
            {
                result.Add(new PredResult()
                {
                    Score = item.Value,
                    Name = actualValues[item.Key]
                });
            }

            Logging.WriteTrace("Prediction Completed");

            return result;

@vegas2ny
Copy link
Author

Hello will close this bug now since the issue is resolved.

Thanks

@deepakkumar1984
Copy link
Member

Thanks for your help

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants