Closed
Description
Environment
- 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 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 .