尝试重建VGG16的参数太多

时间:2018-07-07 00:10:36

标签: python keras computer-vision conv-neural-network

所以我试图使用VGG16架构和imagenet权重在keras中进行一些迁移学习。我希望对最后几层进行随机权重和偏差训练,因此我使用model.pop()摆脱了它们,并手动添加了最后几层。当调用model.summary()时,它看起来完全一样,除了最后一层卷积层(我添加的层)中的参数数量从约230万增加到5100万。我不确定是什么原因导致参数的巨大增加?谁可以给我解释一下这个?这导致我的计算机在以下情况下资源用尽(我假设是ram,因为我只有16 GB)。以下是我用来构建模型的代码。

def build_model():
model = VGG16(weights='imagenet',include_top=True)
model.input
model.summary()
for l in model.layers:
    l.trainable =False
model.layers.pop()
model.layers.pop()
model.layers.pop()
model.layers.pop()
model.layers.pop()
model.layers.pop()
model.layers.pop()
model.layers.pop()


orig = model.input


new_model = Conv2D(512,(14,14),activation='relu',padding='same',name='conv1')(model.get_layer('block4_pool').output)
new_model = Conv2D(512,(14,14),activation='relu',padding='same',name='conv2')(new_model)
new_model =Conv2D(512,(14,14),activation='relu',padding='same',name='conv3')(new_model)
new_model =MaxPooling2D((2,2),name='pool')(new_model)
new_model =Flatten(name='flatten')(new_model)
new_model =Dense(4096,activation='relu',name='fc1')(new_model)
new_model =Dense(1024,activation='relu',name='fc2')(new_model)
new_model =Dense(512,activation='relu',name='fc3')(new_model)
new_model =Dense(2,activation='softmax',name='output')(new_model)
model = Model(orig,new_model)
model.summary()

return model

我很着急,忘了输入model.summary()的输出。 这是原始VGG16的摘要:

图层(类型)输出形状参数#

input_3(InputLayer)(无,224、224、3)0


block1_conv1(Conv2D)(无,224,224,64)1792


block1_conv2(Conv2D)(无,224,224,64)36928


block1_pool(MaxPooling2D)(无,112,112,64)0


block2_conv1(Conv2D)(无,112、112、128)73856


block2_conv2(Conv2D)(无,112、112、128)147584


block2_pool(MaxPooling2D)(无,56、56、128)0


block3_conv1(Conv2D)(无,56,56,56,256)295168


block3_conv2(Conv2D)(无,56、56、256)590080


block3_conv3(Conv2D)(无,56、56、256)590080


block3_pool(MaxPooling2D)(无,28,28,256)0


block4_conv1(Conv2D)(无,28、28、512)1180160


block4_conv2(Conv2D)(无,28、28、512)2359808


block4_conv3(Conv2D)(无,28、28、512)2359808


block4_pool(MaxPooling2D)(无,14,14,14,512)0


block5_conv1(Conv2D)(无,14,14,14,512)2359808


block5_conv2(Conv2D)(无,14,14,14,512)2359808


block5_conv3(Conv2D)(无,14,14,14,512)2359808


block5_pool(MaxPooling2D)(无,7,7,512)0


展平(展平)(无25088)0


fc1(密集)(无,4096)102764544


fc2(密集)(无,4096)16781312


预测(密集)(无,1000)4097000

总参数:138,357,544 可训练的参数:138,357,544 不可训练的参数:0


这是修改后的网络的输出:


图层(类型)输出形状参数#

input_3(InputLayer)(无,224、224、3)0


block1_conv1(Conv2D)(无,224,224,64)1792


block1_conv2(Conv2D)(无,224,224,64)36928


block1_pool(MaxPooling2D)(无,112,112,64)0


block2_conv1(Conv2D)(无,112、112、128)73856


block2_conv2(Conv2D)(无,112、112、128)147584


block2_pool(MaxPooling2D)(无,56、56、128)0


block3_conv1(Conv2D)(无,56,56,56,256)295168


block3_conv2(Conv2D)(无,56、56、256)590080


block3_conv3(Conv2D)(无,56、56、256)590080


block3_pool(MaxPooling2D)(无,28,28,256)0


block4_conv1(Conv2D)(无,28、28、512)1180160


block4_conv2(Conv2D)(无,28、28、512)2359808


block4_conv3(Conv2D)(无,28、28、512)2359808


block4_pool(MaxPooling2D)(无,14,14,14,512)0


conv1(Conv2D)(无,14,14,14,512)51380736


conv2(Conv2D)(无,14,14,14,512)51380736


conv3(Conv2D)(无,14,14,14,512)51380736


pool(MaxPooling2D)(无,7,7,7,512)0


展平(展平)(无25088)0


fc1(密集)(无,4096)102764544


fc2(密集)(无,1024)4195328


fc3(密集)(无,512)524800


输出(密集)(无,2)1026

总参数:269,263,170 可训练的参数:261,627,906 不可训练的参数:7,635,264


0 个答案:

没有答案