ImageNet モデル¶
このサブパッケージは、ImageNet データセットで学習された様々な最先端の学習済みモデルを提供します。
学習済みモデルは、以下のようにして推論や学習に用いることができます。
# Create ResNet-50 for inference
import nnabla as nn
import nnabla.functions as F
import nnabla.parametric_functions as PF
import numpy as np
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...
利用可能なモデルを次の表にまとめます。エラー率は single center crop を用いて算出しています。
名前 |
クラス |
Top-1 エラー |
Top-5 エラー |
学習手法 |
---|---|---|---|---|
ResNet18 |
30.28 |
10.90 |
Neural Network Console |
|
ResNet34 |
26.72 |
8.89 |
Neural Network Console |
|
ResNet50 |
24.59 |
7.48 |
Neural Network Console |
|
ResNet101 |
23.81 |
7.01 |
Neural Network Console |
|
ResNet152 |
23.48 |
7.09 |
Neural Network Console |
|
MobileNet |
29.51 |
10.34 |
Neural Network Console |
|
MobileNetV2 |
29.94 |
10.82 |
Neural Network Console |
|
SENet |
22.04 |
6.29 |
Neural Network Console |
|
SqueezeNetV10 |
42.71 |
20.12 |
Neural Network Console |
|
SqueezeNetV11 |
41.23 |
19.18 |
Neural Network Console |
|
VGG11 |
30.85 |
11.38 |
Neural Network Console |
|
VGG13 |
29.51 |
10.46 |
Neural Network Console |
|
VGG16 |
29.03 |
10.07 |
Neural Network Console |
|
NIN |
42.91 |
20.66 |
Neural Network Console |
|
DenseNet |
23.82 |
7.02 |
Neural Network Console |
|
InceptionV3 |
21.82 |
5.88 |
Neural Network Console |
|
Xception |
23.59 |
6.91 |
Neural Network Console |
|
GoogLeNet |
31.22 |
11.34 |
Neural Network Console |
|
ResNeXt50 |
22.95 |
6.73 |
Neural Network Console |
|
ResNeXt101 |
22.80 |
6.74 |
Neural Network Console |
|
ShuffleNet10 |
34.15 |
13.85 |
Neural Network Console |
|
ShuffleNet05 |
41.99 |
19.64 |
Neural Network Console |
|
ShuffleNet20 |
30.34 |
11.12 |
Neural Network Console |
共通インターフェイス¶
- class nnabla.models.imagenet.base.ImageNetBase[ソース]¶
ほとんどの ImageNet 学習済みモデルは、このクラスから継承され、いくつかの共通インターフェイスを提供します。
- __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)[ソース]¶
読み込んだモデルからネットワーク (計算グラフ) を生成します。
- パラメータ
input_var (Variable, optional) -- 指定された場合、入力Variableは指定されたVariableに置き換えられ、そのVariable上にネットワークが構築されます。特に指定がない場合は、バッチサイズを1としたVariable、
self.input_shape
を元にしたデフォルトのShapeとなります。use_up_to (str) -- ネットワークは文字列で指定したVariableまで構築されます。モデルに対応したVariableの文字列のリストは、各モデルクラスのドキュメントに記載されています。
training (bool) -- このオプションは、構築されたネットワークの追加学習 (ファインチューニング、転移学習など) を有効にします。True の場合、batch normalization の
batch_stat
オプションがTrue
になり、学習可能なVariable (conv weights および bn のガンマとベータなど) のneed_grad
属性がTrue
になります。 デフォルトはFalse
です。force_global_pooling (bool) -- 入力画像のサイズに関わらず、分類層の前の最終 average pooling は、自動的に global average pooling に変換されます。デフォルトは
False
です。check_global_pooling (bool) --
True
かつ最終 average pooling のストライド設定が global pooling を目的としない場合、例外を発生させます。デフォルトはTrue
です。入力空間サイズに関わらず、(7, 7)
などの学習時のストライド で pooling を実行したい場合、False
を指定してください。returns_net (bool) --
True
の場合、NnpNetwork
オブジェクトを返します。それ以外の場合は、単に構築したネットワークの最後のVariableを返します。デフォルトはFalse
です。verbose (bool, or int) -- Verbose レベル。
0
に設定した場合、ネットワーク構築中は何も出力しません。
- property category_names¶
1000 の ImageNet クラスのカテゴリー名を返します。
- property input_shape¶
デフォルトの画像サイズを、 (チャネル、高さ、幅) のタプルとして返します。
モデルのリスト¶
- class nnabla.models.imagenet.ResNet(num_layers=18)[ソース]¶
18、34、50、101、152 層用の ResNet アーキテクチャ。
- パラメータ
num_layers (int) -- 層数 (18、34、50、101、152 のいずれかの値)
__call__
method にあるuse_up_to
オプションで指定できる文字列リストは以下の通り;'classifier'
(デフォルト): 分類のための最終 affine 層の出力。'pool'
: 最終 global average pooling の出力。'lastconv'
: ReLU 活性化なしでの最終 global average pooling の入力。'lastconv+relu'
: ReLU 活性化に続く'lastconv'
までのネットワーク。
参照
- class nnabla.models.imagenet.MobileNet[ソース]¶
MobileNet アーキテクチャ。
__call__
method にあるuse_up_to
オプションで指定できる文字列リストは以下の通り;'classifier'
(デフォルト): 分類のための最終 affine 層の出力。'pool'
: 最終 global average pooling の出力。'lastconv'
: ReLU 活性化なしでの最終 global average pooling の入力。'lastconv+relu'
: ReLU 活性化に続く'lastconv'
までのネットワーク。
参照
- class nnabla.models.imagenet.MobileNetV2[ソース]¶
MobileNetV2 アーキテクチャ。
__call__
method にあるuse_up_to
オプションで指定できる文字列リストは以下の通り;'classifier'
(デフォルト): 分類のための最終 affine 層の出力。'pool'
: 最終 global average pooling の出力。'lastconv'
: ReLU 活性化なしでの最終 global average pooling の入力。'lastconv+relu'
: ReLU 活性化に続く'lastconv'
までのネットワーク。
参照
- class nnabla.models.imagenet.SENet[ソース]¶
改良された ResNeXt アーキテクチャに SE ブロックを統合する SENet-154 モデル。
__call__
method にあるuse_up_to
オプションで指定できる文字列リストは以下の通り;'classifier'
(デフォルト): 分類のための最終 affine 層の出力。'pool'
: 最終 global average pooling の出力。'lastconv'
: ReLU 活性化なしでの最終 global average pooling の入力。'lastconv+relu'
: ReLU 活性化に続く'lastconv'
までのネットワーク。
参照
- class nnabla.models.imagenet.SqueezeNetV10[ソース]¶
SquezeNetV10
SqueezeNet
('v1.0')
のエイリアス。
- class nnabla.models.imagenet.SqueezeNetV11[ソース]¶
SquezeNetV11
SqueezeNet
('v1.1')
のエイリアス。
- class nnabla.models.imagenet.SqueezeNet(version='v1.1')[ソース]¶
アーキテクチャ v1.0 および v1.1 のための SqueezeNet モデル。
- パラメータ
version (str) -- バージョン ('v1.0' または 'v1.1')
__call__
method にあるuse_up_to
オプションで指定できる文字列リストは以下の通り;'classifier'
(デフォルト): 分類のための最終 affine 層の出力。'pool'
: 最終 global average pooling の出力。'lastconv'
: ReLU 活性化なしでの最終 global average pooling の入力。'lastconv+relu'
: ReLU 活性化に続く'lastconv'
までのネットワーク。
参照
- class nnabla.models.imagenet.VGG(num_layers=11)[ソース]¶
11, 13, 16 層用の VGG アーキテクチャ。
- パラメータ
num_layers (int) -- 層の数 (11、13、16 のいずれかの数)
__call__
method にあるuse_up_to
オプションで指定できる文字列リストは以下の通り;'classifier'
(デフォルト): 分類のための最終 affine 層の出力。'pool'
: 最終 global average pooling の出力。'lastconv'
: ReLU 活性化なしでの最終 global average pooling の入力。'lastconv+relu'
: ReLU 活性化に続く'lastconv'
までのネットワーク。'lastfeature'
: 活性化を除く、'classifier'
直前の層までのネットワーク。
参照
- class nnabla.models.imagenet.NIN[ソース]¶
NIN (Network In Network) アーキテクチャ。
__call__
method にあるuse_up_to
オプションで指定できる文字列リストは以下の通り;'classifier'
(デフォルト): 分類のための最終 affine 層の出力。'pool'
: 最終 global average pooling の出力。'lastconv'
: ReLU 活性化なしでの最終 global average pooling の入力。'lastconv+relu'
: ReLU 活性化に続く'lastconv'
までのネットワーク。
参照
- class nnabla.models.imagenet.DenseNet[ソース]¶
__call__
method にあるuse_up_to
オプションで指定できる文字列リストは以下の通り;'classifier'
(デフォルト): 分類のための最終 affine 層の出力。'pool'
: 最終 global average pooling の出力。'lastconv'
: 最後の denseblock からの出力。'lastconv+relu'
: ReLU 活性化に続く'lastconv'
までのネットワーク。
参照
- class nnabla.models.imagenet.InceptionV3[ソース]¶
InceptionV3 アーキテクチャ。
__call__
method にあるuse_up_to
オプションで指定できる文字列リストは以下の通り;'classifier'
(デフォルト): 分類のための最終 affine 層の出力。'pool'
: 最終 global average pooling の出力。最終 global average pooling の入力。すなわち、最終インセプションブロックの出力。
参照
- class nnabla.models.imagenet.Xception[ソース]¶
Xception モデル。
__call__
method にあるuse_up_to
オプションで指定できる文字列リストは以下の通り;'classifier'
(デフォルト): 分類のための最終 affine 層の出力。'pool'
: 最終 global average pooling の出力。'lastconv'
: ReLU 活性化なしでの最終 global average pooling の入力。'lastconv+relu'
: ReLU 活性化に続く'lastconv'
までのネットワーク。
参照
- class nnabla.models.imagenet.GoogLeNet[ソース]¶
GoogLeNet モデル。
__call__
method にあるuse_up_to
オプションで指定できる文字列リストは以下の通り;'classifier'
(デフォルト): 分類のための最終 affine 層の出力。'pool'
: 最終 global average pooling の出力。最終 global average pooling の入力。すなわち、最終インセプションブロックの出力。
参照
- class nnabla.models.imagenet.ResNeXt(num_layers=50)[ソース]¶
50 と 101 層用の ResNeXt アーキテクチャ。
- パラメータ
num_layers (int) -- 層の数 (50、101 のいずれかの数)
__call__
method にあるuse_up_to
オプションで指定できる文字列リストは以下の通り;'classifier'
(デフォルト): 分類のための最終 affine 層の出力。'pool'
: 最終 global average pooling の出力。'lastconv'
: ReLU 活性化なしでの最終 global average pooling の入力。'lastconv+relu'
: ReLU 活性化に続く'lastconv'
までのネットワーク。
参照
- class nnabla.models.imagenet.ShuffleNet10[ソース]¶
ShuffleNet
(10)
のエイリアス。
- class nnabla.models.imagenet.ShuffleNet05[ソース]¶
ShuffleNet
(5)
のエイリアス。
- class nnabla.models.imagenet.ShuffleNet20[ソース]¶
ShuffleNet
(20)
のエイリアス。
- class nnabla.models.imagenet.ShuffleNet(scaling_factor=10)[ソース]¶
ShuffleNet、 ShuffleNet-0.5x、ShufffleNet-2.0x アーキテクチャ用のモデル。
- パラメータ
Factor (Scaling) -- ネットワークを希望の複雑さにカスタマイズするには、チャネル数に scale factor を適用するだけで実現できます。'10'、'5' または '20' から指定します。
__call__
method にあるuse_up_to
オプションで指定できる文字列リストは以下の通り;'classifier'
(デフォルト): 分類のための最終 affine 層の出力。'pool'
: 最終 global average pooling の出力。'lastconv'
: ReLU 活性化なしでの最終 global average pooling の入力。'lastconv+relu'
: ReLU 活性化に続く'lastconv'
までのネットワーク。
参照