3

I've got a fully-convolutional, pretrained model in a .h5 file. Now I would like to change the input resolution and train again.

My current approach is to go through all the layers, create a new layer and assign the pretrained weights.

Here is a minimal sample:

from keras.layers import Input
from keras.layers import BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.models import Model

# this would be the pretrained model
input_layer = Input((10, 10, 3))
conv = Conv2D(16, 3)(input_layer)
bnorm = BatchNormalization()(conv)
model = Model(inputs = input_layer, outputs = bnorm)


# now I want to create a new model with the same architecture but different sizes
new_input = Input((100,100,3))
prev_layer = new_input

for old_layer in model.layers[1:]:
    weights = old_layer.weights

    if type(old_layer) == Conv2D:
        filters = old_layer.filters
        kernel_size = old_layer.kernel_size

        conv_layer = Conv2D(filters = filters, 
                           kernel_size = kernel_size,
                           )(prev_layer)
        prev_layer = conv_layer

    elif type(old_layer) == BatchNormalization:
        bn_layer = BatchNormalization(weights=weights)
        prev_layer = bn_layer(prev_layer)

The code for the Batchnormalization fails. The error message is rather long, the key problem seems to be:

ValueError: Shapes must be equal rank, but are 1 and 0 for 'batch_normalization_3/Assign' (op: 'Assign') with input shapes: [16], [].

The full error message is on pastebin: https://pastebin.com/NVWs4tq2

If I remove the weights parameter in the constructor of the batchnormalization, the code works fine. I've taken a look at the weights that I'm trying to provide in the constructor and at the weights allocated if no weights are provided. The shapes are identical.

[<tf.Variable 'batch_normalization_1/gamma:0' shape=(16,) dtype=float32_ref>,
 <tf.Variable 'batch_normalization_1/beta:0' shape=(16,) dtype=float32_ref>,
 <tf.Variable 'batch_normalization_1/moving_mean:0' shape=(16,) dtype=float32_ref>,
 <tf.Variable 'batch_normalization_1/moving_variance:0' shape=(16,) dtype=float32_ref>]

How can I load weights into a Batchnormalization ?

lhk
  • 27,458
  • 30
  • 122
  • 201

0 Answers0