我目前正在使用nd4j和dl4j来实现一些深度学习算法。但是,我无法让datavec + dl4j在第一时间工作。
这是我的图像转换器:
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;
}
}
这是主要课程:
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);
}
在图像文件夹中,我有一些灰度28x28图像分别位于子文件夹a
,b
。
但是,Exception in thread "main" java.lang.IllegalStateException: Unable to get number of of rows for a non 2d matrix
被抛出。
按dataSetIterator.next().toString()
查看数据,类似于:
[[[...],
...
]]
=================OUTPUT==================
[[1.00, 0.00],
[1.00, 0.00]]
此外,dataSetIterator.next().get(0).toString()
的输出是
[[[[...],
...
]]]
=================OUTPUT==================
[1.00, 0.00]
对于示例中的mnisterIterator,mnisterIterator.next().toString()
应该是这样的:
[[...]...]
=================OUTPUT==================
[[0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
...]
我从中推断出我返回的dataSetIterator
包含的格式错误。
有人知道如何解决它吗?
答案 0 :(得分:1)
我们已经在我们的示例中为您实现了这一点。
4d和2d对我们不重要,只需指定卷积层设置: https://github.com/deeplearning4j/dl4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/convolution/LenetMnistExample.java#L84
如果你再遇到麻烦,你认为讨论会更容易,请来吧! https://gitter.im/deeplearning4j/deeplearning4j
谢谢!
答案 1 :(得分:0)
您的数据集是4d矩阵,但MNist需要2d矩阵https://github.com/deeplearning4j/deeplearning4j/issues/2010