简体   繁体   中英

How to Use Pre-Trained CNN models in Python

So basically I am trying to use a pre-trained VGG CNN model. I have downloaded model from the following website:

http://www.vlfeat.org/matconvnet/pretrained/

which has given me a image-vgg-m-2048.mat file. But the guide gives me how to use it in Matlab using MatconvNet Library. I want to implement the same thing in python. For which I am using Keras.

I have written the following code:

from keras.applications.vgg16 import VGG16
model = VGG16(weights = "imagenet")

when I try to put weights = "image-vgg-m-2408.mat" it gives me an exception.

Can someone help me, How to use the mat file model, weights in python to use this pre-trained model?

Basically if you specify any weights which is not imagenet , it will just use keras model.load_weights to load it and I guess image-vgg-m-2408.mat is not a valid one that keras can load directly here.

https://github.com/keras-team/keras-applications/blob/master/keras_applications/vgg16.py#L197

if weights == 'imagenet':
    if include_top:
        weights_path = keras_utils.get_file(
            'vgg16_weights_tf_dim_ordering_tf_kernels.h5',
            WEIGHTS_PATH,
            cache_subdir='models',
            file_hash='64373286793e3c8b2b4e3219cbf3544b')
    else:
        weights_path = keras_utils.get_file(
            'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',
            WEIGHTS_PATH_NO_TOP,
            cache_subdir='models',
            file_hash='6d6bbae143d832006294945121d1f1fc')
    model.load_weights(weights_path)
    if backend.backend() == 'theano':
        keras_utils.convert_all_kernels_in_model(model)
elif weights is not None:
    model.load_weights(weights)

By default, keras will use imagenet as the default weight and the output class number will be 1000. But if you don't have other valid weight for this model, you can still leverage imagenet weight with a possible way:

  • You can set include_top as False and use the VGG16 Conv output (without the final 3 FC layers) directly.
model = VGG16(include_top=False, weights = "imagenet")

https://github.com/keras-team/keras-applications/blob/master/keras_applications/vgg16.py#L43

    include_top: whether to include the 3 fully-connected
        layers at the top of the network.
    weights: one of `None` (random initialization),
          'imagenet' (pre-training on ImageNet),
          or the path to the weights file to be loaded.
    classes: optional number of classes to classify images
        into, only to be specified if `include_top` is True, and
        if no `weights` argument is specified.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM