PCA တွက်ချက်မှုက ထွက်ပေါ်လာတဲ့ Principal Component Eigenvectors တွေကို မူလ 2D ပုံအရွယ်အစားအတိုင်း ပြန်လည်ပုံဖော်ကြည့်ခြင်း ("Eigen-bears") ဟာ၊ Computer Vision နယ်ပယ်ရဲ့ ဂန္ထဝင်နည်းပညာဖြစ်တဲ့ Eigenfaces (Turk & Pentland, 1991) ရဲ့ အခြေခံသဘောတရား ဖြစ်ပြီး၊ Artificial Intelligence: A Modern Approach (4th Edition) စာအုပ် Chapter 27 နဲ့ Chapter 22 တို့ကြားရှိ အရေးကြီးသော ပေါင်းကူးတံတား ဖြစ်ပါတယ်။
၁။ သီအိုရီ ချိတ်ဆက်မှု (Chapter 27 နှင့် Chapter 22)
(က) Chapter 27 (Computer Vision) — Holistic Appearance-Based Representation
- Section 27.3 (Simple Image Features) & Section 27.5 (Object Recognition) 3D အရာဝတ္ထုများကို 2D ဓာတ်ပုံများမှတစ်ဆင့် မှတ်မိစေရန် ကြိုးပမ်းရာတွင် အရာဝတ္ထု၏ ဂျီသြမေတြီပုံစံ (3D CAD Model) ကို တည်ဆောက်မည့်အစား ဓာတ်ပုံ Pixel တန်ဖိုးများ၏ စာရင်းအင်းဆိုင်ရာ ပြောင်းလဲမှုကို တိုက်ရိုက်အသုံးပြုသည့် နည်းလမ်းကို Appearance-Based Recognition ဟု ခေါ်ပါသည်။
- Linear Combination of Basis Images PCA သည် ဒေတာအားလုံး၏ ပျမ်းမျှပုံရိပ် (Mean Bear, $\mu$) နှင့် အပြောင်းအလဲ အများဆုံး အခြေခံပုံရိပ်များ (Eigen-bears, $v_1, v_2, \dots, v_k$) ကို ရှာဖွေပေးပါသည်။ မည်သည့်ထောင့်မှ ရိုက်ထားသော ဝက်ဝံရုပ်ပုံ $x$ ကိုမဆို အောက်ပါ Linear ညီမျှခြင်းဖြင့် ပြန်လည်တည်ဆောက်နိုင်ပါသည်—$$x \approx \mu + \sum_{i=1}^{k} z_i v_i$$(ဤနေရာတွင် $z_i$ သည် ဂရပ်ဖ်ဆွဲရာတွင် ကျွန်ုပ်တို့ အသုံးပြုခဲ့သော Principal Component Coordinates
T_pcaဖြစ်ပါသည်။)
(ခ) Chapter 22 (Deep Learning) — Linear Autoencoders နှင့် CNN Filters နှိုင်းယှဉ်ချက်
- Section 22.7 (Linear Autoencoders vs. PCA): စာအုပ်တွင် တစ်လွှာတည်းပါသော Linear Autoencoder တစ်ခုကို Mean Squared Error (MSE) အနည်းဆုံးဖြစ်အောင် လေ့ကျင့်ပေးပါက ၎င်း၏ Decoder Weights များသည် PCA ၏ ထိပ်တန်း Eigenvectors (
pca.components_) များ ဖန်တီးထားသော Subspace နှင့် ထပ်တူကျကြောင်း ရှင်းပြထားပါသည်။ ထို့ကြောင့် Eigen-bears အရေအတွက် $k$ ကို တိုး၍ ပုံပြန်ဖော်ကြည့်ခြင်းသည် Autoencoder ၏ Bottleneck အရွယ်အစားကို တိုးမြှင့်စမ်းသပ်ခြင်းနှင့် အတူတူပင် ဖြစ်ပါသည်။ - Section 22.3 (Convolutional Networks — Global vs. Local Features): Eigen-bears များကို ကြည့်ရှုခြင်းဖြင့် PCA ၏ အားနည်းချက်နှင့် CNN များ အဘယ်ကြောင့် လိုအပ်လာရသနည်း ဆိုသည်ကို ရှင်းလင်းစွာ မြင်တွေ့နိုင်ပါသည်—
- PCA (Global / Dense Basis): Eigen-bear တစ်ပုံချင်းစီသည် ဝက်ဝံရုပ် တစ်ကောင်လုံး၏ ပုံသဏ္ဌာန် (Whole-object template) ကို ဖမ်းယူထားပါသည်။ ထို့ကြောင့် ဝက်ဝံရုပ်သည် ပုံဘောင်ထဲတွင် ဘယ်/ညာ အနည်းငယ် ရွေ့သွားရုံမျှဖြင့် Pixel နေရာများ လွဲချော်သွားပြီး PCA ၏ တွက်ချက်မှု ပျက်ယွင်းသွားနိုင်ပါသည်။
- CNN (Local / Sparse Filters): Convolutional Neural Network များကမူ ပုံတစ်ပုံလုံးစာ Template ကြီးများ အစား $3 \times 3$ သို့မဟုတ် $5 \times 5$ အရွယ်အစားရှိ အနားသတ်မျဉ်း (Edges), အမွေးအမျှင် (Textures) စသည့် Local Filters များကိုသာ သင်ယူသဖြင့် ပုံထဲတွင် အရာဝတ္ထု နေရာရွေ့သွားသော်လည်း (Translation Equivariance) တိကျစွာ မှတ်မိနိုင်ပါသည်။
၂။ "Eigen-bears" ထုတ်ယူခြင်းနှင့် Image Reconstruction Python ကုဒ်
အောက်ပါကုဒ်သည်
Datasets ဖိုလ်ဒါထဲရှိ ပုံ ၇၂ ပုံ မှ Mean Bear၊ ထိပ်တန်း Eigen-bears ၇ ပုံ နှင့် Component အရေအတွက် k အလိုက် မူလဝက်ဝံရုပ်ပုံကို ပြန်လည်တည်ဆောက်ပုံ (Image Reconstruction) တို့ကို ထုတ်ပေးပါမည်။Python
import os
import re
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from sklearn import decomposition
plt.style.use('ggplot')
# ==============================================================================
# အပိုင်း (၁) - ALOI/32 ပုံ ၇၂ ပုံကို ဖတ်ယူခြင်းနှင့် မူလ 2D အရွယ်အစား မှတ်သားခြင်း
# AIMA Ch. 27.3 (Simple Image Features: Brightness Intensity Matrix)
# ==============================================================================
dir_32 = os.path.join('Module4', 'Datasets', 'ALOI', '32')
def extract_angle(filename):
match = re.search(r'(\d+)\.png$', filename, re.IGNORECASE)
return int(match.group(1)) if match else 0
png_files = sorted(
[f for f in os.listdir(dir_32) if f.lower().endswith('.png')],
key=extract_angle
)
samples = []
angles = []
img_shape = None # Downsample လုပ်ပြီးသား 2D ပုံအရွယ်အစား (97, 72) ကို သိမ်းဆည်းရန်
for fname in png_files:
img_path = os.path.join(dir_32, fname)
# Grayscale ပုံ (194 x 144) အဖြစ်ဖတ်ခြင်း
img = np.array(Image.open(img_path).convert('L'), dtype=np.float64)
# Downsampling [::2, ::2] -> (97 x 72) နှင့် [0.0, 1.0] Normalization
img_small = img[::2, ::2] / 255.0
if img_shape is None:
img_shape = img_small.shape # (97, 72)
samples.append(img_small.reshape(-1)) # 1D Vector (6,984 features) ပြောင်းခြင်း
angles.append(extract_angle(fname))
X = np.array(samples) # Shape: (72, 6984)
angles = np.array(angles)
print(f"Data Matrix Shape: {X.shape} | Single Image 2D Shape: {img_shape}")
# ==============================================================================
# အပိုင်း (၂) - Full PCA တွက်ချက်ခြင်း (Components အားလုံး = 72 ခု)
# AIMA Ch. 22.7 (Unsupervised Learning: Orthogonal Basis Extraction)
# ==============================================================================
pca = decomposition.PCA()
T_pca = pca.fit_transform(X)
# pca.mean_ -> ပုံ ၇၂ ပုံလုံး၏ ပျမ်းမျှပုံရိပ် (Mean Bear, length 6984)
# pca.components_ -> Principal Component Eigenvectors (Shape: 72 x 6984)
mean_bear = pca.mean_.reshape(img_shape)
eigen_bears = pca.components_.reshape((-1, img_shape[0], img_shape[1]))
var_ratio = pca.explained_variance_ratio_ * 100
# ==============================================================================
# ဂရပ်ဖ် (၁) - Mean Bear နှင့် ထိပ်တန်း Eigen-bears ၇ ပုံကို ပုံဖော်ကြည့်ခြင်း
# သီအိုရီ - Eigenvector တန်ဖိုးများတွင် အပေါင်း (+) နှင့် အနုတ် (-) နှစ်မျိုးလုံးပါဝင်သဖြင့်
# သုည (0) ကို အဖြူရောင်ထား၍ အနီ (+) နှင့် အပြာ (-) ခွဲပြသော 'RdBu_r' Colormap ကို သုံးထားသည်။
# ==============================================================================
fig1, axes1 = plt.subplots(2, 4, figsize=(14, 8))
axes1 = axes1.ravel()
# ပထမဆုံး အကွက်တွင် Mean Bear (ပျမ်းမျှဝက်ဝံရုပ်) ကို ပြသခြင်း
axes1[0].imshow(mean_bear, cmap='gray')
axes1[0].set_title('Mean Bear (μ)\n(Average of all 72 angles)', fontsize=10, fontweight='bold')
axes1[0].axis('off')
# ကျန် ၇ ကွက်တွင် PC 1 မှ PC 7 အထိ Eigen-bears များကို ပြသခြင်း
for i in range(7):
eig_img = eigen_bears[i]
# အပေါင်း/အနုတ် အရောင်စကေး ညီမျှစေရန် အများဆုံး ပကတိတန်ဖိုးကို ရှာခြင်း
vmax = np.max(np.abs(eig_img))
im = axes1[i + 1].imshow(eig_img, cmap='RdBu_r', vmin=-vmax, vmax=vmax)
axes1[i + 1].set_title(f'Eigen-bear PC {i+1}\n(Var: {var_ratio[i]:.2f}%)', fontsize=10)
axes1[i + 1].axis('off')
plt.suptitle('AIMA Ch. 27 & 22.7: Mean Bear & Top 7 Principal Component "Eigen-bears"',
fontsize=13, fontweight='bold')
plt.tight_layout()
plt.show()
# ==============================================================================
# ဂရပ်ဖ် (၂) - Eigen-bears များကို ပေါင်းစပ်၍ မူလပုံ ပြန်လည်တည်ဆောက်ခြင်း (Reconstruction)
# ညီမျှခြင်း: x_hat = μ + sum(z_i * v_i) [AIMA Ch. 22.7 Linear Autoencoder Decoding]
# ==============================================================================
target_idx = 9 # ဥပမာ - Index 9 (45° ဝန်းကျင် စောင်းနေသော ဝက်ဝံရုပ်ပုံ) ကို စမ်းသပ်မည်
original_img = X[target_idx].reshape(img_shape)
target_angle = angles[target_idx]
k_values = [1, 2, 5, 15, 40, 72] # အသုံးပြုမည့် Eigen-bears အရေအတွက် အဆင့်ဆင့်
fig2, axes2 = plt.subplots(2, 4, figsize=(14, 8))
axes2 = axes2.ravel()
# ပထမအကွက် - မူရင်းပုံ (Ground Truth)
axes2[0].imshow(original_img, cmap='gray', vmin=0, vmax=1)
axes2[0].set_title(f'Original Image ({target_angle}°)\n(Full 6,984 Pixels)', fontsize=10, fontweight='bold')
axes2[0].axis('off')
# ဒုတိယအကွက် - k=0 (Mean Bear သက်သက်)
mse_0 = np.mean((original_img - mean_bear) ** 2)
axes2[1].imshow(mean_bear, cmap='gray', vmin=0, vmax=1)
axes2[1].set_title(f'k = 0 (Mean Only)\nMSE: {mse_0:.5f}', fontsize=10)
axes2[1].axis('off')
# ကျန် ၆ ကွက် - k = 1, 2, 5, 15, 40, 72 ဖြင့် ပြန်လည်တည်ဆောက်ထားသော ပုံများ
for idx, k in enumerate(k_values):
# ထိပ်တန်း Component k ခု၏ Score (z_i) နှင့် Eigenvector (v_i) တို့ကို မြှောက်ပေါင်းခြင်း
reconstructed_flat = pca.mean_ + np.dot(T_pca[target_idx, :k], pca.components_[:k, :])
reconstructed_img = reconstructed_flat.reshape(img_shape)
# Reconstruction Error (MSE) နှင့် ထိန်းသိမ်းနိုင်သော Variance ရာခိုင်နှုန်း တွက်ချက်ခြင်း
mse = np.mean((original_img - reconstructed_img) ** 2)
cum_var = np.sum(var_ratio[:k])
ax = axes2[idx + 2]
ax.imshow(reconstructed_img, cmap='gray', vmin=0, vmax=1)
ax.set_title(f'k = {k} Eigen-bears ({cum_var:.1f}% Var)\nMSE: {mse:.5f}', fontsize=10)
ax.axis('off')
plt.suptitle(f'AIMA Ch. 22.7: Progressive Image Reconstruction using Eigen-bears (Angle = {target_angle}°)',
fontsize=13, fontweight='bold')
plt.tight_layout()
plt.show()
၃။ ထွက်ပေါ်လာသော "Eigen-bears" ပုံရိပ်များကို အသေးစိတ် ခွဲခြမ်းစိတ်ဖြာချက်
- Mean Bear ($\mu$)
- ပုံ ၇၂ ပုံလုံးကို ထပ်၍ ပျမ်းမျှယူထားသဖြင့် အလယ်ဗဟိုရှိ ဝက်ဝံရုပ်၏ ကိုယ်ထည်နှင့် ဦးခေါင်းမှာ ထင်ရှားနေပြီး၊ လည်ပတ်ရာတွင် နေရာပြောင်းလဲနေသော နားရွက်နှင့် လက်နှစ်ဖက်တို့မှာ ဝက်ဝံရုပ်ပတ်လည်တွင် မှုန်ဝါးဝါး အရိပ်ကွင်း (Halo) သဖွယ် ပေါ်နေပါမည်။
- Eigen-bear PC 1 နှင့် PC 2 (Fourier Harmonics on a Circle)
- အနီရောင် (
+) နှင့် အပြာရောင် (-) အကွက်များကို သေချာကြည့်ပါက PC 1 သည် ဝက်ဝံရုပ်၏ ဘယ်ဘက်ခြမ်းနှင့် ညာဘက်ခြမ်း ကို အနီ/အပြာ ဆန့်ကျင်ဘက် ခွဲခြားထားပြီး၊ PC 2 သည် အရှေ့ဘက်နှင့် အနောက်ဘက် (သို့မဟုတ် ထောင့်ဖြတ်အခြမ်း) ကို ခွဲခြားထားသည်ကို တွေ့ရပါမည်။ - သင်္ချာသဘောတရားအရ အရာဝတ္ထုတစ်ခုကို $360^\circ$ စက်ဝိုင်းပတ် လှည့်ထားသော ဒေတာပေါ်တွင် PCA တွက်ချက်သောအခါ ထွက်ပေါ်လာသော Eigenvectors များသည် စက်ဝိုင်းပတ်လည်ရှိ Fourier Basis Functions ($\cos \theta, \sin \theta, \cos 2\theta, \sin 2\theta, \dots$) များအဖြစ် အလိုအလျောက် ထွက်ပေါ်လာခြင်း ဖြစ်ပါသည်။
- Eigen-bear PC 3 မှ PC 7 (Higher-Frequency Spatial Details)
- Component နံပါတ် ကြီးလာသည်နှင့်အမျှ အနီနှင့် အပြာ အစင်းကြောင်းလေးများ ပိုမိုစိပ်လာပြီး နားရွက်အစွန်းများ၊ ခြေလက်အချိုးအစားနှင့် ဝက်ဝံရုပ်၏ မျက်နှာ/နှာခေါင်း အသေးစိတ်အချက်အလက်များ (High-frequency visual features) ကို ဖမ်းယူလာသည်ကို မြင်တွေ့ရပါမည်။
- Image Reconstruction (ဒုတိယဂရပ်ဖ်)
- $k = 2$ (2D Plot တွင် သုံးခဲ့သော အရေအတွက်) $45^\circ$ စောင်းနေသည့် ဦးတည်ရာကို ယေဘုယျ ခန့်မှန်းနိုင်သော်လည်း ပုံရိပ်မှာ အလွန်မှုန်ဝါးနေပါမည်။ အကြောင်းမှာ Linear PCA သည် ကွေးကောက်နေသော လည်ပတ်မှုကို မျဉ်းဖြောင့် ၂ ကြောင်းတည်းဖြင့် ဖြတ်ယူထားသောကြောင့် ဖြစ်ပါသည်။
- $k = 15$ မှ $40$ ဝက်ဝံရုပ်၏ မျက်လုံး၊ နှာခေါင်းနှင့် အမွေးအမျှင် အသေးစိတ်များ ပြတ်သားလာပြီး $k = 72$ ရောက်သောအခါ မူရင်းပုံနှင့် တစ်ထပ်တည်း (
MSE ≈ 0.00000) ပြန်လည်ရရှိပါမည်။
နောက်တစ်ဆင့်မှာ ဒီ Eigen-bears တွက်ချက်မှုထဲကို၊ အလင်းရောင်ပြောင်းလဲထားတဲ့ ပုံ ၁၂ ပုံ ကိုပါ ထည့်သွင်းလိုက်တဲ့အခါ၊ ထိပ်တန်း Eigen-bear (
PC 1) ဟာ "ဝက်ဝံရုပ် လည်ပတ်မှု" အစား "မီးအလင်းရောင် အလင်း/အမှောင် ပြောင်းလဲမှု" အဖြစ် ဘယ်လို ချက်ချင်းပြောင်းလဲသွားမလဲဆိုတာကို စမ်းသပ်ကြည့်ပါမယ်။
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.