Consuming more time to pass object from .net to python
See original GitHub issueEnvironment
- Pythonnet version: master
- Python version: 3.5
- Operating System: Windows 7
Details
- I am calling python function from my c# code . Initially I import python module using Py.Import function . After importing , will pass a List<float> data type of size 299x299x3 to python function as argument . This passing of argument taking 1.5 seconds where as a block of python code takes only 90 msec .
Here is c# code
using(Py.GIL())
{
dynamic Python_predict = Py.Import("Prediction");
Python_predict.__init__(modelpath);
List<float> imagelist=GetNormalizedFloatBGRImage(image).Data.Cast<float>().ToList();
DateTime t1 = System.DateTime.Now;
Predicted_Type=Python_predict.Findtype(imagelist);
DateTime t2 = System.DateTime.Now;
Console.WriteLine("Predict timing from c# {0} seconds", (t2.Second + ((float)t2.Millisecond / 1000)) - (t1.Second + ((float)t1.Millisecond / 1000)));
}
Here is python code
def __init__(model_path):
kr.model = kr.load_model(model_path) #Model is loaded only once
def Findtype(ImageData):
t1=time.time()
N_array = np.array(ImageData,dtype=np.float64)#creates Numpy array from .net List<float> array
N_array_reshaped=np.reshape(N_array,(299,299,3))
#convert BGR to RGB
temp_array[:,:,0] = N_array_reshaped[:,:,2]#bgR -> Rgb
temp_array[:,:,2] = N_array_reshaped[:,:,0]#Bgr -> rgB
temp_array[:,:,1] = N_array_reshaped[:,:,1]#bGr -> rGb
img_list[0]=temp_array
#prediction = kr.model.predict(img_list, batch_size=1, verbose=0) # Predictions can be made throughout the lifecycle of MakePrediction class's instance
#predicted_class = np.argmax(prediction, axis=1)
t2=time.time()
print('time on python ',t2-t1)
return predicted_class[0] + 1
So c# code send image array of size 2992993 of float time to python , by which it gets converted into numpy array and makes the prediction on gpu . if i run the standalone python code , it takes 90msec but if i call from .net 1.5 sec for passing an argument .
Kindly help me to reduce the timing since it is for real time application .
here is a attached screenshots
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
IRONPYTHON NET4, Embedded, passing class object ...
There are two ways to approaching this: Keep the python "vanilla" (free of .net/IronPython specific code, may be a requirement):.
Read more >Python for .NET: passing List<dynamic> to C# function
I'm trying to call from python code C# functions from a dll using Python for .NET. Most things worked out of the box...
Read more >Pass by Reference in Python: Background and Best Practices
In this tutorial, you'll explore the concept of passing by reference and learn how it relates to Python's own system for handling function...
Read more >A Beginner's Guide to the Python time Module
In this tutorial, you'll learn how to use the Python time module to represent dates and times in your application, manage code execution, ......
Read more >How much do function calls impact performance?
Use a high-precision timer like rdtsc for your CFastTimer implementation; system time() is not nearly precise enough. I think it really depends ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@denfromufa Thanks a lot . Copying from memory pointer worked for me with lot more performance gain() It would be very useful for users , if all these answers are made into a proper pythonnet usage documentation .
Hi, @lostmsu , I manage to make it run by install the python lib with local pythonnet source directory. Thanks.