トップページデータベース研究CNN による画像分類 (image classification)ImageNet で事前学習済みの CNN を用いた画像分類(MobileNetV2,ResNet-50,DenseNet 121,DenseNet 169,NASNetを使用)(Google Colab あるいは Windows あるいは Ubuntu 上)

ImageNet で事前学習済みの CNN を用いた画像分類(MobileNetV2,ResNet-50,DenseNet 121,DenseNet 169,NASNetを使用)(Google Colab あるいは Windows あるいは Ubuntu 上)

画像分類は,画像からそのクラス名を求めるもの.

Keras では,ImageNet で事前学習済みのモデルを,簡単に使うことができる.

このページでは, Keras の ImageNet で事前学習済みの MobileNetV2, Inception Resnet, ResNet-50DenseNet 121, DenseNet 169NASNetを用いて画像分類を行う.

Keras で利用可能な画像分類のモデルは,https://keras.io/api/applications/ で説明されている.

目次:

  1. Google Colab へのリンク
  2. 前準備
  3. このページで説明のために使用する画像
  4. ImageNet で学習済みの MobileNetV2 を用いた画像分類
  5. ImageNet で学習済みの ResNet-50 を用いた画像分類
  6. ImageNet で学習済みの Inception-ResNet を用いた画像分類
  7. ImageNet で学習済みの DenseNet 121 を用いた画像分類
  8. ImageNet で学習済みの DenseNet 169 を用いた画像分類
  9. ImageNet で学習済みの NASNet Large を用いた画像分類

このページの URL: https://www.kkaneko.jp/dblab/imclassify/resnet50.html

参考文献:

1. Google Colab へのリンク

このページの内容は,Google Colab でも実行できる.

そのために,次の URL で,Google Colab のノートブックを準備している.

次のリンクをクリックすると,Google Colab のノートブックが開く. そして,Google アカウントでログインすると,Google Colab のノートブック内のコードを実行することができる.Google Colab のノートブックは書き換えて使うこともできる.このとき,書き換え後のものを,各自の Google ドライブ内に保存することもできる.

https://colab.research.google.com/drive/1c0uJaZB7B6SDTnxS_5FJukKd9FbYg4-e?usp=sharing

2. 前準備

Google Colab を使うか,パソコンを使う.それぞれの場合の前準備を説明する.

(1) Google Colab を使う場合

  1. Google Colab のWebページを開く

    https://colab.research.google.com

    Google Colab はオンラインの Python 開発環境. 使用するには Google アカウントが必要

  2. ファイル」で、「ノートブックを新規作成」を選ぶ

  3. Google アカウントでのログインが求められたときはログインする

(2) パソコンを使う場合

(NVIDIA GPU を使うとき)TensorFlow のバージョンを確認の上,NIDIA CUDA ツールキットとNIDIA cuDNN のバージョンを確認

TensorFlow を使う場合は,必要となる NVIDIA CUDA ツールキット,NVIDIA cuDNN のバージョン確認

TensorFlow は,そのバージョンによって,必要となるNVIDIA CUDA ツールキット,NVIDIA cuDNN のバージョンが違う(最新の NVIDIA CUDA ツールキット,NVIDIA cuDNN で動くというわけでない). そのことは,https://www.tensorflow.org/install/gpu で確認できる.

そこで, まずは,使用したい TensorFlow のバージョンを確認し,それにより, NVIDIA CUDA ツールキット,NVIDIA cuDNN を確認する.

NVIDIA CUDA ツールキットのバージョン:

指定されているバージョンより高いものは使わない. その根拠は次のページ. URL: https://www.tensorflow.org/install/source#common_installation_problems

NVIDIA cuDNN のバージョン:

その根拠は次のページ. URL: https://www.tensorflow.org/install/source#common_installation_problems

(NVIDIA GPU を使うとき)NVIDIA グラフィックスドライバ,NVIDIA CUDA ツールキット 11.0 ,NVIDIA cuDNN 8.0.5 のインストール

GPU とは,グラフィックス・プロセッシング・ユニットの略で、コンピュータグラフィックス関連の機能,乗算や加算の並列処理の機能などがある.

NVIDIA CUDA は,NVIDIA社が提供している GPU 用のプラットフォームである.

インストール手順の説明

関連 Web ページ

Python のインストール,pip と setuptools の更新,Python 開発環境(JupyterLab, spyder, nteract)のインストール,TensorFlow などのインストール

インストール手順の説明

Python プログラムを動かすために, pythonpython3などのコマンドを使う. あるいは, 開発環境や Python コンソール(Jupyter Qt ConsolespyderPyCharmPyScripter など)の利用も便利である.

GraphViz のインストール

3. このページで説明のために使用する画像

画像ファイル fruits.jpg, home.jpg のダウンロード

画像ファイル fruits.jpg, home.jpg のダウンロード手順は,別ページで説明している.

https://github.com/opencv/opencv/tree/master/samples/data で公開されている fruits.jpg, home.jpg を使用する(謝辞:画像の作者に感謝します)

4. ImageNet で学習済みの MobileNetV2 を用いた画像分類

参考 Web ページ: https://keras.io/ja/applications/

謝辞:ここでは、https://keras.io/ja/applications に記載のプログラムを変更して使用している

  1. モデルの作成

    次の Python プログラムを実行

    from __future__ import absolute_import, division, print_function, unicode_literals
    import tensorflow.compat.v2 as tf
    import tensorflow_datasets as tfds
    from tensorflow.keras.preprocessing import image
    import numpy as np
    import matplotlib.pyplot as plt
    %matplotlib inline
    import warnings
    warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
    tf.enable_v2_behavior()
    from tensorflow.keras import backend as K 
    K.clear_session()
    from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2
    from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
    IMG_SIZE = 224
    m = MobileNetV2(weights='imagenet')
    m.summary()
    

  2. 確認のため,モデルのプロット
    from tensorflow.keras.utils import plot_model
    import pydot
    plot_model(m)
    

  3. 画像分類の実行

    C:/image/fruits.jpg」, 「C:/image/home.jpg」のところには,画像ファイル名を指定すること.

    from matplotlib import pyplot as plt
    %matplotlib inline
    import warnings
    warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
    import PIL
    def preprocess_image(img_path, img_size):
        img = image.load_img(img_path, target_size=(img_size, img_size))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        return preprocess_input(x)
    def plot_image(img_path, img_size):
        img = image.load_img(img_path, target_size=(img_size, img_size))
        plt.imshow(img)
        return
    img_path = 'C:/image/fruits.jpg'
    plot_image(img_path, IMG_SIZE)
    print('Predicted:', decode_predictions(m.predict(preprocess_image(img_path, IMG_SIZE)), top=3)[0])
    

    img_path = 'C:/image/home.jpg'
    plot_image(img_path, IMG_SIZE)
    print('Predicted:', decode_predictions(m.predict(preprocess_image(img_path, IMG_SIZE)), top=3)[0])
    

5. ImageNet で学習済みの ResNet-50 を用いた画像分類

参考 Web ページ: https://keras.io/ja/applications/

謝辞:ここでは、https://keras.io/ja/applications に記載のプログラムを変更して使用している

  1. モデルの作成
    from __future__ import absolute_import, division, print_function, unicode_literals
    import tensorflow.compat.v2 as tf
    import tensorflow_datasets as tfds
    from tensorflow.keras.applications.resnet50 import ResNet50
    from tensorflow.keras.preprocessing import image
    from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
    import numpy as np
    IMG_SIZE = 224
    m = ResNet50(weights='imagenet')
    m.summary()
    

  2. 確認のため,モデルのプロット
    from tensorflow.keras.utils import plot_model
    import pydot
    plot_model(m)
    

  3. 画像分類の実行

    C:/image/fruits.jpg」, 「C:/image/home.jpg」のところには,画像ファイル名を指定すること.

    from matplotlib import pyplot as plt
    %matplotlib inline
    import warnings
    warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
    import PIL
    def preprocess_image(img_path, img_size):
        img = image.load_img(img_path, target_size=(img_size, img_size))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        return preprocess_input(x)
    def plot_image(img_path, img_size):
        img = image.load_img(img_path, target_size=(img_size, img_size))
        plt.imshow(img)
        return
    img_path = 'C:/image/fruits.jpg'
    plot_image(img_path, IMG_SIZE)
    print('Predicted:', decode_predictions(m.predict(preprocess_image(img_path, IMG_SIZE)), top=3)[0])
    

    img_path = 'C:/image/home.jpg'
    plot_image(img_path, IMG_SIZE)
    print('Predicted:', decode_predictions(m.predict(preprocess_image(img_path, IMG_SIZE)), top=3)[0])
    

6. ImageNet で学習済みの Inception-ResNet を用いた画像分類

参考 Web ページ: https://keras.io/ja/applications/

謝辞:ここでは、https://keras.io/ja/applications に記載のプログラムを変更して使用している

  1. モデルの作成

    次の Python プログラムを実行

    from __future__ import absolute_import, division, print_function, unicode_literals
    import tensorflow.compat.v2 as tf
    import tensorflow_datasets as tfds
    from tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2
    from tensorflow.keras.preprocessing import image
    from tensorflow.keras.applications.inception_resnet_v2 import preprocess_input, decode_predictions
    import numpy as np
    IMG_SIZE = 299
    m = InceptionResNetV2(weights='imagenet')
    m.summary()
    

  2. 確認のため,モデルのプロット
    from tensorflow.keras.utils import plot_model
    import pydot
    plot_model(m)
    

  3. 画像分類の実行

    C:/image/fruits.jpg」, 「C:/image/home.jpg」のところには,画像ファイル名を指定すること.

    from matplotlib import pyplot as plt
    %matplotlib inline
    import warnings
    warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
    import PIL
    def preprocess_image(img_path, img_size):
        img = image.load_img(img_path, target_size=(img_size, img_size))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        return preprocess_input(x)
    def plot_image(img_path, img_size):
        img = image.load_img(img_path, target_size=(img_size, img_size))
        plt.imshow(img)
        return
    img_path = 'C:/image/fruits.jpg'
    plot_image(img_path, IMG_SIZE)
    print('Predicted:', decode_predictions(m.predict(preprocess_image(img_path, IMG_SIZE)), top=3)[0])
    

    img_path = 'C:/image/home.jpg'
    plot_image(img_path, IMG_SIZE)
    print('Predicted:', decode_predictions(m.predict(preprocess_image(img_path, IMG_SIZE)), top=3)[0])
    

7. ImageNet で学習済みの DenseNet 121 を用いた画像分類

参考 Web ページ: https://keras.io/ja/applications/

謝辞:ここでは、https://keras.io/ja/applications に記載のプログラムを変更して使用している

  1. モデルの作成

    次の Python プログラムを実行

    from __future__ import absolute_import, division, print_function, unicode_literals
    import tensorflow.compat.v2 as tf
    import tensorflow_datasets as tfds
    from tensorflow.keras.applications.densenet import DenseNet121
    from tensorflow.keras.preprocessing import image
    from tensorflow.keras.applications.densenet import preprocess_input, decode_predictions
    import numpy as np
    IMG_SIZE = 224
    m = DenseNet121(weights='imagenet')
    m.summary()
    

  2. 確認のため,モデルのプロット
    from tensorflow.keras.utils import plot_model
    import pydot
    plot_model(m)
    

  3. 画像分類の実行

    C:/image/fruits.jpg」, 「C:/image/home.jpg」のところには,画像ファイル名を指定すること.

    from matplotlib import pyplot as plt
    %matplotlib inline
    import warnings
    warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
    import PIL
    def preprocess_image(img_path, img_size):
        img = image.load_img(img_path, target_size=(img_size, img_size))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        return preprocess_input(x)
    def plot_image(img_path, img_size):
        img = image.load_img(img_path, target_size=(img_size, img_size))
        plt.imshow(img)
        return
    img_path = 'C:/image/fruits.jpg'
    plot_image(img_path, IMG_SIZE)
    print('Predicted:', decode_predictions(m.predict(preprocess_image(img_path, IMG_SIZE)), top=3)[0])
    

    img_path = 'C:/image/home.jpg'
    plot_image(img_path, IMG_SIZE)
    print('Predicted:', decode_predictions(m.predict(preprocess_image(img_path, IMG_SIZE)), top=3)[0])
    

8. ImageNet で学習済みの DenseNet 169 を用いた画像分類

参考 Web ページ: https://keras.io/ja/applications/

謝辞:ここでは、https://keras.io/ja/applications に記載のプログラムを変更して使用している

  1. モデルの作成

    次の Python プログラムを実行

    from __future__ import absolute_import, division, print_function, unicode_literals
    import tensorflow.compat.v2 as tf
    import tensorflow_datasets as tfds
    from tensorflow.keras.applications.densenet import DenseNet169
    from tensorflow.keras.preprocessing import image
    from tensorflow.keras.applications.densenet import preprocess_input, decode_predictions
    import numpy as np
    IMG_SIZE = 224
    m = DenseNet169(weights='imagenet')
    m.summary()
    

  2. 確認のため,モデルのプロット
    from tensorflow.keras.utils import plot_model
    import pydot
    plot_model(m)
    

  3. 画像分類の実行

    C:/image/fruits.jpg」, 「C:/image/home.jpg」のところには,画像ファイル名を指定すること.

    from matplotlib import pyplot as plt
    %matplotlib inline
    import warnings
    warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
    import PIL
    def preprocess_image(img_path, img_size):
        img = image.load_img(img_path, target_size=(img_size, img_size))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        return preprocess_input(x)
    def plot_image(img_path, img_size):
        img = image.load_img(img_path, target_size=(img_size, img_size))
        plt.imshow(img)
        return
    img_path = 'C:/image/fruits.jpg'
    plot_image(img_path, IMG_SIZE)
    print('Predicted:', decode_predictions(m.predict(preprocess_image(img_path, IMG_SIZE)), top=3)[0])
    

    img_path = 'C:/image/home.jpg'
    plot_image(img_path, IMG_SIZE)
    print('Predicted:', decode_predictions(m.predict(preprocess_image(img_path, IMG_SIZE)), top=3)[0])
    

9. ImageNet で学習済みの NASNet Large を用いた画像分類

参考 Web ページ: https://keras.io/ja/applications/

謝辞:ここでは、https://keras.io/ja/applications に記載のプログラムを変更して使用している

  1. モデルの作成

    次の Python プログラムを実行

    from __future__ import absolute_import, division, print_function, unicode_literals
    import tensorflow.compat.v2 as tf
    import tensorflow_datasets as tfds
    from tensorflow.keras.applications.nasnet import NASNetLarge
    from tensorflow.keras.preprocessing import image
    from tensorflow.keras.applications.nasnet import preprocess_input, decode_predictions
    import numpy as np
    IMG_SIZE = 331
    m = NASNetLarge(weights='imagenet')
    m.summary()
    

  2. 確認のため,モデルのプロット
    from tensorflow.keras.utils import plot_model
    import pydot
    plot_model(m)
    

  3. 画像分類の実行

    C:/image/fruits.jpg」, 「C:/image/home.jpg」のところには,画像ファイル名を指定すること.

    from matplotlib import pyplot as plt
    %matplotlib inline
    import warnings
    warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
    import PIL
    def preprocess_image(img_path, img_size):
        img = image.load_img(img_path, target_size=(img_size, img_size))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        return preprocess_input(x)
    def plot_image(img_path, img_size):
        img = image.load_img(img_path, target_size=(img_size, img_size))
        plt.imshow(img)
        return
    img_path = 'C:/image/fruits.jpg'
    plot_image(img_path, IMG_SIZE)
    print('Predicted:', decode_predictions(m.predict(preprocess_image(img_path, IMG_SIZE)), top=3)[0])
    

    img_path = 'C:/image/home.jpg'
    plot_image(img_path, IMG_SIZE)
    print('Predicted:', decode_predictions(m.predict(preprocess_image(img_path, IMG_SIZE)), top=3)[0])