Keras-real-time-prediction-using-resnet-model

提供:Dev Guides
移動先:案内検索

ResNetモデルを使用したリアルタイム予測

_ResNet_は事前トレーニング済みのモデルです。 _ImageNet_を使用してトレーニングされます。 _ImageNet_で事前トレーニングされたResNetモデルの重み。 次の構文があります-

keras.applications.resnet.ResNet50 (
   include_top = True,
   weights = 'imagenet',
   input_tensor = None,
   input_shape = None,
   pooling = None,
   classes = 1000
)

ここに、

  • include_top は、ネットワークの上部にある完全に接続されたレイヤーを指します。
  • weights ImageNetの事前トレーニングを参照してください。
  • input_tensor は、モデルの画像入力として使用するオプションのKerasテンソルを指します。
  • input_shape はオプションの形状タプルを参照します。 このモデルのデフォルトの入力サイズは224x224です。
  • classes 画像を分類するためにオプションのクラス数を参照します。

簡単な例を書いてモデルを理解しましょう-

ステップ1:モジュールをインポートする

以下に指定されているように必要なモジュールをロードしてみましょう-

>>> import PIL
>>> from keras.preprocessing.image import load_img
>>> from keras.preprocessing.image import img_to_array
>>> from keras.applications.imagenet_utils import decode_predictions
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> from keras.applications.resnet50 import ResNet50
>>> from keras.applications import resnet50

ステップ2:入力を選択する

以下で指定されているように、入力画像 Lotus を選択しましょう-

>>> filename = 'banana.jpg'
>>> ## load an image in PIL format
>>> original = load_img(filename, target_size = (224, 224))
>>> print('PIL image size',original.size)
PIL image size (224, 224)
>>> plt.imshow(original)
<matplotlib.image.AxesImage object at 0x1304756d8>
>>> plt.show()

ここでは、画像*(banana.jpg)*を読み込んで表示しています。

ステップ3:画像をNumPy配列に変換する

入力 Banana をNumPy配列に変換して、予測の目的でモデルに渡すことができるようにします。

>>> #convert the PIL image to a numpy array
>>> numpy_image = img_to_array(original)

>>> plt.imshow(np.uint8(numpy_image))
<matplotlib.image.AxesImage object at 0x130475ac8>

>>> print('numpy array size',numpy_image.shape)
numpy array size (224, 224, 3)

>>> # Convert the image/images into batch format
>>> image_batch = np.expand_dims(numpy_image, axis = 0)

>>> print('image batch size', image_batch.shape)
image batch size (1, 224, 224, 3)
>>>

ステップ4:モデル予測

入力をモデルにフィードして、予測を取得しましょう

>>> prepare the image for the resnet50 model >>>
>>> processed_image = resnet50.preprocess_input(image_batch.copy())

>>> # create resnet model
>>>resnet_model = resnet50.ResNet50(weights = 'imagenet')
>>> Downloavding data from https://github.com/fchollet/deep-learning-models/releas
es/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5
102858752/102853048 [==============================] - 33s 0us/step

>>> # get the predicted probabilities for each class
>>> predictions = resnet_model.predict(processed_image)

>>> # convert the probabilities to class labels
>>> label = decode_predictions(predictions)
Downloading data from https://storage.googleapis.com/download.tensorflow.org/
data/imagenet_class_index.json
40960/35363 [==================================] - 0s 0us/step

>>> print(label)

出力

[
   [
      ('n07753592', 'banana', 0.99229723),
      ('n03532672', 'hook', 0.0014551596),
      ('n03970156', 'plunger', 0.0010738898),
      ('n07753113', 'fig', 0.0009359837) ,
      ('n03109150', 'corkscrew', 0.00028538404)
   ]
]

ここで、モデルは画像をバナナとして正しく予測しました。