Deeplearning4j - java - You can register a SameDiff Lambda layer using KerasLayer.registerLambdaLayer(lambdaLayerName, sameDiffLambdaLayer);

Viewed 134

I am trying to load my model.h5 in my java code using deeplearning4j. I am following this tutorial https://deeplearning4j.konduit.ai/keras-import/model-functional

Java code

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

    String path = "randomjk.h5";
    String fullModel = new ClassPathResource(path).getFile().getPath();
    ComputationGraph computationGraph = KerasModelImport.importKerasModelAndWeights(fullModel);
}

Error:

Exception in thread "main" org.deeplearning4j.nn.modelimport.keras.exceptions.UnsupportedKerasConfigurationException: No SameDiff Lambda layer found for Lambda layer lambda_1. You can register a SameDiff Lambda layer using KerasLayer.registerLambdaLayer(lambdaLayerName, sameDiffLambdaLayer);. Please file an issue at https://github.com/eclipse/deeplearning4j/issues.

how i trained in python

dependency :

<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 imports:

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

How i saved in 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'])

How did I created model in python:

hyper['dropout'] = 0.5
model_size = 'L'

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['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()

What i tried so far :

https://github.com/eclipse/deeplearning4j/blob/master/deeplearning4j/deeplearning4j-modelimport/src/test/java/org/deeplearning4j/nn/modelimport/keras/e2e/KerasLambdaTest.java

This sample is implementing SameDiffLambdaLayer but not sure how to map this with my model

In python I provided only hyper params while loading model .Is it different in deeplearning4j?

1 Answers

The error message does tell you what is wrong: You have a lambda layer, i.e. a layer that is defined with a lambda:

split = Lambda(lambda whole: tf.split(whole, num_or_size_splits=hyper['max_len'], axis=1))(x)

As we can't guess what the lambda layer is doing, you have to explicitly define it. First you define a LambdaLayer and then you map it by name. The exception tells you what name is expected.

Related