# Keras Import: java.lang.String cast to java.util.List

**URL:** <https://community.konduit.ai/t/keras-import-java-lang-string-cast-to-java-util-list/3412>\
**Category:** DL4J\
**Created:** [January 15, 2026, 3:53pm UTC](https://community.konduit.ai/t/keras-import-java-lang-string-cast-to-java-util-list/3412 "2026-01-15T15:53:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelauder](https://avatars.discourse-cdn.com/v4/letter/k/dc4da7/32.png) [@kelauder](https://community.konduit.ai/u/kelauder)\
**Post date:** [January 15, 2026, 3:53pm UTC](https://community.konduit.ai/t/keras-import-java-lang-string-cast-to-java-util-list/3412/1 "2026-01-15T15:53:00Z")

</div>

I’m trying to import a ConvNet that was created it Python. I tried both separated Json model with H5 weights and combined H5 model.

The java code is:

```
    String modelPath = "Model.h5";

    ComputationGraph model = KerasModelImport.importKerasModelAndWeights(modelPath, false);

```

and the error:

Exception in thread “main” java.lang.ClassCastException: class java.lang.String cannot be cast to class java.util.List (java.lang.String and java.util.List are in module java.base of loader ‘bootstrap’)  
at org.deeplearning4j.nn.modelimport.keras.KerasModel.(KerasModel.java:150)  
at org.deeplearning4j.nn.modelimport.keras.KerasModel.(KerasModel.java:97)  
at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelBuilder.buildModel(KerasModelBuilder.java:311)  
at org.deeplearning4j.nn.modelimport.keras.KerasModelImport.importKerasModelAndWeights(KerasModelImport.java:167)  
at kerasloadmodule.Main.main(Main.java:18)

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [January 16, 2026, 12:36am UTC](https://community.konduit.ai/t/keras-import-java-lang-string-cast-to-java-util-list/3412/2 "2026-01-16T00:36:55Z")

</div>

@kelauder this gives me literally _nothing_ to work with. What model are you trying to import? Which version of keras?

---

<div class="post-metadata">

**Author:** ![kelauder](https://avatars.discourse-cdn.com/v4/letter/k/dc4da7/32.png) [@kelauder](https://community.konduit.ai/u/kelauder)\
**Post date:** [January 16, 2026, 3:15pm UTC](https://community.konduit.ai/t/keras-import-java-lang-string-cast-to-java-util-list/3412/3 "2026-01-16T15:15:23Z")

</div>

Here is the simple test test I tried:

print(tf.keras.\__version_\_)

inputs = tf.keras.layers.Input(shape=(48, 48, 3))

x = tf.keras.layers.Conv2D(filters=64, kernel\_size=(3, 3), strides=(2, 2), padding=‘valid’, activation=‘relu’, name=‘block1\_conv1’)(inputs)

x = tf.keras.layers.MaxPool2D(pool\_size=(2, 2), strides=(2, 2), name=‘block1\_pool’)(x)

x = tf.keras.layers.Flatten(name=‘flatten’)(x)

x = tf.keras.layers.Dense(units=1024, activation=‘relu’, name=‘fc1’)(x)

outputs = tf.keras.layers.Dense(100, activation=‘softmax’, name=‘predictions’)(x)

model = tf.keras.Model(inputs=inputs, outputs=outputs, name=‘Simple’)

model.summary()

model.save(“Simple.h5”)

And the output:

Keras: 3.13.0

2026-01-16 06:49:51.010420: I tensorflow/core/platform/cpu\_feature\_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.  
To enable the following instructions: SSE3 SSE4.1 SSE4.2 AVX AVX2 AVX\_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.  
Model: “Simple”  
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┓  
┃ Layer (type) ┃ Output Shape ┃ Param # ┃  
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━┩  
│ input\_layer (InputLayer) │ (None, 48, 48, 3) │ 0 │  
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤  
│ block1\_conv1 (Conv2D) │ (None, 23, 23, 64) │ 1,792 │  
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤  
│ block1\_pool (MaxPooling2D) │ (None, 11, 11, 64) │ 0 │  
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤  
│ flatten (Flatten) │ (None, 7744) │ 0 │  
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤  
│ fc1 (Dense) │ (None, 1024) │ 7,930,880 │  
├──────────────────────────────────────┼─────────────────────────────┼─────────────────┤  
│ predictions (Dense) │ (None, 100) │ 102,500 │  
└──────────────────────────────────────┴─────────────────────────────┴─────────────────┘  
Total params: 8,035,172 (30.65 MB)  
Trainable params: 8,035,172 (30.65 MB)  
Non-trainable params: 0 (0.00 B)  
WARNING:absl:You are saving your model as an HDF5 file via `model.save()` or `keras.saving.save_model(model)`. This file format is considered legacy. We recommend using instead the native Keras format, e.g. `model.save('my_model.keras')` or `keras.saving.save_model(model, 'my_model.keras')`.

The Java Code:

public class Main {

```
public static void main(String\[\] args) throws Exception {

    String modelPath = "Simple.h5";

    ComputationGraph model = KerasModelImport.importKerasModelAndWeights(modelPath, false);

    model.init();

    

    System.out.println(model.summary());

}

```

}

---

<div class="post-metadata">

**Author:** ![kelauder](https://avatars.discourse-cdn.com/v4/letter/k/dc4da7/32.png) [@kelauder](https://community.konduit.ai/u/kelauder)\
**Post date:** [January 21, 2026, 11:38pm UTC](https://community.konduit.ai/t/keras-import-java-lang-string-cast-to-java-util-list/3412/4 "2026-01-21T23:38:45Z")

</div>

OK, I downgraded to Keras 2 and the Import didn’t fail!

But I needed verify that it is importing correctly, so I did a test:

In Python I loaded VGG16 and processed two images from the ImageNet dataset (first one shown):

model = VGG16(weights=‘imagenet’, include\_top=True)

# Save the weights to an HDF5 file

model.save(“VGG16Model.h5”)  
img\_path = ‘n01440764\_10026.JPEG’  
img = image.load\_img(img\_path, target\_size=(224, 224)) # VGG16 expects 224x224 input  
x = image.img\_to\_array(img) # Convert PIL image to a numpy array (H, W, C)  
x = np.expand\_dims(x, axis=0) # Add a batch dimension (N, H, W, C)  
x = preprocess\_input(x) # Apply VGG16-specific preprocessing (BGR conversion, zero-centering)

# Save input to compare with java

np.savetxt(‘n01440764\_10026.csv’, x.flatten(), delimiter=‘,’)

predictions = model.predict(x)

print(np.max(predictions))  
print(np.argmax(predictions))

Output:  
0.75161654  
0

Then in Java:  
public class Main {  
public static void main(String args) throws Exception {  
ComputationGraph vgg16Network = KerasModelImport.importKerasModelAndWeights(“VGG16Model.h5”, false);

```
  vgg16Network.init();

  // System.out.println(vgg16Network.summary());

  File imageFile = new File("n01440764_10026.JPEG");

  // Load the image into an INDArray (DL4J's matrix format)
  NativeImageLoader loader = new NativeImageLoader(224, 224, 3); 
  INDArray image = loader.asMatrix(imageFile);

  // Preprocess the image (mean subtraction is specific to VGG16)
  VGG16ImagePreProcessor preProcessor = new VGG16ImagePreProcessor();
  preProcessor.transform(image); 
  image = image.permute(0, 2, 3, 1); // fix INDArray order

  // Save INDArray to compare with Python
  INDArray flat = image.ravel();
  writeINDArrayToCsv(flat, "n01440764_10026.csv");  

  INDArray output = vgg16Network.outputSingle(image);

  System.out.println(output.maxNumber() + " " + output.argMax());

```

}

public static void writeINDArrayToCsv(INDArray array, String filePath) {  
try (CSVWriter writer = new CSVWriter(new FileWriter(filePath))) {  
long rows = array.shape()[0];

```
     for (long i = 0; i < rows; i++) {
        String[] rowData = new String[1];
        rowData[0] = String.valueOf(array.getDouble(i));
        writer.writeNext(rowData);
     }
  } catch (IOException e) {
     e.printStackTrace();
  }

```

}

}

Output: 0.47710591554641724 0

The other image: Python = 0.9145643, 2 and Java = 0.9373587369918823 2

Neither one matches.

Note in the code that I saved the input array post processing and the flattened arrays were identical. So now I’m confused as to why they aren’t working exactly the same.

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [March 4, 2026, 11:53pm UTC](https://community.konduit.ai/t/keras-import-java-lang-string-cast-to-java-util-list/3412/5 "2026-03-04T23:53:01Z")

</div>

@kelauder if you are still wanting to give any of this a shot I have published some fixes for keras import to snapshots. Let me know if you want help and I”m happy to help you give it a proper shot.
