金子邦彦研究室人工知能CNN による画像分類 (image classification)ImageNet で事前学習済みの畳み込みニューラルネットワーク (CNN) を用いた画像分類(MobileNetV2,ResNet50,DenseNet 121,DenseNet 169,NASNetを使用)(Google Colaboratroy へのリンク有り)

ImageNet で事前学習済みの畳み込みニューラルネットワーク (CNN) を用いた画像分類(MobileNetV2,ResNet50,DenseNet 121,DenseNet 169,NASNetを使用)(Google Colaboratroy へのリンク有り)

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

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

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

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

目次

  1. Google Colaboratory での実行
  2. Windows での実行
  3. このページで説明のために使用する画像
  4. ImageNet で学習済みの MobileNetV2 を用いた画像分類
  5. ImageNet で学習済みの ResNet50 を用いた画像分類
  6. ImageNet で学習済みの Inception-ResNet を用いた画像分類
  7. ImageNet で学習済みの DenseNet 121 を用いた画像分類
  8. ImageNet で学習済みの DenseNet 169 を用いた画像分類
  9. ImageNet で学習済みの NASNet Large を用いた画像分類

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

参考文献:

1. Google Colaboratory での実行

Google Colaboratory のページ:

次のリンクをクリックすると,Google Colaboratoryノートブックが開く. そして,Google アカウントでログインすると,Google Colaboratory のノートブック内のコード等を編集したり再実行したりができる.編集した場合でも,他の人に影響が出たりということはない.そして,編集後のものを,各自の Google ドライブ内に保存することもできる.

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

2. Windows での実行

Python のインストール(Windows 上)

サイト内の関連ページ

関連する外部ページ

Python の公式ページ: https://www.python.org/

TensorFlow,Keras のインストール

Windows での TensorFlowKeras のインストール: 別ページ »で説明

(このページで,Build Tools for Visual Studio 2022,NVIDIA ドライバ, NVIDIA CUDA ツールキットNVIDIA cuDNNのインストールも説明している.)

Graphviz のインストール

Windows での Graphviz のインストール: 別ページ »で説明している.

numpy,matplotlib, seaborn, scikit-learn, pandas, pydot のインストール

  1. Windows で,コマンドプロンプト管理者として実行

    コマンドプロンプトを管理者として実行: 別ページ »で説明

  2. 次のコマンドを実行する.

    python -m pip install -U numpy matplotlib seaborn scikit-learn pandas pydot
    

GraphViz のインストール

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

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

画像ファイル fruits.jpg, home.jpg のダウンロードは, Windows でコマンドプロンプトを管理者として開き 次のコマンドを実行する.

mkdir c:\image
cd c:\image
curl -L https://github.com/opencv/opencv/blob/master/samples/data/fruits.jpg?raw=true -o fruits.jpg
curl -L https://github.com/opencv/opencv/blob/master/samples/data/home.jpg?raw=true -o home.jpg

上のコマンドがうまく実行できないときは, 別ページを参考にダウンロードを行う.

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

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

関連する外部ページhttps://keras.io/ja/applications/

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

  1. モデルの作成

    次の Python プログラムを実行

    from __future__ import absolute_import, division, print_function, unicode_literals
    import tensorflow as tf
    from tensorflow.keras import backend as K 
    K.clear_session()
    import numpy as np
    import tensorflow_datasets as tfds
    from tensorflow.keras.preprocessing import image
    
    %matplotlib inline
    import matplotlib.pyplot as plt
    import warnings
    warnings.filterwarnings('ignore')   # Suppress Matplotlib warnings
    
    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()
    

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

    [image]
  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])
    

    [image]
    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])
    

    [image]

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

関連する外部ページ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()
    

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

    [image]
  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])
    

    [image]
    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])
    

    [image]

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

関連する外部ページ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()
    

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

    [image]
  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])
    

    [image]
    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])
    

    [image]

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

関連する外部ページ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()
    

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

    [image]
  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])
    

    [image]
    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])
    

    [image]

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

関連する外部ページ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()
    

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

    [image]
  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])
    

    [image]
    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])
    

    [image]

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

関連する外部ページ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()
    

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

    [image]
  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])
    

    [image]
    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])
    

    [image]