[英]How to configure DL4j for local images
I'm trying to use DeepLearning4j to categorize 32x32 images in numbers from 0-9.我正在尝试使用 DeepLearning4j 以 0-9 的数字对 32x32 图像进行分类。 I've looked up a number of examples and tutorials, but always run into some exception when fitting the dataset to a network.
我查阅了许多示例和教程,但在将数据集拟合到网络时总是遇到一些异常。
Im currently trying to use a ImageRecordReader with ParentPathLabelGenerator and RecordReaderDataSetIterator.我目前正在尝试将 ImageRecordReader 与 ParentPathLabelGenerator 和 RecordReaderDataSetIterator 一起使用。
The images seem to load fine but i always run into a DL4JInvalidInputException when fitting.图像似乎加载得很好,但我在拟合时总是遇到 DL4JInvalidInputException。
File parentDir = new File(dataPath);
FileSplit filesInDir = new FileSplit(parentDir, NativeImageLoader.ALLOWED_FORMATS);
ParentPathLabelGenerator labelMaker = new ParentPathLabelGenerator();
BalancedPathFilter pathFilter = new BalancedPathFilter(new Random(), labelMaker, 100);
InputSplit[] filesInDirSplit = filesInDir.sample(pathFilter, 80, 20);
InputSplit trainData = filesInDirSplit[0];
InputSplit testData = filesInDirSplit[1];
ImageRecordReader recordReader = new ImageRecordReader(numRows, numColumns, 3, labelMaker);
recordReader.initialize(trainData);
DataSetIterator dataIter = new RecordReaderDataSetIterator(recordReader, 1, 1, outputNum);
When using DenseLayer:使用 DenseLayer 时:
Exception in thread "main" org.deeplearning4j.exception.DL4JInvalidInputException: Input that is not a matrix; expected matrix (rank 2), got rank 4 array with shape [1, 3, 32, 32]. Missing preprocessor or wrong input type? (layer name: layer0, layer index: 0, layer type: DenseLayer)
When using ConvolutionLayer the error occures at the OutputLayer:使用 ConvolutionLayer 时,OutputLayer 会出现错误:
Exception in thread "main" org.deeplearning4j.exception.DL4JInvalidInputException: Input that is not a matrix; expected matrix (rank 2), got rank 4 array with shape [1, 1000, 28, 28]. Missing preprocessor or wrong input type? (layer name: layer1, layer index: 1, layer type: OutputLayer)
Is my attempt at loading the images incorrect or is my network misconfigured?是我加载图像的尝试不正确还是我的网络配置错误?
Configuration:配置:
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.list()
.layer(0, new ConvolutionLayer.Builder()
.nIn(3) // Number of input datapoints.
.nOut(1000) // Number of output datapoints.
.activation(Activation.RELU) // Activation function.
.weightInit(WeightInit.XAVIER) // Weight initialization.
.build())
.layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
.nIn(1000)
.nOut(outputNum)
.activation(Activation.SOFTMAX)
.weightInit(WeightInit.XAVIER)
.build())
.build();
The easiest way is to use the .setInputType
configuration option when defining the network.最简单的方法是在定义网络时使用
.setInputType
配置选项。 It will set up all the necessary pre-processors for you, and it will calculate all the correct .nIn
values too.它将为您设置所有必要的预处理器,并且还将计算所有正确的
.nIn
值。
Take another look at this example https://github.com/eclipse/deeplearning4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/convolution/mnist/MnistClassifier.java#L156再看看这个例子https://github.com/eclipse/deeplearning4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/convolution/mnist/MnistClassifier.java#L156
When you use the .setInputType
way of setting up your network, you don't have to set any .nIn
values at all - you still can, as is evident in the example I've linked, but usually there is no good reason to do so.当您使用
.setInputType
设置网络的方式时,您根本不需要设置任何.nIn
值 - 您仍然可以,正如我链接的示例所示,但通常没有充分的理由这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.