ในบทความนี้ เราจะเรียนรู้เกี่ยวกับ Convolution ใน Python 3.x หรือเวอร์ชันก่อนหน้า บทความนี้อยู่ภายใต้ Neural Networks และ Feature Extraction
IDE ที่แนะนำ − Jupyter Notebook
ข้อกำหนดเบื้องต้น − ติดตั้ง Numpy, Matplotlib
การติดตั้ง
>>> pip install numpy >>> pip install matplotlib
Convolution คืออะไร?
Convolution เป็นประเภทของการดำเนินการที่สามารถทำบนภาพเพื่อแยกฟีเจอร์ (features) ออกมา โดยการใช้คอนเทนเนอร์ (container) ขนาดเล็กที่เรียกว่า kernel/coordinate container เหมือนหน้าต่างเลื่อน (sliding window) ไปบนภาพ ขึ้นอยู่กับค่าในคอนเทนเนอร์ Convolution เราสามารถเลือกฟีเจอร์/รูปแบบเฉพาะจากภาพได้ ที่นี่เราจะเรียนรู้เกี่ยวกับการตรวจจับปลายทางแนวนอนและแนวตั้งในภาพโดยใช้คอนเทนเนอร์พิกัดที่เหมาะสม
ตอนนี้มาดูการนำไปใช้งานจริง
ตัวอย่าง
import numpy as np
from matplotlib import pyplot
# เริ่มต้นภาพ
img1 = np.array([np.array([100, 100]), np.array([80, 80])])
img2 = np.array([np.array([100, 100]), np.array([50, 0])])
img3 = np.array([np.array([100, 50]), np.array([100, 0])])
coordinates_horizontal = np.array([np.array([3, 3]), np.array([-3, -3])])
print(coordinates_horizontal, 'is a coordinates for detecting horizontal end points')
coordinates_vertical = np.array([np.array([3, -3]), np.array([3, -3])])
print(coordinates_vertical, 'is a coordinates for detecting vertical end points')
# จะเป็นการคูณองค์ประกอบ (element-wise) ตามด้วยการบวก
def apply_coordinates(img, coordinates):
return np.sum(np.multiply(img, coordinates))
# Visualizing img1
pyplot.imshow(img1)
pyplot.axis('off')
pyplot.title('sample 1')
pyplot.show()
# ตรวจสอบฟีเจอร์แนวนอนและแนวตั้งในภาพ 1
print('Horizontal end points features score:', apply_coordinates(img1, coordinates_horizontal))
print('Vertical end points features score:', apply_coordinates(img1, coordinates_vertical))
# Visualizing img2
pyplot.imshow(img2)
pyplot.axis('off')
pyplot.title('sample 2')
pyplot.show()
# ตรวจสอบฟีเจอร์แนวนอนและแนวตั้งในภาพ 2
print('Horizontal end points features score:', apply_coordinates(img2, coordinates_horizontal))
print('Vertical end points features score:', apply_coordinates(img2, coordinates_vertical))
# Visualizing img3
pyplot.imshow(img3)
pyplot.axis('off')
pyplot.title('sample 3')
pyplot.show()
# ตรวจสอบฟีเจอร์แนวนอนและแนวตั้งในภาพ 3
print('Horizontal end points features score:', apply_coordinates(img3, coordinates_horizontal))
print('Vertical end points features score:', apply_coordinates(img3, coordinates_vertical))
ผลลัพธ์

สรุป
ในบทความนี้ เราได้เรียนรู้เกี่ยวกับการแนะนำ Convolution ด้วย Python 3.x หรือเวอร์ชันก่อนหน้า และการนำไปใช้งาน