【问题标题】:Changing pretrained AlexNet classification in Keras在 Keras 中更改预训练的 AlexNet 分类
【发布时间】:2016-06-26 19:53:35
【问题描述】:

我正在使用具有预训练权重(heuritech/convnets-keras)的 AlexNet 来解决 8 个类而不是 1000 个类的分类问题。在使用 Model(input=..,output=..) 初始化网络并加载初始权重后,我删除了最后两层, Dense(1000) 和 Activation(softmax),并添加我自己的两层:Dense(8) 和 Activation(softmax)。 但是,运行后我得到一个错误

Error when checking model target: expected softmax to have shape (None, 1000) but got array with shape (32, 8)

32 是我猜来自生成器的批量大小,但我不明白为什么 softmax 仍然期望前一层的 1000 维。

有人可以帮助我吗?我认为它与模型的输出参数有关,但这只是在尝试和谷歌搜索之后的半疯狂猜测。 谢谢!

代码:

import ...

pp = os.path.dirname(os.path.abspath(__file__))

##### Define Model #####
inputs = Input(shape=(3,227,227))
conv_1 = Convolution2D(96, 11, 11,subsample=(4,4),activation='relu', name='conv_1')(inputs)
...
...
...
dense_1 = MaxPooling2D((3, 3), strides=(2,2),name="convpool_5")(conv_5)
dense_1 = Flatten(name="flatten")(dense_1)
dense_1 = Dense(4096, activation='relu',name='dense_1')(dense_1)
dense_2 = Dropout(0.5)(dense_1)
dense_2 = Dense(4096, activation='relu',name='dense_2')(dense_2)
dense_3 = Dropout(0.5)(dense_2)  
dense_3 = Dense(1000,name='dense_3')(dense_3)
prediction = Activation("softmax",name="softmax")(dense_3)

model = Model(input=inputs, output=prediction)

for layer in model.layers[:27]:
    print layer.name
    layer.trainable = False

model.load_weights(pp+"/weights/alexnet_weights.h5")
print model.output_shape

print model.layers[-1]
model.layers.pop()
print model.output_shape
model.layers.pop()
print model.layers[-1]
print model.output_shape
model.layers.append(Dense(8, activation='softmax',name='dense_4'))

print model.layers[-1]
#####  Get Data #####
train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        pp+'/dataset/training', 
        target_size=(227,227), 
        class_mode='categorical')  

validation_generator = test_datagen.flow_from_directory(
        pp+'/dataset/test',
        target_size=(227,227),
        class_mode='categorical')

##### Compile and Fit ####
sgd = SGD(lr=1e-4, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='mse')

model.fit_generator(
        train_generator,
        samples_per_epoch=500,
        nb_epoch=5,
        validation_data=validation_generator,
        nb_val_samples=150)

model.save_weights('first_try.h5')

【问题讨论】:

    标签: neural-network theano deep-learning conv-neural-network keras


    【解决方案1】:

    好的,看来我不能只更改网络定义,因为即使在弹出/放入新层之后,似乎也没有任何改变。 所以我这样做了:

    1) Load the default AlexNet
    
    2) Load the pre-trained weights
    
    3) Pop the 2 top layers
    
    4) Add two new top layers
    
    5) Save the weights
    
    6) Change Network definition to use the two new layers
    
    7) Load the new AlexNet with the saved weights
    
    8) Profit!
    

    虽然我仍然想知道如何更改由功能 api 定义的加载网络。

    【讨论】:

    • 也许我的回答here可以帮助你:)
    猜你喜欢
    • 2019-06-25
    • 2017-06-15
    • 2018-08-24
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多