머신 러닝 실습 8. 올리베티 얼굴 데이터셋

J. Hwang·2025년 4월 9일
0

ML practice

목록 보기
4/4

올리베티 얼굴 데이터셋은 1990년대 AT&T 연구소에서 만든 얼굴 데이터셋이다. 40명의 얼굴을 각각 다른 표정, 각도 등으로 10장씩 촬영하여 총 400장의 얼굴 이미지로 구성되어 있다. 하나의 이미지는 64 x 64의 흑백 이미지이다. 올리베티 얼굴 데이터셋은 scikit-learn을 이용해 불러올 수 있다.

import numpy as np
from sklearn.datasets import fetch_olivetti_faces

faces = fetch_olivetti_faces()

# faces는 4가지 데이터로 구성됨
print(faces.keys())     # 'data', 'images', 'target', 'DESCR'

올리베티 얼굴 데이터셋은 data, images, target, DESCR의 4가지 데이터로 구성되어 있다.
data는 400 x 4096의 numpy array인데, 400장의 얼굴 이미지들을 하나씩 벡터화한 데이터이다. 따라서 여기에서 이미지 한 장을 가져와 64 x 64로 reshape하면, 2차원 얼굴 이미지를 확인할 수 있다.

from matplotlib import pyplot as plt

plt.imshow(faces.data[0].reshape(64, 64), cmap='gray')

images는 얼굴 이미지를 담고 있는 데이터이다. 각 픽셀 값은 0~255를 min-max scaling한 값으로 되어 있다.

plt.imshow(faces.images[0], cmap='gray')

target은 각 얼굴 이미지의 라벨을 의미한다. 40명의 얼굴 데이터가 10장씩 있으므로 0부터 39까지의 수가 10개씩 반복된다.

print(faces.target)
# array([ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1, ..., 39, 39])

DESCR은 데이터셋에 대한 설명을 담고 있다.

print(faces.DESCR)

.. _olivetti_faces_dataset:

The Olivetti faces dataset
--------------------------

`This dataset contains a set of face images`_ taken between April 1992 and
April 1994 at AT&T Laboratories Cambridge. The
:func:`sklearn.datasets.fetch_olivetti_faces` function is the data
fetching / caching function that downloads the data
archive from AT&T.

.. _This dataset contains a set of face images: https://cam-orl.co.uk/facedatabase.html

As described on the original website:

    There are ten different images of each of 40 distinct subjects. For some
    subjects, the images were taken at different times, varying the lighting,
    facial expressions (open / closed eyes, smiling / not smiling) and facial
    details (glasses / no glasses). All the images were taken against a dark
    homogeneous background with the subjects in an upright, frontal position
    (with tolerance for some side movement).

**Data Set Characteristics:**

=================   =====================
Classes                                40
Samples total                         400
Dimensionality                       4096
Features            real, between 0 and 1
=================   =====================

The image is quantized to 256 grey levels and stored as unsigned 8-bit
integers; the loader will convert these to floating point values on the
interval [0, 1], which are easier to work with for many algorithms.

The "target" for this database is an integer from 0 to 39 indicating the
identity of the person pictured; however, with only 10 examples per class, this
relatively small dataset is more interesting from an unsupervised or
semi-supervised perspective.

The original dataset consisted of 92 x 112, while the version available here
consists of 64x64 images.

When using these images, please give credit to AT&T Laboratories Cambridge.

올리베티 얼굴 데이터셋에 PCA를 적용했다가 복원해보자. 아래와 같이 scikit-learn에서 PCA를 호출해서 원하는 차원만큼 줄였다가 inverse_transform을 하면 복원할 수 있다.

from sklearn.decomposition import PCA

pca = PCA(n_components=100)     # 원본 4096차원에서 100차원으로 줄여보자.
pca_result = pca.fit_transform(faces.data)
reconstructed = pca.inverse_transform(pca_result)

n_components 수치를 달리하면서 PCA를 적용했다가 복원하면 어떤 변화가 생기는지 살펴보았다. n_components 수치가 작을수록 더 크게 차원을 줄였다가 복원하게 되므로 그 과정에서 정보 손실이 많이 생겨서 주요 윤곽만 남고 디테일이 사라진 모습을 볼 수 있다.

References

https://scikit-learn.org/stable/auto_examples/decomposition/plot_faces_decomposition.html
https://wikidocs.net/49847

profile
Let it code

0개의 댓글