0

I am trying to create a tower like the one mentioned here (How to have parallel convolutional layers in keras? ) with VGG16 and VGG19. I am reading images from as directory using flow_from_directory for train and valid. I will load VGG16 and VGG19 using pre-trained imagenet-weights and then merge the layers as inputs to some other models. I am having problems trying to figure out how to have the same input fed to multiple models. I have come across this generator function in one forum that I am using. This will feed multiple images to a network as input. However, this seems like an overkill for my case.

def generate_generator_multiple(generator,dir1, dir2, batch_size, img_height,img_width):
    genX1 = generator.flow_from_directory(dir1, target_size = (img_height,img_width), class_mode = 'categorical', batch_size = batch_size, shuffle=False, seed=7)
    genX2 = generator.flow_from_directory(dir2, target_size = (img_height,img_width), class_mode = 'categorical', batch_size = batch_size, shuffle=False, seed=7)
    while True:
            X1i = genX1.next()
            X2i = genX2.next()

            yield [X1i[0], X2i[0]], X2i[1]  #Yield both images and their mutual label 

Is there a simpler way to feed same input to multiple networks instead of giving multiple inputs. I have tried the code from https://datascience.stackexchange.com/questions/30423/how-to-pass-common-inputs-to-a-merged-model-in-keras?answertab=active#tab-top, but it gives me Graph disconnected error.

user1102886
  • 57
  • 2
  • 10

1 Answers1

0

Here is an simple example showing you how to do this, the key is to put the neural network model into a function

import keras
import numpy as np
import tensorflow as tf
from keras.layers import Input, Dense

tf.reset_default_graph()

# assume this is your model
def nn_model(input_x, n):
    feature_maker = Dense(n, activation='relu')(input_x)
    feature_maker = Dense(20, activation='relu')(feature_maker)
    feature_maker = Dense(1, activation='linear')(feature_maker)
    return feature_maker

input_layer = Input(shape=(3, ))
output_1 = nn_model(input_layer, 10)
output_2 = nn_model(input_layer, 20)
output_3 = nn_model(input_layer, 30)

model = keras.models.Model(inputs=input_layer, outputs=[output_1, output_2, output_3])

You can plot this model by

from keras.utils.vis_utils import plot_model

plot_model(model, show_shapes=True)

The model looks like

enter image description here

And this model can be trained by

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

# example training set
x = np.linspace(1, 90, 270).reshape(90, 3)
y1 = np.random.rand(90)
y2 = np.random.rand(90) * 2
y3 = np.random.rand(90) * 3

model.fit(x, [y1, y2, y3], batch_size=32, epochs=10)
meTchaikovsky
  • 7,478
  • 2
  • 15
  • 34
  • So if I want the output of VGG16, what should I write in the nn_model function ? This is the problem I had before. – user1102886 Oct 11 '19 at 20:19