CIFAR 10 データセット(
学習データとして用いる画像データの増強を行う.増量では,TensorFlow の機能を使い,画像を縦横にずらす,コントラストを変化させることを行う.
前提として,このページの手順では,増量後の画像データがメモリに入り切る分量であるとしている.
TensorFlow データセットのCIFAR 10 データセットを使用する.
CNN としては,次のものを使用する.
この資料の URL: https://www.kkaneko.jp/dblab/imclassify/augment.html
サイト内の関連ページ
参考Webページ:
このページの内容は,Google Colab でも実行できる.
そのために,次の URL で,Google Colab のノートブックを準備している.
次のリンクをクリックすると,Google Colab のノートブックが開く. そして,Google アカウントでログインすると,Google Colab のノートブック内のコードを実行することができる.Google Colab のノートブックは書き換えて使うこともできる.このとき,書き換え後のものを,各自の Google ドライブ内に保存することもできる.
https://colab.research.google.com/drive/1osVlD5wpv-pFMrIt2WgXWbTB8t5fKu9f?usp=sharing
Google Colab を使うか,パソコンを使う.それぞれの場合の前準備を説明する.
https://colab.research.google.com
Google Colab はオンラインの Python 開発環境. 使用するには Google アカウントが必要
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
GPU とは,グラフィックス・プロセッシング・ユニットの略で、コンピュータグラフィックス関連の機能,乗算や加算の並列処理の機能などがある.
NVIDIA CUDA は,NVIDIA社が提供している GPU 用のプラットフォームである.
インストール手順の説明
関連 Web ページ
インストール手順の説明
端末で,次のコマンドを実行.
sudo apt -y install python3-dev python3-pip python3-setuptools python3-venv sudo pip3 uninstall ptyprocess sniffio terminado tornado jupyterlab jupyter jupyter-console jupytext nteract_on_jupyter spyder sudo apt -y install jupyter jupyter-qtconsole spyder3 sudo apt -y install python3-ptyprocess python3-sniffio python3-terminado python3-tornado sudo pip3 install -U jupyterlab nteract_on_jupyter sudo pip3 uninstall -y tensorflow tensorflow-cpu tensorflow-gpu tensorflow_datasets tensorflow-hub keras sudo pip3 uninstall six wheel astunparse tensorflow-estimator numpy keras-preprocessing absl-py wrapt gast flatbuffers grpcio opt-einsum protobuf termcolor typing-extensions google-pasta h5py tensorboard-plugin-wit markdown werkzeug requests-oauthlib rsa cachetools google-auth google-auth-oauthlib tensorboard tensorflow sudo apt -y install python3-six python3-wheel python3-numpy python3-grpcio python3-protobuf python3-termcolor python3-typing-extensions python3-h5py python3-markdown python3-werkzeug python3-requests-oauthlib python3-rsa python3-cachetools python3-google-auth sudo apt -y install python3-numpy python3-pil python3-pydot python3-matplotlib python3-keras python3-keras-applications python3-keras-preprocessing sudo pip3 install -U tensorflow tf-models-official tensorflow_datasets tensorflow-hub keras keras-tuner keras-visualizer opencv-python sudo pip3 install git+https://github.com/tensorflow/docs sudo pip3 install git+https://github.com/tensorflow/examples.git
詳細は: 別ページで説明している.
Ubuntu では,システムの Python を使うことができる(その場合,Python のインストールは行わない)
Python プログラムを動かすために, pythonやpython3などのコマンドを使う. あるいは, 開発環境や Python コンソール(Jupyter Qt Console,spyder,PyCharm,PyScripter など)の利用も便利である.
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()
print(tf.__version__)
cifar10, cifar10_info = tfds.load('cifar10', with_info = True, shuffle_files=True, as_supervised=True, batch_size = -1)
MatplotLib を用いて,0 番目の画像を表示する
import matplotlib.pyplot as plt
%matplotlib inline
import warnings
warnings.filterwarnings('ignore') # Suppress Matplotlib warnings
NUM = 0
# NUM 番目の画像を表示
plt.imshow(cifar10['train'][0][NUM])
MatplotLib を用いて,複数の画像を並べて表示する.
def plot25(ds, start):
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
image, label = ds[0][i + start], ds[1][i + start]
plt.imshow(image)
plt.xlabel(label.numpy())
plt.show()
plot25(cifar10['train'], 0)
print(cifar10_info) print(cifar10_info.features["label"].num_classes) print(cifar10_info.features["label"].names)
cifar10['train']: サイズ 32 かける 32 の 50000枚のカラー画像,50000枚のカラー画像それぞれのラベル(0 から 9 のどれか)
cifar10['test']: サイズ 32 かける 32 の 10000枚のカラー画像,10000枚のカラー画像それぞれのラベル(0 から 9 のどれか)
print(cifar10['train'][0].shape) print(cifar10['train'][1].shape) print(cifar10['test'][0].shape) print(cifar10['test'][1].shape)
ds_train, ds_test = cifar10['train'], cifar10['test']
値は,もともと int で 0 から 255 の範囲であるのを, float32 で 0 から 1 の範囲になるように前処理を行う.
ds_train = (ds_train[0].numpy().astype("float32") / 255., ds_train[1])
ds_test = (ds_test[0].numpy().astype("float32") / 255., ds_test[1])
画像データの増強を行う. 画像データは,ds_train[0] にある. TensorFlow の機能を使い,画像を縦横にずらす,コントラストを変化させることを行う. ds_train[1] にはラベルのデータが入っているとする.
結果の入る augmented_ds_train はタップル.うち1つ目は,numpy の配列.2つ目は,要素が tf.Tensor の配列.
INPUT_SHAPE = [32, 32, 3]
CROP_SIZE = 3
# 画像を縦横にずらす.コントラストを変化させる.
def augment(image, seed):
image = tf.image.resize_with_crop_or_pad(image, INPUT_SHAPE[0] + (2 * CROP_SIZE), INPUT_SHAPE[1] + (2 * CROP_SIZE))
# Random crop back to the original size
image = tf.image.stateless_random_crop(image, size=INPUT_SHAPE, seed=seed)
# Make a new seed
new_seed = tf.random.experimental.stateless_split(seed, num=1)[0, :]
image = tf.image.stateless_random_contrast(
image, lower=0.8, upper=0.99, seed=new_seed)
image = tf.clip_by_value(image, 0, 1)
return image
def augment_images(images, seed):
new_images = np.zeros(images.shape)
for i, image in enumerate(images):
new_images[i] = augment(image, (i + seed * 100, 0))
return new_images
augmented_ds_train = (augment_images(ds_train[0], 123), ds_train[1])
plot25(augmented_ds_train, 0)
print(augmented_ds_train[0].shape)
ここで増やした画像データは,あとで使用する
def increase_image_data(ds_train, t):
# t 倍に増やす
x1 = augment_images(ds_train[0], 1)
y1 = ds_train[1]
if t > 1:
for i in range(t - 1):
x2 = augment_images(ds_train[0], i + 1)
# 画像は nparray, ラベルは tf.Tensorを要素とする配列にする
x1 = np.concatenate([x1, x2])
y1 = tf.concat([y1, ds_train[1]], axis = 0)
result = (x1, y1)
return result
augmented_ds_train = increase_image_data(ds_train, 5)
plot25(augmented_ds_train, 0)
print(augmented_ds_train[0].shape) print(augmented_ds_train[1].shape) print(ds_test[0].shape) print(ds_test[1].shape)
最適化器(オプティマイザ) と損失関数とメトリクスを設定する.
NUM_CLASSES = 10
INPUT_SHAPE = [32, 32, 3]
m1 = tf.keras.applications.mobilenet.MobileNet(input_shape=INPUT_SHAPE, weights=None, classes=NUM_CLASSES)
m1.summary()
m1.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
loss='sparse_categorical_crossentropy',
metrics=['sparse_categorical_crossentropy', 'accuracy']
)
Keras のモデルのビジュアライズについては: https://keras.io/ja/visualization/
ここでの表示で,エラーメッセージが出る場合でも,モデル自体は問題なくできていると考えられる.続行する.
from tensorflow.keras.utils import plot_model import pydot plot_model(m1)
学習(訓練)は fit メソッドにより行う. 学習データを投入する.
EPOCHS = 20
history = m1.fit(augmented_ds_train[0], augmented_ds_train[1],
epochs=EPOCHS,
validation_data=(ds_test[0], ds_test[1]),
verbose=1)
ds_test を分類してみる.
print(m1.predict(ds_test[0]))
それぞれの数値の中で、一番大きいものはどれか?
m1.predict(ds_test[0]).argmax(axis=1)
ds_test 内にある正解のラベル(クラス名)を表示する(上の結果と比べるため)
print(ds_test[1])
過学習や学習不足について確認.
import pandas as pd hist = pd.DataFrame(history.history) hist['epoch'] = history.epoch print(hist)
過学習や学習不足について確認.
https://www.tensorflow.org/tutorials/keras/overfit_and_underfit?hl=ja で公開されているプログラムを使用
import matplotlib.pyplot as plt
%matplotlib inline
import warnings
warnings.filterwarnings('ignore') # Suppress Matplotlib warnings
def plot_history(histories, key='binary_crossentropy'):
plt.figure(figsize=(16,10))
for name, history in histories:
val = plt.plot(history.epoch, history.history['val_'+key],
'--', label=name.title()+' Val')
plt.plot(history.epoch, history.history[key], color=val[0].get_color(),
label=name.title()+' Train')
plt.xlabel('Epochs')
plt.ylabel(key.replace('_',' ').title())
plt.legend()
plt.xlim([0,max(history.epoch)])
plot_history([('history', history)], key='sparse_categorical_crossentropy')
plot_history([('history', history)], key='accuracy')
データの増強を行わない場合の結果(学習曲線など)は, 別ページで説明している.