Skip to content

Can't create layer of type "Sum" in function 'getLayerInstance' #14312

@alexionby

Description

@alexionby

Strange error when I am trying to convert model from keras to onnx and run through opencv dnn.

  • OpenCV == 4.1.0
  • Operating System / Platform == Ubuntu 16.04 / 64 Bit
  • Compiler == conda 4.6.11 / libgcc-ng 8.2.0
  • onnxmltools == 1.4.0 / onnx = 1.4.1
  • keras == 2.2.4 / tensorflow == 1.13.1
import os
os.environ['CUDA_VISIBLE_DEVICES'] = ''

import numpy as np

from keras import layers
from keras.models import Model
from keras import backend as K

def get_model():
    inp = layers.Input(shape=(48,80,1))
    x = layers.Conv2D(32, 7, padding='same')(inp)
    x = layers.BatchNormalization()(x)
    x = layers.ReLU()(x)
    x = layers.Reshape([48, 80 * 32, 1])(x)
    x = layers.Lambda(lambda t: K.sum(t, axis=2))(x)
    return Model(inp, x)

if __name__ == '__main__':

    model = get_model()
    print(model.summary())

    model.compile(optimizer='adam', loss='mse')

    X = np.random.rand(32, 48, 80, 1)
    y = np.random.rand(32, 48, 1)

    model.fit(X, y, epochs=1, validation_data=(X, y))
    model.save('model.h5')

Then I converted .h5 model to .pb with repo keras_to_tensorflow and command python keras_to_tensorflow/keras_to_tensorflow.py --input_model="model.h5" --output_model="model.pb" and tried to make inference:

import cv2
net = cv2.dnn.readNetFromTensorflow('model.pb')
shape = (80,48)
image = cv2.imread('image.jpg', 0)
input = cv2.dnn.blobFromImage(image, 1, shape)
net.setInput(input)
output = net.forward()

And achieved an error:

Traceback (most recent call last):
  File "convert_utils/cv_pb_test.py", line 21, in <module>
    output = net.forward('lambda_1/Sum')
cv2.error: OpenCV(4.1.0) ../modules/dnn/src/dnn.cpp:524: error: (-2:Unspecified error) Can't create layer "lambda_1/Sum" of type "Sum" in function 'getLayerInstance'

Models: models.zip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions