スタンフォード/CS131/宿題6-1 画像圧縮

Stanford University/CS131/宿題6をやる。今回の宿題は以下をカバーする。

• image compression using SVD
SVD(Singular Value Decomposition：特異値分解)を使った画像圧縮
• kNN methods for image recognition.
画像認識用KNN(K Nearest Neighbor：k近傍)法
• PCA and LDA to improve kNN
PCA(Principal Component Analysis：主成分分析)とLDA(Linear Discriminant Analysis：線形判別分析)によるKNN性能向上
スポンサーリンク

Image Compression¶

Image compression is used to reduce the cost of storage and transmission of images (or videos).
One lossy compression method is to apply Singular Value Decomposition (SVD) to an image, and only keep the top n singular values.

from time import time
from collections import defaultdict
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
from skimage import io

%matplotlib inline
plt.rcParams['figure.figsize'] = (15.0, 10.0) # set default size of plots
plt.rcParams["font.size"] = "17"
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'

image = io.imread('pitbull.jpg', as_gray=True)
plt.imshow(image)
plt.axis('off')
plt.show()


Let’s implement image compression using SVD.
SVDを使って画像圧縮を実装する。

We first compute the SVD of the image, and as seen in class we keep the n largest singular values and singular vectors to reconstruct the image.

Implement function compress_image in compression.py.
compression.pyにcompress_image関数を実装する。

def compress_image(image, num_values):
"""Compress an image using SVD and keeping the top num_values singular values.
Args:
image: numpy array of shape (H, W)
num_values: number of singular values to keep
Returns:
compressed_image: numpy array of shape (H, W) containing the compressed image
compressed_size: size of the compressed image
"""
compressed_image = None
compressed_size = 0
# YOUR CODE HERE
# Steps:
#     1. Get SVD of the image
#     2. Only keep the top num_values singular values, and compute compressed_image
#     3. Compute the compressed size
u, s, v = np.linalg.svd(image)
u = u[:,:num_values]
s = np.diag(s[:num_values])
v = v[:num_values,:]
compressed_image = u.dot(s.dot(v))
compressed_size = u.size+num_values+v.size
#compressed_size = num_values*u.shape[0]+num_values+num_values*v.shape1
# END YOUR CODE
assert compressed_image.shape == image.shape, \
"Compressed image and original image don't have the same shape"
assert compressed_size > 0, "Don't forget to compute compressed_size"
return compressed_image, compressed_size

compressed_image, compressed_size = compress_image(image, 100)
compression_ratio = float(compressed_size) / image.size
print('Original image shape:', image.shape)
print('Compressed size: %d' % compressed_size)
print('Compression ratio: %.3f' % compression_ratio)

assert compressed_size == 298500

Original image shape: (1704, 1280)
Compressed size: 298500
Compression ratio: 0.137

# Number of singular values to keep
n_values = [10, 50, 100]

for n in n_values:
# Compress the image using n singular values
compressed_image, compressed_size = compress_image(image, n)
compression_ratio = float(compressed_size) / image.size

print("Data size (original): %d" % (image.size))
print("Data size (compressed): %d" % compressed_size)
print("Compression ratio: %f" % (compression_ratio))

plt.imshow(compressed_image, cmap='gray')
title = "n = %s" % n
plt.title(title)
plt.axis('off')
plt.show()

Data size (original): 2181120
Data size (compressed): 29850
Compression ratio: 0.013686

Data size (original): 2181120
Data size (compressed): 149250
Compression ratio: 0.068428

Data size (original): 2181120
Data size (compressed): 298500
Compression ratio: 0.136856

スポンサーリンク

Face Dataset¶

We will use a dataset of faces of celebrities. Download the dataset using the following command:

sh get_dataset.sh

The face dataset for CS131 assignment.
The directory containing the dataset has the following structure:
この宿題用の顔データセット。データセットが保存されているディレクトリは以下の構造を持つ。

faces/
train/
angelina jolie/
anne hathaway/
...
test/
angelina jolie/
anne hathaway/
...

Each class has 50 training images and 10 testing images.

!sh get_dataset.sh

--2018-11-06 14:25:52--  http://vision.stanford.edu/teaching/cs131_fall1718/files/data/hw6/faces.tar.gz
Resolving vision.stanford.edu (vision.stanford.edu)... 171.64.68.10
Connecting to vision.stanford.edu (vision.stanford.edu)|171.64.68.10|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2257223 (2.2M) [application/x-gzip]
Saving to: ‘faces.tar.gz’

faces.tar.gz        100%[===================>]   2.15M   992KB/s    in 2.2s

2018-11-06 14:25:55 (992 KB/s) - ‘faces.tar.gz’ saved [2257223/2257223]

faces/train/johnny depp/39.jpg
faces/train/leonardo dicaprio/19.jpg
faces/train/natalie portman/12.jpg
faces/train/tom cruise/48.jpg
faces/train/leonardo dicaprio/
faces/train/leonardo dicaprio/36.jpg
faces/train/johnny depp/56.jpg
faces/train/angelina jolie/10.jpg
faces/train/scarlett johansson/48.jpg
faces/train/scarlett johansson/5.jpg
faces/train/hillary clinton/22.jpg
faces/test/justin timberlake/13.jpg
faces/train/brad pitt/25.jpg
faces/train/angelina jolie/29.jpg
faces/test/emma watson/2.jpg
faces/test/angelina jolie/11.jpg
faces/train/justin timberlake/19.jpg
faces/train/anne hathaway/27.jpg
faces/train/brad pitt/42.jpg
faces/train/angelina jolie/46.jpg
faces/train/jennifer aniston/43.jpg
faces/train/cristiano ronaldo/15.jpg
faces/train/hillary clinton/58.jpg
faces/test/brad pitt/6.jpg
faces/train/barack obama/11.jpg
faces/train/justin timberlake/36.jpg
faces/train/angelina jolie/2.jpg
faces/train/anne hathaway/44.jpg
faces/train/jennifer aniston/60.jpg
faces/train/cristiano ronaldo/32.jpg
faces/test/anne hathaway/13.jpg
faces/train/george clooney/6.jpg
faces/test/emma watson/
faces/train/emma watson/26.jpg
faces/train/justin timberlake/53.jpg
faces/train/johnny depp/
faces/train/george clooney/20.jpg
faces/test/cristiano ronaldo/7.jpg
faces/train/barack obama/47.jpg
faces/test/angelina jolie/7.jpg
faces/test/leonardo dicaprio/6.jpg
faces/train/emma watson/43.jpg
faces/train/george clooney/39.jpg
faces/test/tom cruise/8.jpg
faces/train/anne hathaway/6.jpg
faces/train/johnny depp/28.jpg
faces/train/tom cruise/37.jpg
faces/train/tom cruise/4.jpg
faces/train/nicole kidman/24.jpg
faces/train/cristiano ronaldo/5.jpg
faces/test/scarlett johansson/7.jpg
faces/train/hillary clinton/1.jpg
faces/train/johnny depp/45.jpg
faces/train/leonardo dicaprio/25.jpg
faces/train/leonardo dicaprio/4.jpg
faces/train/scarlett johansson/37.jpg
faces/test/johnny depp/11.jpg
faces/train/hillary clinton/11.jpg
faces/train/nicole kidman/41.jpg
faces/train/brad pitt/14.jpg
faces/train/angelina jolie/18.jpg
faces/test/nicole kidman/10.jpg
faces/train/angelina jolie/35.jpg
faces/train/leonardo dicaprio/42.jpg
faces/train/anne hathaway/16.jpg
faces/train/
faces/train/brad pitt/31.jpg
faces/test/scarlett johansson/13.jpg
faces/train/jennifer aniston/32.jpg
faces/train/hillary clinton/47.jpg
faces/train/natalie portman/30.jpg
faces/train/justin timberlake/25.jpg
faces/train/anne hathaway/33.jpg
faces/train/nicole kidman/53.jpg
faces/train/cristiano ronaldo/21.jpg
faces/train/barack obama/19.jpg
faces/train/natalie portman/49.jpg
faces/test/johnny depp/4.jpg
faces/train/emma watson/15.jpg
faces/train/justin timberlake/42.jpg
faces/train/anne hathaway/50.jpg
faces/test/jennifer aniston/11.jpg
faces/train/barack obama/3.jpg
faces/test/george clooney/10.jpg
faces/test/jennifer aniston/8.jpg
faces/train/emma watson/32.jpg
faces/train/george clooney/28.jpg
faces/train/brad pitt/4.jpg
faces/train/justin timberlake/2.jpg
faces/train/johnny depp/17.jpg
faces/train/barack obama/53.jpg
faces/train/tom cruise/26.jpg
faces/train/george clooney/45.jpg
faces/train/nicole kidman/13.jpg
faces/test/tom cruise/13.jpg
faces/train/johnny depp/34.jpg
faces/train/leonardo dicaprio/14.jpg
faces/train/scarlett johansson/26.jpg
faces/train/tom cruise/43.jpg
faces/train/nicole kidman/30.jpg
faces/test/barack obama/5.jpg
faces/test/anne hathaway/1.jpg
faces/train/hillary clinton/9.jpg
faces/train/natalie portman/26.jpg
faces/train/johnny depp/51.jpg
faces/train/leonardo dicaprio/31.jpg
faces/train/hillary clinton/19.jpg
faces/train/scarlett johansson/43.jpg
faces/train/brad pitt/20.jpg
faces/train/angelina jolie/24.jpg
faces/train/jennifer aniston/21.jpg
faces/train/brad pitt/56.jpg
faces/train/hillary clinton/36.jpg
faces/train/brad pitt/39.jpg
faces/train/justin timberlake/14.jpg
faces/train/anne hathaway/22.jpg
faces/train/emma watson/5.jpg
faces/train/justin timberlake/
faces/train/cristiano ronaldo/10.jpg
faces/train/johnny depp/6.jpg
faces/train/jennifer aniston/6.jpg
faces/train/natalie portman/38.jpg
faces/train/hillary clinton/53.jpg
faces/test/brad pitt/1.jpg
faces/train/cristiano ronaldo/29.jpg
faces/train/natalie portman/7.jpg
faces/train/justin timberlake/31.jpg
faces/train/barack obama/25.jpg
faces/train/natalie portman/55.jpg
faces/train/george clooney/1.jpg
faces/train/cristiano ronaldo/46.jpg
faces/train/emma watson/21.jpg
faces/train/george clooney/17.jpg
faces/test/barack obama/14.jpg
faces/test/cristiano ronaldo/2.jpg
faces/test/leonardo dicaprio/1.jpg
faces/train/tom cruise/15.jpg
faces/test/tom cruise/3.jpg
faces/train/george clooney/34.jpg
faces/test/angelina jolie/2.jpg
faces/train/anne hathaway/1.jpg
faces/train/johnny depp/23.jpg
faces/test/hillary clinton/4.jpg
faces/train/scarlett johansson/15.jpg
faces/train/tom cruise/32.jpg
faces/test/george clooney/6.jpg
faces/train/natalie portman/15.jpg
faces/train/nicole kidman/38.jpg
faces/test/scarlett johansson/2.jpg
faces/train/johnny depp/40.jpg
faces/train/leonardo dicaprio/20.jpg
faces/train/scarlett johansson/32.jpg
faces/train/angelina jolie/13.jpg
faces/train/nicole kidman/5.jpg
faces/train/johnny depp/59.jpg
faces/train/jennifer aniston/10.jpg
faces/train/leonardo dicaprio/39.jpg
faces/train/scarlett johansson/8.jpg
faces/train/brad pitt/28.jpg
faces/train/anne hathaway/11.jpg
faces/train/jennifer aniston/29.jpg
faces/test/justin timberlake/
faces/test/justin timberlake/6.jpg
faces/test/emma watson/5.jpg
faces/train/leonardo dicaprio/56.jpg
faces/train/hillary clinton/42.jpg
faces/train/brad pitt/45.jpg
faces/train/jennifer aniston/46.jpg
faces/train/cristiano ronaldo/18.jpg
faces/train/justin timberlake/20.jpg
faces/test/brad pitt/9.jpg
faces/train/barack obama/14.jpg
faces/train/natalie portman/44.jpg
faces/train/angelina jolie/5.jpg
faces/train/justin timberlake/39.jpg
faces/train/anne hathaway/47.jpg
faces/train/cristiano ronaldo/35.jpg
faces/train/emma watson/10.jpg
faces/train/george clooney/9.jpg
faces/train/justin timberlake/56.jpg
faces/train/emma watson/29.jpg
faces/test/jennifer aniston/3.jpg
faces/train/george clooney/23.jpg
faces/train/johnny depp/12.jpg
faces/train/emma watson/46.jpg
faces/train/tom cruise/21.jpg
faces/train/george clooney/40.jpg
faces/train/anne hathaway/9.jpg
faces/train/tom cruise/7.jpg
faces/train/nicole kidman/27.jpg
faces/train/scarlett johansson/21.jpg
faces/train/cristiano ronaldo/8.jpg
faces/train/brad pitt/17.jpg
faces/train/hillary clinton/4.jpg
faces/train/johnny depp/48.jpg
faces/train/leonardo dicaprio/28.jpg
faces/train/leonardo dicaprio/7.jpg
faces/test/barack obama/
faces/train/hillary clinton/14.jpg
faces/train/nicole kidman/44.jpg
faces/train/jennifer aniston/18.jpg
faces/train/leonardo dicaprio/45.jpg
faces/train/hillary clinton/31.jpg
faces/train/brad pitt/34.jpg
faces/train/jennifer aniston/35.jpg
faces/train/angelina jolie/38.jpg
faces/train/angelina jolie/55.jpg
faces/train/johnny depp/1.jpg
faces/train/jennifer aniston/1.jpg
faces/train/natalie portman/33.jpg
faces/train/nicole kidman/56.jpg
faces/train/justin timberlake/28.jpg
faces/train/anne hathaway/36.jpg
faces/train/brad pitt/51.jpg
faces/train/natalie portman/2.jpg
faces/train/jennifer aniston/52.jpg
faces/train/cristiano ronaldo/24.jpg
faces/train/barack obama/20.jpg
faces/train/natalie portman/50.jpg
faces/train/justin timberlake/45.jpg
faces/train/emma watson/18.jpg
faces/train/anne hathaway/53.jpg
faces/train/george clooney/12.jpg
faces/train/cristiano ronaldo/41.jpg
faces/train/barack obama/39.jpg
faces/train/barack obama/6.jpg
faces/train/emma watson/35.jpg
faces/train/tom cruise/10.jpg
faces/train/brad pitt/7.jpg
faces/test/george clooney/13.jpg
faces/train/barack obama/56.jpg
faces/test/leonardo dicaprio/11.jpg
faces/train/tom cruise/29.jpg
faces/train/george clooney/48.jpg
faces/train/nicole kidman/16.jpg
faces/train/scarlett johansson/10.jpg
faces/test/
faces/train/leonardo dicaprio/17.jpg
faces/train/scarlett johansson/29.jpg
faces/train/tom cruise/46.jpg
faces/train/nicole kidman/33.jpg
faces/train/natalie portman/29.jpg
faces/train/johnny depp/54.jpg
faces/train/scarlett johansson/3.jpg
faces/train/scarlett johansson/46.jpg
faces/train/hillary clinton/20.jpg
faces/test/justin timberlake/11.jpg
faces/train/jennifer aniston/24.jpg
faces/train/angelina jolie/27.jpg
faces/test/justin timberlake/1.jpg
faces/train/leonardo dicaprio/51.jpg
faces/test/nicole kidman/8.jpg
faces/train/justin timberlake/17.jpg
faces/train/anne hathaway/25.jpg
faces/train/cristiano ronaldo/13.jpg
faces/train/angelina jolie/44.jpg
faces/train/jennifer aniston/41.jpg
faces/train/emma watson/8.jpg
faces/train/jennifer aniston/9.jpg
faces/test/hillary clinton/11.jpg
faces/test/brad pitt/4.jpg
faces/train/hillary clinton/56.jpg
faces/train/brad pitt/59.jpg
faces/train/justin timberlake/34.jpg
faces/train/anne hathaway/42.jpg
faces/train/cristiano ronaldo/30.jpg
faces/test/anne hathaway/11.jpg
faces/train/barack obama/28.jpg
faces/train/george clooney/4.jpg
faces/train/cristiano ronaldo/49.jpg
faces/train/emma watson/24.jpg
faces/train/justin timberlake/51.jpg
faces/test/cristiano ronaldo/5.jpg
faces/train/barack obama/45.jpg
faces/train/tom cruise/18.jpg
faces/train/george clooney/37.jpg
faces/train/emma watson/41.jpg
faces/test/leonardo dicaprio/4.jpg
faces/test/tom cruise/6.jpg
faces/train/anne hathaway/4.jpg
faces/train/johnny depp/26.jpg
faces/test/george clooney/9.jpg
faces/train/scarlett johansson/18.jpg
faces/test/hillary clinton/7.jpg
faces/train/tom cruise/35.jpg
faces/train/tom cruise/2.jpg
faces/train/nicole kidman/22.jpg
faces/train/cristiano ronaldo/3.jpg
faces/train/natalie portman/18.jpg
faces/test/scarlett johansson/5.jpg
faces/train/leonardo dicaprio/2.jpg
faces/train/scarlett johansson/35.jpg
faces/train/nicole kidman/8.jpg
faces/train/angelina jolie/16.jpg
faces/train/hillary clinton/28.jpg
faces/test/justin timberlake/9.jpg
faces/train/anne hathaway/14.jpg
faces/train/angelina jolie/33.jpg
faces/train/jennifer aniston/30.jpg
faces/train/leonardo dicaprio/59.jpg
faces/train/hillary clinton/45.jpg
faces/train/jennifer aniston/49.jpg
faces/train/justin timberlake/23.jpg
faces/train/anne hathaway/31.jpg
faces/train/angelina jolie/50.jpg
faces/train/nicole kidman/51.jpg
faces/train/barack obama/17.jpg
faces/train/natalie portman/47.jpg
faces/train/angelina jolie/8.jpg
faces/test/johnny depp/2.jpg
faces/train/cristiano ronaldo/38.jpg
faces/train/emma watson/13.jpg
faces/train/justin timberlake/40.jpg
faces/train/barack obama/34.jpg
faces/train/george clooney/26.jpg
faces/train/barack obama/1.jpg
faces/train/emma watson/30.jpg
faces/train/brad pitt/2.jpg
faces/train/johnny depp/15.jpg
faces/train/barack obama/51.jpg
faces/train/emma watson/49.jpg
faces/train/tom cruise/24.jpg
faces/test/emma watson/15.jpg
faces/train/george clooney/43.jpg
faces/train/nicole kidman/11.jpg
faces/test/tom cruise/11.jpg
faces/train/johnny depp/32.jpg
faces/train/leonardo dicaprio/12.jpg
faces/train/scarlett johansson/24.jpg
faces/train/tom cruise/41.jpg
faces/test/barack obama/3.jpg
faces/train/hillary clinton/7.jpg
faces/train/natalie portman/24.jpg
faces/train/hillary clinton/17.jpg
faces/train/nicole kidman/47.jpg
faces/train/scarlett johansson/41.jpg
faces/train/angelina jolie/22.jpg
faces/train/brad pitt/54.jpg
faces/train/hillary clinton/34.jpg
faces/train/brad pitt/37.jpg
faces/test/natalie portman/15.jpg
faces/test/nicole kidman/3.jpg
faces/train/jennifer aniston/38.jpg
faces/train/justin timberlake/12.jpg
faces/train/anne hathaway/20.jpg
faces/train/johnny depp/4.jpg
faces/train/jennifer aniston/55.jpg
faces/train/emma watson/3.jpg
faces/train/jennifer aniston/4.jpg
faces/train/natalie portman/36.jpg
faces/train/hillary clinton/51.jpg
faces/train/anne hathaway/39.jpg
faces/train/angelina jolie/58.jpg
faces/train/natalie portman/5.jpg
faces/train/cristiano ronaldo/27.jpg
faces/train/barack obama/23.jpg
faces/train/natalie portman/53.jpg
faces/train/justin timberlake/48.jpg
faces/test/natalie portman/6.jpg
faces/train/george clooney/15.jpg
faces/train/cristiano ronaldo/44.jpg
faces/test/barack obama/12.jpg
faces/train/emma watson/38.jpg
faces/train/barack obama/9.jpg
faces/train/tom cruise/13.jpg
faces/train/justin timberlake/8.jpg
faces/train/george clooney/32.jpg
faces/train/barack obama/59.jpg
faces/test/leonardo dicaprio/14.jpg
faces/train/tom cruise/30.jpg
faces/train/johnny depp/21.jpg
faces/train/scarlett johansson/13.jpg
faces/train/nicole kidman/19.jpg
faces/test/hillary clinton/2.jpg
faces/test/george clooney/4.jpg
faces/train/natalie portman/13.jpg
faces/train/nicole kidman/36.jpg
faces/train/tom cruise/49.jpg
faces/train/scarlett johansson/30.jpg
faces/test/anne hathaway/7.jpg
faces/train/scarlett johansson/49.jpg
faces/train/nicole kidman/3.jpg
faces/train/johnny depp/57.jpg
faces/train/angelina jolie/11.jpg
faces/train/leonardo dicaprio/37.jpg
faces/train/scarlett johansson/6.jpg
faces/train/natalie portman/
faces/train/hillary clinton/23.jpg
faces/train/brad pitt/26.jpg
faces/train/jennifer aniston/27.jpg
faces/test/cristiano ronaldo/10.jpg
faces/test/justin timberlake/4.jpg
faces/test/emma watson/3.jpg
faces/train/leonardo dicaprio/54.jpg
faces/train/brad pitt/43.jpg
faces/train/hillary clinton/40.jpg
faces/train/anne hathaway/28.jpg
faces/train/cristiano ronaldo/16.jpg
faces/train/jennifer aniston/44.jpg
faces/train/angelina jolie/47.jpg
faces/train/jennifer aniston/
faces/train/hillary clinton/59.jpg
faces/train/barack obama/12.jpg
faces/test/brad pitt/7.jpg
faces/train/angelina jolie/3.jpg
faces/train/natalie portman/42.jpg
faces/train/justin timberlake/37.jpg
faces/train/anne hathaway/45.jpg
faces/train/brad pitt/60.jpg
faces/train/cristiano ronaldo/33.jpg
faces/test/anne hathaway/14.jpg
faces/train/george clooney/7.jpg
faces/train/emma watson/27.jpg
faces/train/justin timberlake/54.jpg
faces/test/jennifer aniston/1.jpg
faces/train/george clooney/21.jpg
faces/train/cristiano ronaldo/50.jpg
faces/test/cristiano ronaldo/8.jpg
faces/train/barack obama/48.jpg
faces/test/tom cruise/9.jpg
faces/train/johnny depp/10.jpg
faces/test/angelina jolie/8.jpg
faces/train/emma watson/44.jpg
faces/test/leonardo dicaprio/7.jpg
faces/test/emma watson/10.jpg
faces/train/anne hathaway/7.jpg
faces/train/johnny depp/29.jpg
faces/train/tom cruise/38.jpg
faces/train/tom cruise/5.jpg
faces/train/nicole kidman/25.jpg
faces/train/cristiano ronaldo/6.jpg
faces/train/scarlett johansson/38.jpg
faces/train/hillary clinton/2.jpg
faces/train/johnny depp/46.jpg
faces/train/leonardo dicaprio/26.jpg
faces/train/leonardo dicaprio/5.jpg
faces/test/johnny depp/12.jpg
faces/train/brad pitt/15.jpg
faces/train/hillary clinton/12.jpg
faces/train/nicole kidman/42.jpg
faces/train/angelina jolie/19.jpg
faces/train/jennifer aniston/16.jpg
faces/test/nicole kidman/11.jpg
faces/train/leonardo dicaprio/43.jpg
faces/train/jennifer aniston/33.jpg
faces/train/anne hathaway/17.jpg
faces/test/natalie portman/10.jpg
faces/train/angelina jolie/36.jpg
faces/test/scarlett johansson/14.jpg
faces/train/natalie portman/31.jpg
faces/train/angelina jolie/53.jpg
faces/train/justin timberlake/26.jpg
faces/train/anne hathaway/34.jpg
faces/train/nicole kidman/54.jpg
faces/train/jennifer aniston/50.jpg
faces/train/cristiano ronaldo/22.jpg
faces/test/johnny depp/5.jpg
faces/train/emma watson/16.jpg
faces/train/justin timberlake/43.jpg
faces/train/anne hathaway/51.jpg
faces/test/natalie portman/1.jpg
faces/train/george clooney/10.jpg
faces/test/jennifer aniston/12.jpg
faces/train/barack obama/37.jpg
faces/train/george clooney/29.jpg
faces/test/jennifer aniston/9.jpg
faces/train/barack obama/4.jpg
faces/train/emma watson/33.jpg
faces/train/brad pitt/5.jpg
faces/train/justin timberlake/3.jpg
faces/train/johnny depp/18.jpg
faces/train/tom cruise/27.jpg
faces/train/george clooney/46.jpg
faces/train/nicole kidman/14.jpg
faces/train/emma watson/50.jpg
faces/test/tom cruise/14.jpg
faces/train/scarlett johansson/27.jpg
faces/train/leonardo dicaprio/15.jpg
faces/train/scarlett johansson/1.jpg
faces/train/tom cruise/44.jpg
faces/test/anne hathaway/2.jpg
faces/train/nicole kidman/31.jpg
faces/train/natalie portman/27.jpg
faces/train/johnny depp/52.jpg
faces/test/brad pitt/
faces/train/leonardo dicaprio/32.jpg
faces/train/scarlett johansson/44.jpg
faces/train/cristiano ronaldo/
faces/train/brad pitt/21.jpg
faces/train/jennifer aniston/22.jpg
faces/train/angelina jolie/25.jpg
faces/train/hillary clinton/37.jpg
faces/train/justin timberlake/15.jpg
faces/train/anne hathaway/23.jpg
faces/train/cristiano ronaldo/11.jpg
faces/train/angelina jolie/42.jpg
faces/train/johnny depp/7.jpg
faces/train/emma watson/6.jpg
faces/train/jennifer aniston/7.jpg
faces/test/brad pitt/2.jpg
faces/train/hillary clinton/54.jpg
faces/train/brad pitt/57.jpg
faces/train/natalie portman/8.jpg
faces/train/jennifer aniston/58.jpg
faces/train/anne hathaway/40.jpg
faces/test/tom cruise/
faces/train/barack obama/26.jpg
faces/train/natalie portman/56.jpg
faces/train/george clooney/2.jpg
faces/test/natalie portman/9.jpg
faces/train/george clooney/18.jpg
faces/train/cristiano ronaldo/47.jpg
faces/train/emma watson/22.jpg
faces/test/cristiano ronaldo/3.jpg
faces/train/barack obama/43.jpg
faces/train/tom cruise/16.jpg
faces/train/george clooney/35.jpg
faces/test/angelina jolie/3.jpg
faces/test/leonardo dicaprio/2.jpg
faces/train/anne hathaway/2.jpg
faces/train/johnny depp/24.jpg
faces/train/barack obama/60.jpg
faces/test/george clooney/7.jpg
faces/train/scarlett johansson/16.jpg
faces/test/hillary clinton/5.jpg
faces/train/tom cruise/33.jpg
faces/train/nicole kidman/20.jpg
faces/train/cristiano ronaldo/1.jpg
faces/train/johnny depp/41.jpg
faces/train/leonardo dicaprio/21.jpg
faces/train/nicole kidman/39.jpg
faces/train/scarlett johansson/33.jpg
faces/test/cristiano ronaldo/
faces/train/jennifer aniston/11.jpg
faces/train/brad pitt/10.jpg
faces/train/tom cruise/
faces/train/nicole kidman/6.jpg
faces/train/angelina jolie/14.jpg
faces/train/scarlett johansson/9.jpg
faces/train/hillary clinton/26.jpg
faces/train/brad pitt/29.jpg
faces/train/scarlett johansson/50.jpg
faces/test/emma watson/6.jpg
faces/test/justin timberlake/7.jpg
faces/train/anne hathaway/12.jpg
faces/test/angelina jolie/15.jpg
faces/train/angelina jolie/31.jpg
faces/train/leonardo dicaprio/57.jpg
faces/train/justin timberlake/21.jpg
faces/train/hillary clinton/43.jpg
faces/train/brad pitt/46.jpg
faces/train/nicole kidman/
faces/train/jennifer aniston/47.jpg
faces/train/cristiano ronaldo/19.jpg
faces/
faces/train/natalie portman/45.jpg
faces/train/hillary clinton/60.jpg
faces/train/angelina jolie/6.jpg
faces/train/anne hathaway/48.jpg
faces/train/cristiano ronaldo/36.jpg
faces/train/emma watson/11.jpg
faces/train/barack obama/32.jpg
faces/train/justin timberlake/57.jpg
faces/test/jennifer aniston/4.jpg
faces/train/george clooney/24.jpg
faces/train/johnny depp/13.jpg
faces/train/emma watson/47.jpg
faces/train/tom cruise/22.jpg
faces/train/george clooney/41.jpg
faces/train/scarlett johansson/
faces/train/tom cruise/8.jpg
faces/train/johnny depp/30.jpg
faces/train/leonardo dicaprio/10.jpg
faces/train/nicole kidman/28.jpg
faces/train/scarlett johansson/22.jpg
faces/train/cristiano ronaldo/9.jpg
faces/train/brad pitt/
faces/train/hillary clinton/5.jpg
faces/train/johnny depp/49.jpg
faces/train/leonardo dicaprio/8.jpg
faces/train/nicole kidman/45.jpg
faces/train/angelina jolie/20.jpg
faces/train/leonardo dicaprio/46.jpg
faces/test/angelina jolie/
faces/train/angelina jolie/39.jpg
faces/train/hillary clinton/32.jpg
faces/train/brad pitt/35.jpg
faces/test/natalie portman/13.jpg
faces/test/nicole kidman/1.jpg
faces/train/jennifer aniston/36.jpg
faces/train/justin timberlake/10.jpg
faces/train/johnny depp/2.jpg
faces/train/jennifer aniston/53.jpg
faces/train/emma watson/1.jpg
faces/train/jennifer aniston/2.jpg
faces/train/emma watson/
faces/train/angelina jolie/56.jpg
faces/train/justin timberlake/29.jpg
faces/train/anne hathaway/37.jpg
faces/train/nicole kidman/57.jpg
faces/train/natalie portman/3.jpg
faces/train/cristiano ronaldo/25.jpg
faces/test/johnny depp/8.jpg
faces/train/barack obama/21.jpg
faces/train/natalie portman/51.jpg
faces/train/emma watson/19.jpg
faces/train/justin timberlake/46.jpg
faces/train/george clooney/13.jpg
faces/train/cristiano ronaldo/42.jpg
faces/test/jennifer aniston/15.jpg
faces/test/barack obama/10.jpg
faces/train/barack obama/7.jpg
faces/train/emma watson/36.jpg
faces/train/tom cruise/11.jpg
faces/train/brad pitt/8.jpg
faces/test/george clooney/14.jpg
faces/train/george clooney/30.jpg
faces/train/barack obama/57.jpg
faces/train/george clooney/49.jpg
faces/train/scarlett johansson/11.jpg
faces/test/george clooney/2.jpg
faces/train/nicole kidman/34.jpg
faces/train/natalie portman/11.jpg
faces/train/leonardo dicaprio/18.jpg
faces/train/tom cruise/47.jpg
faces/test/barack obama/9.jpg
faces/test/anne hathaway/5.jpg
faces/train/nicole kidman/1.jpg
faces/train/johnny depp/55.jpg
faces/train/scarlett johansson/47.jpg
faces/train/scarlett johansson/4.jpg
faces/train/brad pitt/24.jpg
faces/train/angelina jolie/28.jpg
faces/train/jennifer aniston/25.jpg
faces/test/justin timberlake/2.jpg
faces/test/angelina jolie/10.jpg
faces/test/emma watson/1.jpg
faces/train/leonardo dicaprio/52.jpg
faces/test/nicole kidman/9.jpg
faces/train/justin timberlake/18.jpg
faces/train/anne hathaway/26.jpg
faces/train/emma watson/9.jpg
faces/train/angelina jolie/45.jpg
faces/train/cristiano ronaldo/14.jpg
faces/test/hillary clinton/12.jpg
faces/train/hillary clinton/57.jpg
faces/test/brad pitt/5.jpg
faces/train/barack obama/10.jpg
faces/train/natalie portman/40.jpg
faces/train/justin timberlake/35.jpg
faces/train/anne hathaway/43.jpg
faces/train/george clooney/
faces/train/cristiano ronaldo/31.jpg
faces/train/barack obama/29.jpg
faces/test/anne hathaway/12.jpg
faces/train/natalie portman/59.jpg
faces/train/george clooney/5.jpg
faces/train/emma watson/25.jpg
faces/test/cristiano ronaldo/6.jpg
faces/train/tom cruise/19.jpg
faces/train/george clooney/38.jpg
faces/train/emma watson/42.jpg
faces/test/tom cruise/7.jpg
faces/train/anne hathaway/5.jpg
faces/train/johnny depp/27.jpg
faces/train/tom cruise/3.jpg
faces/train/scarlett johansson/19.jpg
faces/train/tom cruise/36.jpg
faces/train/nicole kidman/23.jpg
faces/train/leonardo dicaprio/3.jpg
faces/train/cristiano ronaldo/4.jpg
faces/train/natalie portman/19.jpg
faces/test/scarlett johansson/6.jpg
faces/train/johnny depp/44.jpg
faces/train/leonardo dicaprio/24.jpg
faces/train/scarlett johansson/36.jpg
faces/train/angelina jolie/17.jpg
faces/train/hillary clinton/10.jpg
faces/train/brad pitt/13.jpg
faces/train/nicole kidman/9.jpg
faces/train/jennifer aniston/14.jpg
faces/train/leonardo dicaprio/41.jpg
faces/train/hillary clinton/29.jpg
faces/test/scarlett johansson/12.jpg
faces/train/brad pitt/30.jpg
faces/test/emma watson/9.jpg
faces/train/jennifer aniston/31.jpg
faces/train/angelina jolie/34.jpg
faces/train/hillary clinton/46.jpg
faces/train/brad pitt/49.jpg
faces/train/anne hathaway/32.jpg
faces/train/nicole kidman/52.jpg
faces/train/cristiano ronaldo/20.jpg
faces/train/barack obama/18.jpg
faces/train/natalie portman/48.jpg
faces/train/angelina jolie/9.jpg
faces/test/johnny depp/3.jpg
faces/train/cristiano ronaldo/39.jpg
faces/train/emma watson/14.jpg
faces/train/justin timberlake/41.jpg
faces/test/jennifer aniston/10.jpg
faces/train/george clooney/27.jpg
faces/train/barack obama/2.jpg
faces/train/emma watson/31.jpg
faces/train/brad pitt/3.jpg
faces/train/justin timberlake/1.jpg
faces/train/johnny depp/16.jpg
faces/train/barack obama/52.jpg
faces/train/tom cruise/25.jpg
faces/train/george clooney/44.jpg
faces/train/nicole kidman/12.jpg
faces/test/tom cruise/12.jpg
faces/train/angelina jolie/
faces/train/johnny depp/33.jpg
faces/train/leonardo dicaprio/13.jpg
faces/train/scarlett johansson/25.jpg
faces/train/tom cruise/42.jpg
faces/test/barack obama/4.jpg
faces/train/barack obama/
faces/train/natalie portman/25.jpg
faces/train/leonardo dicaprio/30.jpg
faces/train/johnny depp/50.jpg
faces/train/hillary clinton/18.jpg
faces/train/nicole kidman/48.jpg
faces/train/scarlett johansson/42.jpg
faces/test/leonardo dicaprio/
faces/train/jennifer aniston/20.jpg
faces/train/angelina jolie/23.jpg
faces/train/leonardo dicaprio/49.jpg
faces/train/hillary clinton/35.jpg
faces/train/brad pitt/38.jpg
faces/test/nicole kidman/4.jpg
faces/train/jennifer aniston/39.jpg
faces/train/justin timberlake/13.jpg
faces/train/anne hathaway/21.jpg
faces/train/angelina jolie/40.jpg
faces/train/johnny depp/5.jpg
faces/train/emma watson/4.jpg
faces/train/natalie portman/37.jpg
faces/train/brad pitt/55.jpg
faces/train/justin timberlake/30.jpg
faces/train/natalie portman/6.jpg
faces/train/jennifer aniston/56.jpg
faces/train/cristiano ronaldo/28.jpg
faces/train/barack obama/24.jpg
faces/train/natalie portman/54.jpg
faces/train/justin timberlake/49.jpg
faces/test/natalie portman/7.jpg
faces/train/george clooney/16.jpg
faces/train/cristiano ronaldo/45.jpg
faces/train/emma watson/20.jpg
faces/test/barack obama/13.jpg
faces/train/barack obama/41.jpg
faces/train/emma watson/39.jpg
faces/train/tom cruise/14.jpg
faces/train/george clooney/33.jpg
faces/test/angelina jolie/1.jpg
faces/test/tom cruise/2.jpg
faces/train/johnny depp/22.jpg
faces/train/tom cruise/31.jpg
faces/train/scarlett johansson/14.jpg
faces/test/george clooney/5.jpg
faces/test/hillary clinton/3.jpg
faces/train/george clooney/50.jpg
faces/train/hillary clinton/
faces/train/natalie portman/14.jpg
faces/test/scarlett johansson/1.jpg
faces/test/anne hathaway/8.jpg
faces/train/scarlett johansson/31.jpg
faces/train/nicole kidman/4.jpg
faces/train/angelina jolie/12.jpg
faces/train/leonardo dicaprio/38.jpg
faces/train/scarlett johansson/7.jpg
faces/train/hillary clinton/24.jpg
faces/test/justin timberlake/15.jpg
faces/train/brad pitt/27.jpg
faces/test/cristiano ronaldo/11.jpg
faces/train/anne hathaway/10.jpg
faces/test/emma watson/4.jpg
faces/train/anne hathaway/29.jpg
faces/train/hillary clinton/41.jpg
faces/train/brad pitt/44.jpg
faces/train/jennifer aniston/45.jpg
faces/train/cristiano ronaldo/17.jpg
faces/test/hillary clinton/15.jpg
faces/train/barack obama/13.jpg
faces/train/natalie portman/43.jpg
faces/train/justin timberlake/38.jpg
faces/train/angelina jolie/4.jpg
faces/train/anne hathaway/46.jpg
faces/train/cristiano ronaldo/34.jpg
faces/train/george clooney/8.jpg
faces/train/emma watson/28.jpg
faces/train/natalie portman/60.jpg
faces/test/johnny depp/
faces/train/justin timberlake/55.jpg
faces/test/jennifer aniston/2.jpg
faces/train/george clooney/22.jpg
faces/train/barack obama/49.jpg
faces/test/cristiano ronaldo/9.jpg
faces/train/johnny depp/11.jpg
faces/train/emma watson/45.jpg
faces/test/leonardo dicaprio/8.jpg
faces/test/angelina jolie/9.jpg
faces/train/tom cruise/20.jpg
faces/train/tom cruise/6.jpg
faces/train/tom cruise/39.jpg
faces/train/nicole kidman/26.jpg
faces/train/scarlett johansson/20.jpg
faces/train/cristiano ronaldo/7.jpg
faces/train/anne hathaway/
faces/test/scarlett johansson/9.jpg
faces/train/scarlett johansson/39.jpg
faces/train/natalie portman/20.jpg
faces/train/leonardo dicaprio/27.jpg
faces/train/leonardo dicaprio/6.jpg
faces/test/johnny depp/13.jpg
faces/train/hillary clinton/13.jpg
faces/train/nicole kidman/43.jpg
faces/train/brad pitt/16.jpg
faces/train/jennifer aniston/17.jpg
faces/test/scarlett johansson/
faces/train/leonardo dicaprio/44.jpg
faces/train/anne hathaway/18.jpg
faces/train/hillary clinton/30.jpg
faces/train/brad pitt/33.jpg
faces/train/angelina jolie/37.jpg
faces/train/jennifer aniston/34.jpg
faces/train/nicole kidman/55.jpg
faces/train/hillary clinton/49.jpg
faces/train/natalie portman/32.jpg
faces/train/justin timberlake/27.jpg
faces/train/anne hathaway/35.jpg
faces/train/jennifer aniston/51.jpg
faces/train/angelina jolie/54.jpg
faces/train/natalie portman/1.jpg
faces/train/cristiano ronaldo/23.jpg
faces/train/emma watson/17.jpg
faces/train/justin timberlake/44.jpg
faces/train/anne hathaway/52.jpg
faces/test/natalie portman/2.jpg
faces/test/george clooney/
faces/train/george clooney/11.jpg
faces/train/cristiano ronaldo/40.jpg
faces/train/barack obama/38.jpg
faces/train/barack obama/5.jpg
faces/train/emma watson/34.jpg
faces/train/brad pitt/6.jpg
faces/train/justin timberlake/4.jpg
faces/train/johnny depp/19.jpg
faces/train/barack obama/55.jpg
faces/test/leonardo dicaprio/10.jpg
faces/train/tom cruise/28.jpg
faces/train/george clooney/47.jpg
faces/train/nicole kidman/15.jpg
faces/train/johnny depp/36.jpg
faces/train/leonardo dicaprio/16.jpg
faces/train/scarlett johansson/28.jpg
faces/test/anne hathaway/
faces/train/tom cruise/45.jpg
faces/train/nicole kidman/32.jpg
faces/test/anne hathaway/3.jpg
faces/train/natalie portman/28.jpg
faces/train/scarlett johansson/45.jpg
faces/train/johnny depp/53.jpg
faces/train/leonardo dicaprio/33.jpg
faces/train/scarlett johansson/2.jpg
faces/train/brad pitt/22.jpg
faces/train/angelina jolie/26.jpg
faces/train/jennifer aniston/23.jpg
faces/test/jennifer aniston/
faces/train/leonardo dicaprio/50.jpg
faces/test/nicole kidman/7.jpg
faces/train/justin timberlake/16.jpg
faces/train/anne hathaway/24.jpg
faces/train/emma watson/7.jpg
faces/train/jennifer aniston/40.jpg
faces/train/angelina jolie/43.jpg
faces/train/cristiano ronaldo/12.jpg
faces/train/jennifer aniston/8.jpg
faces/test/hillary clinton/10.jpg
faces/train/hillary clinton/55.jpg
faces/test/brad pitt/3.jpg
faces/train/brad pitt/58.jpg
faces/train/natalie portman/9.jpg
faces/train/anne hathaway/41.jpg
faces/train/angelina jolie/60.jpg
faces/train/cristiano ronaldo/48.jpg
faces/train/barack obama/27.jpg
faces/train/emma watson/23.jpg
faces/train/george clooney/3.jpg
faces/test/natalie portman/
faces/train/justin timberlake/50.jpg
faces/train/george clooney/19.jpg
faces/train/barack obama/44.jpg
faces/test/cristiano ronaldo/4.jpg
faces/train/tom cruise/17.jpg
faces/test/leonardo dicaprio/3.jpg
faces/train/george clooney/36.jpg
faces/train/emma watson/40.jpg
faces/test/angelina jolie/4.jpg
faces/train/anne hathaway/3.jpg
faces/train/johnny depp/25.jpg
faces/train/scarlett johansson/17.jpg
faces/train/tom cruise/34.jpg
faces/test/hillary clinton/6.jpg
faces/train/tom cruise/1.jpg
faces/train/nicole kidman/21.jpg
faces/test/brad pitt/15.jpg
faces/train/cristiano ronaldo/2.jpg
faces/train/natalie portman/17.jpg
faces/train/scarlett johansson/34.jpg
faces/train/johnny depp/42.jpg
faces/train/leonardo dicaprio/22.jpg
faces/train/leonardo dicaprio/1.jpg
faces/train/tom cruise/51.jpg
faces/test/nicole kidman/
faces/train/nicole kidman/7.jpg
faces/train/jennifer aniston/12.jpg
faces/train/hillary clinton/27.jpg
faces/train/anne hathaway/13.jpg
faces/test/justin timberlake/8.jpg
faces/test/emma watson/7.jpg
faces/test/scarlett johansson/10.jpg
faces/train/angelina jolie/32.jpg
faces/train/hillary clinton/44.jpg
faces/train/brad pitt/47.jpg
faces/train/jennifer aniston/48.jpg
faces/train/anne hathaway/30.jpg
faces/train/barack obama/16.jpg
faces/train/natalie portman/46.jpg
faces/test/johnny depp/1.jpg
faces/train/cristiano ronaldo/37.jpg
faces/train/emma watson/12.jpg
faces/train/barack obama/33.jpg
faces/train/justin timberlake/58.jpg
faces/test/hillary clinton/
faces/train/george clooney/25.jpg
faces/train/brad pitt/1.jpg
faces/train/tom cruise/9.jpg
faces/train/barack obama/50.jpg
faces/train/emma watson/48.jpg
faces/train/tom cruise/23.jpg
faces/train/george clooney/42.jpg
faces/train/nicole kidman/10.jpg
faces/train/nicole kidman/29.jpg
faces/train/johnny depp/31.jpg
faces/train/leonardo dicaprio/11.jpg
faces/train/scarlett johansson/23.jpg
faces/train/tom cruise/40.jpg
faces/test/barack obama/2.jpg
faces/train/natalie portman/23.jpg
faces/train/leonardo dicaprio/9.jpg
faces/train/brad pitt/19.jpg
faces/train/hillary clinton/16.jpg
faces/train/nicole kidman/46.jpg
faces/train/scarlett johansson/40.jpg
faces/test/nicole kidman/15.jpg
faces/train/angelina jolie/21.jpg
faces/train/leonardo dicaprio/47.jpg
faces/train/hillary clinton/33.jpg
faces/train/brad pitt/36.jpg
faces/test/natalie portman/14.jpg
faces/test/nicole kidman/2.jpg
faces/train/angelina jolie/57.jpg
faces/train/justin timberlake/11.jpg
faces/train/johnny depp/3.jpg
faces/train/emma watson/2.jpg
faces/train/jennifer aniston/3.jpg
faces/train/anne hathaway/38.jpg
faces/train/hillary clinton/50.jpg
faces/train/brad pitt/53.jpg
faces/train/natalie portman/4.jpg
faces/train/jennifer aniston/54.jpg
faces/train/cristiano ronaldo/26.jpg
faces/test/johnny depp/9.jpg
faces/train/barack obama/22.jpg
faces/train/natalie portman/52.jpg
faces/train/justin timberlake/47.jpg
faces/train/anne hathaway/55.jpg
faces/test/natalie portman/5.jpg
faces/train/george clooney/14.jpg
faces/train/cristiano ronaldo/43.jpg
faces/test/barack obama/11.jpg
faces/train/emma watson/37.jpg
faces/train/tom cruise/12.jpg
faces/train/brad pitt/9.jpg
faces/train/justin timberlake/7.jpg
faces/train/george clooney/31.jpg
faces/train/barack obama/58.jpg
faces/train/johnny depp/20.jpg
faces/train/nicole kidman/18.jpg
faces/train/scarlett johansson/12.jpg
faces/test/george clooney/3.jpg
faces/test/brad pitt/10.jpg

from utils import load_dataset

X_train, y_train, classes_train = load_dataset('faces', train=True, as_gray=True)
X_test, y_test, classes_test = load_dataset('faces', train=False, as_gray=True)

assert classes_train == classes_test
classes = classes_train

print('Class names:', classes)
print('Training data shape:', X_train.shape)
print('Training labels shape: ', y_train.shape)
print('Test data shape:', X_test.shape)
print('Test labels shape: ', y_test.shape)

Class names: ['angelina jolie', 'anne hathaway', 'barack obama', 'brad pitt', 'cristiano ronaldo', 'emma watson', 'george clooney', 'hillary clinton', 'jennifer aniston', 'johnny depp', 'justin timberlake', 'leonardo dicaprio', 'natalie portman', 'nicole kidman', 'scarlett johansson', 'tom cruise']
Training data shape: (800, 64, 64)
Training labels shape:  (800,)
Test data shape: (160, 64, 64)
Test labels shape:  (160,)

# Visualize some examples from the dataset.
# We show a few examples of training images from each class.
num_classes = len(classes)
samples_per_class = 10
for y, cls in enumerate(classes):
idxs = np.flatnonzero(y_train == y)
idxs = np.random.choice(idxs, samples_per_class, replace=False)
for i, idx in enumerate(idxs):
plt_idx = i * num_classes + y + 1
plt.subplot(samples_per_class, num_classes, plt_idx)
plt.imshow(X_train[idx])
plt.axis('off')
if i == 0:
plt.title(y)
plt.show()

# Flatten the image data into rows
# we now have one 4096 dimensional featue vector for each example
X_train = np.reshape(X_train, (X_train.shape[0], -1))
X_test = np.reshape(X_test, (X_test.shape[0], -1))
print("Training data shape:", X_train.shape)
print("Test data shape:", X_test.shape)

Training data shape: (800, 4096)
Test data shape: (160, 4096)


スポンサーリンク
スポンサーリンク

フォローする