Tuesday, November 17, 2026

Image Procession-03 PCA & Isomap

Principal Component Analysis (PCA) နဲ့ Isomap (Isometric Feature Mapping) တို့ကို နှိုင်းယှဉ်စမ်းသပ်ခြင်းဟာ Chapter 22, Section 22.7 (Unsupervised Learning and Transfer Learning) မှာ ရှင်းပြထားတဲ့ Linear Dimensionality Reduction နဲ့ Non-linear Manifold Learning တို့ရဲ့ ကွာခြားချက်ကို လက်တွေ့မြင်သာစေတဲ့ အကောင်းဆုံး စမ်းသပ်မှု ဖြစ်ပါတယ်။

၁။ သီအိုရီရှုထောင့်မှ PCA နှင့် Isomap နှိုင်းယှဉ်ချက်

နှိုင်းယှဉ်ချက်PCA (Principal Component Analysis)Isomap (Isometric Feature Mapping)AIMA (4th Ed.) ဆက်စပ်အခန်း
အခြေခံ သင်္ချာသဘောတရားLinear Projection (ဒေတာများကို ပြန့်ပြူးသော မျဉ်းဖြောင့်ပြင်ညီ Subspace ပေါ်သို့ အရိပ်ချခြင်း)Non-linear Manifold Learning (ကွေးကောက်နေသော မျက်နှာပြင်ကို မပျက်စီးစေဘဲ ဖြန့်ထုတ်ခြင်း)Ch. 22, Sec. 22.7
တိုင်းတာသည့် အကွာအဝေးEuclidean Distance (အာကာသထဲမှ တိုက်ရိုက် မျဉ်းဖြောင့်အကွာအဝေး)Geodesic Distance ($k$-NN Graph ပေါ်မှ မျဉ်းကွေးအတိုင်း အတိုဆုံးအကွာအဝေး)Ch. 19, Sec. 19.7 & Ch. 3, Sec. 3.4
အဓိက ရည်မှန်းချက်ဒေတာ၏ Variance (ပြန့်ကျဲမှု) အများဆုံး ဦးတည်ရာများကို ထိန်းသိမ်းခြင်းဒေတာ၏ Intrinsic Geometry (အတွင်းပိုင်း ပုံသဏ္ဌာန်နှင့် ဆက်စပ်မှု) ကို ထိန်းသိမ်းခြင်းCh. 22, Sec. 22.7
ဝက်ဝံရုပ် Dataset ပေါ်ရှိ ရလဒ်အရှေ့ဘက် ($0^\circ$) နှင့် အနောက်ဘက် ($180^\circ$) ပုံရိပ်များ ထပ်နေခြင်း၊ စက်ဝိုင်းပုံစံ လိမ်တွန့်နေခြင်း$0^\circ$ မှ $355^\circ$ အထိ အစီအစဉ်မပျက် ညီညာသော စက်ဝိုင်းကွင်း (Ring) ဖြစ်ပေါ်ခြင်းCh. 21, Sec. 21.1 & Ch. 27, Sec. 27.2

အဘယ်ကြောင့် PCA က လိမ်တွန့်နေပြီး Isomap က စက်ဝိုင်းအတိအကျ ထွက်ရသနည်း။

  1. Curved Manifold in High-Dimensional Space (Ch. 22, Sec. 22.7) ဝက်ဝံရုပ်ကို $5^\circ$ စီ လှည့်ရိုက်ထားသော ပုံ ၇၂ ပုံသည် Pixel ပေါင်း $6,984$ ခုပါဝင်သည့် Space ထဲတွင် ပြားချပ်နေသော စက်ဝိုင်းပြား (Flat Circle) အဖြစ် တည်ရှိနေခြင်း မဟုတ်ပါ။ မွေးပွဝက်ဝံရုပ်၏ ကိုယ်ထည်အချိုးအစားကြောင့် 3D/High-D Space ထဲတွင် အာလူးကြော်ချပ် (Saddle / Pringles shape) ကဲ့သို့ အပေါ်အောက် လိမ်တွန့်ကွေးကောက်နေသော အဝိုင်းကွင်း (Twisted Closed Manifold) ဖြစ်နေပါသည်။

  2. Shadow Projection vs. Unrolling (Ch. 22, Sec. 22.7)

    • PCA သည် ထိုလိမ်တွန့်နေသော 3D/6984D အကွင်းကို အပေါ်တည့်တည့်မှ မီးထိုး၍ ကြမ်းပြင် (2D Plane) ပေါ်သို့ အရိပ် (Linear Projection) ချလိုက်သကဲ့သို့ ဖြစ်ပါသည်။ ထို့ကြောင့် ခေါက်နေသော အစိတ်အပိုင်းများသည် 2D ပြင်ညီပေါ်တွင် ကျဉ်းမြောင်းသွားခြင်း သို့မဟုတ် တစ်ခုပေါ်တစ်ခု ထပ်သွားခြင်း (Overlap / Folding) ဖြစ်ပေါ်ပါသည်။

    • Isomap သည် အဝိုင်းကွင်းတစ်လျှောက် ဘေးချင်းကပ်နေသော $5^\circ$ စီ ကွာခြားသည့် ပုံများ (n_neighbors=6) ကိုသာ ကြိုးဖြင့် ချိတ်ဆက်ပြီး ထိုလိမ်တွန့်နေသော အကွင်းကို ပြေလျော့သွားအောင် ဆွဲဆန့်ဖြန့်ထုတ် (Unroll) လိုက်သဖြင့် ညီညာသော စက်ဝိုင်းပုံစံ အတိအကျ ထွက်ပေါ်လာခြင်း ဖြစ်ပါသည်။

  3. Euclidean vs. Geodesic Distance (Ch. 19, Sec. 19.7 & Ch. 27, Sec. 27.2) ဝက်ဝံရုပ်၏ ရှေ့တည့်တည့်ပုံ ($0^\circ$) နှင့် နောက်ကျောဘက်ပုံ ($180^\circ$) တို့သည် Silhouette (အပြင်ပန်း အရိပ်သဏ္ဌာန်) ချင်း ဆင်တူနေတတ်ပါသည်။ PCA အသုံးပြုသော Euclidean Distance သည် ထိုနှစ်ပုံကြားမှ Pixel တန်ဖိုးကွာခြားချက် နည်းပါးသည်ဟု ယူဆကာ အနီးကပ်ဆွဲထားတတ်သော်လည်း၊ Isomap ၏ Geodesic Distance ကမူ $0^\circ$ မှ $180^\circ$ သို့ ရောက်ရန် အလယ်မှ ဖြတ်မသွားဘဲ စက်ဝိုင်းပတ်လမ်းအတိုင်း $5^\circ \to 10^\circ \to \dots \to 180^\circ$ အဆင့်ဆင့် လျှောက်လှမ်းတွက်ချက်ပါသည်။

၂။ PCA နှင့် Isomap နှိုင်းယှဉ် စမ်းသပ်သည့် Python ကုဒ်

အောက်ပါကုဒ်တွင် $0^\circ$ မှ $355^\circ$ အထိ လည်ပတ်မှု အစီအစဉ်ကို မျက်စိဖြင့် တိုက်ရိုက်ခြေရာခံနိုင်ရန် ပုံဖိုင်နာမည်ထဲမှ ဒီဂရီဂဏန်းအတိုင်း စီစဉ်ပြီး Cyclic Color Gradient (hsv colormap) ဖြင့် အရောင်ခြယ်ပြထားပါသည်။

Python
import os
import re
import numpy as np
import pandas as pd
from PIL import Image
import matplotlib.pyplot as plt
from sklearn import decomposition, manifold

# ဂရပ်ဖ်စတိုင် သတ်မှတ်ခြင်း
plt.style.use('ggplot')

# ==============================================================================
# အပိုင်း (၁) - Dataset ဖတ်ယူခြင်းနှင့် Angle အလိုက် အစီအစဉ်တကျ စီစဉ်ခြင်း
# AIMA Ch. 19.9 (Developing ML Systems) & Ch. 27.2 (Image Formation)
# ==============================================================================
dir_32 = os.path.join('Module4', 'Datasets', 'ALOI', '32')

# ဖိုင်နာမည်ထဲမှ လည်ပတ်ထောင့် ဒီဂရီဂဏန်း (ဥပမာ- 32_r0.png -> 0, 32_r5.png -> 5) ကို
# ဆွဲထုတ်၍ 0° မှ 355° အထိ ဂဏန်းတန်ဖိုးအတိုင်း အစဉ်လိုက် စီစဉ်ရန် Helper Function
def extract_angle(filename):
match = re.search(r'(\d+)\.png$', filename, re.IGNORECASE)
return int(match.group(1)) if match else 0

png_files = [f for f in os.listdir(dir_32) if f.lower().endswith('.png')]
png_files_sorted = sorted(png_files, key=extract_angle)

samples = [] angles = []
for fname in png_files_sorted:
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), Normalization (/ 255.0), Flattening -> (6984,)
img_flat = (img[::2, ::2] / 255.0).reshape(-1)
samples.append(img_flat)
angles.append(extract_angle(fname))

df = pd.DataFrame(samples) angles = np.array(angles)
print(f"Dataset Shape: {df.shape} | Angles Range: {angles.min()}° to {angles.max()}°")

# ==============================================================================
# အပိုင်း (၂) - Linear Model (PCA) နှင့် Non-linear Model (Isomap) တွက်ချက်ခြင်း
# AIMA Ch. 22, Sec. 22.7 (Linear Subspace vs. Non-linear Manifold Learning)
# ==============================================================================
# 1. PCA (Linear Dimensionality Reduction)
pca = decomposition.PCA(n_components=3)
T_pca = pca.fit_transform(df)
var_ratio = pca.explained_variance_ratio_

# 2. Isomap (Non-linear Manifold Learning, K=6)
iso = manifold.Isomap(n_neighbors=6, n_components=3)
T_iso = iso.fit_transform(df)
iso_err = iso.reconstruction_error()

print(f"\n[PCA] Explained Variance Ratio (3 Components): {var_ratio.round(4)}")
print(f"[PCA] Total Variance Captured by 2D: {sum(var_ratio[:2])*100:.2f}% | 3D: {sum(var_ratio)*100:.2f}%")
print(f"[Isomap] Geodesic Reconstruction Error (K=6): {iso_err:.4f}")

# ==============================================================================
# အပိုင်း (၃) - 2D နှိုင်းယှဉ်ချက် (PCA vs. Isomap)
# ရည်ရွယ်ချက် - 0° မှ 355° အထိ အရောင်စဉ် (hsv) အတိုင်း စက်ဝိုင်းပုံ ညီညာမှု ရှိ/မရှိ ကြည့်ရှုရန်
# ==============================================================================
fig_2d, axes = plt.subplots(1, 2, figsize=(15, 6))

# ဘယ်ဘက်ဂရပ်ဖ် - PCA 2D Projection
sc1 = axes[0].scatter(T_pca[:, 0], T_pca[:, 1], c=angles, cmap='hsv',
s=65, edgecolors='k', alpha=0.9)
axes[0].set_title(f'PCA 2D (Linear Projection)\nVariance Explained: {sum(var_ratio[:2])*100:.1f}%', fontsize=12)
axes[0].set_xlabel('Principal Component 1')
axes[0].set_ylabel('Principal Component 2')

# ညာဘက်ဂရပ်ဖ် - Isomap 2D Manifold
sc2 = axes[1].scatter(T_iso[:, 0], T_iso[:, 1], c=angles, cmap='hsv',
s=65, edgecolors='k', alpha=0.9)
axes[1].set_title('Isomap 2D (Non-linear Manifold, K=6)\nPreserves Geodesic Distances', fontsize=12)
axes[1].set_xlabel('Isomap Component 1')
axes[1].set_ylabel('Isomap Component 2')

# 0° နှင့် 180° အမှတ်များကို စာသားဖြင့် ထင်ရှားအောင် ညွှန်ပြခြင်း
for idx in [0, len(angles)//2]:
axes[0].annotate(f"{angles[idx]}°", (T_pca[idx, 0], T_pca[idx, 1]),
fontsize=9, fontweight='bold', xytext=(5, 5), textcoords='offset points')
axes[1].annotate(f"{angles[idx]}°", (T_iso[idx, 0], T_iso[idx, 1]),
fontsize=9, fontweight='bold', xytext=(5, 5), textcoords='offset points')

cbar = fig_2d.colorbar(sc2, ax=axes.ravel().tolist(), shrink=0.85)
cbar.set_label('Rotation Angle (Degrees: 0° → 355°)')
plt.suptitle('AIMA Ch. 22.7: Linear PCA vs. Non-linear Isomap (2D Comparison)', fontsize=14, fontweight='bold')
plt.show()

# ==============================================================================
# အပိုင်း (၄) - 3D နှိုင်းယှဉ်ချက် (High-D Space ထဲမှ လိမ်တွန့်နေသော Manifold ကို မြင်တွေ့ခြင်း)
# ==============================================================================
fig_3d = plt.figure(figsize=(15, 6.5))

# ဘယ်ဘက် 3D - PCA (3D Space ထဲတွင် ကွေးကောက်လိမ်တွန့်နေပုံကို အထင်အရှား မြင်ရမည်)
ax1 = fig_3d.add_subplot(121, projection='3d')
ax1.scatter(T_pca[:, 0], T_pca[:, 1], T_pca[:, 2], c=angles, cmap='hsv', s=60, edgecolors='k')
ax1.set_title('PCA 3D: Twisted Curved Manifold\n(Saddle / Pringles Shape in Pixel Space)', fontsize=11)
ax1.set_xlabel('PC 1')
ax1.set_ylabel('PC 2')
ax1.set_zlabel('PC 3')

# ညာဘက် 3D - Isomap (ကွေးကောက်မှုများကို ဖြန့်ထုတ်ပြီး ညီညာသော အဝိုင်းဖြစ်သွားပုံ)
ax2 = fig_3d.add_subplot(122, projection='3d')
p2 = ax2.scatter(T_iso[:, 0], T_iso[:, 1], T_iso[:, 2], c=angles, cmap='hsv', s=60, edgecolors='k')
ax2.set_title('Isomap 3D: Unrolled Flat Ring\n(True 1D Intrinsic Topology Recovered)', fontsize=11)
ax2.set_xlabel('Iso 1')
ax2.set_ylabel('Iso 2')
ax2.set_zlabel('Iso 3')

fig_3d.colorbar(p2, ax=[ax1, ax2], shrink=0.75, label='Rotation Angle (Degrees)')
plt.suptitle('AIMA Ch. 22.7: 3D Geometry Comparison (PCA vs. Isomap)', fontsize=14, fontweight='bold')
plt.show()

# ==============================================================================
# အပိုင်း (၅) - PCA Cumulative Explained Variance (Scree Plot)
# Linear နည်းလမ်းဖြင့် ဒေတာအချက်အလက် 90% ကျော် ရရှိရန် Dimension မည်မျှလိုအပ်သနည်း?
# ==============================================================================
pca_full = decomposition.PCA().fit(df)
cum_var = np.cumsum(pca_full.explained_variance_ratio_) * 100

plt.figure(figsize=(8, 4.5))
plt.plot(range(1, len(cum_var) + 1), cum_var, marker='o', markersize=4, color='teal', linewidth=2)
plt.axhline(y=90, color='crimson', linestyle='--', label='90% Variance Threshold')
plt.axvline(x=2, color='darkorange', linestyle=':', label='2 Components (2D Plot)')
plt.title('PCA Cumulative Explained Variance (Why Linear Reduction Needs Many Dimensions)', fontsize=11)
plt.xlabel('Number of Linear Principal Components')
plt.ylabel('Cumulative Explained Variance (%)')
plt.legend()
plt.grid(True)
plt.show()

၃။ ထွက်ပေါ်လာသော ဂရပ်ဖ် ၃ ခုကို အသေးစိတ် ဖတ်ရှုသုံးသပ်ချက်

  1. 2D Comparison Plot (ပထမဂရပ်ဖ်)

    • PCA 2D (ဘယ်ဘက်) အရောင်စဉ် (hsv) သည် ညီညာသော စက်ဝိုင်းပုံ မဖြစ်ဘဲ ဘဲဥပုံ ရှုံ့တွနေခြင်း၊ တစ်ဖက်သို့ စောင်းနေခြင်း သို့မဟုတ် ခေါက်ချိုးကျနေခြင်းကို တွေ့ရပါမည်။ အကြောင်းမှာ Linear Projection သည် $6,984$ pixels ၏ Brightness အပြောင်းအလဲ အများဆုံး ဦးတည်ရာ ၂ ခုကိုသာ ဖြတ်ယူလိုက်သောကြောင့် ဖြစ်ပါသည်။

    • Isomap 2D (ညာဘက်) အနီ $\to$ အဝါ $\to$ အစိမ်း $\to$ အပြာ $\to$ ခရမ်း စသည့် အရောင်စဉ်အတိုင်း အစက် ၇၂ စက်သည် အကွာအဝေး ညီညာသော စက်ဝိုင်းအပြည့် (Symmetrical Ring) အဖြစ် ပေါ်ထွက်လာပါမည်။ ၎င်းသည် AIMA Chapter 21 & 22 တွင် ဆိုလိုသော Latent Variable (လည်ပတ်ထောင့် $\theta$) ကို မပျက်မစီး ပြန်လည်ဖော်ထုတ်နိုင်ခြင်း ဖြစ်ပါသည်။

  2. 3D Comparison Plot (ဒုတိယဂရပ်ဖ်)

    • PCA 3D (ဘယ်ဘက်) 2D တွင် ရှုံ့တွနေရသည့် အကြောင်းရင်းအမှန်ကို 3D မြင်ကွင်းတွင် အထင်အရှား မြင်ရပါမည်။ အဝိုင်းကွင်းသည် ပြားချပ်မနေဘဲ 3D Space ထဲတွင် အထက်အောက် လှိုင်းတွန့်သဖွယ် ကွေးကောက် (Saddle curve) နေသည်ကို တွေ့ရပါမည်။

    • Isomap 3D (ညာဘက်) Isomap က ထိုလှိုင်းတွန့်ကွေးကောက်မှုများကို ပြေလျော့သွားအောင် ဖြန့်ထုတ်ပေးလိုက်သဖြင့် 3D Space ထဲတွင် ပြားချပ်ညီညာသော အဝိုင်းကွင်း ဖြစ်သွားသည်ကို တွေ့ရပါမည်။

  3. PCA Cumulative Explained Variance Plot (တတိယဂရပ်ဖ်)

    • ဤဂရပ်ဖ်တွင် Component ၂ ခုတည်း (x=2) ဖြင့် ဒေတာ၏ Variance ကို တွက်ကြည့်ပါက ၆၀% မှ ၇၀% ဝန်းကျင် ခန့်သာ ဖမ်းယူနိုင်ပြီး ကျန် ၃၀% ကျော်သော အချက်အလက်များ ဆုံးရှုံးသွားသည် ကို တွေ့ရပါမည်။ Linear PCA ဖြင့် အချက်အလက် ၉၀% ကျော် ရရှိရန် Component ပေါင်း ၁၀ ခုမှ ၁၅ ခုခန့် အထိ လိုအပ်နေချိန်တွင် Non-linear Isomap ကမူ Component ၂ ခုတည်း ဖြင့် ဝက်ဝံရုပ်၏ လည်ပတ်မှု တစ်ခုလုံးကို အပြည့်အဝ ကိုယ်စားပြုနိုင်ကြောင်း သက်သေပြနေပါသည်။

နောက်လာမဲ့ Image Processing အပိုင်း-၄ မှာ၊ AIMA Chapter 27 (Computer Vision) နဲ့ Chapter 22 တို့ကို ပိုမိုနက်နဲစွာ ဆက်စပ်လေ့လာနိုင်ဖို့၊ PCA က တွက်ချက်ထုတ်ပေးလိုက်တဲ့ အဓိက Component ပုံရိပ်တွေ ("Eigen-bears" — Principal Component Eigenvectors များကို ပုံရိပ်အဖြစ် ပြန်ဖော်ကြည့်ခြင်း) ကို ဆက်လက်ကြည့်ပါမယ်။

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.