Error running dl4j library in android studio

Hi,
I’m working on a project for school using the dl4j library (in android studio) . I checked the documentation on konduit but it appeared to be incorrect. I had many errors with duplication of classes and after I fixed that up I still had a few errors that I had to fix.
Now I am able to run the code but I still get an error when running:

MultiLayerNetwork neuralNetwork = new MultiLayerNetwork(configuration);
neuralNetwork.init();
the gradle code that I added is:
packagingOptions{

pickFirst 'lib/x86/*'
pickFirst 'org/bytedeco/javacpp/windows-x86/*'
pickFirst 'org/bytedeco/javacpp/windows-x86_64/*'

}
and
dependencies {
implementation(group: ‘org.deeplearning4j’, name: ‘deeplearning4j-core’, version: ‘0.9.1’) {
exclude group: ‘org.bytedeco’, module: ‘opencv-platform’
exclude group: ‘org.bytedeco’, module: ‘leptonica-platform’
exclude group: ‘org.bytedeco’, module: ‘hdf5-platform’
exclude group: ‘org.nd4j’, module: ‘nd4j-base64’
exclude group: ‘org.nd4j’, module: ‘protobuf’
exclude group: ‘com.google.guava’, module: ‘guava’

    exclude group: 'com.bytedeco', module: 'javacpp'

}

implementation (group: 'org.nd4j', name: 'nd4j-native', version: '1.0.0-beta6'){exclude group: 'org.nd4j', module: 'protobuf'}
implementation (group: 'org.nd4j', name: 'nd4j-native', version: '1.0.0-beta6', classifier: "android-arm"){exclude group: 'org.nd4j', module: 'protobuf'}
implementation (group: 'org.nd4j', name: 'nd4j-native', version: '1.0.0-beta6', classifier: "android-arm64"){exclude group: 'org.nd4j', module: 'protobuf'}
implementation (group: 'org.nd4j', name: 'nd4j-native', version: '1.0.0-beta6', classifier: "android-x86"){exclude group: 'org.nd4j', module: 'protobuf'}
implementation (group: 'org.nd4j', name: 'nd4j-native', version: '1.0.0-beta6', classifier: "android-x86_64"){exclude group: 'org.nd4j', module: 'protobuf'}
implementation group: 'org.bytedeco', name: 'openblas', version: '0.3.7-1.5.2'
implementation group: 'org.bytedeco', name: 'openblas', version: '0.3.7-1.5.2', classifier: "android-arm"
implementation group: 'org.bytedeco', name: 'openblas', version: '0.3.7-1.5.2', classifier: "android-arm64"
implementation group: 'org.bytedeco', name: 'openblas', version: '0.3.7-1.5.2', classifier: "android-x86"
implementation group: 'org.bytedeco', name: 'openblas', version: '0.3.7-1.5.2', classifier: "android-x86_64"
implementation group: 'org.bytedeco', name: 'opencv', version: '4.1.2-1.5.2'
implementation group: 'org.bytedeco', name: 'opencv', version: '4.1.2-1.5.2', classifier: "android-arm"
implementation group: 'org.bytedeco', name: 'opencv', version: '4.1.2-1.5.2', classifier: "android-arm64"
implementation group: 'org.bytedeco', name: 'opencv', version: '4.1.2-1.5.2', classifier: "android-x86"
implementation group: 'org.bytedeco', name: 'opencv', version: '4.1.2-1.5.2', classifier: "android-x86_64"
implementation group: 'org.bytedeco', name: 'leptonica', version: '1.78.0-1.5.2'
implementation group: 'org.bytedeco', name: 'leptonica', version: '1.78.0-1.5.2', classifier: "android-arm"
implementation group: 'org.bytedeco', name: 'leptonica', version: '1.78.0-1.5.2', classifier: "android-arm64"
implementation group: 'org.bytedeco', name: 'leptonica', version: '1.78.0-1.5.2', classifier: "android-x86"
implementation group: 'org.bytedeco', name: 'leptonica', version: '1.78.0-1.5.2', classifier: "android-x86_64"
testImplementation 'junit:junit:4.13.2'

the error I get is:
Process: com.example.myapplication, PID: 15183
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/common/base/Predicates;
at org.reflections.scanners.AbstractScanner.(AbstractScanner.java:21)
at org.reflections.scanners.TypeAnnotationsScanner.(TypeAnnotationsScanner.java:8)
at org.reflections.util.ConfigurationBuilder.(ConfigurationBuilder.java:53)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration.registerSubtypes(NeuralNetConfiguration.java:456)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration.configureMapper(NeuralNetConfiguration.java:421)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration.initMapper(NeuralNetConfiguration.java:394)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration.(NeuralNetConfiguration.java:123)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration$Builder.build(NeuralNetConfiguration.java:1223)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration$ListBuilder.build(NeuralNetConfiguration.java:279)
at com.example.myapplication.MainActivity.aa(MainActivity.java:112)
at com.example.myapplication.MainActivity.onCreate(MainActivity.java:48)
at android.app.Activity.performCreate(Activity.java:7994)
at android.app.Activity.performCreate(Activity.java:7978)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.ClassNotFoundException: Didn’t find class “com.google.common.base.Predicates” on path: DexPathList[[dex file “/data/data/com.example.myapplication/code_cache/.overlay/base.apk/classes3.dex”, zip file “/data/app/~~LVTWN8DGRcSyfrUoVu2Arw==/com.example.myapplication-R6jpmC6qTDVtIy-1r-YrBg==/base.apk”],nativeLibraryDirectories=[/data/app/~~LVTWN8DGRcSyfrUoVu2Arw==/com.example.myapplication-R6jpmC6qTDVtIy-1r-YrBg==/lib/x86, /data/app/~~LVTWN8DGRcSyfrUoVu2Arw==/com.example.myapplication-R6jpmC6qTDVtIy-1r-YrBg==/base.apk!/lib/x86, /system/lib, /system_ext/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at org.reflections.scanners.AbstractScanner.(AbstractScanner.java:21)
at org.reflections.scanners.TypeAnnotationsScanner.(TypeAnnotationsScanner.java:8)
at org.reflections.util.ConfigurationBuilder.(ConfigurationBuilder.java:53)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration.registerSubtypes(NeuralNetConfiguration.java:456)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration.configureMapper(NeuralNetConfiguration.java:421)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration.initMapper(NeuralNetConfiguration.java:394)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration.(NeuralNetConfiguration.java:123)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration$Builder.build(NeuralNetConfiguration.java:1223)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration$ListBuilder.build(NeuralNetConfiguration.java:279)
at com.example.myapplication.MainActivity.aa(MainActivity.java:112)
at com.example.myapplication.MainActivity.onCreate(MainActivity.java:48)
at android.app.Activity.performCreate(Activity.java:7994)
at android.app.Activity.performCreate(Activity.java:7978)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I would greatly appreciate any help you can offer with this problem.
Thanks in advance

@ciheching could you specify more about your environment? Did you try our example first to see if that works or not? deeplearning4j-examples/android-examples at master · eclipse/deeplearning4j-examples · GitHub

I’m using dl4j version 0.9.1. Maybe the errors are because of that?
I checked the gradle file in the example but I’m still getting errors.

@ciheching could you tell me what prompted you to use that version in partciular? I would highly advise using at least M1.1. That version is more than 2 years old at this point.

If there’s some docs that told you to do that I would appreciate knowing so we can remove that. Thanks.

I used that version because I learned how to use the library with that version (in a Udemy course) and I saw that the newer version has many changes
so I decided to use that version because I need to complete this project quickly.

@ciheching ahhh I didn’t know there was an udemy course. Thanks for telling me! I understand your situation a bit and will keep an eye on that. Please do let me know if you have any issues with the upgrade.

so your saying I should use the new version and check if i have problems with it?
can you give me a simple example NN so I can check if it works maybe a XOR or something of the sort the examples that I saw on github are old

@ciheching could you clarify where you are getting your examples exactly? Is it also the udemy course? Those examples are not in anyway endorsed by or related to the project. The only ones are in the linked repo at: GitHub - eclipse/deeplearning4j-examples: Deeplearning4j Examples (DL4J, DL4J Spark, DataVec)

The XOR example is here: deeplearning4j-examples/ModelXOR.java at master · eclipse/deeplearning4j-examples · GitHub

i created a code for training for one image (letter or number ) input:

public void train(Context context,Bitmap bm, int output) {
    
    try {
        nn = MultiLayerNetwork.load(new File(path),true);
    } catch (Exception e) {

            //no RGB channels just a single channel
            int numOfChannels = 1;
            int width = 28;
            int height = 28;
            //dwe have two classes (happy and sad encoded in digits 0 and 1)
            int numOfOutput = 36;
            //all the items
            int batchSize = 16;
            //an epoch is defined as a full pass of the data set
            int numOfEpochs = 200;
            //the number of parameter updates in a row, for each minibatch
            int seed = 123;
            double rate = 0.0015; // learning rate
            Random random = new Random();

            MultiLayerConfiguration configuration = new NeuralNetConfiguration.Builder()
                    .seed(seed)
                    .activation(Activation.RELU)
                    .weightInit(WeightInit.XAVIER)
                    .updater(new Nadam())
                    .l2(rate * 0.005) // regularize learning model
                    .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
                    .list()

                    //the filter (feature detector) size is 5x5
                    .layer(0, new ConvolutionLayer.Builder(2, 2)
                            //black and white image so no RGB components just 1 channel
                            .nIn(numOfChannels)
                            //takes only a single step over and then down as it slides the filter across the input volume
                            .stride(1, 1)
                            //number of kernels: so the number of feature maps in the output
                            .nOut(20)
                            .activation(Activation.RELU)
                            .build())
                    //pooling layer AKA subsampling layer with MAX pooling
                    .layer(1, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX)
                            //filter size is 2x2
                            .kernelSize(2, 2)
                            //it will step two values as it slides horizontally and two values when it steps down to the next row
                            .stride(1, 1)
                            .build())
                    //convolutional layers + pooling layers right after each other
                    .layer(2, new DenseLayer.Builder().activation(Activation.RELU)
                            .nOut(500).build())
                    .layer(3, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
                            .nOut(numOfOutput)
                            .activation(Activation.SOFTMAX)
                            .build())
                    //emoji dataset 28x28x1 grayscale images
                    .setInputType(InputType.convolutional(28, 28, 1))
                    .build();

            nn = new MultiLayerNetwork(configuration);
            nn.init();

            try {
                nn.save(new File(path), true);
            }
            catch(Exception e2)
            {
                Toast.makeText(context,"error with saving model" + e2.toString(),Toast.LENGTH_SHORT).show();
                return;
            }
        INDArray train = Nd4j.zeros(28, 28);
        double temp = 0;
        bm = Bitmap.createScaledBitmap(bm,28,28,false);
        for (int i = 0;i<28;i++)
            for (int j = 0;j<28;j++){
                train.putScalar(new int[]{i,j},bm.getPixel(i,j)/255);
            }
        INDArray answer = Nd4j.zeros(1, 36);
           answer.putScalar(output,1,1);
            nn.fit(train,answer);
    }

}
does this look good?
I had no problems when running the XOR on 1.0.0-M2

@ciheching yeah that covers the basics. Thanks for checking!

what i wanted you for the most part to confirm was the saving of the cnn and loading and the input to the cnn if i did it correctly(the INDarray)

@ciheching that really depends on your problem. We have an image loader you can just use directly without going through all the pain here as well: deeplearning4j/AndroidNativeImageLoader.java at master · eclipse/deeplearning4j · GitHub

Maybe consider using that instead of doing whatever it is you’re trying to do here yourself.
If you really do need to use bitmaps for some reason then go ahead and do it like that. Pixels are generally 1 - 255 so that should work fine.

Some problems don’t require that kind of normalization though. Double check if your images are fully colored or not. If it’s just 0 and 1 then there’s no point in normalizing. Typical normalization makes data scale 0 to 1.

Beyond that, for some reason it looks like you save the model before calling fit? Is there a point in that? Is that supposed to be a checkpoint or did you intend to save it after the model is updated?

yes i made a mistake i meant to move it to the end the modeling i need to use bitmaps because Im using android studio and its much easier for me to use the bitmaps
iif you can just tell me if this part of the code is correct:

 INDArray answer = Nd4j.zeros(1, 36);
           answer.putScalar(output,1,1);
            nn.fit(train,answer);
i created here the vector for the output nodes with the value one in the corresponding place for the number/letter is this the correct way to do it and when i want to predict a value for an image when i get the output vector it will be an INDarray that is a 1 row by output columns? and is there a easy way for me to check what value in the array has the highest value or do i need to go thru it all with a for loop and check whats the highest?

@ciheching yeah that’s all fine. You’d need to be more specific to get better help from me. Yes you’re using put correctly.

For your labels yes softmax requires it to be in the right column. The correct label should be 1 with everything else being zero.
The output shape will be n (where n is the number of images you put in also called the batch size) by the number of possible labels.

if you can please confirm: when I use the method model.fit(INDarray, INDarray) I am training the model with just one training example and one label, and when I use the method model.fit(dataiter, dataiter) I am training the model with an entire data set (examples, labels)?

thank you

@ciheching sorry just now catching up. Fit on a dataset is just a batch. Fit on an iterator is the whole dataset.

how can i fit for one example?

for an img do i take the entire image and add it as one row?

‪On Mon, May 23, 2022 at 1:48 PM ‫חגי השינג‬‎ <ciheching@gmail.com> wrote:‬

am i supposed to create a INDarray that is 4d and the values (1(batch size),1(channels),imghieght,imgwidth)?

‪On Mon, May 23, 2022 at 1:55 PM ‫חגי השינג‬‎ <ciheching@gmail.com> wrote:‬

@ciheching for images yes it’s typically n (number of images), channels, height width. This is also known as NCHW. Number, Channels, Height, Width layout. Within the framework you can also tell it which one to use for computation. By default it’s NCHW though.