【AI】動かしてみよう

AI

やること

  • 以下のようなボールペンを、AIで認識できるか?を調べます。
  • 学習済のデータで推論を行います。

以下のように、何の写真かを判定します。

 fountain_pen(万年筆): 51%
 ballpoint(ボールペン): 47%
 lighter(ライター): 0%

準備

  • Ubuntu 19.04をインストールしたものを用意します。
  • VMWearやVirtualBoxで作成したもので構いません。
  • 以下のコマンドで環境を準備します。
# パッケージ情報を更新
sudo apt-get update
# 各種パッケージのインストール
sudo apt-get -y install python3-pip python3-dev python-virtualenv ipython3
# viartualenv環境を構築
virtualenv --system-site-packages -p python3 ~/tensorflow
# virtualenv環境を有効化
source ~/tensorflow/bin/activate
# pipを更新
easy_install -U pip
# TensorFlow, Keras, h5py, Pillowをインストール
pip3 install --upgrade tensorflow keras h5py pillow

実行

test.pyの作成

以下の内容で、test.pyを作成します。

#必要なモジュールをインポート
 from keras.applications.resnet50 import ResNet50
 from keras.preprocessing import image
 from keras.applications.resnet50 import preprocess_input, decode_predictions
 import numpy as np

# ResNet50のモデルを使用する.重みはImageNet用のものを使用
 model = ResNet50(weights='imagenet')

# 画像をロード
 img_path = 'pen.jpg'
 img = image.load_img(img_path, target_size=(224, 224))

#前処理
 x = image.img_to_array(img)
 x = np.expand_dims(x, axis=0)
 x = preprocess_input(x)

#推論する
 preds = model.predict(x)

#結果を表示
 for name, description, score in decode_predictions(preds, top=3)[0]:
     print(description + ": " + str(int(score * 100)) + "%")

実行

以下のコマンドを実行します。

$ ipython3 test.py

実行結果は、以下の通りです。

(tensorflow) student@myHost:~$ ipython3 test.py
2020-08-19 05:33:14.315335: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory
2020-08-19 05:33:14.315847: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2020-08-19 05:33:16.401805: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2020-08-19 05:33:16.402211: W tensorflow/stream_executor/cuda/cuda_driver.cc:312] failed call to cuInit: UNKNOWN ERROR (303)
2020-08-19 05:33:16.402626: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (myHost): /proc/driver/nvidia/version does not exist
2020-08-19 05:33:16.424235: I tensorflow/core/platform/profile_utils/cpu_utils.cc:104] CPU Frequency: 3192745000 Hz
2020-08-19 05:33:16.424945: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x43b9c10 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-08-19 05:33:16.425332: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
fountain_pen: 51%
ballpoint: 47%
lighter: 0%

万年筆かボールペンと言う判定ですね。ボールペンですが、万年筆にも見えます。

その他

以下の画像を試してみた。

どうやら、カテゴリが決まって、その中で優先順位を決めているようだ。虫に見えなくは無いが。

 centipede(ムカデ): 29%
 isopod(ワラジムシ): 18%
 trilobite(三葉虫): 14%

スポンサーリンク
AI
Engineerをフォローする
レンサバ