gosha20777 / keras2cpp

it's a small library for running trained Keras 2 models from a native C++ code.
MIT License
183 stars 54 forks source link

Multiple Input #6

Open BrownBear2 opened 5 years ago

BrownBear2 commented 5 years ago

Hi, your library looks very nice. Are you planning on adding support for multiple inputs?

When load InputLayers I get the error

"keras2cpp.py", line 214, in export_model
    f.write(struct.pack('I', LAYERS.index(type(layer)) + 1))
ValueError: tuple.index(x): x not in tuple
gosha20777 commented 5 years ago

Tell me please what do you mean by multiple input? What exactly is a mistake? What is your model structure?

BrownBear2 commented 5 years ago

There's an alternative to sequential models in keras, where you can use different input layers and later on merge them by adding, concatenating and so on. Like this: https://keras.io/getting-started/functional-api-guide/

Even using an Input layer to construct something equivalent to a sequential model doesn't work, because your library doesn't know InputLayer and the merge Layers. Simplest example:

inp = Input(input_shape=(10,10))
out = Dense(5)(inp)
model = Model(inputs=[inp], outputs=[out])
aniketsnv-1997 commented 5 years ago

Hello @gosha20777 Your library is awesome! Thanks for it. I tried to use it for my model which is based on MNIST model. Unfortunately, I encountered this error: Traceback (most recent call last): File "/home/aniketsnv/Desktop/keras2cpp-master/mnistnew.py", line 62, in <module> export_model(model1, 'MNIST.model') File "/home/aniketsnv/Desktop/keras2cpp-master/keras2cpp.py", line 213, in export_model f.write(struct.pack('I', LAYERS.index(type(layer)) + 1)) ValueError: tuple.index(x): x not in tuple The training dataset is in the form of csv containing 2786 tuples and 784 features. total number of classes are 29. Here is the model architecture:

model = keras.Sequential([ keras.layers.Flatten(input_shape=(784, )), keras.layers.Dense(1024, activation=tf.nn.relu), keras.layers.Dense(512, activation=tf.nn.relu), keras.layers.Dense(256, activation=tf.nn.relu), keras.layers.Dense(128, activation=tf.nn.relu), keras.layers.Dense(29, activation=tf.nn.softmax)

Can you advice me what changes should I make in keras2.cpp ?

TikhonovAleksey-AI commented 3 years ago

Hello @gosha20777, your library is really very good. Thanks for it! I am having a problem export model with multiple inputs. My architecture model:

vgg_net_1 = VGG19(weights='imagenet', include_top=False, input_shape=(256, 256, 3)) vgg_net_2 = VGG19(weights='imagenet', include_top=False, input_shape=(256, 256, 3)) vgg_net_1._name = 'vgg19_1' vgg_net_2._name = 'vgg19_2'

vgg_net_1.trainable = True trainable = False for layer in vgg_net_1.layers: if ( (layer.name == 'block2_conv1') or (layer.name == 'block3_conv1') or (layer.name == 'block4_conv1') or (layer.name == 'block5_conv1') ): trainable = True layer.trainable = trainable

vgg_net_2.trainable = True trainable = False for layer in vgg_net_2.layers: if ( (layer.name == 'block2_conv1') or (layer.name == 'block3_conv1') or (layer.name == 'block4_conv1') or (layer.name == 'block5_conv1') ): trainable = True layer.trainable = trainable

inputA = Input(shape=input_shape) inputB = Input(shape=input_shape)

x = vgg_net_1(inputA) x = Model(inputs=inputA, outputs=x, name='MapNetSAT')

y = vgg_net_2(inputB) y = Model(inputs=inputB, outputs=y, name='MapNetELEV')

combined = concatenate( [x.output, y.output] )

z = Flatten()(combined) z = Dense(512, activation='relu')(z) z = Dense(256, activation='relu')(z) z = Dense(128, activation='relu')(z) z = Dense(64, activation='relu')(z) z = Dropout(0.5)(z) z = Dense(1, activation='sigmoid')(z)

model = Model(inputs=[x.input, y.input], outputs=z, name='MapNet')

learning_rate_fn = tf.keras.optimizers.schedules.PiecewiseConstantDecay( [1030, 1545], [1e-5, 1e-6, 1e-7])

model.compile(loss='binary_crossentropy', optimizer=Adam(learning_rate=learning_rate_fn), metrics=['accuracy'])

When load InputLayers I get the error:

Using TensorFlow backend. Exporting layer: <class 'keras.engine.input_layer.InputLayer'> Traceback (most recent call last): File "F:\PythonProject\MyPro\ExportNetModel.py", line 16, in <module> export_model(model, 'expNetModel.model') File "F:\PythonProject\MyPro\keras2cpp.py", line 217, in export_model f.write(struct.pack('I', LAYERS.index(type(layer)) + 1)) ValueError: tuple.index(x): x not in tuple

What should I change in the code to make the export work !? I really need help! Thank!

Upliner commented 1 year ago

There is experimental support for multiple inputs in by fork of the library