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 creates 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 を用いて算出しています。

利用可能な ImageNet モデル

名前

クラス

Top-1 エラー

Top-5 エラー

学習手法

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

ResNeXt-50

ResNeXt50

22.95

6.73

Neural Network Console

ResNeXt-101

ResNeXt101

22.80

6.74

Neural Network Console

ShuffleNet

ShuffleNet10

34.15

13.85

Neural Network Console

ShuffleNet-0.5x

ShuffleNet05

41.99

19.64

Neural Network Console

ShuffleNet-2.0x

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.ResNet18[ソース]

ResNet (18) のエイリアス。

class nnabla.models.imagenet.ResNet34[ソース]

ResNet (34) のエイリアス。

class nnabla.models.imagenet.ResNet50[ソース]

ResNet (50) のエイリアス。

class nnabla.models.imagenet.ResNet101[ソース]

ResNet (101) のエイリアス。

class nnabla.models.imagenet.ResNet152[ソース]

ResNet (152) のエイリアス。

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.VGG11[ソース]

VGG (11) のエイリアス。

class nnabla.models.imagenet.VGG13[ソース]

VGG (13) のエイリアス。

class nnabla.models.imagenet.VGG16[ソース]

VGG (16) のエイリアス。

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.ResNeXt50[ソース]

ResNeXt (50) のエイリアス。

class nnabla.models.imagenet.ResNeXt101[ソース]

ResNeXt (101) のエイリアス。

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' までのネットワーク。

参照