Ошибка загрузки модели keras в Deeplearning4j - java - PullRequest
0 голосов
/ 02 апреля 2020

Я тренировал свою модель в python Керас. Я пытаюсь загрузить это в java код, но получаю следующую ошибку. Как исправить эту проблему.

Ссылка:

https://towardsdatascience.com/deploying-keras-deep-learning-models-with-java-62d80464f34a

https://deeplearning4j.konduit.ai/keras-import/overview

Exception in thread "main" org.deeplearning4j.nn.modelimport.keras.exceptions.InvalidKerasConfigurationException: Model class name must be Sequential (found Model). For more information, see http://deeplearning4j.org/docs/latest/keras-import-overview
    at org.deeplearning4j.nn.modelimport.keras.KerasSequentialModel.<init>(KerasSequentialModel.java:90)
    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 Jktes.jk(Jktes.java:24)
    at Jktes.main(Jktes.java:13)

код:

public static void jk()
    throws IOException, InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {

    String simpleMlp = new ClassPathResource(
        "randomjk.h5").getFile().getPath();
    MultiLayerNetwork model = KerasModelImport.
        importKerasSequentialModelAndWeights(simpleMlp);

}

зависимость:

<dependency>
    <groupId>org.nd4j</groupId>
    <artifactId>nd4j-native-platform</artifactId>
    <version>1.0.0-beta6</version>
</dependency>
<dependency>
    <groupId>org.deeplearning4j</groupId>
    <artifactId>deeplearning4j-modelimport</artifactId>
    <version>1.0.0-beta6</version>
</dependency>
<dependency>
    <groupId>org.deeplearning4j</groupId>
    <artifactId>deeplearning4j-core</artifactId>
    <version>0.9.1</version>
</dependency>

My python -3.6 импорт:

import datetime
import keras.backend as K
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import statistics
import sys
import tensorflow as tf
import uuid

from IPython.display import display, FileLink

from keras.layers import Activation, BatchNormalization, Conv2D, Dense, Dropout, Flatten, Input, Lambda, MaxPooling2D
from keras.models import Model, Sequential, load_model
from keras.optimizers import Adam, SGD

Как я сохранил в python:

model_name_jk = "model_name_jk"
hyper['uuid'] = model_name_jk
stamp('%.1f%% (%.1f%% training) %s' % (test_accuracy, train_accuracy, hyper))
model.save('saved_models/%s.h5' % hyper['uuid'])

Как я создал модель в python:

hyper['dropout'] = 0.5
model_size = 'L'
if model_size == 'S':
    hyper['conv_filters'] = [32, 64]
    hyper['pool_size'] = (8, 8)
elif model_size == 'M':
    hyper['conv_filters'] = [32, 64, 128]
    hyper['pool_size'] = (4, 4)
else:
    hyper['conv_filters'] = [32, 64, 128, 256, 512]
    hyper['pool_size'] = (2, 2)
hyper['batch_normalization'] = True
hyper['dense_units'] = [6144]
hyper['share_per_character_weights'] = False
hyper['post_shared_dense'] = False
hyper['batch_normalization'] = True

def create_per_character_model(activation):
    inputs = Input(shape=(hyper['charset_len'],))
    x = Dense(hyper['charset_len'], activation='softmax')(inputs)
    return Model(inputs, x, name='char_model')

def create_model():
    x = Input(shape=(hyper['image_height'], hyper['image_width'], 1), name='input')
    image_input = x

    # Shared convolutional layers
    for layer, filters in enumerate(hyper['conv_filters']):
        if hyper['batch_normalization']:
            x = BatchNormalization()(x)
        x = Conv2D(filters, (3, 3), strides=(1, 1), padding='same', name=f'conv_{layer}', activation='relu')(x)
        x = MaxPooling2D(pool_size=hyper['pool_size'], padding='same', name=f'maxpool_{layer}')(x)
        x = Dropout(hyper['dropout'], name=f'conv_dropout_{layer}')(x)

    # Shared dense layers
    x = Flatten()(x)
    for layer, units in enumerate(hyper['dense_units']):
        x = Dense(units, activation='relu', name=f'dense_{layer}')(x)
        x = Dropout(hyper['dropout'], name=f'dense_dropout_{layer}')(x)

    x = Dense(hyper['max_len'] * hyper['charset_len'], name='wide_output', activation='linear')(x)

    # Per-character output layers
    split = Lambda(lambda whole: tf.split(whole, num_or_size_splits=hyper['max_len'], axis=1))(x)
    if hyper['share_per_character_weights']:
        per_character_model = create_per_character_model(activation='relu' if hyper['post_shared_dense'] else 'softmax')
        if hyper['post_shared_dense']:
            outputs = [Dense(hyper['charset_len'], name='output_char_%d' % ii, activation='softmax')(per_character_model(split[ii])) for ii in range(hyper['max_len'])]
        else:
            outputs = [per_character_model(split[ii]) for ii in range(hyper['max_len'])]
    else:
        outputs = [Dense(hyper['charset_len'], name='output_char_%d' % ii, activation='softmax')(split[ii]) for ii in range(hyper['max_len'])]

    model = Model(inputs=[image_input], outputs=outputs)
    model.summary()

    return model
model = create_model()

1 Ответ

0 голосов
/ 02 апреля 2020

Вы используете функциональность для последовательного импорта модели , но создаете модель с использованием функционального API.

Для импорта моделей, созданных с помощью функционального API, необходимо использовать другой импортер. https://deeplearning4j.konduit.ai/keras-import/model-functional показывает, как это сделать.

TL; DR заключается в том, что вы должны использовать
KerasModelImport.importKerasModelAndWeights(simpleMlp); вместо KerasModelImport.importKerasSequentialModelAndWeights(simpleMlp);

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...