Vengineerの妄想(準備期間)

人生は短いけど、長いです。人生を楽しみましょう!

nnablaで学習済みモデル(nnabla.modelsパッケージ)を公開


nnabla v1.0.11が公開されて、
学習済みモデルを簡単に推論と追加学習向けに使うことができるnnabla.modelsパッケージが追加されたようです。

引用します。

まずは、推論
# Create ResNet-18 for inference
from nnabla.models.imagenet import ResNet
model = ResNet(18)
batch_size = 1
# model.input_shape returns (3, 224, 224) when ResNet-18
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])

nnabla.models.imagenetから ResNet を取り込んで、ResNet(18)とかにすると、ResNet18ができちゃう。
Resnet(18)の入力データの型(model.input_shape)は、(2,224,224)なので、バッチサイズ(batch_size)を1にセットして、
推論用のモデル(y = model(x, training=False) として扱うのね。

imreadを使って、JPEGファイルをリードして、入力( x.d[0] ) を代入して、y.forward() を実行。
推論結果からラベル取り出して、表示。。。

そして、学習は、
# Create ResNet-18 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))

学習では、batch_sizeを32にして、training=Trueにして、。。。。

こうやって、普通にディープ・ラーニングしたい人用に、モデル提供したりしないと、使ってくれないからね。。。

追記)、
v1.0.12のお知らせ
引用します
nnablaのv1.0.12を公開しました!固定長RNNが目玉の追加となっております!添付画像のように複数層の固定長LSTMを定義できます。
https://blog.nnabla.org/ja/release/v1-0-12/
また、v1.0.11の変更で引き起こしたメモリリーク問題が修正したので、お使いの方はお手数ですが、v1.0.12にバージョンアップお願いします。