text_plain_output_5.png application_vnd.jupyter.stderr_output_2.png text_plain_output_9.png application_vnd.jupyter.stderr_output_4.png application_vnd.jupyter.stderr_output_6.png application_vnd.jupyter.stderr_output_12.png application_vnd.jupyter.stderr_output_8.png application_vnd.jupyter.stderr_output_10.png text_plain_output_3.png text_plain_output_7.png text_plain_output_1.png text_plain_output_11.png
from skimage import io

from torchvision import datasets, transforms

import os

import pandas as pd

def fetch_dataset(path, attrs_name='lfw_attributes.txt', images_name='lfw-deepfunneled', dx=80, dy=80, dimx=64, dimy=64):
    if not os.path.exists(images_name):
        os.system('wget http://vis-www.cs.umass.edu/lfw/lfw-deepfunneled.tgz -O tmp.tgz')
        os.system('tar xvzf tmp.tgz && rm tmp.tgz')
        assert os.path.exists(images_name)
    if not os.path.exists(attrs_name):
        os.system('wget http://www.cs.columbia.edu/CAVE/databases/pubfig/download/%s' % attrs_name)
    transform = transforms.Compose([transforms.ToPILImage(), transforms.CenterCrop((dx, dy)), transforms.Resize((dimx, dimy)), transforms.ToTensor()])
    df_attrs = pd.read_csv(os.path.join(path, attrs_name), sep='\t', skiprows=1)
    df_attrs = pd.DataFrame(df_attrs.iloc[:, :-1].values, columns=df_attrs.columns[1:])
    photo_ids = []
    for dirpath, dirnames, filenames in os.walk(os.path.join(path, images_name)):
        for fname in filenames:
            if fname.endswith('.jpg'):
                fpath = os.path.join(dirpath, fname)
                photo_id = fname[:-4].replace('_', ' ').split()
                person_id = ' '.join(photo_id[:-1])
                photo_number = int(photo_id[-1])
                photo_ids.append({'person': person_id, 'imagenum': photo_number, 'photo_path': fpath})
    photo_ids = pd.DataFrame(photo_ids)
    df = pd.merge(df_attrs, photo_ids, on=('person', 'imagenum'))
    assert len(df) == len(df_attrs), 'lost some data when merging dataframes'
    all_photos = df['photo_path'].apply(io.imread).apply(transform)
    all_photos = all_photos.values
    all_attrs = df.drop(['photo_path', 'person', 'imagenum'], axis=1)
    return (all_photos, all_attrs)

img_size = 64
path = os.path.abspath('')
data, attrs = fetch_dataset(path=path, dimx=img_size, dimy=img_size)