Skip to content

Instantly share code, notes, and snippets.

@EncodeTS
Last active February 19, 2024 06:56
Show Gist options
  • Save EncodeTS/6bbe8cb8bebad7a672f0d872561782d9 to your computer and use it in GitHub Desktop.
Save EncodeTS/6bbe8cb8bebad7a672f0d872561782d9 to your computer and use it in GitHub Desktop.

Revisions

  1. EncodeTS revised this gist Jul 22, 2016. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions keras VGG-Face Model.md
    Original file line number Diff line number Diff line change
    @@ -20,14 +20,15 @@ Please cite the paper if you use the models.
    ### Contents:

    model and usage demo: see `vgg-face-keras.py` or `vgg-face-keras-fc.py`
    The only difference between them is the last few layers,but they produce the **same result**.

    The only difference between them is the **last few layers**(see the code and you'll understand),but they produce the **same result**.

    weights: 

    - [Google Drive](https://drive.google.com/open?id=0B4ChsjFJvew3NkF0dTc1OGxsOFU)
    - [Baidu Yun](http://pan.baidu.com/s/1o88KALW)(password:12i0)
    - [fc Google Drive](https://drive.google.com/open?id=0B4ChsjFJvew3c2VpZWQ0eE85LVE)
    - [fc Baidu Yun](http://pan.baidu.com/s/1pKLt9LT)(password:d318)
    - [fc-version Google Drive](https://drive.google.com/open?id=0B4ChsjFJvew3c2VpZWQ0eE85LVE)
    - [fc-version Baidu Yun](http://pan.baidu.com/s/1pKLt9LT)(password:d318)

    ### Notice:
    Please use this model in **theano mode**.
  2. EncodeTS renamed this gist Jul 22, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. EncodeTS revised this gist Jul 22, 2016. 3 changed files with 4 additions and 2 deletions.
    File renamed without changes.
    3 changes: 2 additions & 1 deletion vgg-face-keras-fc.py
    Original file line number Diff line number Diff line change
    @@ -69,4 +69,5 @@ def vgg_face(weights_path=None):
    # Test pretrained model
    model = vgg_face('vgg-face-keras-fc.h5')
    out = model.predict(im)
    print(out[0][0])
    print(out[0][0])

    3 changes: 2 additions & 1 deletion vgg-face-keras.py
    Original file line number Diff line number Diff line change
    @@ -70,4 +70,5 @@ def vgg_face(weights_path=None):
    # Test pretrained model
    model = vgg_face('vgg-face-keras.h5')
    out = model.predict(im)
    print(out[0][0])
    print(out[0][0])

  4. EncodeTS renamed this gist Jul 22, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. EncodeTS revised this gist Jul 22, 2016. 3 changed files with 79 additions and 8 deletions.
    14 changes: 7 additions & 7 deletions VGG-Face-keras.md
    Original file line number Diff line number Diff line change
    @@ -2,11 +2,10 @@

    This is the [Keras](http://keras.io/) model of [VGG-Face](http://www.robots.ox.ac.uk/~vgg/software/vgg_face/).

    It has been obtained through the following steps:
    It has been obtained through the following method:

    - export the weights of the vgg-face matconvnet model to .mat file
    - use scipy to load the weights,and convert the weight from tf mode to th mode
    - set the weights to keras model and then save the model
    - vgg-face-keras:directly convert the vgg-face matconvnet model to keras model
    - vgg-face-keras-fc:first convert vgg-face caffe model to mxnet model,and then convert it to keras model

    Details about the network architecture can be found in the following paper:

    @@ -20,14 +19,15 @@ Please cite the paper if you use the models.

    ### Contents:

    model and usage demo: see `vgg-face-keras.py`
    model and usage demo: see `vgg-face-keras.py` or `vgg-face-keras-fc.py`
    The only difference between them is the last few layers,but they produce the **same result**.

    weights: 

    - [Google Drive](https://drive.google.com/open?id=0B4ChsjFJvew3NkF0dTc1OGxsOFU)

    - [Baidu Yun](http://pan.baidu.com/s/1o88KALW)(password:12i0)

    - [fc Google Drive](https://drive.google.com/open?id=0B4ChsjFJvew3c2VpZWQ0eE85LVE)
    - [fc Baidu Yun](http://pan.baidu.com/s/1pKLt9LT)(password:d318)

    ### Notice:
    Please use this model in **theano mode**.
    72 changes: 72 additions & 0 deletions vgg-face-keras-fc.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    from keras.models import Model
    from keras.layers import Input, Convolution2D, ZeroPadding2D, MaxPooling2D, Flatten, Dense, Dropout
    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)

    flat = Flatten()(pool5)
    fc6 = Dense(4096, activation='relu', name='fc6')(flat)
    fc6_drop = Dropout(0.5)(fc6)
    fc7 = Dense(4096, activation='relu', name='fc7')(fc6_drop)
    fc7_drop = Dropout(0.5)(fc7)
    out = Dense(2622, activation='softmax', name='fc8')(fc7_drop)

    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-fc.h5')
    out = model.predict(im)
    print(out[0][0])
    1 change: 0 additions & 1 deletion VGG-Face-keras.py → vgg-face-keras.py
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,6 @@ def vgg_face(weights_path=None):
    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)
  6. EncodeTS revised this gist Jul 22, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion VGG-Face-keras.py
    Original file line number Diff line number Diff line change
    @@ -71,4 +71,4 @@ def vgg_face(weights_path=None):
    # Test pretrained model
    model = vgg_face('vgg-face-keras.h5')
    out = model.predict(im)
    print(np.argmax(out))
    print(out[0][0])
  7. EncodeTS created this gist Jul 22, 2016.
    33 changes: 33 additions & 0 deletions VGG-Face-keras.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    ## VGG-Face model for Keras

    This is the [Keras](http://keras.io/) model of [VGG-Face](http://www.robots.ox.ac.uk/~vgg/software/vgg_face/).

    It has been obtained through the following steps:

    - export the weights of the vgg-face matconvnet model to .mat file
    - use scipy to load the weights,and convert the weight from tf mode to th mode
    - set the weights to keras model and then save the model

    Details about the network architecture can be found in the following paper:

    ```
    Deep Face Recognition
    O. M. Parkhi, A. Vedaldi, A. Zisserman
    British Machine Vision Conference, 2015
    ```

    Please cite the paper if you use the models.

    ### Contents:

    model and usage demo: see `vgg-face-keras.py`

    weights: 

    - [Google Drive](https://drive.google.com/open?id=0B4ChsjFJvew3NkF0dTc1OGxsOFU)

    - [Baidu Yun](http://pan.baidu.com/s/1o88KALW)(password:12i0)


    ### Notice:
    Please use this model in **theano mode**.
    74 changes: 74 additions & 0 deletions VGG-Face-keras.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    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(np.argmax(out))