Reputation: 77
I have this multi-input network:
def build_model():
inputRGB = tf.keras.Input(shape=(128,128,3), name='train_ds')
inputFixed = tf.keras.Input(shape=(128,128,3), name='fixed_ds')
inputDinamic = tf.keras.Input(shape=(128,128,3), name='dinamic_ds')
# rete per Immagini RGB
rgb = models.Sequential()
rgb = layers.Conv2D(32, (5, 5), padding='SAME')(inputRGB)
rgb = layers.PReLU()(rgb)
rgb = layers.MaxPooling2D((2, 2))(rgb)
rgb = layers.BatchNormalization()(rgb)
rgb = layers.Conv2D(64, (3, 3))(rgb)
rgb = layers.PReLU()(rgb)
rgb = layers.Conv2D(64, (3, 3))(rgb)
rgb = layers.PReLU()(rgb)
rgb = layers.Conv2D(64, (3, 3))(rgb)
rgb = layers.PReLU()(rgb)
rgb = layers.Dropout(0.5)(rgb)
rgb = layers.GlobalAvgPool2D()(rgb)
rgb = Model(inputs = inputRGB, outputs=rgb)
# rete per Density Map con "Pallini"
fixed = models.Sequential()
fixed = layers.Conv2D(32, (5, 5), padding='SAME')(inputFixed)
fixed = layers.PReLU()(fixed)
fixed = layers.MaxPooling2D((2, 2))(fixed)
fixed = layers.BatchNormalization()(fixed)
fixed = layers.Conv2D(64, (3, 3))(fixed)
fixed = layers.PReLU()(fixed)
fixed = layers.Conv2D(64, (3, 3))(fixed)
fixed = layers.PReLU()(fixed)
fixed = layers.Conv2D(64, (3, 3))(fixed)
fixed = layers.PReLU()(fixed)
fixed = layers.Dropout(0.5)(fixed)
fixed = layers.GlobalAvgPool2D()(fixed)
fixed = Model(inputs = inputFixed, outputs=fixed)
# rete per Density map per "assembramenti"
dinamic = models.Sequential()
dinamic = layers.Conv2D(32, (5, 5), padding='SAME')(inputDinamic)
dinamic = layers.PReLU()(dinamic)
dinamic = layers.MaxPooling2D((2, 2))(dinamic)
dinamic = layers.BatchNormalization()(dinamic)
dinamic = layers.Conv2D(64, (3, 3))(dinamic)
dinamic = layers.PReLU()(dinamic)
dinamic = layers.Conv2D(64, (3, 3))(dinamic)
dinamic = layers.PReLU()(dinamic)
dinamic = layers.Conv2D(64, (3, 3))(dinamic)
dinamic = layers.PReLU()(dinamic)
dinamic = layers.Dropout(0.5)(dinamic)
dinamic = layers.GlobalAvgPool2D()(dinamic)
dinamic = Model(inputs = inputDinamic, outputs=dinamic)
concat = layers.concatenate([rgb.output, fixed.output, dinamic.output]) # merge the outputs of the two models
k = layers.Dense(1)(concat)
modelFinal = Model(inputs={'train_ds':inputRGB, 'fixed_ds':inputFixed, 'dinamic_ds':inputDinamic}, outputs=[k])
opt = tf.keras.optimizers.Adam(learning_rate=0.001, amsgrad=False)
modelFinal.compile(optimizer=opt , loss='mae', metrics=['mae'])
return modelFinal
I would like to extract from the best model, that I've saved and reloaded with this following lines of code:
best_model = tf.keras.models.load_model(checkpoint_path + 'regression_count_128.30-1.11.hdf5')
just the first part of previous shown multi-input neural network. Specifically, I would like to extract the part that takes in the RGB image as input in order to test the model (trained with the 3 different types of images) only on RGB test images.
Upvotes: 0
Views: 1548
Reputation: 33410
Use the name of input layer (which is 'train_ds'
) and the name of output layer of RGB part (which you have not named, but you can use best_model.summary()
to find it; it starts with 'global_average_pooling2d'
) to construct a new model like this:
rgb_model = Model(
best_model.get_layer('train_ds').output,
best_model.get_layer(name_of_rgb_output_layer).output
)
Side note: the lines ... = model.Sequential()
are redundant and could be removed, because you are using functional API of keras to define your models.
Upvotes: 3