0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Kaggle Coursesの解説(Computer Vision - Convolution and ReLU)

Posted at

#はじめに
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)

まずはサンプル画像とカーネルを用意する。サンプル画像には縦と横の線が入っている。

サンプル画像
image.png
image.png
サンプルカーネル
image.png

画像に対してカーネルを移動しながら掛けていく。
image.png

各ピクセルに対して活性化関数(ReLU)を掛ける。ネガティブなセルはすべて0となり、ポジティブなセルはy=xの関係で計算される。
image.png

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()))

カーネルを掛けたあと
image.png

ReLU関数を掛けたあと
image.png

以上のように、カーネルとReLU関数の処理によって縦の線だけが抽出された。カーネルを変えることで、画像内のいろいろな特徴を抽出できる。

参考Tensorの変形

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?