ImageNet Models¶
This subpackage provides a variety of pre-trained state-of-the-art models which is trained on ImageNet dataset.
The pre-trained models can be used for both inference and training as following:
# Create ResNet-50 for inference
from nnabla.models.imagenet import ResNet50
model = ResNet50()
batch_size = 1
# model.input_shape returns (3, 224, 224) when ResNet-50
x = nn.Variable((batch_size,) + model.input_shape)
y = model(x, training=False)
# Execute inference
# Load input image as uint8 array with shape of (3, 224, 224)
from nnabla.utils.image_utils import imread
img = imread('example.jpg', size=model.input_shape[1:], channel_first=True)
x.d[0] = img
y.forward()
predicted_label = np.argmax(y.d[0])
print('Predicted label:', model.category_names[predicted_label])
# Create ResNet-50 for fine-tuning
batch_size=32
x = nn.Variable((batch_size,) + model.input_shape)
# * By training=True, it sets batch normalization mode for training
# and gives trainable attributes to parameters.
# * By use_up_to='pool', it creats a network up to the output of
# the final global average pooling.
pool = model(x, training=True, use_up_to='pool')
# Add a classification layer for another 10 category dataset
# and loss function
num_classes = 10
y = PF.affine(pool, num_classes, name='classifier10')
t = nn.Variable((batch_size, 1))
loss = F.sum(F.softmax_cross_entropy(y, t))
# Training...
Available models are summarized in the following table. Error rates are calculated using single center crop.
Name | Class | Top-1 error | Top-5 error | Trained by/with |
---|---|---|---|---|
ResNet-18 | ResNet18 | 30.28 | 10.90 | Neural Network Console |
ResNet-34 | ResNet34 | 26.72 | 8.89 | Neural Network Console |
ResNet-50 | ResNet50 | 24.59 | 7.48 | Neural Network Console |
ResNet-101 | ResNet101 | 23.81 | 7.01 | Neural Network Console |
ResNet-152 | ResNet152 | 23.48 | 7.09 | Neural Network Console |
MobileNet | MobileNet | 29.51 | 10.34 | Neural Network Console |
MobileNetV2 | MobileNetV2 | 29.94 | 10.82 | Neural Network Console |
SENet-154 | SENet | 22.04 | 6.29 | Neural Network Console |
SqueezeNet v1.0 | SqueezeNetV10 | 42.71 | 20.12 | Neural Network Console |
SqueezeNet v1.1 | SqueezeNetV11 | 41.23 | 19.18 | Neural Network Console |
VGG-11 | VGG11 | 30.85 | 11.38 | Neural Network Console |
VGG-13 | VGG13 | 29.51 | 10.46 | Neural Network Console |
VGG-16 | VGG16 | 29.03 | 10.07 | Neural Network Console |
NIN | NIN | 42.91 | 20.66 | Neural Network Console |
DenseNet-161 | DenseNet | 23.82 | 7.02 | Neural Network Console |
InceptionV3 | InceptionV3 | 21.82 | 5.88 | Neural Network Console |
Xception | Xception | 23.59 | 6.91 | Neural Network Console |
GoogLeNet | GoogLeNet | 31.22 | 11.34 | Neural Network Console |
Common interfaces¶
-
class
nnabla.models.imagenet.base.
ImageNetBase
[source]¶ Most of ImageNet pretrained models are inherited from this class so that it provides some common interfaces.
-
__call__
(input_var=None, use_from=None, use_up_to='classifier', training=False, force_global_pooling=False, check_global_pooling=True, returns_net=False, verbose=0)[source]¶ Create a network (computation graph) from a loaded model.
Parameters: - input_var (Variable, optional) – If given, input variable is replaced with the given variable and a network is constructed on top of the variable. Otherwise, a variable with batch size as 1 and a default shape from
self.input_shape
. - use_up_to (str) – Network is constructed up to a variable specified by a string. A list of string-variable correspondences in a model is described in documentation for each model class.
- training (bool) – This option enables additional training (fine-tuning, transfer learning etc.) for the constructed network. If True, the
batch_stat
option in batch normalization is turnedTrue
, andneed_grad
attribute in trainable variables (conv weights and gamma and beta of bn etc.) is turnedTrue
. The default isFalse
. - force_global_pooling (bool) – Regardless the input image size, the final average pooling before classification layer will be automatically transformed to a global average pooling. The default is
False
. - check_global_pooling (bool) – If
True
, and if the stride configuration of the final average pooling is not for global pooling, it raises an exception. The default isTrue
. UseFalse
when user want to do the pooling with the trained stride(7, 7)
regardless the input spatial size. - returns_net (bool) – When
True
, it returns aNnpNetwork
object. Otherwise, It only returns the last variable of the constructed network. The default isFalse
. - verbose (bool, or int) – Verbose level. With
0
, it says nothing during network construction.
- input_var (Variable, optional) – If given, input variable is replaced with the given variable and a network is constructed on top of the variable. Otherwise, a variable with batch size as 1 and a default shape from
-
category_names
¶ Returns category names of 1000 ImageNet classes.
-
input_shape
¶ Should returns default image size (channel, height, width) as a tuple.
-
List of models¶
-
class
nnabla.models.imagenet.
ResNet
(num_layers=18)[source]¶ ResNet architectures for 18, 34, 50, 101, and 152 of number of layers.
Parameters: num_layers (int) – Number of layers chosen from 18, 34, 50, 101, and 152. The following is a list of string that can be specified to
use_up_to
option in__call__
method;'classifier'
(default): The output of the final affine layer for classification.'pool'
: The output of the final global average pooling.'lastconv'
: The input of the final global average pooling without ReLU activation..'lastconv+relu'
: Network up to'lastconv'
followed by ReLU activation.
References
-
class
nnabla.models.imagenet.
MobileNet
[source]¶ MobileNet architecture.
The following is a list of string that can be specified to
use_up_to
option in__call__
method;'classifier'
(default): The output of the final affine layer for classification.'pool'
: The output of the final global average pooling.'lastconv'
: The input of the final global average pooling without ReLU activation..'lastconv+relu'
: Network up to'lastconv'
followed by ReLU activation.
References
-
class
nnabla.models.imagenet.
MobileNetV2
[source]¶ MobileNetV2 architecture.
The following is a list of string that can be specified to
use_up_to
option in__call__
method;'classifier'
(default): The output of the final affine layer for classification.'pool'
: The output of the final global average pooling.'lastconv'
: The input of the final global average pooling without ReLU activation..'lastconv+relu'
: Network up to'lastconv'
followed by ReLU activation.
References
-
class
nnabla.models.imagenet.
SENet
[source]¶ SENet-154 model which integrates SE blocks with a modified ResNeXt architecture.
The following is a list of string that can be specified to
use_up_to
option in__call__
method;'classifier'
(default): The output of the final affine layer for classification.'pool'
: The output of the final global average pooling.'lastconv'
: The input of the final global average pooling without ReLU activation..'lastconv+relu'
: Network up to'lastconv'
followed by ReLU activation.
References
-
class
nnabla.models.imagenet.
SqueezeNetV10
[source]¶ SquezeNetV10 An alias of
SqueezeNet
('v1.0')
.
-
class
nnabla.models.imagenet.
SqueezeNetV11
[source]¶ SquezeNetV11 An alias of
SqueezeNet
('v1.1')
.
-
class
nnabla.models.imagenet.
SqueezeNet
(version='v1.1')[source]¶ SqueezeNet model for architecture-v1.0 and v1.1 .
Parameters: version (str) – Version chosen from ‘v1.0’ and ‘v1.1’. The following is a list of string that can be specified to
use_up_to
option in__call__
method;'classifier'
(default): The output of the final affine layer for classification.'pool'
: The output of the final global average pooling.'lastconv'
: The input of the final global average pooling without ReLU activation..'lastconv+relu'
: Network up to'lastconv'
followed by ReLU activation.
References
-
class
nnabla.models.imagenet.
VGG
(num_layers=11)[source]¶ VGG architectures for 11, 13, 16 layers.
Parameters: num_layers (int) – Number of layers chosen from 11, 13, 16. The following is a list of string that can be specified to
use_up_to
option in__call__
method;'classifier'
(default): The output of the final affine layer for classification.'pool'
: The output of the final global average pooling.'lastconv'
: The input of the final global average pooling without ReLU activation..'lastconv+relu'
: Network up to'lastconv'
followed by ReLU activation.
References
-
class
nnabla.models.imagenet.
NIN
[source]¶ NIN(Network In Network) architecture.
The following is a list of string that can be specified to
use_up_to
option in__call__
method;'classifier'
(default): The output of the final affine layer for classification.'pool'
: The output of the final global average pooling.'lastconv'
: The input of the final global average pooling without ReLU activation..'lastconv+relu'
: Network up to'lastconv'
followed by ReLU activation.
References
-
class
nnabla.models.imagenet.
DenseNet
[source]¶ The following is a list of string that can be specified to
use_up_to
option in__call__
method;'classifier'
(default): The output of the final affine layer for classification.'pool'
: The output of the final global average pooling.'lastconv'
: The output from last denseblock..'lastconv+relu'
: Network up to'lastconv'
followed by ReLU activation.
References
-
class
nnabla.models.imagenet.
InceptionV3
[source]¶ InceptionV3 architecture. The following is a list of string that can be specified to
use_up_to
option in__call__
method; *'classifier'
(default): The output of the final affine layer for classification. *'pool'
: The output of the final global average pooling. *'prepool'
: The input of the final global average pooling, i.e. the output of the final inception block. .. rubric:: References
-
class
nnabla.models.imagenet.
Xception
[source]¶ Xception model.
The following is a list of string that can be specified to
use_up_to
option in__call__
method;'classifier'
(default): The output of the final affine layer for classification.'pool'
: The output of the final global average pooling.'lastconv'
: The input of the final global average pooling without ReLU activation..'lastconv+relu'
: Network up to'lastconv'
followed by ReLU activation.
References
-
class
nnabla.models.imagenet.
GoogLeNet
[source]¶ GoogLeNet model.
The following is a list of string that can be specified to
use_up_to
option in__call__
method;'classifier'
(default): The output of the final affine layer for classification.'pool'
: The output of the final global average pooling.'prepool'
: The input of the final global average pooling, i.e. the output of the final inception block.
References