Debian Codegraph @

keras_applications/vgg16.py

raw symbols SCIP
1 """VGG16 model for Keras.
2
3 # Reference
4
5 - [Very Deep Convolutional Networks for Large-Scale Image Recognition](
6 https://arxiv.org/abs/1409.1556) (ICLR 2015)
7
8 """
9 from __future__scip-python python python-stdlib 3.11 __future__/__init__: import absolute_importscip-python python python-stdlib 3.11 __future__/absolute_import.absolute_import.
10 from __future__scip-python python python-stdlib 3.11 __future__/__init__: import divisionscip-python python python-stdlib 3.11 __future__/division.division.
11 from __future__scip-python python python-stdlib 3.11 __future__/__init__: import print_functionscip-python python python-stdlib 3.11 __future__/print_function.print_function.
12
13 import osscip-python python python-stdlib 3.11 os/__init__:
14
15 from . import get_submodules_from_kwargsscip-python python Keras_Applications 1.0.8 keras_applications/get_submodules_from_kwargs().
16 from . import imagenet_utilsscip-python python Keras_Applications 1.0.8 imagenet_utils/__init__:
17 from .imagenet_utilsscip-python python Keras_Applications 1.0.8 imagenet_utils/__init__: import decode_predictionsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/decode_predictions().
18 from .imagenet_utilsscip-python python Keras_Applications 1.0.8 imagenet_utils/__init__: import _obtain_input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().
19
20 preprocess_inputscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/preprocess_input. = imagenet_utilsscip-python python Keras_Applications 1.0.8 imagenet_utils/__init__:.preprocess_inputscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/preprocess_input().
21
22 WEIGHTS_PATHscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/WEIGHTS_PATH. = ('https://github.com/fchollet/deep-learning-models/'
23 'releases/download/v0.1/'
24 'vgg16_weights_tf_dim_ordering_tf_kernels.h5')
25 WEIGHTS_PATH_NO_TOPscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/WEIGHTS_PATH_NO_TOP. = ('https://github.com/fchollet/deep-learning-models/'
26 'releases/download/v0.1/'
27 'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5')
28
29
30 def VGG16scip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(include_topscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(include_top)=True,
31 weightsscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(weights)='imagenet',
32 input_tensorscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(input_tensor)=None,
33 input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(input_shape)=None,
34 poolingscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(pooling)=None,
35 classesscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(classes)=1000,
36 **kwargsscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(kwargs)):
37 """Instantiates the VGG16 architecture.
38
39 Optionally loads weights pre-trained on ImageNet.
40 Note that the data format convention used by the model is
41 the one specified in your Keras config at `~/.keras/keras.json`.
42
43 # Arguments
44 include_top: whether to include the 3 fully-connected
45 layers at the top of the network.
46 weights: one of `None` (random initialization),
47 'imagenet' (pre-training on ImageNet),
48 or the path to the weights file to be loaded.
49 input_tensor: optional Keras tensor
50 (i.e. output of `layers.Input()`)
51 to use as image input for the model.
52 input_shape: optional shape tuple, only to be specified
53 if `include_top` is False (otherwise the input shape
54 has to be `(224, 224, 3)`
55 (with `channels_last` data format)
56 or `(3, 224, 224)` (with `channels_first` data format).
57 It should have exactly 3 input channels,
58 and width and height should be no smaller than 32.
59 E.g. `(200, 200, 3)` would be one valid value.
60 pooling: Optional pooling mode for feature extraction
61 when `include_top` is `False`.
62 - `None` means that the output of the model will be
63 the 4D tensor output of the
64 last convolutional block.
65 - `avg` means that global average pooling
66 will be applied to the output of the
67 last convolutional block, and thus
68 the output of the model will be a 2D tensor.
69 - `max` means that global max pooling will
70 be applied.
71 classes: optional number of classes to classify images
72 into, only to be specified if `include_top` is True, and
73 if no `weights` argument is specified.
74
75 # Returns
76 A Keras model instance.
77
78 # Raises
79 ValueError: in case of invalid argument for `weights`,
80 or invalid input shape.
81 """
82 backend, layers, models, keras_utils = get_submodules_from_kwargsscip-python python Keras_Applications 1.0.8 keras_applications/get_submodules_from_kwargs().(kwargsscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(kwargs))
83
84 if not (weightsscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(weights) in {'imagenet', None} or osscip-python python python-stdlib 3.11 os/__init__:.pathscip-python python python-stdlib 3.11 os/path..existsscip-python python python-stdlib 3.11 genericpath/exists().(weightsscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(weights))):
85 raise ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#('The `weights` argument should be either '
86 '`None` (random initialization), `imagenet` '
87 '(pre-training on ImageNet), '
88 'or the path to the weights file to be loaded.')
89
90 if weightsscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(weights) == 'imagenet' and include_topscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(include_top) and classesscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(classes) != 1000:
91 raise ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#('If using `weights` as `"imagenet"` with `include_top`'
92 ' as true, `classes` should be 1000')
93 # Determine proper input shape
94 input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(input_shape) = _obtain_input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(input_shape),
95 default_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(default_size)=224,
96 min_sizescip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(min_size)=32,
97 data_formatscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(data_format)=backend.image_data_format(),
98 require_flattenscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(require_flatten)=include_topscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(include_top),
99 weightsscip-python python Keras_Applications 1.0.8 `keras_applications.imagenet_utils`/_obtain_input_shape().(weights)=weightsscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(weights))
100
101 if input_tensorscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(input_tensor) is None:
102 img_input = layers.Input(shape=input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(input_shape))
103 else:
104 if not backend.is_keras_tensor(input_tensorscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(input_tensor)):
105 img_input = layers.Input(tensor=input_tensorscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(input_tensor), shape=input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(input_shape))
106 else:
107 img_input = input_tensorscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(input_tensor)
108 # Block 1
109 x = layers.Conv2D(64, (3, 3),
110 activation='relu',
111 padding='same',
112 name='block1_conv1')(img_input)
113 x = layers.Conv2D(64, (3, 3),
114 activation='relu',
115 padding='same',
116 name='block1_conv2')(x)
117 x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
118
119 # Block 2
120 x = layers.Conv2D(128, (3, 3),
121 activation='relu',
122 padding='same',
123 name='block2_conv1')(x)
124 x = layers.Conv2D(128, (3, 3),
125 activation='relu',
126 padding='same',
127 name='block2_conv2')(x)
128 x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)
129
130 # Block 3
131 x = layers.Conv2D(256, (3, 3),
132 activation='relu',
133 padding='same',
134 name='block3_conv1')(x)
135 x = layers.Conv2D(256, (3, 3),
136 activation='relu',
137 padding='same',
138 name='block3_conv2')(x)
139 x = layers.Conv2D(256, (3, 3),
140 activation='relu',
141 padding='same',
142 name='block3_conv3')(x)
143 x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)
144
145 # Block 4
146 x = layers.Conv2D(512, (3, 3),
147 activation='relu',
148 padding='same',
149 name='block4_conv1')(x)
150 x = layers.Conv2D(512, (3, 3),
151 activation='relu',
152 padding='same',
153 name='block4_conv2')(x)
154 x = layers.Conv2D(512, (3, 3),
155 activation='relu',
156 padding='same',
157 name='block4_conv3')(x)
158 x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)
159
160 # Block 5
161 x = layers.Conv2D(512, (3, 3),
162 activation='relu',
163 padding='same',
164 name='block5_conv1')(x)
165 x = layers.Conv2D(512, (3, 3),
166 activation='relu',
167 padding='same',
168 name='block5_conv2')(x)
169 x = layers.Conv2D(512, (3, 3),
170 activation='relu',
171 padding='same',
172 name='block5_conv3')(x)
173 x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)
174
175 if include_topscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(include_top):
176 # Classification block
177 x = layers.Flatten(name='flatten')(x)
178 x = layers.Dense(4096, activation='relu', name='fc1')(x)
179 x = layers.Dense(4096, activation='relu', name='fc2')(x)
180 x = layers.Dense(classesscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(classes), activation='softmax', name='predictions')(x)
181 else:
182 if poolingscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(pooling) == 'avg':
183 x = layers.GlobalAveragePooling2D()(x)
184 elif poolingscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(pooling) == 'max':
185 x = layers.GlobalMaxPooling2D()(x)
186
187 # Ensure that the model takes into account
188 # any potential predecessors of `input_tensor`.
189 if input_tensorscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(input_tensor) is not None:
190 inputs = keras_utils.get_source_inputs(input_tensorscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(input_tensor))
191 else:
192 inputs = img_input
193 # Create model.
194 model = models.Model(inputs, x, name='vgg16')
195
196 # Load weights.
197 if weightsscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(weights) == 'imagenet':
198 if include_topscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(include_top):
199 weights_path = keras_utils.get_file(
200 'vgg16_weights_tf_dim_ordering_tf_kernels.h5',
201 WEIGHTS_PATHscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/WEIGHTS_PATH.,
202 cache_subdir='models',
203 file_hash='64373286793e3c8b2b4e3219cbf3544b')
204 else:
205 weights_path = keras_utils.get_file(
206 'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',
207 WEIGHTS_PATH_NO_TOPscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/WEIGHTS_PATH_NO_TOP.,
208 cache_subdir='models',
209 file_hash='6d6bbae143d832006294945121d1f1fc')
210 model.load_weights(weights_path)
211 if backend.backend() == 'theano':
212 keras_utils.convert_all_kernels_in_model(model)
213 elif weightsscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(weights) is not None:
214 model.load_weights(weightsscip-python python Keras_Applications 1.0.8 `keras_applications.vgg16`/VGG16().(weights))
215
216 return model