简体   繁体   中英

NullPointer Exception on initialization of BasicMLDataSet with Input and Ideal Double 2d Arrays

I am currently working on a CNN that can tell the difference between a dog and a cat. This is the code I have worked out so far:

public bool trained = false;
public BasicNetwork network;
public Form1()
{
  InitializeComponent();
}
[..]
private void trainNN()
{    
  double[][] input = LoadImages();
  double[][] ideal = LoadIdeal();
  var trainingSet = new BasicMLDataSet(input, ideal);
  var train = new ResilientPropagation(network, trainingSet);
  network = CreateNetwork();
}
    private double[][] LoadImages()
    {
        status.Text = "Loading images...";
        String[] dogimgs = Directory.GetFiles(Directory.GetCurrentDirectory() + "\\dog_img\\", "*", SearchOption.TopDirectoryOnly);
        String[] catimgs = Directory.GetFiles(Directory.GetCurrentDirectory() + "\\cat_img\\", "*", SearchOption.TopDirectoryOnly);
        int dogimgscount = dogimgs.Length;
        int catimgscount = catimgs.Length;
        int totalimgscount = dogimgscount + catimgscount;
        double[][] images = new double[totalimgscount][];
        for (int dogloop = 0; dogloop < dogimgscount; dogloop++)
        {
            status.Text = "Loading images... [" + (dogloop + 1) + "/" + totalimgscount + "]";
            images[dogloop] = Image2Matrix(new Bitmap(dogimgs[dogloop]));
        }
        for (int catloop = 0; catloop < catimgscount; catloop++)
        {
            status.Text = "Loading images... [" + (catloop + dogimgscount) + "/" + totalimgscount + "]";
            images[catloop + dogimgscount - 1] = Image2Matrix(new Bitmap(catimgs[catloop]));
        }
        status.Text = "Images loaded.";
        return images;
    }
    private double[][] LoadIdeal()
    {
        String[] dogimgs = Directory.GetFiles(Directory.GetCurrentDirectory() + "\\dog_img\\", "*", SearchOption.TopDirectoryOnly);
        String[] catimgs = Directory.GetFiles(Directory.GetCurrentDirectory() + "\\cat_img\\", "*", SearchOption.TopDirectoryOnly);
        int dogimgscount = dogimgs.Length;
        int catimgscount = catimgs.Length;
        int totalimgscount = dogimgscount + catimgscount;
        double[][] ideal = new double[totalimgscount][];
        for (int dogloop = 0; dogloop < dogimgscount; dogloop++)
        {
            ideal[dogloop] = new[] { 0.0, 1.0 };
        }
        for (int catloop = 0; catloop < catimgscount; catloop++)
        {
            ideal[catloop + dogimgscount - 1] = new[] { 1.0, 0.0 };
        }
        return ideal;
    }

I know this might not be the smartest way to load the images, but I just want to see the concept work before I start boosting performance. My problem is the following: If I put 4 images, 2 in dog_img, 2 in cat_img, the program loads the images fine and both the input and the ideal array have a length of 4 and they both are filled with double values. But on the line

var trainingSet = new BasicMLDataSet(input, ideal);

the program throws a NullPointerException Error. Both arrays are clearly initialised and not null or empty, yet it still throws the error. Any help is appreciated. Thank you. FritzFurtz

Encog does not support CNN's, all input to Encog neural networks are 1D vectors. So, if you want to feed an image to Encog, you need to squash it to a 1D vector and wrap it in a BasicMLData object. Encog is probably not the best solution for computer vision/CNN. For C#, I would look at CNTK .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM