onnx / keras-onnx

Convert tf.keras/Keras models to ONNX
Apache License 2.0
379 stars 109 forks source link

The layer <keras.layers.core.Lambda object at 0x0000020B36761208> doesn't have a specific converter, fall back. #580

Open masayakitazawasan opened 4 years ago

masayakitazawasan commented 4 years ago

//question////////// I tried to convert keras model (VAE) to onnx, and onnx model is built. But I noticed error during converting. Why does the error occur?

//Error////////// The layer <keras.layers.core.Lambda object at 0x0000020B36761208> doesn't have a specific converter, fall back.

Further information I built an onnx model as a following code(VAE model by keras)

//code////////// ' image_size = 28 * 8 // network parameters input_shape = (image_size, image_size, 1) intermediate_dim = 512 batch_size = 128 latent_dim = 2 epochs = 1000 // VAE model = encoder + decoder // build encoder model inputs = Input(shape=input_shape, name='encoder_input') x = Conv2D(16, (3, 3), activation='relu', padding='same')(inputs) x = MaxPooling2D((2, 2), padding='same')(x) x = Conv2D(8, (3, 3), activation='relu', padding='same')(x) x = MaxPooling2D((2, 2), padding='same')(x) x = Conv2D(8, (3, 3), activation='relu', padding='same')(x) x = MaxPooling2D((2, 2), padding='same',name='encoded')(x) shape = K.int_shape(x) print("shape[1], shape[2], shape[3]",shape[1], shape[2], shape[3]) x = Flatten()(x)

z_mean = Dense(latent_dim, name='z_mean')(x) z_log_var = Dense(latent_dim, name='z_log_var')(x)

// use reparameterization trick to push the sampling out as input // note that "output_shape" isn't necessary with the TensorFlow backend z = Lambda(sampling, output_shape=(latent_dim,), name='z')([z_mean, z_log_var])

// instantiate encoder model encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder') encoder.summary()

// build decoder model // decoder latent_inputs = Input(shape=(latent_dim,), name='z_sampling') x = Dense(shape[1] shape[2] shape[3], activation='relu')(latent_inputs) x = Reshape((shape[1], shape[2], shape[3]))(x) x = Conv2D(8, (3, 3), activation='relu', padding='same')(x) x = UpSampling2D((2, 2))(x) x = Conv2D(8, (2, 2), activation='relu', padding='same')(x) x = UpSampling2D((2, 2))(x) x = Conv2D(16, (2, 2), activation='relu', padding='same')(x) x = UpSampling2D((2, 2))(x) outputs = Conv2D(1, (2, 2), activation='sigmoid', padding='same')(x)

// instantiate decoder model decoder = Model(latent_inputs, outputs, name='decoder') decoder.summary()

// instantiate VAE model outputs = decoder(encoder(inputs)[2]) vae = Model(inputs, outputs, name='vae_mlp')

// Compute VAE loss reconstruction_loss = binary_crossentropy(K.flatten(inputs), K.flatten(outputs)) reconstruction_loss = image_size image_size kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var) kl_loss = K.sum(kl_loss, axis=-1) kl_loss *= -0.5

vae_loss = K.mean(reconstruction_loss + kl_loss) vae.add_loss(vae_loss) vae.compile(optimizer='adam')

x_train1 = x_train #[y_train==7] x_test1 = x_test #[y_test==7] vae.fit(x_train1,epochs=epochs,batch_size=batch_size,validation_data=(x_test1, None))

import keras2onnx onnx1 = keras2onnx.convert_keras(vae,target_opset=7) onnx.save_model(onnx1,'vae.onnx') '

jiafatom commented 4 years ago

Does the model define the lambda object?