Closed Darelbi closed 2 months ago
Hey there! Regarding your last question, I do have an answer. Here's an example I took from my code:
// In my production application, I have the images in a server, but here I'm using a local machine path example
const string imagePath = "C:/Users/{username}/Downloads/bear.jpg";
const int width = 480;
const int height = 480;
const int channels = 3;
var predictionScores = new List<decimal>(); // You can use float, if you wish
var threads = PythonEngine.BeginAllowThreads();
using (Py.GIL())
{
var imageArray = PreProcessing.ImportImage(imagePath, width, height);
var predictions = model.Predict(imageArray);
for (var i = 0; i < np.array(predictions[0]).len; i++)
{
predictionScores.Add((decimal)Math.Round(np.array(predictions[0])[i].item<float>() * 100, 2));
}
}
PythonEngine.EndAllowThreads(threads);
// Here you can transform the list into an array
predictionScores.ToArray();
And inside my PreProcessing class
public static NDarray ImportImage(string imagePath, int width, int height)
{
var img = ImageUtil.LoadImg(imagePath, target_size: (width, height), interpolation: "bilinear");
var originalImageArray = ImageUtil.ImageToArray(img, dtype: "uint8");
var imageArray = np.reshape(originalImageArray, (-1, height, width, 3));
imageArray.astype(np.float32);
imageArray /= 255;
return imageArray;
}
Perhaps this ImportImage
method can also be helpful to your first question.
Stale issue message
The library has some usability issues for c# developers. I understand you needed to leverage the power of underlying stuff but, but c# if well used offers very nice features. I just complain about the following things:
Assume I have this input:
This is just a list of tuples where each tuple contains all inputs for training and all outputs for training.
How do I convert them to NDarrays in order to train? like this? of Course the usual things with neural networks is that input layer is an array of floats, therefore to train you need an array of arrays of floats.
the ideal thing, is that you accept as input to "Fit" method a IEnumerable<(float[] input, float[] output)> because that would also allow to load data on demand => Imagine having a 40 GB dataset, the user could implement his own enumerable that allows to load from HD 5 MB at a time simply by implementing correctly the enumerable. With the currenct C# interface you are just limited by the amount of RAM you have. If you cannot provide all the data at once you are out of the game.
Then the last point, how the heck do you get arrays back?
the result is not IEnumerable so I cannot use ".To.Array()"