API - Models¶
TensorLayer provides many pretrained models, you can easily use the whole or a part of the pretrained models via these APIs.
|
The |
|
Pre-trained VGG16 model. |
|
Pre-trained VGG19 model. |
|
Pre-trained SqueezeNetV1 model (static mode). |
|
Pre-trained MobileNetV1 model (static mode). |
|
vanilla stacked layer Seq2Seq model. |
|
Luong Attention-based Seq2Seq model. |
Base Model¶
-
class
tensorlayer.models.
Model
(inputs=None, outputs=None, name=None)[source]¶ The
Model
class represents a neural network.It should be subclassed when implementing a dynamic model, where ‘forward’ method must be overwritten. Otherwise, please specify ‘inputs’ tensor(s) and ‘outputs’ tensor(s) to create a static model. In that case, ‘inputs’ tensors should come from tl.layers.Input().
- Parameters
inputs (a Layer or list of Layer) – The input(s) to the model.
outputs (a Layer or list of Layer) – The output(s) to the model.
name (None or str) – The name of the model.
-
inputs
()¶ Get input tensors to this network (only avaiable for static model).
-
outputs
()¶ Get output tensors to this network (only avaiable for static model).
-
all_layers
()¶ Get all layer objects of this network in a list of layers.
-
weights
()¶ Get the weights of this network in a list of tensors.
-
as_layer
()[source]¶ Set this network as a ModelLayer so that it can be integrated into another Model.
-
release_memory
()[source]¶ Release the memory that was taken up by tensors which are maintained by this network.
-
save_weights
(self, filepath, format='hdf5')[source]¶ Save the weights of this network in a given format.
-
load_weights
(self, filepath, format=None, in_order=True, skip=False)[source]¶ Load weights into this network from a specified file.
Examples
>>> import tensorflow as tf >>> import numpy as np >>> from tensorlayer.layers import Input, Dense, Dropout >>> from tensorlayer.models import Model
Define static model
>>> class CustomModel(Model): >>> def __init__(self): >>> super(CustomModel, self).__init__() >>> self.dense1 = Dense(n_units=800, act=tf.nn.relu, in_channels=784) >>> self.dropout1 = Dropout(keep=0.8) >>> self.dense2 = Dense(n_units=10, in_channels=800) >>> def forward(self, x): >>> z = self.dense1(x) >>> z = self.dropout1(z) >>> z = self.dense2(z) >>> return z >>> M_dynamic = CustomModel()
Define static model
>>> ni = Input([None, 784]) >>> nn = Dense(n_units=800, act=tf.nn.relu)(ni) >>> nn = Dropout(keep=0.8)(nn) >>> nn = Dense(n_units=10, act=tf.nn.relu)(nn) >>> M_static = Model(inputs=ni, outputs=nn, name="mlp")
Get network information
>>> print(M_static) ... Model( ... (_inputlayer): Input(shape=[None, 784], name='_inputlayer') ... (dense): Dense(n_units=800, relu, in_channels='784', name='dense') ... (dropout): Dropout(keep=0.8, name='dropout') ... (dense_1): Dense(n_units=10, relu, in_channels='800', name='dense_1') ... )
Forwarding through this network
>>> data = np.random.normal(size=[16, 784]).astype(np.float32) >>> outputs_d = M_dynamic(data) >>> outputs_s = M_static(data)
Save and load weights
>>> M_static.save_weights('./model_weights.h5') >>> M_static.load_weights('./model_weights.h5')
Save and load the model
>>> M_static.save('./model.h5') >>> M = Model.load('./model.h5')
Convert model to layer
>>> M_layer = M_static.as_layer()
VGG16¶
-
tensorlayer.models.
VGG16
(pretrained=False, end_with='outputs', mode='dynamic', name=None)¶ 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 ‘static’. Default ‘dynamic’.
name (None or str) – A unique layer name.
Examples
Classify ImageNet classes with VGG16, see tutorial_models_vgg.py With TensorLayer
>>> # get the whole model, without pre-trained VGG parameters >>> vgg = tl.models.vgg16() >>> # get the whole model, restore pre-trained VGG parameters >>> vgg = tl.models.vgg16(pretrained=True) >>> # use for inferencing >>> output = vgg(img, is_train=False) >>> probs = tf.nn.softmax(output)[0].numpy()
Extract features with VGG16 and Train a classifier with 100 classes
>>> # get VGG without the last layer >>> cnn = tl.models.vgg16(end_with='fc2_relu', mode='static').as_layer() >>> # add one more layer and build a new model >>> ni = Input([None, 224, 224, 3], name="inputs") >>> nn = cnn(ni) >>> nn = tl.layers.Dense(n_units=100, name='out')(nn) >>> model = tl.models.Model(inputs=ni, outputs=nn) >>> # train your own classifier (only update the last layer) >>> train_params = model.get_layer('out').trainable_weights
Reuse model
>>> # in dynamic model, we can directly use the same model >>> # in static model >>> vgg_layer = tl.models.vgg16().as_layer() >>> ni_1 = tl.layers.Input([None, 224, 244, 3]) >>> ni_2 = tl.layers.Input([None, 224, 244, 3]) >>> a_1 = vgg_layer(ni_1) >>> a_2 = vgg_layer(ni_2) >>> M = Model(inputs=[ni_1, ni_2], outputs=[a_1, a_2])
VGG19¶
-
tensorlayer.models.
VGG19
(pretrained=False, end_with='outputs', mode='dynamic', name=None)¶ Pre-trained VGG19 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 ‘static’. Default ‘dynamic’.
name (None or str) – A unique layer name.
Examples
Classify ImageNet classes with VGG19, see tutorial_models_vgg.py With TensorLayer
>>> # get the whole model, without pre-trained VGG parameters >>> vgg = tl.models.vgg19() >>> # get the whole model, restore pre-trained VGG parameters >>> vgg = tl.models.vgg19(pretrained=True) >>> # use for inferencing >>> output = vgg(img, is_train=False) >>> probs = tf.nn.softmax(output)[0].numpy()
Extract features with VGG19 and Train a classifier with 100 classes
>>> # get VGG without the last layer >>> cnn = tl.models.vgg19(end_with='fc2_relu', mode='static').as_layer() >>> # add one more layer and build a new model >>> ni = Input([None, 224, 224, 3], name="inputs") >>> nn = cnn(ni) >>> nn = tl.layers.Dense(n_units=100, name='out')(nn) >>> model = tl.models.Model(inputs=ni, outputs=nn) >>> # train your own classifier (only update the last layer) >>> train_params = model.get_layer('out').trainable_weights
Reuse model
>>> # in dynamic model, we can directly use the same model >>> # in static model >>> vgg_layer = tl.models.vgg19().as_layer() >>> ni_1 = tl.layers.Input([None, 224, 244, 3]) >>> ni_2 = tl.layers.Input([None, 224, 244, 3]) >>> a_1 = vgg_layer(ni_1) >>> a_2 = vgg_layer(ni_2) >>> M = Model(inputs=[ni_1, ni_2], outputs=[a_1, a_2])
SqueezeNetV1¶
-
tensorlayer.models.
SqueezeNetV1
(pretrained=False, end_with='out', name=None)[source]¶ Pre-trained SqueezeNetV1 model (static mode). Input shape [?, 224, 224, 3], value range [0, 1].
- Parameters
pretrained (boolean) – Whether to load pretrained weights. Default False.
end_with (str) – The end point of the model [conv1, maxpool1, fire2, fire3, fire4, …, out]. Default
out
i.e. the whole model.name (None or str) – Name for this model.
Examples
Classify ImageNet classes, see tutorial_models_squeezenetv1.py
>>> # get the whole model >>> squeezenet = tl.models.SqueezeNetV1(pretrained=True) >>> # use for inferencing >>> output = squeezenet(img1, is_train=False) >>> prob = tf.nn.softmax(output)[0].numpy()
Extract features and Train a classifier with 100 classes
>>> # get model without the last layer >>> cnn = tl.models.SqueezeNetV1(pretrained=True, end_with='drop1').as_layer() >>> # add one more layer and build new model >>> ni = Input([None, 224, 224, 3], name="inputs") >>> nn = cnn(ni) >>> nn = Conv2d(100, (1, 1), (1, 1), padding='VALID', name='conv10')(nn) >>> nn = GlobalMeanPool2d(name='globalmeanpool')(nn) >>> model = tl.models.Model(inputs=ni, outputs=nn) >>> # train your own classifier (only update the last layer) >>> train_params = model.get_layer('conv10').trainable_weights
- Returns
- Return type
static SqueezeNetV1.
MobileNetV1¶
-
tensorlayer.models.
MobileNetV1
(pretrained=False, end_with='out', name=None)[source]¶ Pre-trained MobileNetV1 model (static mode). Input shape [?, 224, 224, 3], value range [0, 1].
- Parameters
pretrained (boolean) – Whether to load pretrained weights. Default False.
end_with (str) – The end point of the model [conv, depth1, depth2 … depth13, globalmeanpool, out]. Default
out
i.e. the whole model.name (None or str) – Name for this model.
Examples
Classify ImageNet classes, see tutorial_models_mobilenetv1.py
>>> # get the whole model with pretrained weights >>> mobilenetv1 = tl.models.MobileNetV1(pretrained=True) >>> # use for inferencing >>> output = mobilenetv1(img1, is_train=False) >>> prob = tf.nn.softmax(output)[0].numpy()
Extract features and Train a classifier with 100 classes
>>> # get model without the last layer >>> cnn = tl.models.MobileNetV1(pretrained=True, end_with='reshape').as_layer() >>> # add one more layer and build new model >>> ni = Input([None, 224, 224, 3], name="inputs") >>> nn = cnn(ni) >>> nn = Conv2d(100, (1, 1), (1, 1), name='out')(nn) >>> nn = Flatten(name='flatten')(nn) >>> model = tl.models.Model(inputs=ni, outputs=nn) >>> # train your own classifier (only update the last layer) >>> train_params = model.get_layer('out').trainable_weights
- Returns
- Return type
static MobileNetV1.
Seq2seq¶
-
class
tensorlayer.models.
Seq2seq
(decoder_seq_length, cell_enc, cell_dec, n_units=256, n_layer=3, embedding_layer=None, name=None)[source]¶ vanilla stacked layer Seq2Seq model.
- Parameters
decoder_seq_length (int) – The length of your target sequence
cell_enc (TensorFlow cell function) – The RNN function cell for your encoder stack, e.g tf.keras.layers.GRUCell
cell_dec (TensorFlow cell function) – The RNN function cell for your decoder stack, e.g. tf.keras.layers.GRUCell
n_layer (int) – The number of your RNN layers for both encoder and decoder block
embedding_layer (tl.Layer) – A embedding layer, e.g. tl.layers.Embedding(vocabulary_size=voc_size, embedding_size=emb_dim)
name (str) – The model name
Examples
Classify stacked-layer Seq2Seq model, see chatbot
- Returns
- Return type
static stacked-layer Seq2Seq model.
Seq2seq Luong Attention¶
-
class
tensorlayer.models.
Seq2seqLuongAttention
(hidden_size, embedding_layer, cell, method, name=None)[source]¶ Luong Attention-based Seq2Seq model. Implementation based on https://arxiv.org/pdf/1508.04025.pdf.
- Parameters
hidden_size (int) – The hidden size of both encoder and decoder RNN cells
cell (TensorFlow cell function) – The RNN function cell for your encoder and decoder stack, e.g. tf.keras.layers.GRUCell
embedding_layer (tl.Layer) – A embedding layer, e.g. tl.layers.Embedding(vocabulary_size=voc_size, embedding_size=emb_dim)
method (str) – The three alternatives to calculate the attention scores, e.g. “dot”, “general” and “concat”
name (str) – The model name
- Returns
- Return type
static single layer attention-based Seq2Seq model.