使用deeplearning4j(https://deeplearning4j.org/)在Java中打开keras模型时遇到问题

时间:2019-04-18 20:43:37

标签: java tensorflow keras deeplearning4j

我使用keras + tensorflow训练了一个小模型。在这种模型下,我尝试识别一些图像类别。当我使用python + keras加载此模型时,它工作正常,并以令人满意的方式进行了预测。

当我尝试使用deeplearning4j lib在Java应用程序中打开此模型时,问题就开始了

下面是用于创建keras模型并将其导出到.h5文件和java import .h5文件的源代码

这是我在python中定义的模型结构


img_width, img_height = 150, 150
batch_size = 32
samples_per_epoch = 1000
validation_steps = 300
nb_filters1 = 32
nb_filters2 = 64
conv1_size = 3
conv2_size = 2
pool_size = 2
classes_num = 3
lr = 0.0004

model = Sequential()
model.add(Convolution2D(nb_filters1, conv1_size, conv1_size, border_mode ="same", input_shape=(img_width, img_height, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))

model.add(Convolution2D(nb_filters2, conv2_size, conv2_size, border_mode ="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size), dim_ordering='th'))


model.add(Flatten())
model.add(Dense(256))
model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Dense(classes_num, activation='softmax'))


model.compile(loss='categorical_crossentropy',
              optimizer=optimizers.RMSprop(lr=lr),
              metrics=['accuracy'])
...
...
...

model.save('./testesAndre/Exemplos_Keras/model/Docs_model.h5')

此处为打开keras模型的Java代码

import org.deeplearning4j.nn.modelimport.keras.KerasModelImport;

...

String simpleMlp = new ClassPathResource(sFilePath).getFile().getPath();
this.kerasH5Model = KerasModelImport.importKerasSequentialModelAndWeights(simpleMlp,true);

当我尝试使用deeplearning4j lib在Java中导入模型时出错

org.deeplearning4j.nn.modelimport.keras.exceptions.InvalidKerasConfigurationException: Cannot assign arrays: arrays must both be scalars, both vectors, or shapes must be equal other than size 1 dimensions. Attempting to do x.assign(y) with x.shape=[87616, 256] and y.shape=[88800, 256]
Tried to set weights for layer with name dense_1, of class org.deeplearning4j.nn.conf.layers.DenseLayer.
Failed to set weights for parameter W
Expected shape for this parameter: Rank: 2,Offset: 0
 Order: f Shape: [87616,256],  stride: [1,87616], 
got: Rank: 2,Offset: 0
 Order: c Shape: [88800,256],  stride: [256,1]. For more information, see http://deeplearning4j.org/model-import-keras.
    at org.deeplearning4j.nn.modelimport.keras.KerasLayer.copyWeightsToLayer(KerasLayer.java:334)
    at org.deeplearning4j.nn.modelimport.keras.utils.KerasModelUtils.copyWeightsToModel(KerasModelUtils.java:76)
    at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.getMultiLayerNetwork(KerasSequentialModel.java:248)
    at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.getMultiLayerNetwork(KerasSequentialModel.java:235)
    at org.deeplearning4j.nn.modelimport.keras.KerasModelImport.importKerasSequentialModelAndWeights(KerasModelImport.java:181)

对不起,如果我犯了一些主要错误,因为这是我第一次使用python和deeplearnig4l lib

谢谢大家的帮助

0 个答案:

没有答案