<s> """<STR_LIT>""" <EOL> from os import listdir , makedirs , remove <EOL> from os . path import join , exists , isdir <EOL> from sklearn . utils import deprecated <EOL> import logging <EOL> import numpy as np <EOL> try : <EOL> import urllib . request as urllib <EOL> except ImportError : <EOL> import urllib <EOL> from . base import get_data_home , Bunch <EOL> from . . externals . joblib import Memory <EOL> from . . externals . six import b <EOL> logger = logging . getLogger ( __name__ ) <EOL> BASE_URL = "<STR_LIT>" <EOL> ARCHIVE_NAME = "<STR_LIT>" <EOL> FUNNELED_ARCHIVE_NAME = "<STR_LIT>" <EOL> TARGET_FILENAMES = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> def scale_face ( face ) : <EOL> """<STR_LIT>""" <EOL> scaled = face - face . min ( ) <EOL> scaled /= scaled . max ( ) <EOL> return scaled <EOL> def check_fetch_lfw ( data_home = None , funneled = True , download_if_missing = True ) : <EOL> """<STR_LIT>""" <EOL> data_home = get_data_home ( data_home = data_home ) <EOL> lfw_home = join ( data_home , "<STR_LIT>" ) <EOL> if funneled : <EOL> archive_path = join ( lfw_home , FUNNELED_ARCHIVE_NAME ) <EOL> data_folder_path = join ( lfw_home , "<STR_LIT>" ) <EOL> archive_url = BASE_URL + FUNNELED_ARCHIVE_NAME <EOL> else : <EOL> archive_path = join ( lfw_home , ARCHIVE_NAME ) <EOL> data_folder_path = join ( lfw_home , "<STR_LIT>" ) <EOL> archive_url = BASE_URL + ARCHIVE_NAME <EOL> if not exists ( lfw_home ) : <EOL> makedirs ( lfw_home ) <EOL> for target_filename in TARGET_FILENAMES : <EOL> target_filepath = join ( lfw_home , target_filename ) <EOL> if not exists ( target_filepath ) : <EOL> if download_if_missing : <EOL> url = BASE_URL + target_filename <EOL> logger . warning ( "<STR_LIT>" , url ) <EOL> urllib . urlretrieve ( url , target_filepath ) <EOL> else : <EOL> raise IOError ( "<STR_LIT>" % target_filepath ) <EOL> if not exists ( data_folder_path ) : <EOL> if not exists ( archive_path ) : <EOL> if download_if_missing : <EOL> logger . warning ( "<STR_LIT>" , <EOL> archive_url ) <EOL> urllib . urlretrieve ( archive_url , archive_path ) <EOL> else : <EOL> raise IOError ( "<STR_LIT>" % target_filepath ) <EOL> import tarfile <EOL> logger . info ( "<STR_LIT>" , data_folder_path ) <EOL> tarfile . open ( archive_path , "<STR_LIT>" ) . extractall ( path = lfw_home ) <EOL> remove ( archive_path ) <EOL> return lfw_home , data_folder_path <EOL> def _load_imgs ( file_paths , slice_ , color , resize ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> try : <EOL> from scipy . misc import imread <EOL> except ImportError : <EOL> from scipy . misc . pilutil import imread <EOL> from scipy . misc import imresize <EOL> except ImportError : <EOL> raise ImportError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> default_slice = ( slice ( <NUM_LIT:0> , <NUM_LIT> ) , slice ( <NUM_LIT:0> , <NUM_LIT> ) ) <EOL> if slice_ is None : <EOL> slice_ = default_slice <EOL> else : <EOL> slice_ = tuple ( s or ds for s , ds in zip ( slice_ , default_slice ) ) <EOL> h_slice , w_slice = slice_ <EOL> h = ( h_slice . stop - h_slice . start ) // ( h_slice . step or <NUM_LIT:1> ) <EOL> w = ( w_slice . stop - w_slice . start ) // ( w_slice . step or <NUM_LIT:1> ) <EOL> if resize is not None : <EOL> resize = float ( resize ) <EOL> h = int ( resize * h ) <EOL> w = int ( resize * w ) <EOL> n_faces = len ( file_paths ) <EOL> if not color : <EOL> faces = np . zeros ( ( n_faces , h , w ) , dtype = np . float32 ) <EOL> else : <EOL> faces = np . zeros ( ( n_faces , h , w , <NUM_LIT:3> ) , dtype = np . float32 ) <EOL> for i , file_path in enumerate ( file_paths ) : <EOL> if i % <NUM_LIT:1000> == <NUM_LIT:0> : <EOL> logger . info ( "<STR_LIT>" , i + <NUM_LIT:1> , n_faces ) <EOL> img = imread ( file_path ) <EOL> if img . ndim is <NUM_LIT:0> : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % file_path ) <EOL> face = np . asarray ( img [ slice_ ] , dtype = np . float32 ) <EOL> face /= <NUM_LIT> <EOL> if resize is not None : <EOL> face = imresize ( face , resize ) <EOL> if not color : <EOL> face = face . mean ( axis = <NUM_LIT:2> ) <EOL> faces [ i , ... ] = face <EOL> return faces <EOL> def _fetch_lfw_people ( data_folder_path , slice_ = None , color = False , resize = None , <EOL> min_faces_per_person = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> person_names , file_paths = [ ] , [ ] <EOL> for person_name in sorted ( listdir ( data_folder_path ) ) : <EOL> folder_path = join ( data_folder_path , person_name ) <EOL> if not isdir ( folder_path ) : <EOL> continue <EOL> paths = [ join ( folder_path , f ) for f in listdir ( folder_path ) ] <EOL> n_pictures = len ( paths ) <EOL> if n_pictures >= min_faces_per_person : <EOL> person_name = person_name . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) <EOL> person_names . extend ( [ person_name ] * n_pictures ) <EOL> file_paths . extend ( paths ) <EOL> n_faces = len ( file_paths ) <EOL> if n_faces == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> min_faces_per_person ) <EOL> target_names = np . unique ( person_names ) <EOL> target = np . searchsorted ( target_names , person_names ) <EOL> faces = _load_imgs ( file_paths , slice_ , color , resize ) <EOL> indices = np . arange ( n_faces ) <EOL> np . random . RandomState ( <NUM_LIT> ) . shuffle ( indices ) <EOL> faces , target = faces [ indices ] , target [ indices ] <EOL> return faces , target , target_names <EOL> def fetch_lfw_people ( data_home = None , funneled = True , resize = <NUM_LIT:0.5> , <EOL> min_faces_per_person = <NUM_LIT:0> , color = False , <EOL> slice_ = ( slice ( <NUM_LIT> , <NUM_LIT> ) , slice ( <NUM_LIT> , <NUM_LIT> ) ) , <EOL> download_if_missing = True ) : <EOL> """<STR_LIT>""" <EOL> lfw_home , data_folder_path = check_fetch_lfw ( <EOL> data_home = data_home , funneled = funneled , <EOL> download_if_missing = download_if_missing ) <EOL> logger . info ( '<STR_LIT>' , lfw_home ) <EOL> m = Memory ( cachedir = lfw_home , compress = <NUM_LIT:6> , verbose = <NUM_LIT:0> ) <EOL> load_func = m . cache ( _fetch_lfw_people ) <EOL> faces , target , target_names = load_func ( <EOL> data_folder_path , resize = resize , <EOL> min_faces_per_person = min_faces_per_person , color = color , slice_ = slice_ ) <EOL> return Bunch ( data = faces . reshape ( len ( faces ) , - <NUM_LIT:1> ) , images = faces , <EOL> target = target , target_names = target_names , <EOL> DESCR = "<STR_LIT>" ) <EOL> def _fetch_lfw_pairs ( index_file_path , data_folder_path , slice_ = None , <EOL> color = False , resize = None ) : <EOL> """<STR_LIT>""" <EOL> with open ( index_file_path , '<STR_LIT:rb>' ) as index_file : <EOL> split_lines = [ ln . strip ( ) . split ( b ( '<STR_LIT:\t>' ) ) for ln in index_file ] <EOL> pair_specs = [ sl for sl in split_lines if len ( sl ) > <NUM_LIT:2> ] <EOL> n_pairs = len ( pair_specs ) <EOL> target = np . zeros ( n_pairs , dtype = np . int ) <EOL> file_paths = list ( ) <EOL> for i , components in enumerate ( pair_specs ) : <EOL> if len ( components ) == <NUM_LIT:3> : <EOL> target [ i ] = <NUM_LIT:1> <EOL> pair = ( <EOL> ( components [ <NUM_LIT:0> ] , int ( components [ <NUM_LIT:1> ] ) - <NUM_LIT:1> ) , <EOL> ( components [ <NUM_LIT:0> ] , int ( components [ <NUM_LIT:2> ] ) - <NUM_LIT:1> ) , <EOL> ) <EOL> elif len ( components ) == <NUM_LIT:4> : <EOL> target [ i ] = <NUM_LIT:0> <EOL> pair = ( <EOL> ( components [ <NUM_LIT:0> ] , int ( components [ <NUM_LIT:1> ] ) - <NUM_LIT:1> ) , <EOL> ( components [ <NUM_LIT:2> ] , int ( components [ <NUM_LIT:3> ] ) - <NUM_LIT:1> ) , <EOL> ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % ( i + <NUM_LIT:1> , components ) ) <EOL> for j , ( name , idx ) in enumerate ( pair ) : <EOL> try : <EOL> person_folder = join ( data_folder_path , name ) <EOL> except TypeError : <EOL> person_folder = join ( data_folder_path , str ( name , '<STR_LIT>' ) ) <EOL> filenames = list ( sorted ( listdir ( person_folder ) ) ) <EOL> file_path = join ( person_folder , filenames [ idx ] ) <EOL> file_paths . append ( file_path ) <EOL> pairs = _load_imgs ( file_paths , slice_ , color , resize ) <EOL> shape = list ( pairs . shape ) <EOL> n_faces = shape . pop ( <NUM_LIT:0> ) <EOL> shape . insert ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> shape . insert ( <NUM_LIT:0> , n_faces // <NUM_LIT:2> ) <EOL> pairs . shape = shape <EOL> return pairs , target , np . array ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> @ deprecated ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def load_lfw_people ( download_if_missing = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return fetch_lfw_people ( download_if_missing = download_if_missing , ** kwargs ) <EOL> def fetch_lfw_pairs ( subset = '<STR_LIT:train>' , data_home = None , funneled = True , resize = <NUM_LIT:0.5> , <EOL> color = False , slice_ = ( slice ( <NUM_LIT> , <NUM_LIT> ) , slice ( <NUM_LIT> , <NUM_LIT> ) ) , <EOL> download_if_missing = True ) : <EOL> """<STR_LIT>""" <EOL> lfw_home , data_folder_path = check_fetch_lfw ( <EOL> data_home = data_home , funneled = funneled , <EOL> download_if_missing = download_if_missing ) <EOL> logger . info ( '<STR_LIT>' , subset , lfw_home ) <EOL> m = Memory ( cachedir = lfw_home , compress = <NUM_LIT:6> , verbose = <NUM_LIT:0> ) <EOL> load_func = m . cache ( _fetch_lfw_pairs ) <EOL> label_filenames = { <EOL> '<STR_LIT:train>' : '<STR_LIT>' , <EOL> '<STR_LIT:test>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> if subset not in label_filenames : <EOL> raise ValueError ( "<STR_LIT>" % ( <EOL> subset , list ( sorted ( label_filenames . keys ( ) ) ) ) ) <EOL> index_file_path = join ( lfw_home , label_filenames [ subset ] ) <EOL> pairs , target , target_names = load_func ( <EOL> index_file_path , data_folder_path , resize = resize , color = color , <EOL> slice_ = slice_ ) <EOL> return Bunch ( data = pairs . reshape ( len ( pairs ) , - <NUM_LIT:1> ) , pairs = pairs , <EOL> target = target , target_names = target_names , <EOL> DESCR = "<STR_LIT>" % subset ) <EOL> @ deprecated ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def load_lfw_pairs ( download_if_missing = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return fetch_lfw_pairs ( download_if_missing = download_if_missing , ** kwargs ) </s>
