无法加载没有卷积层的keras模型,但可以加载包含conv层的模型

我试图在dl4j中使用预训练的keras模型,并且可以正确加载(不是真的,但这是不同的问题),但是当我删除卷积层时,会出现空指针异常。

正确加载的模型:

model = Sequential([
    Conv1D(filters = filter_size, kernel_size = filter_size , input_shape = X.shape[1:], activation = 'relu'),
    Conv1D(filters = filter_size, kernel_size = filter_size , input_shape = X.shape[1:], activation = 'relu'),
    Flatten(), 
    Dense(128, activation='relu'), 
    Dense(128, activation='relu'), 
    Dense(128, activation='relu'),
    Dropout(0.1),
    Dense(Y.shape[1])
])

奇怪的是,该模型根本无法加载:

model = Sequential([
    Flatten(), 
    Dense(128, activation='relu'), 
    Dense(128, activation='relu'), 
    Dense(128, activation='relu'),
    Dropout(0.1),
    Dense(Y.shape[1])
])

用Python保存模型

model.save('radius_position_nn.h5')

用Java加载

        try {
            String modelPatch = new ClassPathResource(modelName).getFile().getPath();
            model = KerasModelImport.importKerasSequentialModelAndWeights(modelPatch);
        } catch (IOException | UnsupportedKerasConfigurationException | InvalidKerasConfigurationException e) {
            e.printStackTrace();
        }

堆栈跟踪:

java.lang.NullPointerException
    at org.deeplearning4j.nn.modelimport.keras.layers.KerasInput.<init>(KerasInput.java:103)
    at org.deeplearning4j.nn.modelimport.keras.layers.KerasInput.<init>(KerasInput.java:83)
    at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.<init>(KerasSequentialModel.java:120)
    at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.<init>(KerasSequentialModel.java:57)
    at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelBuilder.buildSequential(KerasModelBuilder.java:322)
    at org.deeplearning4j.nn.modelimport.keras.KerasModelImport.importKerasSequentialModelAndWeights(KerasModelImport.java:223)
    at NeuralNetwork.<init>(NeuralNetwork.java:17)
    at Main.setup(Main.java:35)
    at processing.core.PApplet.handleDraw(PApplet.java:2412)
    at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
    at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

在我看来,这完全是倒退,我甚至不知道问题可能在哪里。

评论