Pre-trained VGG16 model. Parameters ------------ pretrained : boolean Whether to load pretrained weights. Default False. end_with : str The end point of the model. Default ``fc3_relu`` i.e. the whole model. mode : str. Model building mode, 'dynamic' or 's
(pretrained=False, end_with='outputs', mode='dynamic', name=None)
| 197 | |
| 198 | |
| 199 | def vgg16(pretrained=False, end_with='outputs', mode='dynamic', name=None): |
| 200 | """Pre-trained VGG16 model. |
| 201 | |
| 202 | Parameters |
| 203 | ------------ |
| 204 | pretrained : boolean |
| 205 | Whether to load pretrained weights. Default False. |
| 206 | end_with : str |
| 207 | The end point of the model. Default ``fc3_relu`` i.e. the whole model. |
| 208 | mode : str. |
| 209 | Model building mode, 'dynamic' or 'static'. Default 'dynamic'. |
| 210 | name : None or str |
| 211 | A unique layer name. |
| 212 | |
| 213 | Examples |
| 214 | --------- |
| 215 | Classify ImageNet classes with VGG16, see `tutorial_models_vgg.py <https://github.com/tensorlayer/tensorlayer/blob/master/example/tutorial_models_vgg.py>`__ |
| 216 | With TensorLayer |
| 217 | |
| 218 | >>> # get the whole model, without pre-trained VGG parameters |
| 219 | >>> vgg = tl.models.vgg16() |
| 220 | >>> # get the whole model, restore pre-trained VGG parameters |
| 221 | >>> vgg = tl.models.vgg16(pretrained=True) |
| 222 | >>> # use for inferencing |
| 223 | >>> output = vgg(img, is_train=False) |
| 224 | >>> probs = tf.nn.softmax(output)[0].numpy() |
| 225 | |
| 226 | Extract features with VGG16 and Train a classifier with 100 classes |
| 227 | |
| 228 | >>> # get VGG without the last layer |
| 229 | >>> cnn = tl.models.vgg16(end_with='fc2_relu', mode='static').as_layer() |
| 230 | >>> # add one more layer and build a new model |
| 231 | >>> ni = Input([None, 224, 224, 3], name="inputs") |
| 232 | >>> nn = cnn(ni) |
| 233 | >>> nn = tl.layers.Dense(n_units=100, name='out')(nn) |
| 234 | >>> model = tl.models.Model(inputs=ni, outputs=nn) |
| 235 | >>> # train your own classifier (only update the last layer) |
| 236 | >>> train_params = model.get_layer('out').trainable_weights |
| 237 | |
| 238 | Reuse model |
| 239 | |
| 240 | >>> # in dynamic model, we can directly use the same model |
| 241 | >>> # in static model |
| 242 | >>> vgg_layer = tl.models.vgg16().as_layer() |
| 243 | >>> ni_1 = tl.layers.Input([None, 224, 244, 3]) |
| 244 | >>> ni_2 = tl.layers.Input([None, 224, 244, 3]) |
| 245 | >>> a_1 = vgg_layer(ni_1) |
| 246 | >>> a_2 = vgg_layer(ni_2) |
| 247 | >>> M = Model(inputs=[ni_1, ni_2], outputs=[a_1, a_2]) |
| 248 | |
| 249 | """ |
| 250 | if mode == 'dynamic': |
| 251 | model = VGG(layer_type='vgg16', batch_norm=False, end_with=end_with, name=name) |
| 252 | elif mode == 'static': |
| 253 | model = VGG_static(layer_type='vgg16', batch_norm=False, end_with=end_with, name=name) |
| 254 | else: |
| 255 | raise Exception("No such mode %s" % mode) |
| 256 | if pretrained: |
searching dependent graphs…