728x90
반응형
SMALL
컨벌루션 (Convolution)
수학 (특히 기능 분석)에서 컨벌루션은 함수의 모양이 다른 하나에 의해 수정되는 방식을 나타낸다. 컨볼루션이라는 용어는 결과 함수와 계산 프로세스를 모두 나타낸다. 그것은 하나가 반전되고 이동된 후 두 함수의 곱의 적분으로 정의된다. 적분은 모든 shift 값에 대해 평가되어 컨볼루션 함수를 생성한다.
from tensorflow.keras.datasets import mnist
import matplotlib.pyplot as plt
# mnist data download
(x_train, y_train), (x_test, y_test) = mnist.load_data()
plt.imshow(x_train[0], cmap='gray') # 1번째 데이터
plt.show()
필터 (커널) 정의
import numpy as np
horizontal_filter = np.array([[1., 1., 1.],
[0., 0., 0.],
[-1., -1., -1.]])
vertical_filter = np.array([[1., 0., -1.],
[1., 0., -1.],
[1., 0., -1.]])
sharpen_filter = np.array([[0., -1., 0.],
[-1., 5., -1.],
[0., -1., 0.]])
blur_filter = np.array([[0.11, 0.11, 0.11],
[0.11, 0.11, 0.11],
[0.11, 0.11, 0.11]])
edge_1_filter = np.array([[1., 0., -1.],
[0., 0., 0.],
[-1., 0., 1.]])
edge_2_filter = np.array([[0., -1., 0.],
[-1., 4., -1.],
[0., -1., 0.]])
edge_3_filter = np.array([[-1., -1., -1.],
[-1., 8., -1.],
[-1., -1., -1.]])
컨벌루션 연산 함수 정의
def conv2d_simple(input_image, filter, filter_size):
original_image_size = input_image.shape[0]
conv_output_size = int((original_image_size - 3)/1 + 1)
filtered_image = np.zeros((conv_output_size, conv_output_size))
for i in range(conv_output_size):
for j in range(conv_output_size):
# 컨볼루션 연산
conv_result = input_image[i:(i + filter_size), j:(j + filter_size)] * filter
conv_sum = np.sum(conv_result)
if(conv_sum > 255):
conv_sum = 255
filtered_image[i, j] = conv_sum
return filtered_image
수직 / 수평 필터
vertical_filtered_image = conv2d_simple(x_train[0], vertical_filter, 3)
horizontal_filtered_image = conv2d_simple(x_train[0], horizontal_filter, 3)
plt.figure(figsize=(6,4))
plt.subplot(1, 2, 1)
plt.title('vertical')
plt.imshow(vertical_filtered_image, cmap='gray')
plt.subplot(1, 2, 2)
plt.title('horizontal')
plt.imshow(horizontal_filtered_image, cmap='gray')
plt.tight_layout()
plt.show()
blur / sharpen 필터
blur_filtered_image = conv2d_simple(x_train[0], blur_filter, 3)
sharpen_filtered_image = conv2d_simple(x_train[0], sharpen_filter, 3)
plt.figure(figsize=(6,4))
plt.subplot(1, 2, 1)
plt.title('blur')
plt.imshow(blur_filtered_image,cmap='gray')
plt.subplot(1, 2, 2)
plt.title('sharpen')
plt.imshow(sharpen_filtered_image,cmap='gray')
plt.tight_layout()
plt.show()
edge 필터
edge_1_filtered_image = conv2d_simple(x_train[0], edge_1_filter, 3)
edge_2_filtered_image = conv2d_simple(x_train[0], edge_2_filter, 3)
edge_3_filtered_image = conv2d_simple(x_train[0], edge_3_filter, 3)
plt.figure(figsize=(8,8))
plt.subplot(1, 3, 1)
plt.title('edge1')
plt.imshow(edge_1_filtered_image,cmap='gray')
plt.subplot(1, 3, 2)
plt.title('edge2')
plt.imshow(edge_2_filtered_image,cmap='gray')
plt.subplot(1, 3, 3)
plt.title('edge3')
plt.imshow(edge_3_filtered_image,cmap='gray')
plt.tight_layout()
plt.show()
https://en.wikipedia.org/wiki/Convolution
728x90
반응형
LIST
'Visual Intelligence > Image Deep Learning' 카테고리의 다른 글
[시각 지능] CNN Basic Architecture (0) | 2022.08.06 |
---|---|
[시각 지능] CNN (Convolutional Neural Network) (0) | 2022.08.06 |
[시각 지능] Fashion MNIST (0) | 2022.07.31 |
[시각 지능] MNIST (0) | 2022.07.30 |
[시각 지능] 컴퓨터 비전 (Computer Vision) (0) | 2022.07.30 |