Wednesday, November 18, 2026

Image Processing-08 PCA and Isomap Test with Face_Data.mat

အခုလေ့လာမဲ့ ပရိုဂရမ်က၊ Machine Learning / AI နယ်ပယ်ရှိ Dimensionality Reduction (အတိုင်းအတာ လျှော့ချခြင်း) နည်းလမ်းနှစ်မျိုးဖြစ်တဲ့ Linear PCA (Principal Component Analysis) နဲ့ Non-linear Isomap (Isometric Feature Mapping) တို့၏ စွမ်းဆောင်ရည်ကို၊ ယခင် ဝက်ဝံရုပ် ပုံရိပ်တွေထက် ပိုများတဲ့ မျက်နှာပုံရိပ် ဒေတာအစုအဝေး (face_data.mat) နဲ့ လက်တွေ့နှိုင်းယှဉ်စမ်းသပ်ရန် ရေးသားထားခြင်း ဖြစ်ပါတယ်။

Dataset ၏ အတွင်းပိုင်း ဖွဲ့စည်းပုံ (face_data.mat)

·         ဤ Dataset သည် Isomap သီအိုရီကို စတင်မိတ်ဆက်ခဲ့သည့် Tenenbaum et al. (2000) သုတေသနစာတမ်းမှ နာမည်ကျော် စံပြဒေတာဖြစ်ပြီး၊ 3D မျက်နှာရုပ်တုတစ်ခုတည်းကို ထောင့်ပေါင်းစုံနှင့် အလင်းရောင်အမျိုးမျိုးဖြင့် ရိုက်ကူးထားသော အဖြူအမဲပုံပေါင်း ၆၉၈ ပုံ (num_images = 698) ပါဝင်ပါသည်။ 

·     ပုံတစ်ပုံချင်းစီသည်  pixels အရွယ်အစားရှိသဖြင့် Pixel အရေအတွက် စုစုပေါင်း 4,096 features ရှိပါသည်။ ဆိုလိုသည်မှာ ပုံတစ်ပုံသည် 4,096-dimensional space အတွင်းရှိ အမှတ်တစ်ခု ဖြစ်နေပါသည်။ 

·         သို့သော် လက်တွေ့တွင် ထိုမျက်နှာပုံ ၆၉၈ ပုံလုံးကို ပြောင်းလဲစေသော အခြေခံအကြောင်းတရား (Degrees of Freedom / Latent Factors) မှာ အောက်ပါ ၃ မျိုးတည်း သာ ရှိပါသည်—

o   Left–Right Pose (Yaw): မျက်နှာကို ဘယ်မှညာသို့ လှည့်ခြင်း

o   Up–Down Pose (Pitch): မျက်နှာကို အပေါ်မော့ခြင်း၊ အောက်ငုံ့ခြင်း

o   Lighting Direction (Illumination): အလင်းရောင် ကျရောက်သည့် ဦးတည်ရာ ပြောင်းလဲခြင်း

 ပရိုဂရမ်၏ အဓိက ရည်မှန်းချက် ၃ ရပ်

    1. Latent Factors ၃ ခုကို ပြန်လည်ဖော်ထုတ်ခြင်း လူသား၏ ကြိုတင်သင်ကြားပေးမှု (Labels) လုံးဝမပါဘဲ 4,096-dimensional pixel data သက်သက်မှ အဆိုပါ မမြင်ရသော အကြောင်းတရား ၃ ခု (n_components=3) ကို Unsupervised Learning ဖြင့် အလိုအလျောက် ရှာဖွေဖော်ထုတ်ရန် ဖြစ်ပါသည်။ 
    1. Linear vs. Non-linear နှိုင်းယှဉ်ခြင်း မျက်နှာလှည့်ခြင်းသည် ကွေးကောက်သော မျက်နှာပြင် (Curved Manifold) ဖြစ်သောကြောင့် မျဉ်းဖြောင့်သဘောတရားသုံး PCA ထက် မျဉ်းကွေးအကွာအဝေး (Geodesic Distance) ကို တိုင်းတာသော Isomap က ပိုမိုတိကျစွာ ခွဲခြားနိုင်ကြောင်း လက်တွေ့မြင်တွေ့ရန် ဖြစ်ပါသည်။ 
    1. Visual Verification (Plot2D Function) ပရိုဂရမ်ထဲတွင် အသင့်ရေးပေးထားသော Plot2D သည် Scatter Plot အစက်များပေါ်တွင် သက်ဆိုင်ရာ မျက်နှာပုံလေးများကို တိုက်ရိုက်ကပ်၍ ဆွဲပေးပါသည်။ ထို့ကြောင့် Component ဝင်ရိုးတစ်ခုချင်းစီသည် ဘယ်/ညာ လှည့်ခြင်းကို ကိုယ်စားပြုသလား၊ အပေါ်/အောက် မော့ခြင်းကို ကိုယ်စားပြုသလား ဆိုသည်ကို မျက်စိဖြင့် တိုက်ရိုက်စစ်ဆေးနိုင်မည် ဖြစ်ပါသည်။

 

အဆင့်တစ်ခုချင်းစီ၏ အသေးစိတ် ရှင်းလင်းချက်

  1. PCA ကို အကောင်အထည်ဖော်ခြင်း (n_components=3) နှင့် Plot2D ခေါ်ဆိုခြင်း
    • sklearn.decomposition.PCA(n_components=3) ကို အသုံးပြု၍ df () ကို Component ၃ ခု (T_pca, shape ) သို့ လျှော့ချရပါမည်။
    • ထို့နောက် Plot2D(T_pca, title, x, y, num_to_plot=40) ကို ခေါ်ဆိုရာတွင် Component ၃ ခုလုံး၏ အဓိပ္ပာယ်ကို မြင်နိုင်ရန် (x=0, y=1) နှင့် (x=1, y=2) နှစ်ခုလုံးကို ဆွဲကြည့်သင့်ပါသည်။ 
  1. Isomap ကို အကောင်အထည်ဖော်ခြင်း (n_components=3) နှင့် Plot2D ခေါ်ဆိုခြင်း
    • sklearn.manifold.Isomap(n_neighbors=6, n_components=3) ကို အသုံးပြု၍ df ကို Component ၃ ခု (T_iso, shape ) သို့ လျှော့ချရပါမည်။
    • ပထမ Component ၂ ခု (x=0, y=1) ဖြင့် Plot2D ကို ခေါ်ဆို၍ မျက်နှာလှည့်ပုံ အစီအစဉ်ကို ကြည့်ရှုပြီး၊ ဒုတိယနှင့် တတိယ Component (x=1, y=2) ကိုပါ ထပ်ဆွဲကြည့်ပါက မီးအလင်းရောင် ပြောင်းလဲမှုကိုပါ မြင်တွေ့ရပါမည်။

 

  1. 3D Scatter Plot များ ရေးဆွဲခြင်း (3D Challenge):
    • T_pca နှင့် T_iso နှစ်ခုလုံးရှိ Component ၃ ခု ([:, 0], [:, 1], [:, 2]) ကို 3D Space (projection='3d') ပေါ်တွင် ယှဉ်တွဲရေးဆွဲရပါမည်။ 3D ဂရပ်ဖ်တွင် Component 3 ([:, 2] — Lighting direction) ကို အရောင်အဖြစ် ထည့်သွင်းပြသထားသဖြင့် မျက်နှာပုံစံ ၃ ဖက်မြင် ဖွဲ့စည်းပုံကို ပိုမိုရှင်းလင်းစွာ မြင်တွေ့ရပါမည်။

 

import os

import math

import random

import numpy as np

import pandas as pd

import scipy.io

 

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D  # 3D plotting အတွက် လိုအပ်သော module

from sklearn import decomposition, manifold

 

plt.style.use('ggplot')

 

# ==============================================================================

# Plot2D Function: 2D Scatter Plot အစက်များပေါ်တွင် ကျပန်းမျက်နှာပုံလေးများ ကပ်၍ဆွဲပေးခြင်း

# AIMA (4th Ed.) Ch. 19, Sec. 19.9 (Developing ML Systems: Exploratory Visualization)

# ရှင်းလင်းချက် - Dimension လျှော့ချလိုက်သော ဝင်ရိုး (Component x နှင့် y) တစ်ခုချင်းစီသည်

# မည်သည့် လက်တွေ့ရုပ်ပိုင်းဆိုင်ရာ ပြောင်းလဲမှု (ဥပမာ- ဘယ်/ညာ လှည့်ခြင်း) ကို ကိုယ်စားပြုနေသလဲ

# ဆိုသည်ကို မျက်စိဖြင့် တိုက်ရိုက်စစ်ဆေးနိုင်ရန် ဤ Function ကို အသုံးပြုပါသည်။

# ==============================================================================

def Plot2D(T, title, x, y, num_to_plot=40):

    fig = plt.figure(figsize=(9, 7))

    ax = fig.add_subplot(111)

    ax.set_title(title, fontsize=12, fontweight='bold')

    ax.set_xlabel('Component: {0}'.format(x))

    ax.set_ylabel('Component: {0}'.format(y))

   

    x_size = (max(T[:, x]) - min(T[:, x])) * 0.08

    y_size = (max(T[:, y]) - min(T[:, y])) * 0.08

   

    for i in range(num_to_plot):

        img_num = int(random.random() * num_images)

        x0, y0 = T[img_num, x] - x_size / 2., T[img_num, y] - y_size / 2.

        x1, y1 = T[img_num, x] + x_size / 2., T[img_num, y] + y_size / 2.

       

        # [Python 3.12 / Pandas 2.x Fix]: Series တွင် .reshape() မရှိတော့သဖြင့် .to_numpy() သုံးရမည်

        img = df.iloc[img_num, :].to_numpy().reshape(num_pixels, num_pixels)

        ax.imshow(img, aspect='auto', cmap=plt.cm.gray, interpolation='nearest',

                  zorder=100000, extent=(x0, x1, y0, y1))

 

    # ပုံ ၆၉၈ ပုံလုံး၏ တည်နေရာကို Scatter Plot အစက်များဖြင့် နောက်ခံတွင် ပြသခြင်း

    ax.scatter(T[:, x], T[:, y], marker='.', color='royalblue', alpha=0.7)

 

 

# ==============================================================================

# Dataset ဖတ်ယူခြင်းနှင့် ပုံများကို အတည့်ပြန်လှည့်ခြင်း (Data Preprocessing)

# AIMA (4th Ed.) Ch. 27, Sec. 27.2 (Image Formation: Pose & Illumination Variations)

# သီအိုရီ - မျက်နှာပုံ ၆၉၈ ပုံသည် 4,096-dimensional pixel space ထဲတွင် ရှိနေသော်လည်း

# ၎င်းတို့ကို ဖန်တီးနေသော အခြေခံအကြောင်းတရား (Latent Variables) မှာ ၃ ခုတည်းသာ ရှိသည်-

# (၁) ဘယ်/ညာ လှည့်ခြင်း (Yaw), (၂) အပေါ်/အောက် မော့ခြင်း (Pitch), (၃) မီးအလင်းရောင် (Lighting)။

# ==============================================================================

mat_path = os.path.join('Datasets', 'face_data.mat')

mat = scipy.io.loadmat(mat_path)

df = pd.DataFrame(mat['images']).T

num_images, num_pixels = df.shape

num_pixels = int(math.sqrt(num_pixels))  # 4096 -> 64 x 64 pixels

 

# MATLAB ဖိုင်ထဲတွင် ပုံများသည် Column-major order ဖြင့် စောင်းနေသဖြင့် Transpose (.T) လုပ်၍ အတည့်ပြန်လှည့်ခြင်း

for i in range(num_images):

    df.loc[i, :] = df.loc[i, :].to_numpy().reshape(num_pixels, num_pixels).T.reshape(-1)

 

print(f"Loaded Face Dataset: {num_images} images, each {num_pixels}x{num_pixels} ({num_images}x{num_pixels**2} matrix)")

 

 

# ==============================================================================

# TODO 1: PCA (Principal Component Analysis) ကို အကောင်အထည်ဖော်ခြင်း (n_components=3)

# AIMA (4th Ed.) Ch. 22, Sec. 22.7 (Unsupervised Learning: Linear Dimensionality Reduction)

# သီအိုရီ - PCA သည် ဒေတာ၏ Variance (ပြန့်ကျဲမှု) အများဆုံး ဦးတည်ရာ မျဉ်းဖြောင့်ဝင်ရိုး

# (Orthogonal Linear Subspace) ၃ ခုပေါ်သို့ အရိပ်ချ (Project) ပေးသည်။

# အားနည်းချက် - မျက်နှာလှည့်ခြင်းသည် ကွေးကောက်သော မျက်နှာပြင် (Curved Manifold) ဖြစ်သောကြောင့်

# Linear PCA သည် ထောင့်စွန်းပုံရိပ်များကို ပြားကပ်သွားစေခြင်း သို့မဟုတ် ရောထွေးသွားစေခြင်း ဖြစ်တတ်သည်။

# ==============================================================================

pca = decomposition.PCA(n_components=3)

pca.fit(df)

T_pca = pca.transform(df)

 

print(f"[PCA] Explained Variance Ratio (3 Components): {pca.explained_variance_ratio_.round(4)}")

print(f"[PCA] Total Variance Captured: {sum(pca.explained_variance_ratio_)*100:.2f}%")

 

# PCA ရလဒ်ကို 2D Scatter Plot ဖြင့် ပုံဖော်ခြင်း (Component 0 vs 1 နှင့် Component 1 vs 2)

Plot2D(T_pca, 'PCA 2D (Linear): Component 0 vs Component 1', x=0, y=1, num_to_plot=40)

Plot2D(T_pca, 'PCA 2D (Linear): Component 1 vs Component 2', x=1, y=2, num_to_plot=40)

 

 

# ==============================================================================

# TODO 2: Isomap (Isometric Feature Mapping) ကို အကောင်အထည်ဖော်ခြင်း (n_components=3)

# AIMA (4th Ed.) Ch. 22, Sec. 22.7 (The Manifold Hypothesis & Non-linear Reduction)

# AIMA (4th Ed.) Ch. 19, Sec. 19.7 (k-Nearest Neighbors Graph) & Ch. 3, Sec. 3.4 (Shortest Path)

# AIMA (4th Ed.) Ch. 21, Sec. 21.1 - 21.3 (Recovering Unobserved Latent Variables)

# သီအိုရီ - Isomap သည် အဆင့် ၃ ဆင့်ဖြင့် ကွေးကောက်နေသော မျက်နှာပြင်ကို ဖြန့်ထုတ်ပေးသည်-

#   ၁။ Point တစ်ခုချင်းစီကို အနီးဆုံး အိမ်နီးချင်း k=6 ခုနှင့် ချိတ်ဆက်၍ Neighborhood Graph ဆောက်သည်။

#   ၂။ Dijkstra/Floyd-Warshall (Uniform-Cost Search) ဖြင့် မျက်နှာပြင်အကွေးအတိုင်း အတိုဆုံး

#      အကွာအဝေး (Geodesic Distance) ကို တွက်ချက်သည်။

#   ၃။ Multidimensional Scaling (MDS) ဖြင့် Geodesic Distance များကို ထိန်းသိမ်းကာ 3D သို့ လျှော့ချသည်။

# ==============================================================================

iso = manifold.Isomap(n_neighbors=6, n_components=3)

iso.fit(df)

T_iso = iso.transform(df)

 

print(f"[Isomap] Geodesic Reconstruction Error (K=6): {iso.reconstruction_error():.4f}")

 

# Isomap ရလဒ်ကို 2D Scatter Plot ဖြင့် ပုံဖော်ခြင်း (ပထမ Component ၂ ခု: 0 vs 1)

Plot2D(T_iso, 'Isomap 2D (Non-linear, K=6): Component 0 vs Component 1 (Head Pose)', x=0, y=1, num_to_plot=40)

# Component 1 vs 2 ကိုပါ ထပ်ကြည့်ခြင်းဖြင့် တတိယ Latent Factor (Lighting) ကို မြင်တွေ့နိုင်သည်

Plot2D(T_iso, 'Isomap 2D (Non-linear, K=6): Component 1 vs Component 2 (Tilt & Lighting)', x=1, y=2, num_to_plot=40)

 

 

# ==============================================================================

# TODO 3: 3D Challenge — PCA နှင့် Isomap ရလဒ်များကို 3D Space အတွင်း ယှဉ်တွဲရေးဆွဲခြင်း

# AIMA (4th Ed.) Ch. 22, Sec. 22.7 (Intrinsic 3D Manifold Geometry in 4,096D Space)

# သီအိုရီ - မျက်နှာပုံ ၆၉၈ ပုံ၏ စစ်မှန်သော Degrees of Freedom ၃ ခုလုံး (Yaw, Pitch, Lighting) ကို

# 3D Space ထဲတွင် တစ်ပြိုင်နက် မြင်တွေ့ရမည်။ Component 2 (Lighting) ကို အရောင်ဖြင့် ပြသထားသည်။

# ==============================================================================

fig_3d = plt.figure(figsize=(15, 6.5))

 

# ဘယ်ဘက် 3D Plot: Linear PCA 3D Manifold

ax1 = fig_3d.add_subplot(121, projection='3d')

sc1 = ax1.scatter(T_pca[:, 0], T_pca[:, 1], T_pca[:, 2],

                  c=T_pca[:, 2], cmap='viridis', marker='o', s=25, alpha=0.8)

ax1.set_title('PCA 3D Manifold (Linear Projection)', fontsize=12, fontweight='bold')

ax1.set_xlabel('Component 0')

ax1.set_ylabel('Component 1')

ax1.set_zlabel('Component 2')

 

# ညာဘက် 3D Plot: Non-linear Isomap 3D Manifold

ax2 = fig_3d.add_subplot(122, projection='3d')

sc2 = ax2.scatter(T_iso[:, 0], T_iso[:, 1], T_iso[:, 2],

                  c=T_iso[:, 2], cmap='viridis', marker='o', s=25, alpha=0.8)

ax2.set_title('Isomap 3D Manifold (K=6, Geodesic Geometry)', fontsize=12, fontweight='bold')

ax2.set_xlabel('Component 0 (Left ↔ Right Pose)')

ax2.set_ylabel('Component 1 (Up ↔ Down Pose)')

ax2.set_zlabel('Component 2 (Lighting Direction)')

 

fig_3d.colorbar(sc2, ax=[ax1, ax2], shrink=0.75, label='Component 2 Value (Illumination Axis)')

plt.suptitle('AIMA Ch. 22.7 & Ch. 21: 3D Latent Manifold Comparison (PCA vs. Isomap)',

             fontsize=14, fontweight='bold')

plt.show()

 

ထွက်ပေါ်လာသော ဂရပ်ဖ်များကို လေ့လာသုံးသပ်ချက်

  1. Isomap 2D (Component 0 vs Component 1) တွင် မြင်တွေ့ရမည့် အံ့အားသင့်ဖွယ် ရလဒ်
    • မျက်နှာပုံလေးများကို ဂရုတစိုက် ကြည့်ရှုပါက အလျားလိုက် ဝင်ရိုး (Component 0) တစ်လျှောက်တွင် ဘယ်ဘက်အစွန်းမှ ညာဘက်အစွန်းသို့ မျက်နှာက ဘယ်မှညာသို့ အစဉ်လိုက် လှည့်သွားသည် ကို တွေ့ရပါမည်။
    • တစ်ချိန်တည်းမှာပင် ဒေါင်လိုက် ဝင်ရိုး (Component 1) တစ်လျှောက်တွင် အောက်ခြေမှ အပေါ်သို့ မျက်နှာက အောက်ငုံ့ရာမှ အပေါ်သို့ မော့လာသည် ကို အစဉ်လိုက် တွေ့ရပါမည်။
    • အဓိပ္ပာယ်မှာ Isomap သည် Label တစ်ခုမျှ မပါဘဲ Pixel 4,096 ခုထဲမှ လူသား၏ ဦးခေါင်းလှုပ်ရှားမှု ဝင်ရိုး ၂ ခု (Yaw နှင့် Pitch) ကို အတိအကျ ခွဲထုတ်ပေးနိုင်ခြင်း ဖြစ်ပါသည်။

 

  1. PCA vs. Isomap 2D နှင့် 3D ကွာခြားချက်
    • PCA သည် မျဉ်းဖြောင့်အတိုင်း ဖြတ်ယူသဖြင့် အလင်းအမှောင် (Lighting) ပြောင်းလဲမှုနှင့် မျက်နှာလှည့်ခြင်း (Pose) တို့ကို ရောထွေးပစ်ပြီး အစွန်းဘက်ရှိ မျက်နှာပုံများကို တစ်နေရာတည်းတွင် စုပြုံကျဉ်းမြောင်းသွားစေပါသည်။
    • Isomap ကမူ မျက်နှာပြင်အကွေးအတိုင်း ဖြန့်ထုတ်ပေးသဖြင့် 2D နှင့် 3D နှစ်ခုလုံးတွင် အစက်များသည် ညီညာပြန့်ပြူးသော လေးထောင့်/ခုံးခုံးပုံစံ မျက်နှာပြင် (Smooth 3D Sheet/Manifold) အဖြစ် ပေါ်ထွက်လာပြီး Component ဝင်ရိုး ၃ ခုလုံးက မတူညီသော အဓိပ္ပာယ် ၃ ခုကို သီးခြားစီ ကိုယ်စားပြုနိုင်ကြောင်း တွေ့ရပါမည်။

No comments:

Post a Comment

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