<s> import argparse <EOL> import os <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT:1>' <EOL> import caffe <EOL> import numpy as np <EOL> import plyvel <EOL> import skimage <EOL> from caffe_pb2 import Datum <EOL> def parse_command_line ( ) : <EOL> parser = argparse . ArgumentParser ( description = """<STR_LIT>""" ) <EOL> parser . add_argument ( "<STR_LIT>" , help = """<STR_LIT>""" , default = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , help = """<STR_LIT>""" , <EOL> type = str , default = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , help = """<STR_LIT>""" , type = str , <EOL> default = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , help = "<STR_LIT>" , type = str , <EOL> default = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , help = "<STR_LIT>" , type = int , <EOL> default = <NUM_LIT> ) <EOL> parser . add_argument ( "<STR_LIT>" , help = "<STR_LIT>" , type = int , <EOL> default = <NUM_LIT> ) <EOL> args = vars ( parser . parse_args ( ) ) <EOL> image = os . path . abspath ( args [ "<STR_LIT:image>" ] ) <EOL> deploy = os . path . abspath ( args [ "<STR_LIT>" ] ) <EOL> input_weight_file = os . path . abspath ( args [ "<STR_LIT>" ] ) <EOL> training_mean_pickle = os . path . abspath ( args [ "<STR_LIT>" ] ) <EOL> predict ( image , deploy , input_weight_file , training_mean_pickle , args [ "<STR_LIT>" ] , <EOL> args [ "<STR_LIT>" ] ) <EOL> def predict ( image_path , deploy_file , input_weight_file , training_mean_pickle , inference_width , <EOL> inference_height ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" % image_path <EOL> net , transformer = _initialize_caffe ( deploy_file , input_weight_file , training_mean_pickle , <EOL> inference_width , inference_height ) <EOL> im = caffe . io . load_image ( image_path ) <EOL> prob = _predict_image ( im , net , transformer ) <EOL> print "<STR_LIT>" . format ( prob ) <EOL> def test_validation ( threshold , output_log_prefix , validation_leveldb , deploy_file , width , height , <EOL> inference_width , inference_height , input_weight_file , training_mean_pickle ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> validation_data = _load_validation_data ( validation_leveldb , width , height ) <EOL> target_details = _run_through_caffe ( validation_data , deploy_file , input_weight_file , threshold , <EOL> training_mean_pickle , inference_width , inference_height ) <EOL> statistics = _calculate_positives_negatives ( target_details ) <EOL> accuracy = _calculate_accuracy ( statistics ) <EOL> precision = _calculate_precision ( statistics ) <EOL> recall = _calculate_recall ( statistics ) <EOL> f1 = _calculate_f1 ( precision , recall ) <EOL> results = "<STR_LIT>" <EOL> results += "<STR_LIT:\n>" <EOL> results += "<STR_LIT>" % threshold <EOL> results += "<STR_LIT>" . format ( accuracy ) <EOL> results += "<STR_LIT>" % precision <EOL> results += "<STR_LIT>" % recall <EOL> results += "<STR_LIT>" % f1 <EOL> results += "<STR_LIT:\n>" <EOL> results += _print_confusion_matrix ( statistics ) <EOL> print results <EOL> with open ( output_log_prefix + "<STR_LIT>" , "<STR_LIT:w>" ) as f : <EOL> f . write ( results ) <EOL> def _load_validation_data ( validation_leveldb , width , height ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> input_vectors = [ ] <EOL> expected_targets = [ ] <EOL> db = plyvel . DB ( validation_leveldb ) <EOL> for key , value in db : <EOL> datum = Datum ( ) <EOL> datum . ParseFromString ( value ) <EOL> data = np . fromstring ( datum . data , dtype = np . uint8 ) <EOL> data = np . reshape ( data , ( <NUM_LIT:3> , height , width ) ) <EOL> data = np . swapaxes ( data , <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> data = np . swapaxes ( data , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> input_vectors . append ( data ) <EOL> expected_targets . append ( datum . label ) <EOL> db . close ( ) <EOL> print "<STR_LIT>" % len ( input_vectors ) <EOL> return { <EOL> "<STR_LIT>" : np . asarray ( input_vectors ) , <EOL> "<STR_LIT>" : np . asarray ( expected_targets ) <EOL> } <EOL> def _initialize_caffe ( deploy_file , input_weight_file , training_mean_pickle , inference_width , <EOL> inference_height ) : <EOL> """<STR_LIT>""" <EOL> caffe . set_mode_gpu ( ) <EOL> net = caffe . Net ( deploy_file , input_weight_file , caffe . TEST ) <EOL> transformer = caffe . io . Transformer ( { "<STR_LIT:data>" : net . blobs [ "<STR_LIT:data>" ] . data . shape } ) <EOL> transformer . set_transpose ( "<STR_LIT:data>" , ( <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> transformer . set_mean ( "<STR_LIT:data>" , np . load ( training_mean_pickle ) . mean ( <NUM_LIT:1> ) . mean ( <NUM_LIT:1> ) ) <EOL> transformer . set_raw_scale ( "<STR_LIT:data>" , <NUM_LIT:255> ) <EOL> transformer . set_channel_swap ( "<STR_LIT:data>" , ( <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> net . blobs [ "<STR_LIT:data>" ] . reshape ( <NUM_LIT:1> , <NUM_LIT:3> , inference_height , inference_width ) <EOL> return ( net , transformer ) <EOL> def _run_through_caffe ( validation_data , deploy_file , input_weight_file , threshold , <EOL> training_mean_pickle , inference_width , inference_height ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> net , transformer = _initialize_caffe ( deploy_file , input_weight_file , training_mean_pickle , <EOL> inference_width , inference_height ) <EOL> print "<STR_LIT>" <EOL> results = [ ] <EOL> for idx in range ( len ( validation_data [ "<STR_LIT>" ] ) ) : <EOL> im = validation_data [ "<STR_LIT>" ] [ idx ] <EOL> prob = _predict_image ( im , net , transformer ) <EOL> expected_target = validation_data [ "<STR_LIT>" ] [ idx ] <EOL> predicted_target = <NUM_LIT:0> <EOL> if prob >= threshold : <EOL> predicted_target = <NUM_LIT:1> <EOL> results . append ( { <EOL> "<STR_LIT>" : expected_target , <EOL> "<STR_LIT>" : predicted_target <EOL> } ) <EOL> return results <EOL> def _predict_image ( im , net , transformer ) : <EOL> """<STR_LIT>""" <EOL> net . blobs [ "<STR_LIT:data>" ] . data [ ... ] = transformer . preprocess ( "<STR_LIT:data>" , im ) <EOL> out = net . forward ( ) <EOL> probs = out [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> prob_cloud = probs [ <NUM_LIT:1> ] * <NUM_LIT> <EOL> return prob_cloud <EOL> def _calculate_positives_negatives ( target_details ) : <EOL> """<STR_LIT>""" <EOL> true_positive = <NUM_LIT:0> <EOL> true_negative = <NUM_LIT:0> <EOL> false_negative = <NUM_LIT:0> <EOL> false_positive = <NUM_LIT:0> <EOL> actual_positive = <NUM_LIT:0> <EOL> actual_negative = <NUM_LIT:0> <EOL> for idx in range ( len ( target_details ) ) : <EOL> predicted_target = target_details [ idx ] [ "<STR_LIT>" ] <EOL> expected_target = target_details [ idx ] [ "<STR_LIT>" ] <EOL> if expected_target == <NUM_LIT:1> : <EOL> actual_positive = actual_positive + <NUM_LIT:1> <EOL> else : <EOL> actual_negative = actual_negative + <NUM_LIT:1> <EOL> if predicted_target == <NUM_LIT:1> and expected_target == <NUM_LIT:1> : <EOL> true_positive = true_positive + <NUM_LIT:1> <EOL> elif predicted_target == <NUM_LIT:0> and expected_target == <NUM_LIT:0> : <EOL> true_negative = true_negative + <NUM_LIT:1> <EOL> elif predicted_target == <NUM_LIT:1> and expected_target == <NUM_LIT:0> : <EOL> false_positive = false_positive + <NUM_LIT:1> <EOL> elif predicted_target == <NUM_LIT:0> and expected_target == <NUM_LIT:1> : <EOL> false_negative = false_negative + <NUM_LIT:1> <EOL> return { <EOL> "<STR_LIT>" : float ( true_positive ) , <EOL> "<STR_LIT>" : float ( false_positive ) , <EOL> "<STR_LIT>" : float ( actual_positive ) , <EOL> "<STR_LIT>" : float ( true_negative ) , <EOL> "<STR_LIT>" : float ( false_negative ) , <EOL> "<STR_LIT>" : float ( actual_negative ) , <EOL> } <EOL> def _calculate_accuracy ( s ) : <EOL> top = ( s [ "<STR_LIT>" ] + s [ "<STR_LIT>" ] ) <EOL> bottom = ( s [ "<STR_LIT>" ] + s [ "<STR_LIT>" ] ) <EOL> return ( top / bottom ) * <NUM_LIT> <EOL> def _calculate_precision ( s ) : <EOL> return s [ "<STR_LIT>" ] / ( s [ "<STR_LIT>" ] + s [ "<STR_LIT>" ] ) <EOL> def _calculate_recall ( s ) : <EOL> return s [ "<STR_LIT>" ] / ( s [ "<STR_LIT>" ] + s [ "<STR_LIT>" ] ) <EOL> def _calculate_f1 ( precision , recall ) : <EOL> return <NUM_LIT> * ( ( precision * recall ) / ( precision + recall ) ) <EOL> def _print_confusion_matrix ( s ) : <EOL> results = "<STR_LIT>" <EOL> results += "<STR_LIT>" <EOL> results += "<STR_LIT>" <EOL> results += "<STR_LIT>" % ( s [ "<STR_LIT>" ] , s [ "<STR_LIT>" ] , s [ "<STR_LIT>" ] ) <EOL> results += "<STR_LIT>" % ( s [ "<STR_LIT>" ] , s [ "<STR_LIT>" ] , s [ "<STR_LIT>" ] ) <EOL> return results <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> parse_command_line ( ) </s>
