GPU上での実行

CPU、Neuronコア、それぞれで実行した Keras Resnet-50モデルによる画像分類を、NVIDIA T4 GPUを搭載したAmazon EC2 G4インスタンス上でも実行してみましょう。

Step 1. EC2 G4dn インスタンスを起動

EC2 ポータルに移動し、Deep Learning AMI (Ubuntu 18.04) バージョン 51 を選択し、g4dn.2xlargeを起動します。

Step 2. G4dn 環境をセットアップ

Deep Learning AMI にプリインストール済みのTensorFlow 仮想環境を立ち上げます。

source activate tensorflow_p37

tensorflow_p37ではデフォルトで NVIDIA CUDA 11.0 環境に設定されています

Step 3. SavedModelと入力イメージの準備

章4.1で使用したCPU向けSavedModelと入力イメージを再利用しますので、章4.1の内容を再度実行して下さい。

Step 4. 推論実行 Python スクリプトを作成

以下の内容でinfer_resnet50_batch_g4dn.py というファイル名の推論実行 Python スクリプトを作成します。

import os
import time
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
from concurrent import futures

# Load model
model_dir = 'resnet50'
predictor_gpu = tf.contrib.predictor.from_saved_model(model_dir)

# measure the performance per batch size
for batch_size in [1, 2, 4, 8, 16, 32, 64, 128]:
    USER_BATCH_SIZE = batch_size
    print("batch_size: {}, USER_BATCH_SIZE: {}". format(batch_size, USER_BATCH_SIZE))

# Create input from image
    img_sgl = image.load_img('kitten_small.jpg', target_size=(224, 224))
    img_arr = image.img_to_array(img_sgl)
    img_arr2 = np.expand_dims(img_arr, axis=0)
    img_arr3 = preprocess_input(np.repeat(img_arr2, USER_BATCH_SIZE, axis=0))

    model_feed_dict={'input': img_arr3}

# warmup
    infa_rslts = predictor_gpu(model_feed_dict) 

    num_loops = 100
    num_inferences = num_loops * USER_BATCH_SIZE

# Run inference on GPU, Display results
    start = time.time()
    with futures.ThreadPoolExecutor(8) as exe:
        fut_list = []
        for _ in range (num_loops):
            fut = exe.submit(predictor_gpu, model_feed_dict)
            fut_list.append(fut)
        for fut in fut_list:
            infa_rslts = fut.result()
    elapsed_time = time.time() - start

    print('By GPU         - num_inferences:{:>6}[images], elapsed_time:{:6.2f}[sec], Throughput:{:8.2f}[images/sec]'.format(num_inferences, elapsed_time, num_inferences / elapsed_time))

Step 5. 推論スクリプトを実行

推論実行スクリプトinfer_resnet50_batch_g4dn.pyを実行します。

python infer_resnet50_batch_g4dn.py

出力結果から、Batchサイズを大きくした場合にGPU上での推論スループットが向上している事が確認できます。

batch_size: 1, USER_BATCH_SIZE: 1
By GPU         - num_inferences:   100[images], elapsed_time:  1.30[sec], Throughput:   77.10[images/sec]
batch_size: 2, USER_BATCH_SIZE: 2
By GPU         - num_inferences:   200[images], elapsed_time:  0.92[sec], Throughput:  217.61[images/sec]
batch_size: 4, USER_BATCH_SIZE: 4
By GPU         - num_inferences:   400[images], elapsed_time:  1.53[sec], Throughput:  261.87[images/sec]
batch_size: 8, USER_BATCH_SIZE: 8
By GPU         - num_inferences:   800[images], elapsed_time:  2.72[sec], Throughput:  294.40[images/sec]
batch_size: 16, USER_BATCH_SIZE: 16
By GPU         - num_inferences:  1600[images], elapsed_time:  4.85[sec], Throughput:  329.68[images/sec]
batch_size: 32, USER_BATCH_SIZE: 32
By GPU         - num_inferences:  3200[images], elapsed_time:  9.25[sec], Throughput:  345.84[images/sec]
batch_size: 64, USER_BATCH_SIZE: 64
By GPU         - num_inferences:  6400[images], elapsed_time: 18.29[sec], Throughput:  350.01[images/sec]
batch_size: 128, USER_BATCH_SIZE: 128
By GPU         - num_inferences: 12800[images], elapsed_time: 36.99[sec], Throughput:  346.06[images/sec]