Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NDArray <-> float[] conversion and Fit(IEnumerable<(float[] input, float[] output)>) #258

Open
Darelbi opened this issue Mar 11, 2024 · 1 comment

Comments

@Darelbi
Copy link

Darelbi commented Mar 11, 2024

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:

List<(float[] input, float[] output)> samplesForTraining = new List<(float[] input, float[] output)>();

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.

var x = np.array(samplesForTraining .Select(x => np.array(x.input)));
var y = np.array(samplesForTraining .Select(x => np.array(x.output)));

model.Fit(x, y, batch_size: 2, epochs: 1000, verbose: 1);

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?

var result = model.Predict( np.array( samplesForTraining[0].input)); // how do I cast result to float[] good luck!

the result is not IEnumerable so I cannot use ".To.Array()"

@GilmarCoop
Copy link

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.

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

No branches or pull requests

2 participants