我自己的图像上的异常检测使用deeplearning4j

时间:2017-01-10 03:43:30

标签: deeplearning4j

我将使用deeplearning4j平台上的示例对我自己的图像进行异常检测。我改变了这样的代码:

    int rngSeed=123;
    Random rnd = new Random(rngSeed);
    int width=28;
    int height=28;
    int batchSize = 128;
    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
            .seed(12345)
            .iterations(1)
            .weightInit(WeightInit.XAVIER) 
            .updater(Updater.ADAGRAD)
            .activation(Activation.RELU)
            .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
            .learningRate(0.05)
            .regularization(true).l2(0.0001)
            .list()
            .layer(0, new DenseLayer.Builder().nIn(784).nOut(250)
                    .build())
            .layer(1, new DenseLayer.Builder().nIn(250).nOut(10)
                    .build())
            .layer(2, new DenseLayer.Builder().nIn(10).nOut(250)
                    .build())
            .layer(3, new OutputLayer.Builder().nIn(250).nOut(784)
                    .lossFunction(LossFunctions.LossFunction.MSE)
                    .build())
            .pretrain(false).backprop(true)
            .build();

    MultiLayerNetwork net = new MultiLayerNetwork(conf);
    net.setListeners(Collections.singletonList((IterationListener) new ScoreIterationListener(1)));
    File trainData = new File("mnist_png/training");
    FileSplit fsTrain = new FileSplit(trainData, NativeImageLoader.ALLOWED_FORMATS, rnd);

    ImageRecordReader recorderReader = new ImageRecordReader(height, width);
    recorderReader.initialize(fsTrain);


    DataSetIterator dataIt = new RecordReaderDataSetIterator(recorderReader, batchSize);
    List<INDArray> featuresTrain = new ArrayList<>();
    while(dataIt.hasNext()){
        DataSet ds = dataIt.next();
        featuresTrain.add(ds.getFeatureMatrix());
    }


    System.out.println("************ training **************");
    int nEpochs = 30;
    for( int epoch=0; epoch<nEpochs; epoch++ ){
        for(INDArray data : featuresTrain){
            net.fit(data,data);
        }
        System.out.println("Epoch " + epoch + " complete");
    }

它在训练时引发了异常:

Exception in thread "main" org.deeplearning4j.exception.DL4JInvalidInputException: Input that is not a matrix; expected matrix (rank 2), got rank 4 array with shape [128, 1, 28, 28]
    at org.deeplearning4j.nn.layers.BaseLayer.preOutput(BaseLayer.java:363)
    at org.deeplearning4j.nn.layers.BaseLayer.activate(BaseLayer.java:384)
    at org.deeplearning4j.nn.layers.BaseLayer.activate(BaseLayer.java:405)
    at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.activationFromPrevLayer(MultiLayerNetwork.java:590)
    at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.feedForwardToLayer(MultiLayerNetwork.java:713)
    at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.computeGradientAndScore(MultiLayerNetwork.java:1821)
    at org.deeplearning4j.optimize.solvers.BaseOptimizer.gradientAndScore(BaseOptimizer.java:151)
    at org.deeplearning4j.optimize.solvers.StochasticGradientDescent.optimize(StochasticGradientDescent.java:54)
    at org.deeplearning4j.optimize.Solver.optimize(Solver.java:51)
    at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.fit(MultiLayerNetwork.java:1443)
    at org.deeplearning4j.nn.multilayer.MultiLayerNetwork.fit(MultiLayerNetwork.java:1408)
    at org.deeplearning4j.examples.dataExamples.AnomalyTest.main(AnomalyTest.java:86)

似乎我的输入数据集有4列,而它只需要2个字符,所以问题是如何转换imagerecorderread或其他东西以使其正常运行?

1 个答案:

答案 0 :(得分:1)

首先,您可能想要了解张量是什么: http://nd4j.org/tensor

记录阅读器返回多维图像,您需要将其展平以便与2d神经网络一起使用,除非您计划在部分训练中使用CNN。

如果你看一下例外(你真的应该熟悉ndarrays,它们不是新的并且在每个深度学习库中使用):你会看到一个形状: [128,1,28,28]

这是按行x列的通道批量大小。你需要做一个: .setInputType(InputType.convolutional(28,28,1))

这将告诉dl4j需要将4d压平为2d。在这种情况下,它表示存在28 x 28 x 1

的行,列,通道

如果将其添加到配置的底部,它将起作用。

值得注意的是,如果您正在尝试进行异常检测,我们也会提供您可能想要查看的变分自动编码器。