<s> import sys <EOL> import numpy as np <EOL> import theano <EOL> import theano . tensor as T <EOL> import lasagne as nn <EOL> import time <EOL> import os <EOL> import string <EOL> import importlib <EOL> import utils <EOL> import nn_plankton <EOL> if not ( <NUM_LIT:3> <= len ( sys . argv ) <= <NUM_LIT:5> ) : <EOL> sys . exit ( "<STR_LIT>" ) <EOL> config_name = sys . argv [ <NUM_LIT:1> ] <EOL> metadata_path = sys . argv [ <NUM_LIT:2> ] <EOL> if len ( sys . argv ) >= <NUM_LIT:4> : <EOL> subset = sys . argv [ <NUM_LIT:3> ] <EOL> else : <EOL> print "<STR_LIT>" <EOL> subset = "<STR_LIT:test>" <EOL> if len ( sys . argv ) >= <NUM_LIT:5> : <EOL> avg_method = sys . argv [ <NUM_LIT:4> ] <EOL> supported_methods = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> if avg_method not in supported_methods : <EOL> sys . exit ( "<STR_LIT>" % ( avg_method , supported_methods . join ( "<STR_LIT:U+002C>" ) ) ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> avg_method = "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> metadata = np . load ( metadata_path ) <EOL> param_values = metadata [ '<STR_LIT>' ] <EOL> if config_name == "<STR_LIT:_>" : <EOL> config_name = metadata [ '<STR_LIT>' ] <EOL> config = importlib . import_module ( "<STR_LIT>" % config_name ) <EOL> filename = os . path . splitext ( os . path . basename ( metadata_path ) ) [ <NUM_LIT:0> ] <EOL> target_path = "<STR_LIT>" % ( subset , config_name , filename , avg_method ) <EOL> assert metadata [ '<STR_LIT>' ] == config . num_chunks_train - <NUM_LIT:1> <EOL> print "<STR_LIT>" <EOL> l_ins , l_out = config . build_model ( ) [ : <NUM_LIT:2> ] <EOL> if avg_method == "<STR_LIT>" : <EOL> if not isinstance ( l_out , nn_plankton . NonlinLayer ) : <EOL> sys . exit ( "<STR_LIT>" ) <EOL> l_out = l_out . input_layer <EOL> all_layers = nn . layers . get_all_layers ( l_out ) <EOL> num_params = nn . layers . count_params ( l_out ) <EOL> print "<STR_LIT>" % num_params <EOL> print "<STR_LIT>" <EOL> for layer in all_layers : <EOL> name = string . ljust ( layer . __class__ . __name__ , <NUM_LIT:32> ) <EOL> print "<STR_LIT>" % ( name , layer . get_output_shape ( ) , ) <EOL> output = l_out . get_output ( deterministic = True ) <EOL> if avg_method == "<STR_LIT>" : <EOL> output = T . log ( output ) <EOL> input_ndims = [ len ( l_in . get_output_shape ( ) ) for l_in in l_ins ] <EOL> xs_shared = [ nn . utils . shared_empty ( dim = ndim ) for ndim in input_ndims ] <EOL> idx = T . lscalar ( '<STR_LIT>' ) <EOL> givens = { } <EOL> for l_in , x_shared in zip ( l_ins , xs_shared ) : <EOL> givens [ l_in . input_var ] = x_shared [ idx * config . batch_size : ( idx + <NUM_LIT:1> ) * config . batch_size ] <EOL> compute_output = theano . function ( [ idx ] , output , givens = givens , on_unused_input = '<STR_LIT:ignore>' ) <EOL> nn . layers . set_all_param_values ( l_out , param_values ) <EOL> print "<STR_LIT>" <EOL> config . data_loader . set_params ( metadata [ '<STR_LIT>' ] ) <EOL> augment = not subset . endswith ( "<STR_LIT>" ) <EOL> if subset . startswith ( "<STR_LIT:test>" ) : <EOL> config . data_loader . load_test ( ) <EOL> if hasattr ( config , '<STR_LIT>' ) : <EOL> gen = config . create_eval_test_gen ( ) <EOL> images = config . data_loader . images_test <EOL> else : <EOL> images = config . data_loader . images_test <EOL> gen = config . data_loader . create_fixed_gen ( images , augment = augment ) <EOL> elif subset . startswith ( "<STR_LIT>" ) : <EOL> config . data_loader . load_train ( ) <EOL> if hasattr ( config , '<STR_LIT>' ) : <EOL> gen = config . create_eval_valid_gen ( ) <EOL> else : <EOL> images = config . data_loader . images_valid <EOL> gen = config . data_loader . create_fixed_gen ( images , augment = augment ) <EOL> elif subset . startswith ( "<STR_LIT:train>" ) : <EOL> config . data_loader . load_train ( ) <EOL> if hasattr ( config , '<STR_LIT>' ) : <EOL> gen = config . create_eval_train_gen ( ) <EOL> else : <EOL> images = config . data_loader . images_train <EOL> gen = config . data_loader . create_fixed_gen ( images , augment = augment ) <EOL> else : <EOL> print "<STR_LIT>" % subset <EOL> if augment : <EOL> print "<STR_LIT>" <EOL> num_test_tfs = len ( config . data_loader . augmentation_transforms_test ) <EOL> else : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> num_batches_chunk = config . chunk_size // config . batch_size <EOL> outputs = [ ] <EOL> remainder = None <EOL> for e , ( xs_chunk , chunk_length ) in enumerate ( gen ) : <EOL> num_batches_chunk = int ( np . ceil ( chunk_length / float ( config . batch_size ) ) ) <EOL> print "<STR_LIT>" % ( e + <NUM_LIT:1> ) <EOL> print "<STR_LIT>" <EOL> for x_shared , x_chunk in zip ( xs_shared , xs_chunk ) : <EOL> x_shared . set_value ( x_chunk ) <EOL> print "<STR_LIT>" <EOL> outputs_chunk = [ ] <EOL> for b in xrange ( num_batches_chunk ) : <EOL> out = compute_output ( b ) <EOL> outputs_chunk . append ( out ) <EOL> outputs_chunk = np . vstack ( outputs_chunk ) <EOL> outputs_chunk = outputs_chunk [ : chunk_length ] <EOL> if augment and num_test_tfs > <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> if remainder is not None : <EOL> outputs_chunk = np . vstack ( [ remainder , outputs_chunk ] ) <EOL> l = ( outputs_chunk . shape [ <NUM_LIT:0> ] // num_test_tfs ) * num_test_tfs <EOL> remainder = outputs_chunk [ l : ] <EOL> if avg_method == "<STR_LIT>" : <EOL> outputs_chunk = outputs_chunk [ : l ] <EOL> h = utils . entropy ( outputs_chunk ) <EOL> outputs_chunk *= np . exp ( - h ) [ : , None ] <EOL> outputs_chunk = outputs_chunk . reshape ( l // num_test_tfs , num_test_tfs , outputs_chunk . shape [ <NUM_LIT:1> ] ) . sum ( <NUM_LIT:1> ) <EOL> z = np . exp ( - h ) . reshape ( l // num_test_tfs , num_test_tfs ) . sum ( <NUM_LIT:1> ) <EOL> outputs_chunk /= z [ : , None ] <EOL> else : <EOL> outputs_chunk = outputs_chunk [ : l ] . reshape ( l // num_test_tfs , num_test_tfs , outputs_chunk . shape [ <NUM_LIT:1> ] ) . mean ( <NUM_LIT:1> ) <EOL> outputs . append ( outputs_chunk ) <EOL> assert ( remainder is None ) or remainder . size == <NUM_LIT:0> <EOL> outputs = np . vstack ( outputs ) <EOL> if avg_method == "<STR_LIT>" : <EOL> print "<STR_LIT>" <EOL> outputs = utils . softmax ( outputs ) <EOL> elif avg_method == "<STR_LIT>" : <EOL> print "<STR_LIT>" <EOL> outputs = utils . softmax ( outputs ) <EOL> print "<STR_LIT>" <EOL> np . save ( target_path , outputs ) <EOL> print "<STR_LIT>" % target_path </s>
