deepjavalibrary / djl

An Engine-Agnostic Deep Learning Framework in Java
https://djl.ai
Apache License 2.0
4.13k stars 655 forks source link

Support need while trying to run a predicition. #1578

Closed DxsSucuk closed 2 years ago

DxsSucuk commented 2 years ago

Hey! Basicly I am trying to train my first model which itself work but now I have Issues with the translator, I continue to get a "The array has not been initialized", and I am unsure how to fix this Issue. I am thankfully for every kind of help!

Current Translator:

        Translator<Image, Classifications> translator = new Translator<>() {

        @Override
        public Classifications processOutput(TranslatorContext ctx, NDList list) {
            // Create a Classifications with the output probabilities
            NDArray probabilities = list.singletonOrThrow().softmax(0);
            List<String> classNames = IntStream.range(0, output).mapToObj(String::valueOf).toList();
            return new Classifications(classNames, probabilities);
        }

        @Override
        public NDList processInput(TranslatorContext ctx, Image input) {
            NDArray array = input.toNDArray(ctx.getNDManager(), Image.Flag.COLOR);
            return new NDList(NDImageUtils.toTensor(NDImageUtils.resize(array, 128 * 128)));
        }

        @Override
        public Batchifier getBatchifier() {
            return Batchifier.STACK;
        }
    }; 

Model Creator:

   long inputSize = 128 * 128 * 3;

    SequentialBlock sequentialBlock = new SequentialBlock();

    sequentialBlock.add(Blocks.batchFlattenBlock(inputSize));
    sequentialBlock.add(Linear.builder().setUnits(128).build());
    sequentialBlock.add(Activation::relu);
    sequentialBlock.add(Linear.builder().setUnits(64).build());
    sequentialBlock.add(Activation::relu);
    sequentialBlock.add(Linear.builder().setUnits(outputSize).build());

    Model model = Model.newInstance("mlp");
    model.setBlock(sequentialBlock);

Trainer:

    trainer.initialize(new Shape(128 * 128 * 3));

    EasyTrain.fit(trainer, 2, dataset, null);

    saveModel(model);
DxsSucuk commented 2 years ago

If more of my code is required to understand and find the cause of the Issue, I am gladly read to share it.

DxsSucuk commented 2 years ago

I have now uploaded my Project on Github, if that helps in any kind of way. https://github.com/DxsSucuk/PissAI

frankfliu commented 2 years ago

@DxsSucuk After trained and saved your model, you need load the parameters into your model for inference. See our inference example:

https://github.com/deepjavalibrary/djl/blob/master/examples/src/main/java/ai/djl/examples/inference/ImageClassification.java#L58-L63

DxsSucuk commented 2 years ago

Thanks it helped and yeah I just forgot to load the model data!