In [1]:
%matplotlib inline
import matplotlib
import numpy as np
import pandas as pd
import scipy.io
import matplotlib.pyplot as plt
from IPython.display import Image, display
In [2]:
import h2o
from h2o.estimators.deeplearning import H2OAutoEncoderEstimator
h2o.init()
In [3]:
!wget -c http://www.cl.cam.ac.uk/Research/DTG/attarchive/pub/data/att_faces.tar.Z
In [4]:
!tar xzvf att_faces.tar.Z;rm att_faces.tar.Z;
We now need some code to read pgm files. Thanks to StackOverflow we have some code to leverage:
In [5]:
import re
def read_pgm(filename, byteorder='>'):
"""Return image data from a raw PGM file as numpy array.
Format specification: http://netpbm.sourceforge.net/doc/pgm.html
"""
with open(filename, 'rb') as f:
buffer = f.read()
try:
header, width, height, maxval = re.search(
b"(^P5\s(?:\s*#.*[\r\n])*"
b"(\d+)\s(?:\s*#.*[\r\n])*"
b"(\d+)\s(?:\s*#.*[\r\n])*"
b"(\d+)\s(?:\s*#.*[\r\n]\s)*)", buffer).groups()
except AttributeError:
raise ValueError("Not a raw PGM file: '%s'" % filename)
return np.frombuffer(buffer,
dtype='u1' if int(maxval) < 256 else byteorder+'u2',
count=int(width)*int(height),
offset=len(header)
).reshape((int(height), int(width)))
In [6]:
image = read_pgm("orl_faces/s12/6.pgm", byteorder='<')
In [7]:
image.shape
Out[7]:
In [8]:
plt.imshow(image, plt.cm.gray)
plt.show()
In [9]:
import glob
import os
from collections import defaultdict
In [10]:
images = glob.glob("orl_faces/**/*.pgm")
data = defaultdict(list)
image_data = []
for img in images:
_,label,_ = img.split(os.path.sep)
imgdata = read_pgm(img, byteorder='<').flatten().tolist()
data[label].append(imgdata)
image_data.append(imgdata)
Let's import it to H2O
In [11]:
faces = h2o.H2OFrame(image_data)
In [12]:
faces.shape
Out[12]:
In [13]:
from h2o.estimators.deeplearning import H2OAutoEncoderEstimator
In [15]:
model = H2OAutoEncoderEstimator(
activation="Tanh",
hidden=[50],
l1=1e-4,
epochs=10
)
model.train(x=faces.names, training_frame=faces)
In [16]:
model
Out[16]:
Now that we have our model trained, we would like to understand better what is the internal representation of this model? What makes a face a .. face?
We will provide to the model some gaussian noise and see what is the results.
We star by creating some gaussian noise:
In [17]:
import pandas as pd
In [18]:
gaussian_noise = np.random.randn(10304)
In [19]:
plt.imshow(gaussian_noise.reshape(112, 92), plt.cm.gray);
Then we import this data inside H2O. We have to first map the columns to the gaussian data.
In [20]:
gaussian_noise_pre = dict(zip(faces.names,gaussian_noise))
In [21]:
gaussian_noise_hf = h2o.H2OFrame.from_python(gaussian_noise_pre)
In [22]:
result = model.predict(gaussian_noise_hf)
In [28]:
result.shape
Out[28]:
In [29]:
img = result.as_data_frame()
In [30]:
img_data = img.T.values.reshape(112, 92)
In [31]:
plt.imshow(img_data, plt.cm.gray);