<s> import time <EOL> import sys <EOL> import os <EOL> from PIL import Image <EOL> import numpy as np <EOL> import lasagne as nn <EOL> import theano <EOL> import theano . tensor as T <EOL> import h5py <EOL> from fuel . datasets . hdf5 import H5PYDataset <EOL> from fuel . schemes import ShuffledScheme , SequentialScheme <EOL> from fuel . streams import DataStream <EOL> def train_with_hdf5 ( data , num_epochs , train_fn , test_fn , <EOL> tr_transform = lambda x : x , <EOL> te_transform = lambda x : x , <EOL> verbose = True , train_shuffle = True , <EOL> save_best_params_to = None , <EOL> last_layer = None , <EOL> max_per_epoch = - <NUM_LIT:1> ) : <EOL> tr_stream = data . streamer ( training = True , shuffled = train_shuffle ) <EOL> te_stream = data . streamer ( training = False , shuffled = False ) <EOL> from tqdm import tqdm <EOL> ret = [ ] <EOL> mve_params = None <EOL> mve = None <EOL> best_epoch = <NUM_LIT:0> ; <EOL> for epoch in range ( num_epochs ) : <EOL> start = time . time ( ) <EOL> tr_err , tr_batches = <NUM_LIT:0> , <NUM_LIT:0> <EOL> for imb in tqdm ( tr_stream . get_epoch_iterator ( ) , total = data . ntrain / data . batch_size ) : <EOL> if imb [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] != data . batch_size : <EOL> continue <EOL> imb = tr_transform ( imb ) <EOL> if not isinstance ( imb , tuple ) : <EOL> imb = ( imb , ) <EOL> tr_err += train_fn ( * imb ) <EOL> tr_batches += <NUM_LIT:1> <EOL> if max_per_epoch > <NUM_LIT:0> and tr_batches > max_per_epoch : <EOL> break <EOL> tr_err /= tr_batches <EOL> val_err , val_batches = <NUM_LIT:0> , <NUM_LIT:0> <EOL> if te_stream is not None : <EOL> for imb in tqdm ( te_stream . get_epoch_iterator ( ) , total = data . ntest / data . batch_size ) : <EOL> if imb [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] != data . batch_size : <EOL> continue <EOL> imb = te_transform ( imb ) <EOL> if not isinstance ( imb , tuple ) : <EOL> imb = ( imb , ) <EOL> val_err += test_fn ( * imb ) <EOL> val_batches += <NUM_LIT:1> <EOL> if max_per_epoch > <NUM_LIT:0> and val_batches > max_per_epoch : <EOL> break <EOL> val_err /= val_batches <EOL> if save_best_params_to is not None : <EOL> if mve is None or val_err < mve : <EOL> mve = val_err <EOL> mve_params = [ np . copy ( p ) for p in ( nn . layers . get_all_param_values ( last_layer ) ) ] <EOL> best_epoch = epoch ; <EOL> if verbose : <EOL> print ( '<STR_LIT>' . format ( <EOL> epoch , num_epochs , tr_err , val_err , time . time ( ) - start ) ) <EOL> ret . append ( ( tr_err , val_err ) ) <EOL> if mve_params is None : <EOL> best_epoch = epoch ; <EOL> mve_params = [ np . copy ( p ) for p in ( nn . layers . get_all_param_values ( last_layer ) ) ] <EOL> if save_best_params_to is not None : <EOL> print ( "<STR_LIT>" . format ( best_epoch ) ) ; <EOL> save_params ( mve_params , save_best_params_to ) <EOL> return ret , best_epoch <EOL> def raw_to_floatX ( imb , pixel_shift = <NUM_LIT:0.5> , square = True , center = False , rng = None ) : <EOL> rng = rng if rng else np . random <EOL> w , h = imb . shape [ <NUM_LIT:2> ] , imb . shape [ <NUM_LIT:3> ] <EOL> x , y = <NUM_LIT:0> , <NUM_LIT:0> <EOL> if square : <EOL> if w > h : <EOL> if center : <EOL> x = ( w - h ) / <NUM_LIT:2> <EOL> else : <EOL> x = rng . randint ( w - h ) <EOL> w = h <EOL> elif h > w : <EOL> if center : <EOL> y = ( h - w ) / <NUM_LIT:2> <EOL> else : <EOL> y = rng . randint ( h - w ) <EOL> h = w <EOL> return nn . utils . floatX ( imb ) [ : , : , x : x + w , y : y + h ] / <NUM_LIT> - pixel_shift <EOL> class DataH5PyStreamer : <EOL> def __init__ ( self , h5filename , ntrain = None , ntest = None , batch_size = <NUM_LIT:1> , folds = None ) : <EOL> if folds is None : <EOL> te_sets = ( '<STR_LIT:test>' , ) <EOL> tr_sets = ( '<STR_LIT:train>' , ) <EOL> else : <EOL> notest = ( folds [ <NUM_LIT:0> ] == folds [ <NUM_LIT:1> ] ) ; <EOL> te_sets = ( ) if notest else ( '<STR_LIT>' . format ( folds [ <NUM_LIT:1> ] ) , ) <EOL> tr_sets = tuple ( [ '<STR_LIT>' . format ( i ) for i in range ( folds [ <NUM_LIT:0> ] ) if i != folds [ <NUM_LIT:1> ] ] ) <EOL> self . batch_size = batch_size <EOL> self . tr_data = H5PYDataset ( h5filename , which_sets = tr_sets ) <EOL> self . te_data = None if notest else H5PYDataset ( h5filename , which_sets = te_sets ) <EOL> self . ntrain = ntrain or self . tr_data . num_examples <EOL> self . ntest = ntest or self . te_data . num_examples if self . te_data else <NUM_LIT:0> <EOL> def dataset ( self , training = True ) : <EOL> return self . tr_data if training else self . te_data <EOL> def streamer ( self , training = True , shuffled = False ) : <EOL> n = self . ntrain if training else self . ntest <EOL> if n == <NUM_LIT:0> : <EOL> return None ; <EOL> func = ShuffledScheme if shuffled else SequentialScheme <EOL> sch = func ( examples = n , batch_size = self . batch_size ) <EOL> data = self . tr_data if training else self . te_data <EOL> return DataStream ( data , iteration_scheme = sch ) <EOL> def log_likelihood ( tgt , mu , ls ) : <EOL> return T . sum ( - ( np . float32 ( <NUM_LIT:0.5> * np . log ( <NUM_LIT:2> * np . pi ) ) + ls ) <EOL> - <NUM_LIT:0.5> * T . sqr ( tgt - mu ) / T . exp ( <NUM_LIT:2> * ls ) ) <EOL> def get_picture_array ( X , index , shift = <NUM_LIT:0.5> ) : <EOL> ch , w , h = X . shape [ <NUM_LIT:1> ] , X . shape [ <NUM_LIT:2> ] , X . shape [ <NUM_LIT:3> ] <EOL> ret = ( ( X [ index ] + shift ) * <NUM_LIT> ) . reshape ( ch , w , h ) . transpose ( <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:0> ) . clip ( <NUM_LIT:0> , <NUM_LIT:255> ) . astype ( np . uint8 ) <EOL> if ch == <NUM_LIT:1> : <EOL> ret = ret . reshape ( h , w ) <EOL> return ret <EOL> def get_image_pair ( X , Xpr , index = - <NUM_LIT:1> , shift = <NUM_LIT:0.5> ) : <EOL> mode = '<STR_LIT>' if X . shape [ <NUM_LIT:1> ] == <NUM_LIT:3> else '<STR_LIT:L>' <EOL> index = np . random . randint ( X . shape [ <NUM_LIT:0> ] ) if index == - <NUM_LIT:1> else index <EOL> original_image = Image . fromarray ( get_picture_array ( X , index , shift = shift ) , mode = mode ) <EOL> new_size = ( original_image . size [ <NUM_LIT:0> ] , original_image . size [ <NUM_LIT:1> ] * <NUM_LIT:2> ) <EOL> new_im = Image . new ( mode , new_size ) <EOL> new_im . paste ( original_image , ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> rec_image = Image . fromarray ( get_picture_array ( Xpr , index , shift = shift ) , mode = mode ) <EOL> new_im . paste ( rec_image , ( <NUM_LIT:0> , original_image . size [ <NUM_LIT:1> ] ) ) <EOL> return new_im <EOL> def arr_from_img_storage ( im ) : <EOL> w , h = im . size <EOL> arr = np . asarray ( im . getdata ( ) , dtype = np . uint8 ) <EOL> c = np . product ( arr . size ) / ( w * h ) <EOL> return arr . reshape ( h , w , c ) . transpose ( <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> def arr_from_img ( im , shift = <NUM_LIT:0.5> ) : <EOL> w , h = im . size <EOL> arr = np . asarray ( im . getdata ( ) , dtype = theano . config . floatX ) <EOL> c = np . product ( arr . size ) / ( w * h ) <EOL> return arr . reshape ( ( h , w , c ) ) . transpose ( <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:0> ) / <NUM_LIT> - shift <EOL> def load_params ( model , fn ) : <EOL> if '<STR_LIT>' in fn : <EOL> with np . load ( fn ) as f : <EOL> param_values = [ f [ '<STR_LIT>' % i ] for i in range ( len ( f . files ) ) ] <EOL> nn . layers . set_all_param_values ( model , param_values ) <EOL> else : <EOL> with open ( fn , '<STR_LIT:r>' ) as re : <EOL> import pickle <EOL> nn . layers . set_all_param_values ( model , pickle . load ( re ) ) <EOL> def save_params ( model , fn ) : <EOL> if '<STR_LIT>' in fn : <EOL> param_vals = model if isinstance ( model , list ) else nn . layers . get_all_param_values ( model ) <EOL> np . savez ( fn , * param_vals ) <EOL> else : <EOL> with open ( fn , '<STR_LIT:w>' ) as wr : <EOL> import pickle <EOL> pickle . dump ( param_vals , wr ) <EOL> def reset_accs ( updates , params ) : <EOL> for key in updates : <EOL> if not key in params : <EOL> v = key . get_value ( borrow = True ) <EOL> key . set_value ( np . zeros ( v . shape , dtype = v . dtype ) ) <EOL> def build_vae_loss ( input_var , l_z_mu , l_z_ls , l_x_mu_list , l_x_ls_list , l_x_list , l_x , <EOL> deterministic , binary , L ) : <EOL> layer_outputs = nn . layers . get_output ( [ l_z_mu , l_z_ls ] + l_x_mu_list + l_x_ls_list <EOL> + l_x_list + [ l_x ] , deterministic = deterministic ) <EOL> z_mu = layer_outputs [ <NUM_LIT:0> ] <EOL> z_ls = layer_outputs [ <NUM_LIT:1> ] <EOL> x_mu = [ ] if binary else layer_outputs [ <NUM_LIT:2> : <NUM_LIT:2> + L ] <EOL> x_ls = [ ] if binary else layer_outputs [ <NUM_LIT:2> + L : <NUM_LIT:2> + <NUM_LIT:2> * L ] <EOL> x_list = layer_outputs [ <NUM_LIT:2> : <NUM_LIT:2> + L ] if binary else layer_outputs [ <NUM_LIT:2> + <NUM_LIT:2> * L : <NUM_LIT:2> + <NUM_LIT:3> * L ] <EOL> x = layer_outputs [ - <NUM_LIT:1> ] <EOL> kl_div = <NUM_LIT:0.5> * T . sum ( <NUM_LIT:1> + <NUM_LIT:2> * z_ls - T . sqr ( z_mu ) - T . exp ( <NUM_LIT:2> * z_ls ) ) <EOL> if binary : <EOL> logpxz = sum ( nn . objectives . binary_crossentropy ( x , input_var ) . sum ( ) <EOL> for x in x_list ) * ( - <NUM_LIT:1.> / L ) <EOL> prediction = x_list [ <NUM_LIT:0> ] if deterministic else x <EOL> else : <EOL> logpxz = sum ( log_likelihood ( input_var . flatten ( <NUM_LIT:2> ) , mu , ls ) <EOL> for mu , ls in zip ( x_mu , x_ls ) ) / L <EOL> prediction = x_mu [ <NUM_LIT:0> ] if deterministic else T . sum ( x_mu , axis = <NUM_LIT:0> ) / L <EOL> loss = - <NUM_LIT:1> * ( logpxz + kl_div ) <EOL> return loss , prediction </s>
