<s> import numpy <EOL> import sys <EOL> import getopt as opt <EOL> from util import * <EOL> from math import sqrt , ceil , floor <EOL> import os <EOL> from gpumodel import IGPUModel <EOL> import random as r <EOL> import numpy . random as nr <EOL> from convnet import ConvNet <EOL> from options import * <EOL> import numpy as np <EOL> import time <EOL> try : <EOL> import pylab as pl <EOL> except : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> class ShowNetError ( Exception ) : <EOL> pass <EOL> class ShowConvNet ( ConvNet ) : <EOL> def __init__ ( self , op , load_dic ) : <EOL> ConvNet . __init__ ( self , op , load_dic ) <EOL> def get_gpus ( self ) : <EOL> self . need_gpu = self . op . get_value ( '<STR_LIT>' ) or self . op . get_value ( '<STR_LIT>' ) or self . op . get_value ( '<STR_LIT>' ) <EOL> if self . need_gpu : <EOL> ConvNet . get_gpus ( self ) <EOL> def init_data_providers ( self ) : <EOL> class Dummy : <EOL> def advance_batch ( self ) : <EOL> pass <EOL> if self . need_gpu : <EOL> ConvNet . init_data_providers ( self ) <EOL> else : <EOL> self . train_data_provider = self . test_data_provider = Dummy ( ) <EOL> def import_model ( self ) : <EOL> if self . need_gpu : <EOL> ConvNet . import_model ( self ) <EOL> def init_model_state ( self ) : <EOL> if self . op . get_value ( '<STR_LIT>' ) : <EOL> self . sotmax_idx = self . get_layer_idx ( self . op . get_value ( '<STR_LIT>' ) , check_type = '<STR_LIT>' ) <EOL> if self . op . get_value ( '<STR_LIT>' ) : <EOL> self . ftr_layer_idx = self . get_layer_idx ( self . op . get_value ( '<STR_LIT>' ) ) <EOL> def init_model_lib ( self ) : <EOL> if self . need_gpu : <EOL> ConvNet . init_model_lib ( self ) <EOL> def plot_cost ( self ) : <EOL> if self . show_cost not in self . train_outputs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] : <EOL> raise ShowNetError ( "<STR_LIT>" % self . show_cost ) <EOL> train_errors = [ o [ <NUM_LIT:0> ] [ self . show_cost ] [ self . cost_idx ] for o in self . train_outputs ] <EOL> test_errors = [ o [ <NUM_LIT:0> ] [ self . show_cost ] [ self . cost_idx ] for o in self . test_outputs ] <EOL> numbatches = len ( self . train_batch_range ) <EOL> test_errors = numpy . row_stack ( test_errors ) <EOL> test_errors = numpy . tile ( test_errors , ( <NUM_LIT:1> , self . testing_freq ) ) <EOL> test_errors = list ( test_errors . flatten ( ) ) <EOL> test_errors += [ test_errors [ - <NUM_LIT:1> ] ] * max ( <NUM_LIT:0> , len ( train_errors ) - len ( test_errors ) ) <EOL> test_errors = test_errors [ : len ( train_errors ) ] <EOL> numepochs = len ( train_errors ) / float ( numbatches ) <EOL> pl . figure ( <NUM_LIT:1> ) <EOL> x = range ( <NUM_LIT:0> , len ( train_errors ) ) <EOL> pl . plot ( x , train_errors , '<STR_LIT>' , label = '<STR_LIT>' ) <EOL> pl . plot ( x , test_errors , '<STR_LIT>' , label = '<STR_LIT>' ) <EOL> pl . legend ( ) <EOL> ticklocs = range ( numbatches , len ( train_errors ) - len ( train_errors ) % numbatches + <NUM_LIT:1> , numbatches ) <EOL> epoch_label_gran = int ( ceil ( numepochs / <NUM_LIT> ) ) <EOL> epoch_label_gran = int ( ceil ( float ( epoch_label_gran ) / <NUM_LIT:10> ) * <NUM_LIT:10> ) <EOL> ticklabels = map ( lambda x : str ( ( x [ <NUM_LIT:1> ] / numbatches ) ) if x [ <NUM_LIT:0> ] % epoch_label_gran == epoch_label_gran - <NUM_LIT:1> else '<STR_LIT>' , enumerate ( ticklocs ) ) <EOL> pl . xticks ( ticklocs , ticklabels ) <EOL> pl . xlabel ( '<STR_LIT>' ) <EOL> pl . title ( self . show_cost ) <EOL> def make_filter_fig ( self , filters , filter_start , fignum , _title , num_filters , combine_chans ) : <EOL> FILTERS_PER_ROW = <NUM_LIT:16> <EOL> MAX_ROWS = <NUM_LIT:16> <EOL> MAX_FILTERS = FILTERS_PER_ROW * MAX_ROWS <EOL> num_colors = filters . shape [ <NUM_LIT:0> ] <EOL> f_per_row = int ( ceil ( FILTERS_PER_ROW / float ( <NUM_LIT:1> if combine_chans else num_colors ) ) ) <EOL> filter_end = min ( filter_start + MAX_FILTERS , num_filters ) <EOL> filter_rows = int ( ceil ( float ( filter_end - filter_start ) / f_per_row ) ) <EOL> filter_size = int ( sqrt ( filters . shape [ <NUM_LIT:1> ] ) ) <EOL> fig = pl . figure ( fignum ) <EOL> fig . text ( <NUM_LIT> , <NUM_LIT> , '<STR_LIT>' % ( _title , filter_size , filter_size , filter_start , filter_end - <NUM_LIT:1> ) , horizontalalignment = '<STR_LIT>' ) <EOL> num_filters = filter_end - filter_start <EOL> if not combine_chans : <EOL> bigpic = n . zeros ( ( filter_size * filter_rows + filter_rows + <NUM_LIT:1> , filter_size * num_colors * f_per_row + f_per_row + <NUM_LIT:1> ) , dtype = n . single ) <EOL> else : <EOL> bigpic = n . zeros ( ( <NUM_LIT:3> , filter_size * filter_rows + filter_rows + <NUM_LIT:1> , filter_size * f_per_row + f_per_row + <NUM_LIT:1> ) , dtype = n . single ) <EOL> for m in xrange ( filter_start , filter_end ) : <EOL> filter = filters [ : , : , m ] <EOL> y , x = ( m - filter_start ) / f_per_row , ( m - filter_start ) % f_per_row <EOL> if not combine_chans : <EOL> for c in xrange ( num_colors ) : <EOL> filter_pic = filter [ c , : ] . reshape ( ( filter_size , filter_size ) ) <EOL> bigpic [ <NUM_LIT:1> + ( <NUM_LIT:1> + filter_size ) * y : <NUM_LIT:1> + ( <NUM_LIT:1> + filter_size ) * y + filter_size , <EOL> <NUM_LIT:1> + ( <NUM_LIT:1> + filter_size * num_colors ) * x + filter_size * c : <NUM_LIT:1> + ( <NUM_LIT:1> + filter_size * num_colors ) * x + filter_size * ( c + <NUM_LIT:1> ) ] = filter_pic <EOL> else : <EOL> filter_pic = filter . reshape ( ( <NUM_LIT:3> , filter_size , filter_size ) ) <EOL> bigpic [ : , <EOL> <NUM_LIT:1> + ( <NUM_LIT:1> + filter_size ) * y : <NUM_LIT:1> + ( <NUM_LIT:1> + filter_size ) * y + filter_size , <EOL> <NUM_LIT:1> + ( <NUM_LIT:1> + filter_size ) * x : <NUM_LIT:1> + ( <NUM_LIT:1> + filter_size ) * x + filter_size ] = filter_pic <EOL> pl . xticks ( [ ] ) <EOL> pl . yticks ( [ ] ) <EOL> if not combine_chans : <EOL> pl . imshow ( bigpic , cmap = pl . cm . gray , interpolation = '<STR_LIT>' ) <EOL> else : <EOL> bigpic = bigpic . swapaxes ( <NUM_LIT:0> , <NUM_LIT:2> ) . swapaxes ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> pl . imshow ( bigpic , interpolation = '<STR_LIT>' ) <EOL> def plot_filters ( self ) : <EOL> filter_start = <NUM_LIT:0> <EOL> layer_names = [ l [ '<STR_LIT:name>' ] for l in self . layers ] <EOL> if self . show_filters not in layer_names : <EOL> raise ShowNetError ( "<STR_LIT>" % self . show_filters ) <EOL> layer = self . layers [ layer_names . index ( self . show_filters ) ] <EOL> filters = layer [ '<STR_LIT>' ] [ self . input_idx ] <EOL> if layer [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> num_filters = layer [ '<STR_LIT>' ] <EOL> channels = self . channels <EOL> elif layer [ '<STR_LIT:type>' ] in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> num_filters = layer [ '<STR_LIT>' ] <EOL> channels = layer [ '<STR_LIT>' ] [ self . input_idx ] <EOL> if layer [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> filters = filters . reshape ( ( layer [ '<STR_LIT>' ] , layer [ '<STR_LIT>' ] [ self . input_idx ] * channels , num_filters ) ) <EOL> filter_start = r . randint ( <NUM_LIT:0> , layer [ '<STR_LIT>' ] - <NUM_LIT:1> ) * num_filters <EOL> filters = filters . swapaxes ( <NUM_LIT:0> , <NUM_LIT:1> ) . reshape ( channels * layer [ '<STR_LIT>' ] [ self . input_idx ] , num_filters * layer [ '<STR_LIT>' ] ) <EOL> num_filters *= layer [ '<STR_LIT>' ] <EOL> filters = filters . reshape ( channels , filters . shape [ <NUM_LIT:0> ] / channels , filters . shape [ <NUM_LIT:1> ] ) <EOL> if self . yuv_to_rgb and channels == <NUM_LIT:3> : <EOL> R = filters [ <NUM_LIT:0> , : , : ] + <NUM_LIT> * filters [ <NUM_LIT:2> , : , : ] <EOL> G = filters [ <NUM_LIT:0> , : , : ] + - <NUM_LIT> * filters [ <NUM_LIT:1> , : , : ] + - <NUM_LIT> * filters [ <NUM_LIT:2> , : , : ] <EOL> B = filters [ <NUM_LIT:0> , : , : ] + <NUM_LIT> * filters [ <NUM_LIT:1> , : , : ] <EOL> filters [ <NUM_LIT:0> , : , : ] , filters [ <NUM_LIT:1> , : , : ] , filters [ <NUM_LIT:2> , : , : ] = R , G , B <EOL> combine_chans = not self . no_rgb and channels == <NUM_LIT:3> <EOL> filters = filters - filters . min ( ) <EOL> filters = filters / filters . max ( ) <EOL> self . make_filter_fig ( filters , filter_start , <NUM_LIT:2> , '<STR_LIT>' % self . show_filters , num_filters , combine_chans ) <EOL> def plot_predictions ( self ) : <EOL> data = self . get_next_batch ( train = False ) [ <NUM_LIT:2> ] <EOL> num_classes = self . test_data_provider . get_num_classes ( ) <EOL> NUM_ROWS = <NUM_LIT:2> <EOL> NUM_COLS = <NUM_LIT:4> <EOL> NUM_IMGS = NUM_ROWS * NUM_COLS <EOL> NUM_TOP_CLASSES = min ( num_classes , <NUM_LIT:4> ) <EOL> label_names = self . test_data_provider . batch_meta [ '<STR_LIT>' ] <EOL> if self . only_errors : <EOL> preds = n . zeros ( ( data [ <NUM_LIT:0> ] . shape [ <NUM_LIT:1> ] , num_classes ) , dtype = n . single ) <EOL> else : <EOL> preds = n . zeros ( ( NUM_IMGS , num_classes ) , dtype = n . single ) <EOL> rand_idx = nr . randint ( <NUM_LIT:0> , data [ <NUM_LIT:0> ] . shape [ <NUM_LIT:1> ] , NUM_IMGS ) <EOL> data [ <NUM_LIT:0> ] = n . require ( data [ <NUM_LIT:0> ] [ : , rand_idx ] , requirements = '<STR_LIT:C>' ) <EOL> data [ <NUM_LIT:1> ] = n . require ( data [ <NUM_LIT:1> ] [ : , rand_idx ] , requirements = '<STR_LIT:C>' ) <EOL> data += [ preds ] <EOL> self . libmodel . startFeatureWriter ( data , self . sotmax_idx ) <EOL> self . finish_batch ( ) <EOL> fig = pl . figure ( <NUM_LIT:3> ) <EOL> fig . text ( <NUM_LIT> , <NUM_LIT> , '<STR_LIT>' % ( '<STR_LIT>' if self . only_errors else '<STR_LIT>' ) ) <EOL> if self . only_errors : <EOL> err_idx = nr . permutation ( n . where ( preds . argmax ( axis = <NUM_LIT:1> ) != data [ <NUM_LIT:1> ] [ <NUM_LIT:0> , : ] ) [ <NUM_LIT:0> ] ) [ : NUM_IMGS ] <EOL> data [ <NUM_LIT:0> ] , data [ <NUM_LIT:1> ] , preds = data [ <NUM_LIT:0> ] [ : , err_idx ] , data [ <NUM_LIT:1> ] [ : , err_idx ] , preds [ err_idx , : ] <EOL> data [ <NUM_LIT:0> ] = self . test_data_provider . get_plottable_data ( data [ <NUM_LIT:0> ] ) <EOL> for r in xrange ( NUM_ROWS ) : <EOL> for c in xrange ( NUM_COLS ) : <EOL> img_idx = r * NUM_COLS + c <EOL> if data [ <NUM_LIT:0> ] . shape [ <NUM_LIT:0> ] <= img_idx : <EOL> break <EOL> pl . subplot ( NUM_ROWS * <NUM_LIT:2> , NUM_COLS , r * <NUM_LIT:2> * NUM_COLS + c + <NUM_LIT:1> ) <EOL> pl . xticks ( [ ] ) <EOL> pl . yticks ( [ ] ) <EOL> img = data [ <NUM_LIT:0> ] [ img_idx , : , : , : ] <EOL> pl . imshow ( img , interpolation = '<STR_LIT>' ) <EOL> true_label = int ( data [ <NUM_LIT:1> ] [ <NUM_LIT:0> , img_idx ] ) <EOL> img_labels = sorted ( zip ( preds [ img_idx , : ] , label_names ) , key = lambda x : x [ <NUM_LIT:0> ] ) [ - NUM_TOP_CLASSES : ] <EOL> pl . subplot ( NUM_ROWS * <NUM_LIT:2> , NUM_COLS , ( r * <NUM_LIT:2> + <NUM_LIT:1> ) * NUM_COLS + c + <NUM_LIT:1> , aspect = '<STR_LIT>' ) <EOL> ylocs = n . array ( range ( NUM_TOP_CLASSES ) ) + <NUM_LIT:0.5> <EOL> height = <NUM_LIT:0.5> <EOL> width = max ( ylocs ) <EOL> pl . barh ( ylocs , [ l [ <NUM_LIT:0> ] * width for l in img_labels ] , height = height , color = [ '<STR_LIT:r>' if l [ <NUM_LIT:1> ] == label_names [ true_label ] else '<STR_LIT:b>' for l in img_labels ] ) <EOL> pl . title ( label_names [ true_label ] ) <EOL> pl . yticks ( ylocs + height / <NUM_LIT:2> , [ l [ <NUM_LIT:1> ] for l in img_labels ] ) <EOL> pl . xticks ( [ width / <NUM_LIT> , width ] , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> pl . ylim ( <NUM_LIT:0> , ylocs [ - <NUM_LIT:1> ] + height * <NUM_LIT:2> ) <EOL> def do_write_features ( self ) : <EOL> if not os . path . exists ( self . feature_path ) : <EOL> os . makedirs ( self . feature_path ) <EOL> next_data = self . get_next_batch ( train = False ) <EOL> b1 = next_data [ <NUM_LIT:1> ] <EOL> num_ftrs = self . layers [ self . ftr_layer_idx ] [ '<STR_LIT>' ] <EOL> while True : <EOL> batch = next_data [ <NUM_LIT:1> ] <EOL> data = next_data [ <NUM_LIT:2> ] <EOL> ftrs = n . zeros ( ( data [ <NUM_LIT:0> ] . shape [ <NUM_LIT:1> ] , num_ftrs ) , dtype = n . single ) <EOL> self . libmodel . startFeatureWriter ( data + [ ftrs ] , self . ftr_layer_idx ) <EOL> next_data = self . get_next_batch ( train = False ) <EOL> self . finish_batch ( ) <EOL> path_out = os . path . join ( self . feature_path , '<STR_LIT>' % batch ) <EOL> pickle ( path_out , { '<STR_LIT:data>' : ftrs , '<STR_LIT>' : data [ <NUM_LIT:1> ] } ) <EOL> print "<STR_LIT>" % path_out <EOL> if next_data [ <NUM_LIT:1> ] == b1 : <EOL> break <EOL> pickle ( os . path . join ( self . feature_path , '<STR_LIT>' ) , { '<STR_LIT>' : self . load_file , <EOL> '<STR_LIT>' : num_ftrs } ) <EOL> def make_predictions ( self , net , data , labels , num_classes ) : <EOL> data = np . require ( data , requirements = '<STR_LIT:C>' ) <EOL> labels = np . require ( labels , requirements = '<STR_LIT:C>' ) <EOL> preds = np . zeros ( ( data . shape [ <NUM_LIT:1> ] , num_classes ) , dtype = np . single ) <EOL> softmax_idx = net . get_layer_idx ( '<STR_LIT>' , check_type = '<STR_LIT>' ) <EOL> t0 = time . time ( ) <EOL> net . libmodel . startFeatureWriter ( <EOL> [ data , labels , preds ] , softmax_idx ) <EOL> net . finish_batch ( ) <EOL> print "<STR_LIT>" % ( <EOL> labels . shape [ <NUM_LIT:1> ] , time . time ( ) - t0 ) <EOL> if net . multiview_test : <EOL> num_views = net . test_data_provider . num_views <EOL> num_samples = labels . shape [ <NUM_LIT:1> ] / num_views <EOL> split_sections = range ( <EOL> num_samples , num_samples * num_views , num_samples ) <EOL> preds = np . split ( preds , split_sections , axis = <NUM_LIT:0> ) <EOL> labels = np . split ( labels , split_sections , axis = <NUM_LIT:1> ) <EOL> preds = reduce ( np . add , preds ) <EOL> labels = labels [ <NUM_LIT:0> ] <EOL> return preds , labels <EOL> def get_predictions ( self ) : <EOL> num_classes = self . test_data_provider . get_num_classes ( ) <EOL> all_preds = np . zeros ( ( <NUM_LIT:0> , num_classes ) , dtype = np . single ) <EOL> all_labels = np . zeros ( ( <NUM_LIT:0> , <NUM_LIT:1> ) , dtype = np . single ) <EOL> all_metadata = [ ] <EOL> num_batches = len ( self . test_data_provider . batch_range ) <EOL> db = self . test_data_provider . batch_meta . get ( '<STR_LIT>' , { } ) <EOL> for batch_index in range ( num_batches ) : <EOL> epoch , batchnum , ( data , labels ) = self . get_next_batch ( train = False ) <EOL> if data . shape [ <NUM_LIT:1> ] != labels . shape [ <NUM_LIT:1> ] : <EOL> data = data [ : , : labels . shape [ <NUM_LIT:1> ] ] <EOL> preds , labels = self . make_predictions ( self , data , labels , num_classes ) <EOL> all_preds = np . vstack ( [ all_preds , preds ] ) <EOL> all_labels = np . vstack ( [ all_labels , labels . T ] ) <EOL> if db : <EOL> ids = self . test_data_provider . get_batch ( batchnum ) . get ( '<STR_LIT>' ) <EOL> all_metadata . extend ( [ db [ id ] for id in ids ] ) <EOL> self . _predictions = all_preds , all_labels , all_metadata <EOL> return self . _predictions <EOL> def do_write_predictions ( self ) : <EOL> preds , labels , metadata = self . get_predictions ( ) <EOL> pickle ( self . write_predictions , { '<STR_LIT:data>' : preds , '<STR_LIT>' : labels } ) <EOL> print "<STR_LIT>" % self . write_predictions <EOL> def start ( self ) : <EOL> self . op . print_values ( ) <EOL> if self . show_cost : <EOL> self . plot_cost ( ) <EOL> if self . show_filters : <EOL> self . plot_filters ( ) <EOL> if self . show_preds : <EOL> self . plot_predictions ( ) <EOL> if self . write_features : <EOL> self . do_write_features ( ) <EOL> if self . write_predictions : <EOL> self . do_write_predictions ( ) <EOL> pl . show ( ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> @ classmethod <EOL> def get_options_parser ( cls ) : <EOL> op = ConvNet . get_options_parser ( ) <EOL> for option in list ( op . options ) : <EOL> if option not in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> op . delete_option ( option ) <EOL> op . add_option ( "<STR_LIT>" , "<STR_LIT>" , StringOptionParser , "<STR_LIT>" , default = "<STR_LIT>" ) <EOL> op . add_option ( "<STR_LIT>" , "<STR_LIT>" , StringOptionParser , "<STR_LIT>" , default = "<STR_LIT>" ) <EOL> op . add_option ( "<STR_LIT>" , "<STR_LIT>" , IntegerOptionParser , "<STR_LIT>" , default = <NUM_LIT:0> ) <EOL> op . add_option ( "<STR_LIT>" , "<STR_LIT>" , IntegerOptionParser , "<STR_LIT>" , default = <NUM_LIT:0> ) <EOL> op . add_option ( "<STR_LIT>" , "<STR_LIT>" , BooleanOptionParser , "<STR_LIT>" , default = False ) <EOL> op . add_option ( "<STR_LIT>" , "<STR_LIT>" , BooleanOptionParser , "<STR_LIT>" , default = False ) <EOL> op . add_option ( "<STR_LIT>" , "<STR_LIT>" , IntegerOptionParser , "<STR_LIT>" , default = <NUM_LIT:0> ) <EOL> op . add_option ( "<STR_LIT>" , "<STR_LIT>" , StringOptionParser , "<STR_LIT>" , default = "<STR_LIT>" ) <EOL> op . add_option ( "<STR_LIT>" , "<STR_LIT>" , BooleanOptionParser , "<STR_LIT>" , default = False , requires = [ '<STR_LIT>' ] ) <EOL> op . add_option ( "<STR_LIT>" , "<STR_LIT>" , StringOptionParser , "<STR_LIT>" , default = "<STR_LIT>" , requires = [ '<STR_LIT>' ] ) <EOL> op . add_option ( "<STR_LIT>" , "<STR_LIT>" , StringOptionParser , "<STR_LIT>" , default = "<STR_LIT>" ) <EOL> op . add_option ( "<STR_LIT>" , "<STR_LIT>" , StringOptionParser , "<STR_LIT>" , default = "<STR_LIT>" ) <EOL> op . add_option ( "<STR_LIT>" , "<STR_LIT>" , BooleanOptionParser , "<STR_LIT>" , default = <NUM_LIT:0> ) <EOL> op . options [ '<STR_LIT>' ] . default = None <EOL> return op <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> try : <EOL> op = ShowConvNet . get_options_parser ( ) <EOL> op , load_dic = IGPUModel . parse_options ( op ) <EOL> model = ShowConvNet ( op , load_dic ) <EOL> model . start ( ) <EOL> except ( UnpickleError , ShowNetError , opt . GetoptError ) , e : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print e </s>
