Home > Blockchain >  Multiple Input and Multiple Output Tensorflow Model
Multiple Input and Multiple Output Tensorflow Model

Time:12-23

I am trying to build a tensorflow model which takes 3 images as an input and gives 3 output embedding for each input image. The images are saved in a list called triplet The part of code is given below:

def inner_model(act_func='relu', input_shape=(112,112,3):
    model_input = layers.Input(shape=input_shape)
    x = layers.Conv2D(filters=16, kernel_size=3, padding='same', activation=act_func, name='Conv1')(model_input)
    x = layers.MaxPool2D()(x)
    x = layers.Conv2D(filters=32, kernel_size=3, padding='same', activation=act_func, name='Conv2')(x)
    x = layers.MaxPool2D()(x)
    x = layers.Conv2D(filters=64, kernel_size=3, padding='same', activation=act_func, name='Conv3')(x)
    x = layers.MaxPool2D()(x)
    x = layers.Conv2D(filters=128, kernel_size=3, padding='same', activation=act_func, name='Conv4')(x)
    x = layers.MaxPool2D()(x)
    x = layers.Conv2D(filters=256, kernel_size=3, padding='same', activation=act_func, name='Conv5')(x)
    x = layers.GlobalAvgPool2D(name='GAP')(x)
    output = layers.Dense(128, activation=act_func, name='Dense1')(x)
    model = Model(inputs=model_input, outputs=output)
    model.summary()
    return model

def tripler_trainer(input_shape=(112,112,3)):
    anchor_input = layers.Input(shape=input_shape)
    positive_input = layers.Input(shape=input_shape)
    negative_input = layers.Input(shape=input_shape)

    embedder = inner_model(act_func='relu', input_shape=(112,112,3)

    anchor_embedding = embedder(anchor_input)
    positive_embedding = embedder(positive_input)
    negative_embedding = embedder(negative_input)
    outer_network = Model(inputs=(anchor_input, positive_input, negative_input),
                          outputs=(anchor_embedding, positive_embedding, negative_embedding))
    return outer_network

triplet_model = tripler_trainer(input_shape=(112,112,3))
triplets = triplet_model(triplet[0], triplet[1], triplet[2])

When I try to run the code I get the following error:

Traceback (most recent call last):
  File "C:/Users/G5205GK/Desktop/Working Dir/code/con_learning/main.py", line 35, in <module>
    main()
  File "C:/Users/G5205GK/Desktop/Working Dir/code/con_learning/main.py", line 31, in main
    training_instance.train()
  File "C:\Users\G5205GK\Desktop\Working Dir\code\con_learning\train.py", line 36, in train
    anchor_embedding = self.model(triplet[0], triplet[1], triplet[2])
  File "C:\Users\G5205GK\Anaconda3\envs\my_gpu_env\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1013, in __call__
    input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
  File "C:\Users\G5205GK\Anaconda3\envs\my_gpu_env\lib\site-packages\tensorflow\python\keras\engine\input_spec.py", line 200, in assert_input_compatibility
    raise ValueError('Layer '   layer_name   ' expects '  
ValueError: Layer model_1 expects 3 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor: shape=(112, 112, 3), dtype=float32,

Can any one please suggest where the problem is. Also the code provided, is simplified for stack overflow. so it may have syntax issue. Thanks in advance.

CodePudding user response:

You should provide multiple inputs and outputs to your model with square brackets, since your model expects a list of inputs instead of a tuple of inputs. Try this:

import tensorflow as tf

def inner_model(act_func='relu', input_shape=(112,112,3)):
    model_input = tf.keras.layers.Input(shape=input_shape)
    x = tf.keras.layers.Conv2D(filters=16, kernel_size=3, padding='same', activation=act_func, name='Conv1')(model_input)
    x = tf.keras.layers.MaxPool2D()(x)
    x = tf.keras.layers.Conv2D(filters=32, kernel_size=3, padding='same', activation=act_func, name='Conv2')(x)
    x = tf.keras.layers.MaxPool2D()(x)
    x = tf.keras.layers.Conv2D(filters=64, kernel_size=3, padding='same', activation=act_func, name='Conv3')(x)
    x = tf.keras.layers.MaxPool2D()(x)
    x = tf.keras.layers.Conv2D(filters=128, kernel_size=3, padding='same', activation=act_func, name='Conv4')(x)
    x = tf.keras.layers.MaxPool2D()(x)
    x = tf.keras.layers.Conv2D(filters=256, kernel_size=3, padding='same', activation=act_func, name='Conv5')(x)
    x = tf.keras.layers.GlobalAvgPool2D(name='GAP')(x)
    output = tf.keras.layers.Dense(128, activation=act_func, name='Dense1')(x)
    model = tf.keras.Model(inputs=model_input, outputs=output)
    model.summary()
    return model

def tripler_trainer(input_shape=(112,112,3)):
    anchor_input = tf.keras.layers.Input(shape=input_shape)
    positive_input = tf.keras.layers.Input(shape=input_shape)
    negative_input = tf.keras.layers.Input(shape=input_shape)

    embedder = inner_model(act_func='relu', input_shape=(112,112,3))

    anchor_embedding = embedder(anchor_input)
    positive_embedding = embedder(positive_input)
    negative_embedding = embedder(negative_input)
    outer_network = tf.keras.Model(inputs=[anchor_input, positive_input, negative_input],
                          outputs=[anchor_embedding, positive_embedding, negative_embedding])
    return outer_network

triplet_model = tripler_trainer(input_shape=(112,112,3))
triplet1, triplet2, triplet3 = tf.random.normal((1,112,112,3)), tf.random.normal((1,112,112,3)), tf.random.normal((1,112,112,3)) 
triplets = triplet_model([triplet1, triplet2, triplet3])
print(triplets[0].shape, triplets[1].shape, triplets[2].shape)
# (1, 128) (1, 128) (1, 128)
  • Related