Skip to content Skip to sidebar Skip to footer

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 i

Solution 1:

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 isnotNone:
    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` isTrue, andif no `weights` argument is specified.

Post a Comment for "How To Use Pre-trained Cnn Models In Python"