更改vggface保留模型的输入大小

时间:2019-06-04 05:18:09

标签: tensorflow keras deep-learning vgg-net

我已经尝试一周,每次更改某些内容时,我都会尝试更改训练后的Vgg人脸模型的输入大小,这使我出现错误,如何将输入大小从224x224x3更改为64x64x3,有没有办法直接更改它无需再次训练模型,只需直接使用训练的权重

model.add(ZeroPadding2D((1, 1),include_top=False,input_shape=(64, 64, 3)))

model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))

model.add(Convolution2D(4096, (7, 7), activation='relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(4096, (1, 1), activation='relu'))
model.add(Dropout(0.5))
model.add(Convolution2D(2622, (1, 1)))
model.add(Flatten())
model.add(Activation('softmax'))


from keras.models import model_from_json

deep= model.load_weights('/Users/macbookpro/PycharmProjects/untitled/venv/vgg_face_weights.h5')

代码: vggface

当我尝试调用include_top时出现此错误:

TypeError: ('Keyword argument not understood:', 'include_top')

当我直接更改输入大小时,出现此错误:

ValueError: Negative dimension size caused by subtracting 7 from 2 for 'conv2d_14/convolution' (op: 'Conv2D') with input shapes: [?,2,2,512], [7,7,512,4096].

1 个答案:

答案 0 :(得分:0)

首先,删除include_top=False
您的问题是,这种架构对于64x64输入而言太深了。特别是这一行:

model.add(Convolution2D(4096, (7, 7), activation='relu'))

试图对大小为2x2的输入执行7x7卷积,这是不可能的。
一种可能的解决方案是在最后一个MaxPooling层之后删除卷积,而改用一些Dense层。