Python の kerasパッケージには,次のデータセットを簡単にダウンロードできる機能があります.
この Web ページでは、CIFAR10, CIFAR100, MNIST, Fashion MNIST を説明する. 残りの IMDB, Reuters newswire topics, Boston Housing Price は、別のページで説明します
【このページの目次】
サイト内の関連ページ:
手順の要点: Python, TensorFlow, Keras, Jupyter Qt Console, Ubuntu では,Python の仮想環境(~/keras)
ソフトウエア等の利用条件は,利用者で確認すること.
謝辞:ソフトウエアの作者に感謝します
Python の URL: http://www.python.org/
インストール手順の詳細は: 別ページで説明している.
コマンドプロンプトを管理者として実行し,次のコマンドを実行.
python -m pip install -U pip setuptools python -m pip install -U jupyterlab jupyter jupyter-console jupytext spyder
システム Python を使用(インストール操作は不要)
次のコマンドを実行.
sudo apt -yV install python3-dev python3-pip python3-setuptools sudo apt -yV install jupyter-qtconsole jupyter-notebook python3-jupyter-client python3-jupyter-console python3-spyder spyder3
sudo apt -yV install git
※ GPU とは,グラフィックス・プロセッシング・ユニットの略で、コンピュータグラフィックス関連の機能,乗算や加算の並列処理の機能などがある.
ダウンロードページ
詳しくは,次のように別ページで説明している.インストール手順が分からなくなったり,不具合があったときは,次に書いている別ページの説明を確認すること.
※ CUDA とは,NVIDIA社が提供している GPU 用のプラットフォームである.
ダウンロードページ
TensorFlow 2.1 以上の GPU 版での,NVIDIA CUDA のバージョンは 10.1 が指定されている. TensorFlow 1.15の GPU 版での,NVIDIA CUDA のバージョンは 10.0 が指定されている. 指定されているバージョンより高いものは使わない. その根拠は次のページ. URL: https://www.tensorflow.org/install/source#common_installation_problems
詳細説明
ダウンロードページ
ダウンロードして展開(解凍)したら,パスを通しておくこと.
URL: https://www.tensorflow.org/install/source#common_installation_problems
詳細説明
python -m pip install -U pip setuptools
※ トラブルの可能性を減らすために,最初にアンインストールを行っておく.
※ 「jupyterlab jupyter jupyter-console jupytext spyder matplotlib」は,TensorFlow, Keras ではないが,のちほど,Python のプログラム実行に使いたいためインストールしている.
python -m pip uninstall -y tensorflow tensorflow-cpu tensorflow-gpu tensorflow_datasets keras python -m pip install -U tensorflow-gpu tensorflow_datasets keras jupyterlab jupyter jupyter-console jupytext spyder matplotlib
※ Windows でのTensorFlow, Keras のインストールについて,詳しくは,別ページで説明している. インストール手順が分からなくなったり,不具合があったときは,この別ページの説明を確認すること.
※ バージョン番号が表示されれば OK.下の図とは違うバージョンが表示されることがある.
python -c "import tensorflow as tf; print( tf.__version__ )"
TensorFlow が GPU を認識できているかの確認は,端末で,次を実行して行う.
python -c "from tensorflow.python.client import device_lib; print(device_lib.list_local_devices())"
Ubuntu では,運用を簡単にする(apt とpip の混在を避けたいなど)ために,venv を用いて,隔離された Python 仮想環境を作ることにする.
次の手順により,システム Python とは別に,pyenv を用いて Pytnon 3.6 をインストールする.(システム Python の設定は変えたくないため).
図などの入った詳しい説明は別ページ.
sudo apt -yV install --no-install-recommends make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
cd /tmp
curl https://pyenv.run | bash
echo 'export PYENV_ROOT="${HOME}/.pyenv"' >> ~/.bashrc
echo 'if [ -d "${PYENV_ROOT}" ]; then' >> ~/.bashrc
echo ' export PATH=${PYENV_ROOT}/bin:$PATH' >> ~/.bashrc
echo ' eval "$(pyenv init -)"' >> ~/.bashrc
echo ' eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
echo 'fi' >> ~/.bashrc
exec $SHELL -l
pyenv install 3.6.10
下の例では,隔離された Python 仮想環境のためのディレクトリを ~/kerasに作成している.
pyenv shell 3.6.10 python -m venv ~/keras
source ~/keras/bin/activate
venv を使い,孤立した Python 仮想環境を使っているときは,Windows でも Ubuntu でも同じで,「python -m pip install ...」. いま,venv を使っているかどうかは,プロンプトの「(venv)」で分かる.
※ 「jupyterlab jupyter jupyter-console jupytext spyder matplotlib」は,TensorFlow, Keras ではないが,のちほど,Python のプログラム実行に使いたいためインストールしている.
python -m pip install -U pip setuptools python -m pip uninstall -y tensorflow tensorflow-cpu tensorflow-gpu tensorflow_datasets keras python -m pip install -U tensorflow-gpu tensorflow_datasets keras jupyterlab jupyter jupyter-console jupytext spyder
※ バージョン番号が表示されれば OK.下の図とは違うバージョンが表示されることがある.
python -c "import tensorflow as tf; print( tf.__version__ )"
TensorFlow が GPU を認識できているかの確認は,端末で,次を実行して行う.
python -c "from tensorflow.python.client import device_lib; print(device_lib.list_local_devices())"
Python プログラムを実行しながら結果を確認.結果は,画像などでプロットされる場合がある.
Ubuntu では, venv の隔離された Python 仮想環境を使うことにしている.有効化する
※ Windows では下のコマンドは実行しない
source ~/keras/bin/activate
jupyter qtconsole
Python プログラムを動かして,結果をビジュアルに見たい.
そのために,開発環境や Python コンソール(Jupyter Qt Console,spyder,PyCharm,PyScripter など)が便利.
※ 「jupyter qtconsole」を入れたのに,jupyter qtconsole が起動しない という場合には,次の操作で,インストールを行ってから,もう一度試してみる.
python -m pip install -U jupyterlab jupyter jupyter-console jupytext spyder
ここから先は,Jupyter Qt Console の画面で説明する.
import numpy as np a = np.array([8,5,4,1,3]) print( a.shape ) print( a.ndim ) print(a) x = np.array([[1,2,3,4], [10,20,30,40], [100,200,300,400]]) print( x.shape ) print( x.ndim ) print(x)
データセットとは、「データの集まり」のこと.
keras に付属のデータセットを取得するときのオプションについては, https://keras.io/ja/datasets/(日本語版), https://keras.io/datasets/(英語版) に説明されている.
ここでの、オブジェクトの名前付けのルール
from keras.datasets import cifar10 (X_train, y_train), (X_test, y_test) = cifar10.load_data()
配列のサイズ確認
print( X_train.shape ) print( y_train.shape ) print( X_test.shape ) print( y_test.shape )
X_train の先頭要素の表示(確認のため)
import matplotlib.pyplot as plt plt.imshow( X_train[0] )
from keras.datasets import cifar100 (X_train, y_train), (X_test, y_test) = cifar100.load_data(label_mode='fine')
配列のサイズ確認
print( X_train.shape ) print( y_train.shape ) print( X_test.shape ) print( y_test.shape )
X_train の先頭要素の表示(確認のため)
import matplotlib.pyplot as plt plt.imshow( X_train[0] )
from keras.datasets import mnist (X_train, y_train), (X_test, y_test) = mnist.load_data()
配列のサイズ確認
print( X_train.shape ) print( y_train.shape ) print( X_test.shape ) print( y_test.shape )
X_train の先頭要素の表示(確認のため)
import matplotlib.pyplot as plt plt.imshow( X_train[0], cmap='gray' )
from keras.datasets import fashion_mnist (X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()
配列のサイズ確認
print( X_train.shape ) print( y_train.shape ) print( X_test.shape ) print( y_test.shape )
X_train の先頭要素の表示(確認のため)
import matplotlib.pyplot as plt plt.imshow( X_train[0], cmap='gray' )
本サイトは金子邦彦研究室のWebページです.サイトマップは,サイトマップのページをご覧下さい. 本サイト内の検索は,サイト内検索のページをご利用下さい.
問い合わせ先: 金子邦彦(かねこ くにひこ) ![]()