Hari
Hari

Reputation: 83

Using pre-trained weights in Alexnet model in Keras

I'm trying to implement AlexNet using pre-trained weights from bvlc_alexnet.npy:

#load the weight data
weights_dic = numpy.load('bvlc_alexnet.npy', encoding='bytes').item()
conv1W = weights_dic["conv1"][0] # <class 'numpy.ndarray'> (11, 11, 3, 96)
conv1b = weights_dic["conv1"][1] # <class 'numpy.ndarray'> (96,)

model = Sequential()
model.add(Conv2D(96, kernel_size=[11, 11], kernel_initializer = <???>, 
             bias_initializer = <???>, dtype=np.ndarray), activation='relu', strides=4, padding="same")

Here I'm stuck with how to assign these weights (conv1W and conv1b) to kernel_initializer and to bias_initializer attributes.

Upvotes: 2

Views: 2609

Answers (1)

today
today

Reputation: 33410

First construct the model without the need to set any initializers. Then put all the weights in a list in the same order that the layers appear in the model (e.g. conv1_weights, conv1_biases, conv2_weights, conv2_biases, etc.) and then call set_weights method of the model:

model.set_weights(weights)

Alternatively, you can set the weights of each layer individually:

model.layers[layer_index].set_weights([layer_weights, layer_biases])

# or using layer's name if you have specified names for them
model.get_layer(layer_name).set_weights([layer_weights, layer_biases])

Upvotes: 3

Related Questions