#はじめに
KaggleのCoursesを使って勉強している筆者が、Courseで提供されるコードを解説をしながら学んだことをメモしました。
本記事では以下のコースを解説します。
Computer Vision - Convolution and ReLU
#本質的な理解
以下の説明がわかりやすく、ConvolutionとReLUのエッセンスがまとまっている。
# Sympy is a python library for symbolic mathematics. It has a nice
# pretty printer for matrices, which is all we'll use it for.
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import sympy
sympy.init_printing()
from IPython.display import display
image = np.array([
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 1, 1, 1],
[0, 1, 0, 0, 0, 0],
])
kernel = np.array([
[1, -1],
[1, -1],
])
display(sympy.Matrix(image))
plt.imshow(image,cmap="jet")
display(sympy.Matrix(kernel))
# Reformat for Tensorflow
image = tf.cast(image, dtype=tf.float32)
image = tf.reshape(image, [1, *image.shape, 1])
kernel = tf.reshape(kernel, [*kernel.shape, 1, 1])
kernel = tf.cast(kernel, dtype=tf.float32)
まずはサンプル画像とカーネルを用意する。サンプル画像には縦と横の線が入っている。
各ピクセルに対して活性化関数(ReLU)を掛ける。ネガティブなセルはすべて0となり、ポジティブなセルはy=xの関係で計算される。
image_filter = tf.nn.conv2d(
input=image,
filters=kernel,
strides=1,
padding='VALID',
)
image_detect = tf.nn.relu(image_filter)
# The first matrix is the image after convolution, and the second is
# the image after ReLU.
display(sympy.Matrix(tf.squeeze(image_filter).numpy()))
display(sympy.Matrix(tf.squeeze(image_detect).numpy()))
以上のように、カーネルとReLU関数の処理によって縦の線だけが抽出された。カーネルを変えることで、画像内のいろいろな特徴を抽出できる。