Debian Codegraph @

keras_applications/vgg19.py

raw symbols SCIP
1 """VGG19 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.vgg19`/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.vgg19`/WEIGHTS_PATH. = ('https://github.com/fchollet/deep-learning-models/'
23 'releases/download/v0.1/'
24 'vgg19_weights_tf_dim_ordering_tf_kernels.h5')
25 WEIGHTS_PATH_NO_TOPscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/WEIGHTS_PATH_NO_TOP. = ('https://github.com/fchollet/deep-learning-models/'
26 'releases/download/v0.1/'
27 'vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5')
28
29
30 def VGG19scip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(include_topscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(include_top)=True,
31 weightsscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(weights)='imagenet',
32 input_tensorscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(input_tensor)=None,
33 input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(input_shape)=None,
34 poolingscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(pooling)=None,
35 classesscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(classes)=1000,
36 **kwargsscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(kwargs)):
37 """Instantiates the VGG19 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 inputs 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.vgg19`/VGG19().(kwargs))
83
84 if not (weightsscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(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.vgg19`/VGG19().(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.vgg19`/VGG19().(weights) == 'imagenet' and include_topscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(include_top) and classesscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(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.vgg19`/VGG19().(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.vgg19`/VGG19().(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.vgg19`/VGG19().(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.vgg19`/VGG19().(weights))
100
101 if input_tensorscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(input_tensor) is None:
102 img_input = layers.Input(shape=input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(input_shape))
103 else:
104 if not backend.is_keras_tensor(input_tensorscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(input_tensor)):
105 img_input = layers.Input(tensor=input_tensorscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(input_tensor), shape=input_shapescip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(input_shape))
106 else:
107 img_input = input_tensorscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(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.Conv2D(256, (3, 3),
144 activation='relu',
145 padding='same',
146 name='block3_conv4')(x)
147 x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)
148
149 # Block 4
150 x = layers.Conv2D(512, (3, 3),
151 activation='relu',
152 padding='same',
153 name='block4_conv1')(x)
154 x = layers.Conv2D(512, (3, 3),
155 activation='relu',
156 padding='same',
157 name='block4_conv2')(x)
158 x = layers.Conv2D(512, (3, 3),
159 activation='relu',
160 padding='same',
161 name='block4_conv3')(x)
162 x = layers.Conv2D(512, (3, 3),
163 activation='relu',
164 padding='same',
165 name='block4_conv4')(x)
166 x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)
167
168 # Block 5
169 x = layers.Conv2D(512, (3, 3),
170 activation='relu',
171 padding='same',
172 name='block5_conv1')(x)
173 x = layers.Conv2D(512, (3, 3),
174 activation='relu',
175 padding='same',
176 name='block5_conv2')(x)
177 x = layers.Conv2D(512, (3, 3),
178 activation='relu',
179 padding='same',
180 name='block5_conv3')(x)
181 x = layers.Conv2D(512, (3, 3),
182 activation='relu',
183 padding='same',
184 name='block5_conv4')(x)
185 x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)
186
187 if include_topscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(include_top):
188 # Classification block
189 x = layers.Flatten(name='flatten')(x)
190 x = layers.Dense(4096, activation='relu', name='fc1')(x)
191 x = layers.Dense(4096, activation='relu', name='fc2')(x)
192 x = layers.Dense(classesscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(classes), activation='softmax', name='predictions')(x)
193 else:
194 if poolingscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(pooling) == 'avg':
195 x = layers.GlobalAveragePooling2D()(x)
196 elif poolingscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(pooling) == 'max':
197 x = layers.GlobalMaxPooling2D()(x)
198
199 # Ensure that the model takes into account
200 # any potential predecessors of `input_tensor`.
201 if input_tensorscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(input_tensor) is not None:
202 inputs = keras_utils.get_source_inputs(input_tensorscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(input_tensor))
203 else:
204 inputs = img_input
205 # Create model.
206 model = models.Model(inputs, x, name='vgg19')
207
208 # Load weights.
209 if weightsscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(weights) == 'imagenet':
210 if include_topscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(include_top):
211 weights_path = keras_utils.get_file(
212 'vgg19_weights_tf_dim_ordering_tf_kernels.h5',
213 WEIGHTS_PATHscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/WEIGHTS_PATH.,
214 cache_subdir='models',
215 file_hash='cbe5617147190e668d6c5d5026f83318')
216 else:
217 weights_path = keras_utils.get_file(
218 'vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5',
219 WEIGHTS_PATH_NO_TOPscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/WEIGHTS_PATH_NO_TOP.,
220 cache_subdir='models',
221 file_hash='253f8cb515780f3b799900260a226db6')
222 model.load_weights(weights_path)
223 if backend.backend() == 'theano':
224 keras_utils.convert_all_kernels_in_model(model)
225 elif weightsscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(weights) is not None:
226 model.load_weights(weightsscip-python python Keras_Applications 1.0.8 `keras_applications.vgg19`/VGG19().(weights))
227
228 return model