Search code examples
nd4jdl4j

dl4j - Unable to get number of of rows for a non 2d matrix


I am current using nd4j and dl4j to implement some deep learning algorithm. However, I can not get datavec + dl4j working in the first place.

Here is my image converter:

public class ImageConverter {

    private static Logger log = LoggerFactory.getLogger(ImageConverter.class);

    public DataSetIterator Convert() throws IOException, InterruptedException {
        log.info("Start to convert images...");

        File parentDir = new File(System.getProperty("user.dir"), "src/main/resources/images/");
        ParentPathLabelGenerator parentPathLabelGenerator = new ParentPathLabelGenerator();

        ImageRecordReader recordReader = new ImageRecordReader(28,28,1,parentPathLabelGenerator);

        FileSplit fs = new FileSplit(parentDir);
        InputSplit[] filesInDirSplit = fs.sample(null, 100);

        recordReader.initialize(filesInDirSplit[0]);
        DataSetIterator dataIter = new RecordReaderDataSetIterator(recordReader, 2, 1, 2);

        log.info("Image convert finished.");

        return dataIter;
    }

}

Here is the main class:

ImageConverter icv = new ImageConverter();
        DataSetIterator dataSetIterator = icv.Convert();

        log.info("Build model....");
        int numEpochs = 10;

        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
              .seed(123)
              .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
              .iterations(1)
              .learningRate(0.006)
              .updater(Updater.NESTEROVS).momentum(0.9)
              .regularization(true).l2(1e-4)
              .list()
              .layer(0, new ConvolutionLayer.Builder(5, 5)
                      .nIn(28 * 28)
                      .stride(1, 1)
                      .nOut(20)
                      .activation("identity")
                      .build())
              .layer(1, new OutputLayer.Builder(LossFunction.NEGATIVELOGLIKELIHOOD)
                      .nIn(24 * 24)
                      .nOut(2)
                      .activation("softmax")
                      .build())
              .pretrain(false)
              .backprop(true)
              .build();

        MultiLayerNetwork model = new MultiLayerNetwork(conf);
        model.init();
        model.setListeners(new ScoreIterationListener(1));


        log.info("Train model....");
        for( int i=0; i<numEpochs; i++ ){
            model.fit(dataSetIterator);
        }

In the image folders, I have some grey scale 28x28 images located in sub-folder a,b respectively.

However, Exception in thread "main" java.lang.IllegalStateException: Unable to get number of of rows for a non 2d matrix is thrown.

Looking into the data by dataSetIterator.next().toString(), it is something like:

[[[...],
...
]]

=================OUTPUT==================
[[1.00, 0.00],
 [1.00, 0.00]]

Moreover, the output of dataSetIterator.next().get(0).toString() is

[[[[...],
...
]]]

=================OUTPUT==================
[1.00, 0.00]

And for the mnisterIterator in the examples, mnisterIterator.next().toString() should be something like:

[[...]...]
=================OUTPUT==================
[[0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
...]

From which I infer that the dataSetIterator I returned contained data in wrong format.

Anybody knows how to fix it?


Solution

  • We've implemented this for you already in our examples.

    4d and 2d don't matter to us, just specify the convolution layer setup: https://github.com/deeplearning4j/dl4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/convolution/LenetMnistExample.java#L84

    If you face anymore trouble, where you think a discussion would be easier, come on our gitter: https://gitter.im/deeplearning4j/deeplearning4j

    Thanks!