I am trying to load a .h5 model (tf.keras) in scala using deeplearning4j.
I’m using SBT with following dependencies :
libraryDependencies ++= Seq(
"org.deeplearning4j" % "deeplearning4j-core" % "1.0.0-M2.1",
"org.nd4j" % "nd4j-native" % "1.0.0-M2.1",
)
When I try to load models containing LSTM or RNN layers, the following error is raised:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x000072104b173475, pid=76220, tid=76222
#
# JRE version: OpenJDK Runtime Environment (11.0.24+8) (build 11.0.24+8-post-Ubuntu-1ubuntu322.04)
# Java VM: OpenJDK 64-Bit Server VM (11.0.24+8-post-Ubuntu-1ubuntu322.04, mixed mode, sharing, tiered, compressed oops, g1 gc, linux-amd64)
# Problematic frame:
# C [libnd4jcpu.so+0x1573475] sd::NDArray::getOffset(long long) const+0x25
#
# Core dump will be written. Default location: Core dumps may be processed with "/usr/share/apport/apport -p%p -s%s -c%c -d%d -P%P -u%u -g%g -- %E" (or dumping to /home/userr/IdeaProjects/Tor_scala/core.76220)
#
# An error report file with more information is saved as:
# /home/userr/IdeaProjects/Tor_scala/hs_err_pid76220.log
Process finished with exit code 130 (interrupted by signal 2:SIGINT)
However, models containing layers other than LSTM/RNN doesnt raise any errors. I tried loading models containing Dense layers and CNN. They worked perfectly. This issue was seen only when I try with LSTM/RNN layers.
I tried with other versions - M2, M1.1 and M1
The same issue was seen with M2 version too.
But when I used M1.1 and M1, I got the following error for all models regardless of the type of layers used in the model:
Debug: Deallocating org.bytedeco.hdf5.Group[address=0x71ce3568b530,position=0,limit=1,capacity=1,deallocator=org.bytedeco.javacpp.Pointer$NativeDeallocator[ownerAddress=0x71ce3568b530,deallocatorAddress=0x71ce026a7dc0]]
[error] java.lang.NullPointerException: Cannot invoke "org.deeplearning4j.nn.modelimport.keras.KerasLayer.getNumParams()" because the return value of "java.util.Map.get(Object)" is null
[error] at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelUtils.importWeights(KerasModelUtils.java:235)
[error] at org.deeplearning4j.nn.modelimport.keras.KerasModel.<init>(KerasModel.java:190)
[error] at org.deeplearning4j.nn.modelimport.keras.KerasModel.<init>(KerasModel.java:99)
[error] at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelBuilder.buildModel(KerasModelBuilder.java:311)
[error] at org.deeplearning4j.nn.modelimport.keras.KerasModelImport.importKerasModelAndWeights(KerasModelImport.java:150)
[error] at tesst$.main(tesst.scala:13)
[error] at tesst.main(tesst.scala)
[error] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
[error] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] at java.base/java.lang.reflect.Method.invoke(Method.java:569)
[error] stack trace is suppressed; run last Compile / run for the full output
[error] (Compile / run) java.lang.NullPointerException: Cannot invoke "org.deeplearning4j.nn.modelimport.keras.KerasLayer.getNumParams()" because the return value of "java.util.Map.get(Object)" is null
[error] Total time: 3 s, completed 21-Oct-2024, 2:26:27 pm
My model was compiled and trained in Python and saved as .h5. Following is my scala code for loading the model:
import org.deeplearning4j.nn.modelimport.keras.KerasModelImport
import org.deeplearning4j.nn.graph.ComputationGraph
object tesst {
def main(args: Array[String]): Unit = {
// Path to your .h5 file
System.setProperty("org.bytedeco.javacpp.logger.debug", "true")
System.setProperty("org.bytedeco.javacpp.logger", "sl4j")
val modelPath="/home/userr/Models/lstm_actual.h5"
val model: ComputationGraph = KerasModelImport.importKerasModelAndWeights(modelPath,false)
println(model.summary())
}
}
I’m new to DL4J. Can someone guide me and let me know why it happens?