今回は「畳み込みニューラルネットワークの理論
- KIKAGAKU」を学ぶ。
Table of Contents [Disable]
実習環境
Colaboratory を使わない場合、 OpenCV をインストールしないといけないが、ここに書かれている cv2 だとインストールできなかった。
pip install opencv_python でインストールした。
なお、PIL はすでに pip install pillow でインストール済み。
学習内容
画像処理の実装
- OpenCV
- Pillow
- グレースケール変換
画像処理の基礎
- 画像とは
- 画像の特徴抽出
- 画像を切り取る
- ヒストグラムを取る
- CNN 以前の画像分類のモデル
- フィルタとは
- エッジ検出
畳み込みニューラルネットワーク
- 畳み込み( Convolution )
- プーリング( Pooling )
- 全結合層( Fully Connected Layer )
出力結果
OpenCV では画像が BGR の順に格納されており、Matplotlib では画像が RGB の順で格納されているため青味がかった色になっている。
OpenCV に格納された画像を cvtColor() で RGB の順に変換した結果。
グレースケール変換した結果。Matplotlib が RGB の 3 チャンネルの画像の入力を標準としているため、白黒になっていない。
plt.imshow(img_gray, cmap='gray') で表示した結果。
横方向のエッジ検出を行った結果。
縦方向のエッジ検出を行った結果。
出力ログ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<class 'numpy.ndarray'> | |
(512, 512, 3) | |
uint8 | |
AxesImage(80,52.8;496x369.6) | |
AxesImage(80,52.8;496x369.6) | |
<class 'PIL.PngImagePlugin.PngImageFile'> | |
<class 'numpy.ndarray'> | |
(512, 512, 3) | |
uint8 | |
(512, 512) (512, 512, 3) |
ソースコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
# OpenCV のインポート | |
import cv2 | |
from PIL import Image | |
# 画像の読み込み | |
img = cv2.imread('sample.png') | |
# 変数の型を確認 | |
print(type(img)) | |
# 形の確認 | |
print(img.shape) | |
# データ型の確認 | |
print(img.dtype) | |
print(plt.imshow(img)) | |
plt.show() | |
# BGR -> RGB | |
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) | |
print(plt.imshow(img_rgb)) | |
plt.show() | |
# 画像の読み込み | |
img = Image.open('sample.png') | |
# 型の確認 | |
print(type(img)) | |
# ndarray に変換 | |
img = np.array(img) | |
# 型の確認 | |
print(type(img)) | |
# 形の確認 | |
print(img.shape) | |
# データ型の確認 | |
print(img.dtype) | |
# グレースケール変換 | |
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
# 画像の表示 | |
plt.imshow(img_gray) | |
plt.show() | |
print(img_gray.shape, img.shape) | |
# 画像を表示 | |
plt.imshow(img_gray, cmap='gray') | |
plt.show() | |
# エッジ検出のフィルタの定義(横方向) | |
kernel = np.array([ | |
[-1, 0, 1], | |
[-1, 0, 1], | |
[-1, 0, 1] | |
]) | |
# 畳み込み演算 | |
img_conv = cv2.filter2D(img_gray, -1, kernel) | |
# 結果を可視化 | |
plt.imshow(img_conv, cmap='gray') | |
plt.show() | |
# エッジ検出のフィルタの定義(縦方向) | |
kernel = np.array([ | |
[-1, -1, -1], | |
[0, 0, 0], | |
[1, 1, 1] | |
]) | |
# 畳み込み演算 | |
img_conv = cv2.filter2D(img_gray, -1, kernel) | |
# 結果を可視化 | |
plt.imshow(img_conv, cmap='gray') | |
plt.show() |