<s> """<STR_LIT>""" <EOL> import numpy as np <EOL> import theano <EOL> import theano . tensor as T <EOL> import layers <EOL> import cc_layers <EOL> import custom <EOL> import load_data <EOL> import realtime_augmentation as ra <EOL> import time <EOL> import csv <EOL> import os <EOL> import cPickle as pickle <EOL> BATCH_SIZE = <NUM_LIT:32> <EOL> NUM_INPUT_FEATURES = <NUM_LIT:3> <EOL> CHUNK_SIZE = <NUM_LIT> <EOL> ANALYSIS_PATH = "<STR_LIT>" <EOL> DO_VALID = True <EOL> DO_TEST = True <EOL> target_filename = os . path . basename ( ANALYSIS_PATH ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> target_path_valid = os . path . join ( "<STR_LIT>" , target_filename ) <EOL> target_path_test = os . path . join ( "<STR_LIT>" , target_filename ) <EOL> print "<STR_LIT>" <EOL> analysis = np . load ( ANALYSIS_PATH ) <EOL> input_sizes = [ ( <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT> ) ] <EOL> ds_transforms = [ <EOL> ra . build_ds_transform ( <NUM_LIT> , target_size = input_sizes [ <NUM_LIT:0> ] ) , <EOL> ra . build_ds_transform ( <NUM_LIT> , target_size = input_sizes [ <NUM_LIT:1> ] ) + ra . build_augmentation_transform ( rotation = <NUM_LIT> ) ] <EOL> num_input_representations = len ( ds_transforms ) <EOL> num_train = load_data . num_train <EOL> num_valid = num_train // <NUM_LIT:10> <EOL> num_train -= num_valid <EOL> num_test = load_data . num_test <EOL> valid_ids = load_data . train_ids [ num_train : ] <EOL> train_ids = load_data . train_ids [ : num_train ] <EOL> test_ids = load_data . test_ids <EOL> train_indices = np . arange ( num_train ) <EOL> valid_indices = np . arange ( num_train , num_train + num_valid ) <EOL> test_indices = np . arange ( num_test ) <EOL> y_valid = np . load ( "<STR_LIT>" ) [ num_train : ] <EOL> print "<STR_LIT>" <EOL> l0 = layers . Input2DLayer ( BATCH_SIZE , NUM_INPUT_FEATURES , input_sizes [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , input_sizes [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> l0_45 = layers . Input2DLayer ( BATCH_SIZE , NUM_INPUT_FEATURES , input_sizes [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] , input_sizes [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> l0r = layers . MultiRotSliceLayer ( [ l0 , l0_45 ] , part_size = <NUM_LIT> , include_flip = True ) <EOL> l0s = cc_layers . ShuffleBC01ToC01BLayer ( l0r ) <EOL> l1a = cc_layers . CudaConvnetConv2DLayer ( l0s , n_filters = <NUM_LIT:32> , filter_size = <NUM_LIT:8> , weights_std = <NUM_LIT> , init_bias_value = <NUM_LIT:0.1> , dropout = <NUM_LIT:0.0> , partial_sum = <NUM_LIT:1> , untie_biases = True ) <EOL> l1 = cc_layers . CudaConvnetPooling2DLayer ( l1a , pool_size = <NUM_LIT:2> ) <EOL> l2a = cc_layers . CudaConvnetConv2DLayer ( l1 , n_filters = <NUM_LIT:64> , filter_size = <NUM_LIT:4> , weights_std = <NUM_LIT> , init_bias_value = <NUM_LIT:0.1> , dropout = <NUM_LIT:0.0> , partial_sum = <NUM_LIT:1> , untie_biases = True ) <EOL> l2 = cc_layers . CudaConvnetPooling2DLayer ( l2a , pool_size = <NUM_LIT:2> ) <EOL> l3a = cc_layers . CudaConvnetConv2DLayer ( l2 , n_filters = <NUM_LIT> , filter_size = <NUM_LIT:3> , weights_std = <NUM_LIT> , init_bias_value = <NUM_LIT:0.1> , dropout = <NUM_LIT:0.0> , partial_sum = <NUM_LIT:1> , untie_biases = True ) <EOL> l3b = cc_layers . CudaConvnetConv2DLayer ( l3a , n_filters = <NUM_LIT> , filter_size = <NUM_LIT:3> , pad = <NUM_LIT:0> , weights_std = <NUM_LIT:0.1> , init_bias_value = <NUM_LIT:0.1> , dropout = <NUM_LIT:0.0> , partial_sum = <NUM_LIT:1> , untie_biases = True ) <EOL> l3 = cc_layers . CudaConvnetPooling2DLayer ( l3b , pool_size = <NUM_LIT:2> ) <EOL> l3s = cc_layers . ShuffleC01BToBC01Layer ( l3 ) <EOL> j3 = layers . MultiRotMergeLayer ( l3s , num_views = <NUM_LIT:4> ) <EOL> l4a = layers . DenseLayer ( j3 , n_outputs = <NUM_LIT> , weights_std = <NUM_LIT> , init_bias_value = <NUM_LIT> , dropout = <NUM_LIT:0.5> , nonlinearity = layers . identity ) <EOL> l4 = layers . FeatureMaxPoolingLayer ( l4a , pool_size = <NUM_LIT:2> , feature_dim = <NUM_LIT:1> , implementation = '<STR_LIT>' ) <EOL> l5 = layers . DenseLayer ( l4 , n_outputs = <NUM_LIT> , weights_std = <NUM_LIT> , init_bias_value = <NUM_LIT:0.1> , dropout = <NUM_LIT:0.5> , nonlinearity = layers . identity ) <EOL> l6 = custom . OptimisedDivGalaxyOutputLayer ( l5 ) <EOL> xs_shared = [ theano . shared ( np . zeros ( ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) , dtype = theano . config . floatX ) ) for _ in xrange ( num_input_representations ) ] <EOL> idx = T . lscalar ( '<STR_LIT>' ) <EOL> givens = { <EOL> l0 . input_var : xs_shared [ <NUM_LIT:0> ] [ idx * BATCH_SIZE : ( idx + <NUM_LIT:1> ) * BATCH_SIZE ] , <EOL> l0_45 . input_var : xs_shared [ <NUM_LIT:1> ] [ idx * BATCH_SIZE : ( idx + <NUM_LIT:1> ) * BATCH_SIZE ] , <EOL> } <EOL> compute_output = theano . function ( [ idx ] , l6 . predictions ( dropout_active = False ) , givens = givens ) <EOL> print "<STR_LIT>" <EOL> layers . set_param_values ( l6 , analysis [ '<STR_LIT>' ] ) <EOL> print "<STR_LIT>" <EOL> augmentation_transforms = [ ] <EOL> for zoom in [ <NUM_LIT:1> / <NUM_LIT> , <NUM_LIT:1.0> , <NUM_LIT> ] : <EOL> for angle in np . linspace ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:10> , endpoint = False ) : <EOL> augmentation_transforms . append ( ra . build_augmentation_transform ( rotation = angle , zoom = zoom ) ) <EOL> augmentation_transforms . append ( ra . build_augmentation_transform ( rotation = ( angle + <NUM_LIT> ) , zoom = zoom , shear = <NUM_LIT> ) ) <EOL> print "<STR_LIT>" % len ( augmentation_transforms ) <EOL> augmented_data_gen_valid = ra . realtime_fixed_augmented_data_gen ( valid_indices , '<STR_LIT:train>' , augmentation_transforms = augmentation_transforms , chunk_size = CHUNK_SIZE , target_sizes = input_sizes , ds_transforms = ds_transforms , processor_class = ra . LoadAndProcessFixedPysexCenteringRescaling ) <EOL> valid_gen = load_data . buffered_gen_mp ( augmented_data_gen_valid , buffer_size = <NUM_LIT:1> ) <EOL> augmented_data_gen_test = ra . realtime_fixed_augmented_data_gen ( test_indices , '<STR_LIT:test>' , augmentation_transforms = augmentation_transforms , chunk_size = CHUNK_SIZE , target_sizes = input_sizes , ds_transforms = ds_transforms , processor_class = ra . LoadAndProcessFixedPysexCenteringRescaling ) <EOL> test_gen = load_data . buffered_gen_mp ( augmented_data_gen_test , buffer_size = <NUM_LIT:1> ) <EOL> approx_num_chunks_valid = int ( np . ceil ( num_valid * len ( augmentation_transforms ) / float ( CHUNK_SIZE ) ) ) <EOL> approx_num_chunks_test = int ( np . ceil ( num_test * len ( augmentation_transforms ) / float ( CHUNK_SIZE ) ) ) <EOL> print "<STR_LIT>" % approx_num_chunks_valid <EOL> print "<STR_LIT>" % approx_num_chunks_test <EOL> if DO_VALID : <EOL> print <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> predictions_list = [ ] <EOL> start_time = time . time ( ) <EOL> for e , ( chunk_data , chunk_length ) in enumerate ( valid_gen ) : <EOL> print "<STR_LIT>" % ( e + <NUM_LIT:1> ) <EOL> xs_chunk = chunk_data <EOL> xs_chunk = [ x_chunk . transpose ( <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> ) for x_chunk in xs_chunk ] <EOL> print "<STR_LIT>" <EOL> for x_shared , x_chunk in zip ( xs_shared , xs_chunk ) : <EOL> x_shared . set_value ( x_chunk ) <EOL> num_batches_chunk = int ( np . ceil ( chunk_length / float ( BATCH_SIZE ) ) ) <EOL> predictions_chunk_list = [ ] <EOL> for b in xrange ( num_batches_chunk ) : <EOL> if b % <NUM_LIT:1000> == <NUM_LIT:0> : <EOL> print "<STR_LIT>" % ( b + <NUM_LIT:1> , num_batches_chunk ) <EOL> predictions = compute_output ( b ) <EOL> predictions_chunk_list . append ( predictions ) <EOL> predictions_chunk = np . vstack ( predictions_chunk_list ) <EOL> predictions_chunk = predictions_chunk [ : chunk_length ] <EOL> print "<STR_LIT>" <EOL> predictions_chunk_avg = predictions_chunk . reshape ( - <NUM_LIT:1> , len ( augmentation_transforms ) , <NUM_LIT> ) . mean ( <NUM_LIT:1> ) <EOL> predictions_list . append ( predictions_chunk_avg ) <EOL> time_since_start = time . time ( ) - start_time <EOL> print "<STR_LIT>" % load_data . hms ( time_since_start ) <EOL> all_predictions = np . vstack ( predictions_list ) <EOL> print "<STR_LIT>" % target_path_valid <EOL> load_data . save_gz ( target_path_valid , all_predictions ) <EOL> print "<STR_LIT>" <EOL> rmse_valid = analysis [ '<STR_LIT>' ] [ - <NUM_LIT:1> ] <EOL> rmse_augmented = np . sqrt ( np . mean ( ( y_valid - all_predictions ) ** <NUM_LIT:2> ) ) <EOL> print "<STR_LIT>" % rmse_valid <EOL> print "<STR_LIT>" % rmse_augmented <EOL> if DO_TEST : <EOL> print <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> predictions_list = [ ] <EOL> start_time = time . time ( ) <EOL> for e , ( chunk_data , chunk_length ) in enumerate ( test_gen ) : <EOL> print "<STR_LIT>" % ( e + <NUM_LIT:1> ) <EOL> xs_chunk = chunk_data <EOL> xs_chunk = [ x_chunk . transpose ( <NUM_LIT:0> , <NUM_LIT:3> , <NUM_LIT:1> , <NUM_LIT:2> ) for x_chunk in xs_chunk ] <EOL> print "<STR_LIT>" <EOL> for x_shared , x_chunk in zip ( xs_shared , xs_chunk ) : <EOL> x_shared . set_value ( x_chunk ) <EOL> num_batches_chunk = int ( np . ceil ( chunk_length / float ( BATCH_SIZE ) ) ) <EOL> predictions_chunk_list = [ ] <EOL> for b in xrange ( num_batches_chunk ) : <EOL> if b % <NUM_LIT:1000> == <NUM_LIT:0> : <EOL> print "<STR_LIT>" % ( b + <NUM_LIT:1> , num_batches_chunk ) <EOL> predictions = compute_output ( b ) <EOL> predictions_chunk_list . append ( predictions ) <EOL> predictions_chunk = np . vstack ( predictions_chunk_list ) <EOL> predictions_chunk = predictions_chunk [ : chunk_length ] <EOL> print "<STR_LIT>" <EOL> predictions_chunk_avg = predictions_chunk . reshape ( - <NUM_LIT:1> , len ( augmentation_transforms ) , <NUM_LIT> ) . mean ( <NUM_LIT:1> ) <EOL> predictions_list . append ( predictions_chunk_avg ) <EOL> time_since_start = time . time ( ) - start_time <EOL> print "<STR_LIT>" % load_data . hms ( time_since_start ) <EOL> all_predictions = np . vstack ( predictions_list ) <EOL> print "<STR_LIT>" % target_path_test <EOL> load_data . save_gz ( target_path_test , all_predictions ) <EOL> print "<STR_LIT>" </s>
