|
from keras.models import Model |
|
from keras.layers import Input, Convolution2D, ZeroPadding2D, MaxPooling2D, Flatten, Dropout, Activation |
|
from PIL import Image |
|
import numpy as np |
|
|
|
def vgg_face(weights_path=None): |
|
img = Input(shape=(3, 224, 224)) |
|
|
|
pad1_1 = ZeroPadding2D(padding=(1, 1))(img) |
|
conv1_1 = Convolution2D(64, 3, 3, activation='relu', name='conv1_1')(pad1_1) |
|
pad1_2 = ZeroPadding2D(padding=(1, 1))(conv1_1) |
|
conv1_2 = Convolution2D(64, 3, 3, activation='relu', name='conv1_2')(pad1_2) |
|
pool1 = MaxPooling2D((2, 2), strides=(2, 2))(conv1_2) |
|
|
|
pad2_1 = ZeroPadding2D((1, 1))(pool1) |
|
conv2_1 = Convolution2D(128, 3, 3, activation='relu', name='conv2_1')(pad2_1) |
|
pad2_2 = ZeroPadding2D((1, 1))(conv2_1) |
|
conv2_2 = Convolution2D(128, 3, 3, activation='relu', name='conv2_2')(pad2_2) |
|
pool2 = MaxPooling2D((2, 2), strides=(2, 2))(conv2_2) |
|
|
|
pad3_1 = ZeroPadding2D((1, 1))(pool2) |
|
conv3_1 = Convolution2D(256, 3, 3, activation='relu', name='conv3_1')(pad3_1) |
|
pad3_2 = ZeroPadding2D((1, 1))(conv3_1) |
|
conv3_2 = Convolution2D(256, 3, 3, activation='relu', name='conv3_2')(pad3_2) |
|
pad3_3 = ZeroPadding2D((1, 1))(conv3_2) |
|
conv3_3 = Convolution2D(256, 3, 3, activation='relu', name='conv3_3')(pad3_3) |
|
pool3 = MaxPooling2D((2, 2), strides=(2, 2))(conv3_3) |
|
|
|
pad4_1 = ZeroPadding2D((1, 1))(pool3) |
|
conv4_1 = Convolution2D(512, 3, 3, activation='relu', name='conv4_1')(pad4_1) |
|
pad4_2 = ZeroPadding2D((1, 1))(conv4_1) |
|
conv4_2 = Convolution2D(512, 3, 3, activation='relu', name='conv4_2')(pad4_2) |
|
pad4_3 = ZeroPadding2D((1, 1))(conv4_2) |
|
conv4_3 = Convolution2D(512, 3, 3, activation='relu', name='conv4_3')(pad4_3) |
|
pool4 = MaxPooling2D((2, 2), strides=(2, 2))(conv4_3) |
|
|
|
pad5_1 = ZeroPadding2D((1, 1))(pool4) |
|
conv5_1 = Convolution2D(512, 3, 3, activation='relu', name='conv5_1')(pad5_1) |
|
pad5_2 = ZeroPadding2D((1, 1))(conv5_1) |
|
conv5_2 = Convolution2D(512, 3, 3, activation='relu', name='conv5_2')(pad5_2) |
|
pad5_3 = ZeroPadding2D((1, 1))(conv5_2) |
|
conv5_3 = Convolution2D(512, 3, 3, activation='relu', name='conv5_3')(pad5_3) |
|
pool5 = MaxPooling2D((2, 2), strides=(2, 2))(conv5_3) |
|
|
|
fc6 = Convolution2D(4096, 7, 7, activation='relu', name='fc6')(pool5) |
|
fc6_drop = Dropout(0.5)(fc6) |
|
fc7 = Convolution2D(4096, 1, 1, activation='relu', name='fc7')(fc6_drop) |
|
fc7_drop = Dropout(0.5)(fc7) |
|
fc8 = Convolution2D(2622, 1, 1, name='fc8')(fc7_drop) |
|
flat = Flatten()(fc8) |
|
out = Activation('softmax')(flat) |
|
|
|
model = Model(input=img, output=out) |
|
|
|
if weights_path: |
|
model.load_weights(weights_path) |
|
|
|
return model |
|
|
|
if __name__ == "__main__": |
|
im = Image.open('A.J._Buckley.jpg') |
|
im = im.resize((224,224)) |
|
im = np.array(im).astype(np.float32) |
|
# im[:,:,0] -= 129.1863 |
|
# im[:,:,1] -= 104.7624 |
|
# im[:,:,2] -= 93.5940 |
|
im = im.transpose((2,0,1)) |
|
im = np.expand_dims(im, axis=0) |
|
|
|
# Test pretrained model |
|
model = vgg_face('vgg-face-keras.h5') |
|
out = model.predict(im) |
|
print(out[0][0]) |
|
|
Hi!
I am using your model. Well, a version of it because i am trying to impliment a finetuning of the last Fully Connected layer:
Here's the code
`
'''OJO: PARA QUE FUNCIONE :
nano ~/.keras/keras.json
{
"image_dim_ordering": "th",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "tensorflow"
}
'''
`
And i've got the following error:
File "freeze_2.py", line 258, in
model=entrenamos_modelo('vggface_weights_tensorflow.h5') #Construction of the model
File "freeze_2.py", line 168, in entrenamos_modelo
model2.fit(train_data,label, nb_epoch=nb_epoch, batch_size=64)
File "/imatge/psereno/workspace/venv-tfg/local/lib/python2.7/site-packages/keras/engine/training.py", line 1057, in fit
batch_size=batch_size)
File "/imatge/psereno/workspace/venv-tfg/local/lib/python2.7/site-packages/keras/engine/training.py", line 984, in _standardize_user_data
exception_prefix='model input')
File "/imatge/psereno/workspace/venv-tfg/local/lib/python2.7/site-packages/keras/engine/training.py", line 111, in standardize_input_data
str(array.shape))
Exception: Error when checking model input: expected input_2 to have shape (None, 3, 224, 224) but got array with shape (1576, 4096, 1, 1)
srun: error: c7: task 0: Exited with exit code 1
Do you know why i have an error with dimensions?
Thank you very much and sorry for the inconvenience.
PS: I am using a adapted version to tensorflow of the weight. I've done the conversion with the following code:
`
model = Model(input=img, output=out)
weights_path = 'vgg-face-keras.h5'
model.load_weights(weights_path)
ops = []
for layer in model.layers:
if layer.class.name in ['Convolution1D', 'Convolution2D', 'Convolution3D', 'AtrousConvolution2D']:
original_w = K.get_value(layer.W)
converted_w = convert_kernel(original_w)
ops.append(tf.assign(layer.W, converted_w).op)
K.get_session().run(ops)
model.save_weights('vggface_weights_tensorflow.h5')
`
PS2: If i comment the fit line there's no error..so i've assumed that the error is there.
PS3: I am using Keras(1.1.1) + Theano (0.9.0.dev4) + Tensorflow(0.10.0rc0)