📚 The CoCalc Library - books, templates and other resources
cocalc-examples / data-science-ipython-notebooks / deep-learning / keras-tutorial / deep_learning_models / vgg16.py
132930 viewsLicense: OTHER
# -*- coding: utf-8 -*-1'''VGG16 model for Keras.23# Reference:45- [Very Deep Convolutional Networks for Large-Scale Image Recognition](https://arxiv.org/abs/1409.1556)67'''8from __future__ import print_function910import numpy as np11import warnings1213from keras.models import Model14from keras.layers import Flatten, Dense, Input15from keras.layers import Convolution2D, MaxPooling2D16from keras.preprocessing import image17from keras.utils.layer_utils import convert_all_kernels_in_model18from keras.utils.data_utils import get_file19from keras import backend as K20# from imagenet_utils import decode_predictions, preprocess_input212223TH_WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_th_dim_ordering_th_kernels.h5'24TF_WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5'25TH_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_th_dim_ordering_th_kernels_notop.h5'26TF_WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'272829def VGG16(include_top=True, weights='imagenet',30input_tensor=None):31'''Instantiate the VGG16 architecture,32optionally loading weights pre-trained33on ImageNet. Note that when using TensorFlow,34for best performance you should set35`image_dim_ordering="tf"` in your Keras config36at ~/.keras/keras.json.3738The model and the weights are compatible with both39TensorFlow and Theano. The dimension ordering40convention used by the model is the one41specified in your Keras config file.4243# Arguments44include_top: whether to include the 3 fully-connected45layers at the top of the network.46weights: one of `None` (random initialization)47or "imagenet" (pre-training on ImageNet).48input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)49to use as image input for the model.5051# Returns52A Keras model instance.53'''54if weights not in {'imagenet', None}:55raise ValueError('The `weights` argument should be either '56'`None` (random initialization) or `imagenet` '57'(pre-training on ImageNet).')58# Determine proper input shape59if K.image_dim_ordering() == 'th':60if include_top:61input_shape = (3, 224, 224)62else:63input_shape = (3, None, None)64else:65if include_top:66input_shape = (224, 224, 3)67else:68input_shape = (None, None, 3)6970if input_tensor is None:71img_input = Input(shape=input_shape)72else:73if not K.is_keras_tensor(input_tensor):74img_input = Input(tensor=input_tensor)75else:76img_input = input_tensor77# Block 178x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='block1_conv1')(img_input)79x = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='block1_conv2')(x)80x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)8182# Block 283x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='block2_conv1')(x)84x = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='block2_conv2')(x)85x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)8687# Block 388x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv1')(x)89x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv2')(x)90x = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv3')(x)91x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)9293# Block 494x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv1')(x)95x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv2')(x)96x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv3')(x)97x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)9899# Block 5100x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv1')(x)101x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv2')(x)102x = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv3')(x)103x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)104105if include_top:106# Classification block107x = Flatten(name='flatten')(x)108x = Dense(4096, activation='relu', name='fc1')(x)109x = Dense(4096, activation='relu', name='fc2')(x)110x = Dense(1000, activation='softmax', name='predictions')(x)111112# Create model113model = Model(img_input, x)114115# load weights116if weights == 'imagenet':117print('K.image_dim_ordering:', K.image_dim_ordering())118if K.image_dim_ordering() == 'th':119if include_top:120weights_path = get_file('vgg16_weights_th_dim_ordering_th_kernels.h5',121TH_WEIGHTS_PATH,122cache_subdir='models')123else:124weights_path = get_file('vgg16_weights_th_dim_ordering_th_kernels_notop.h5',125TH_WEIGHTS_PATH_NO_TOP,126cache_subdir='models')127model.load_weights(weights_path)128if K.backend() == 'tensorflow':129warnings.warn('You are using the TensorFlow backend, yet you '130'are using the Theano '131'image dimension ordering convention '132'(`image_dim_ordering="th"`). '133'For best performance, set '134'`image_dim_ordering="tf"` in '135'your Keras config '136'at ~/.keras/keras.json.')137convert_all_kernels_in_model(model)138else:139if include_top:140weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels.h5',141TF_WEIGHTS_PATH,142cache_subdir='models')143else:144weights_path = get_file('vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',145TF_WEIGHTS_PATH_NO_TOP,146cache_subdir='models')147model.load_weights(weights_path)148if K.backend() == 'theano':149convert_all_kernels_in_model(model)150return model151152153if __name__ == '__main__':154model = VGG16(include_top=True, weights='imagenet')155156img_path = 'elephant.jpg'157img = image.load_img(img_path, target_size=(224, 224))158x = image.img_to_array(img)159x = np.expand_dims(x, axis=0)160x = preprocess_input(x)161print('Input image shape:', x.shape)162163preds = model.predict(x)164print('Predicted:', decode_predictions(preds))165166167