How to load model weights partially? For example I want to load only block1 of VGG19 model using original imagenet weights(vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5):
def VGG19_part(input_shape=None):
img_input = tf.keras.layers.Input(shape=input_shape)
# Block 1
x = tf.keras.layers.Conv2D(64, (3, 3),
activation='linear',
padding='same',
name='block1_conv1')(img_input)
x = tf.keras.layers.Activation('relu')(x)
x = tf.keras.layers.Conv2D(64, (3, 3),
activation='linear',
padding='same',
name='block1_conv2')(x)
x = tf.keras.layers.Activation('relu')(x)
x = tf.keras.layers.MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
model = tf.keras.Model(img_input, x, name='vgg19')
model.load_weights('/Users/myuser/.keras/models/vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5')
print(model.summary())
return model
This code produce an error: ValueError: You are trying to load a weight file containing 16 layers into a model with 2 layers.