简体   繁体   中英

Problem in using pre-trained weights in AlexNet (Keras)

I'm using Keras to train an AlexNet model for speech data. I'm going to use this model to predict a number using some wav files as input. I've read similar topics here like this one but my problem is different. My model weights have different shapes from the pre-trained weights'. My pre-trained weights are from bvlc_alexnet.npy . here are my weights VS the pre-trained weights for conv layers :

conv 1 : (11, 11, 3, 96)  vs (11, 11, 3, 96)
conv 2 : (5, 5, 96, 256)  vs (5, 5, 48, 256)
conv 3 : (3, 3, 256, 384) vs (3, 3, 256, 384)
conv 4 : (3, 3, 384, 384) vs (3, 3, 192, 384)
conv 5 : (3, 3, 384, 256) vs (3, 3, 192, 256)

here is my model:

    model = Sequential()
# 1st Convolutional Layer
model.add(Conv2D(filters=96, input_shape=(277, 277, 3), kernel_size=(11, 11), strides=(4, 4),  padding="valid"))
model.add(Activation("relu"))
# Max Pooling
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding="valid"))

# 2nd Convolutional Layer
model.add(Conv2D(filters=256, kernel_size=(11, 11), strides=(1, 1), padding="valid"))
model.add(Activation("relu"))
# Max Pooling
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding="valid"))

# 3rd Convolutional Layer
model.add(Conv2D(filters=384, kernel_size=(3, 3), strides=(1, 1), padding="valid"))
model.add(Activation("relu"))

# 4th Convolutional Layer
model.add(Conv2D(filters=384, kernel_size=(3, 3), strides=(1, 1), padding="valid"))
model.add(Activation("relu"))

# 5th Convolutional Layer
model.add(Conv2D(filters=256, kernel_size=(3, 3), strides=(1, 1), padding="valid"))
model.add(Activation("relu"))
# Max Pooling
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding="valid"))

# Fully Connected layers
model.add(Flatten())
# 1st Fully Connected Layer
model.add(Dense(4096, input_shape=(277 * 277 * 3,)))
model.add(Activation("relu"))
model.add(Dropout(0.4))

# 2nd Fully Connected Layer
model.add(Dense(4096))
model.add(Activation("relu"))
model.add(Dropout(0.4))

# 3rd Fully Connected Layer
model.add(Dense(1000))
model.add(Activation("relu"))
model.add(Dropout(0.4))

# Output Layer
model.add(Dense(10))
model.add(Activation("softmax"))

You can't use the pre-trained weight if your model is different. if you are referring to transfer learning , you need the exact same model and you could only connect output layer from pre-trained weights to new layers you added as input

Details explanation: Inside a neural network as neurons forward into layers, a dot product of weight and input (adding bias if needed) is being calculated and forward propagated into corresponds connected neurons , adding even one neuron or changing the input metrics by a little could result in a total different output (hence totally different weight if retrained) from the neural network, so it is very hard (but not impossible) to trim less important neurons down within layer and still be able to get the neural network to work as before it was trim.

神经网络架构

Model I used before for voice classification

model = Sequential()
input_shape=(128, 128, 1)

model.add(Conv2D(24, (5, 5), strides=(1, 1), input_shape=input_shape))
model.add(MaxPooling2D((4, 2), strides=(4, 2)))
model.add(Activation('relu'))

model.add(Conv2D(48, (5, 5), padding="valid"))
model.add(MaxPooling2D((4, 2), strides=(4, 2)))
model.add(Activation('relu'))

model.add(Conv2D(48, (5, 5), padding="valid"))
model.add(Activation('relu'))

model.add(Flatten())
model.add(Dropout(rate=0.8))

model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(rate=0.8))

model.add(Dense(num_of_class))
model.add(Activation('softmax'))

I used batch size 64 to train and accuracy is 80%+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM