在Keras的Alexnet模型中使用预先训练的权重

问题描述:

我正在尝试使用 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")

在这里,我陷入了如何将这些权重(conv1Wconv1b)分配给kernel_initializerbias_initializer属性的问题.

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

首先构造模型,而无需设置任何初始化程序.然后按照图层在模型中出现的顺序将所有权重放入列表中(例如conv1_weights,conv1_biases,conv2_weights,conv2_biases等),然后调用模型的set_weights方法:

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])