<s> """<STR_LIT>""" <EOL> import argparse <EOL> import os <EOL> import time <EOL> from google . protobuf import text_format <EOL> import numpy as np <EOL> import PIL . Image <EOL> import scipy . misc <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT:2>' <EOL> import caffe <EOL> from caffe . proto import caffe_pb2 <EOL> def get_net ( caffemodel , deploy_file , use_gpu = True ) : <EOL> """<STR_LIT>""" <EOL> if use_gpu : <EOL> caffe . set_mode_gpu ( ) <EOL> return caffe . Net ( deploy_file , caffemodel , caffe . TEST ) <EOL> def get_transformer ( deploy_file , mean_file = None ) : <EOL> """<STR_LIT>""" <EOL> network = caffe_pb2 . NetParameter ( ) <EOL> with open ( deploy_file ) as infile : <EOL> text_format . Merge ( infile . read ( ) , network ) <EOL> if network . input_shape : <EOL> dims = network . input_shape [ <NUM_LIT:0> ] . dim <EOL> else : <EOL> dims = network . input_dim [ : <NUM_LIT:4> ] <EOL> t = caffe . io . Transformer ( <EOL> inputs = { '<STR_LIT:data>' : dims } <EOL> ) <EOL> t . set_transpose ( '<STR_LIT:data>' , ( <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> if dims [ <NUM_LIT:1> ] == <NUM_LIT:3> : <EOL> t . set_channel_swap ( '<STR_LIT:data>' , ( <NUM_LIT:2> , <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> if mean_file : <EOL> with open ( mean_file , '<STR_LIT:rb>' ) as infile : <EOL> blob = caffe_pb2 . BlobProto ( ) <EOL> blob . MergeFromString ( infile . read ( ) ) <EOL> if blob . HasField ( '<STR_LIT>' ) : <EOL> blob_dims = blob . shape <EOL> assert len ( blob_dims ) == <NUM_LIT:4> , '<STR_LIT>' % blob . shape <EOL> elif blob . HasField ( '<STR_LIT>' ) and blob . HasField ( '<STR_LIT>' ) and blob . HasField ( '<STR_LIT>' ) and blob . HasField ( '<STR_LIT:width>' ) : <EOL> blob_dims = ( blob . num , blob . channels , blob . height , blob . width ) <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> pixel = np . reshape ( blob . data , blob_dims [ <NUM_LIT:1> : ] ) . mean ( <NUM_LIT:1> ) . mean ( <NUM_LIT:1> ) <EOL> t . set_mean ( '<STR_LIT:data>' , pixel ) <EOL> return t <EOL> def load_image ( path , height , width , mode = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> image = PIL . Image . open ( path ) <EOL> image = image . convert ( mode ) <EOL> image = np . array ( image ) <EOL> image = scipy . misc . imresize ( image , ( height , width ) , '<STR_LIT>' ) <EOL> return image <EOL> def forward_pass ( images , net , transformer , batch_size = None ) : <EOL> """<STR_LIT>""" <EOL> if batch_size is None : <EOL> batch_size = <NUM_LIT:1> <EOL> caffe_images = [ ] <EOL> for image in images : <EOL> if image . ndim == <NUM_LIT:2> : <EOL> caffe_images . append ( image [ : , : , np . newaxis ] ) <EOL> else : <EOL> caffe_images . append ( image ) <EOL> dims = transformer . inputs [ '<STR_LIT:data>' ] [ <NUM_LIT:1> : ] <EOL> scores = None <EOL> for chunk in [ caffe_images [ x : x + batch_size ] for x in xrange ( <NUM_LIT:0> , len ( caffe_images ) , batch_size ) ] : <EOL> new_shape = ( len ( chunk ) , ) + tuple ( dims ) <EOL> if net . blobs [ '<STR_LIT:data>' ] . data . shape != new_shape : <EOL> net . blobs [ '<STR_LIT:data>' ] . reshape ( * new_shape ) <EOL> for index , image in enumerate ( chunk ) : <EOL> image_data = transformer . preprocess ( '<STR_LIT:data>' , image ) <EOL> net . blobs [ '<STR_LIT:data>' ] . data [ index ] = image_data <EOL> start = time . time ( ) <EOL> output = net . forward ( ) [ net . outputs [ - <NUM_LIT:1> ] ] <EOL> end = time . time ( ) <EOL> if scores is None : <EOL> scores = np . copy ( output ) <EOL> else : <EOL> scores = np . vstack ( ( scores , output ) ) <EOL> print '<STR_LIT>' % ( len ( scores ) , len ( caffe_images ) , ( end - start ) ) <EOL> return scores <EOL> def read_labels ( labels_file ) : <EOL> """<STR_LIT>""" <EOL> if not labels_file : <EOL> print '<STR_LIT>' <EOL> return None <EOL> labels = [ ] <EOL> with open ( labels_file ) as infile : <EOL> for line in infile : <EOL> label = line . strip ( ) <EOL> if label : <EOL> labels . append ( label ) <EOL> assert len ( labels ) , '<STR_LIT>' <EOL> return labels <EOL> def classify ( caffemodel , deploy_file , image_files , <EOL> mean_file = None , labels_file = None , batch_size = None , use_gpu = True ) : <EOL> """<STR_LIT>""" <EOL> net = get_net ( caffemodel , deploy_file , use_gpu ) <EOL> transformer = get_transformer ( deploy_file , mean_file ) <EOL> _ , channels , height , width = transformer . inputs [ '<STR_LIT:data>' ] <EOL> if channels == <NUM_LIT:3> : <EOL> mode = '<STR_LIT>' <EOL> elif channels == <NUM_LIT:1> : <EOL> mode = '<STR_LIT:L>' <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % channels ) <EOL> images = [ load_image ( image_file , height , width , mode ) for image_file in image_files ] <EOL> labels = read_labels ( labels_file ) <EOL> scores = forward_pass ( images , net , transformer , batch_size = batch_size ) <EOL> indices = ( - scores ) . argsort ( ) [ : , : <NUM_LIT:5> ] <EOL> classifications = [ ] <EOL> for image_index , index_list in enumerate ( indices ) : <EOL> result = [ ] <EOL> for i in index_list : <EOL> if labels is None : <EOL> label = '<STR_LIT>' % i <EOL> else : <EOL> label = labels [ i ] <EOL> result . append ( ( label , round ( <NUM_LIT> * scores [ image_index , i ] , <NUM_LIT:4> ) ) ) <EOL> classifications . append ( result ) <EOL> for index , classification in enumerate ( classifications ) : <EOL> print '<STR_LIT>' . format ( '<STR_LIT>' % image_files [ index ] ) <EOL> for label , confidence in classification : <EOL> print '<STR_LIT>' . format ( confidence / <NUM_LIT> , label ) <EOL> print <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> script_start_time = time . time ( ) <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> nargs = '<STR_LIT:+>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> type = int ) <EOL> parser . add_argument ( '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = "<STR_LIT>" ) <EOL> args = vars ( parser . parse_args ( ) ) <EOL> classify ( args [ '<STR_LIT>' ] , args [ '<STR_LIT>' ] , args [ '<STR_LIT>' ] , <EOL> args [ '<STR_LIT>' ] , args [ '<STR_LIT>' ] , args [ '<STR_LIT>' ] , not args [ '<STR_LIT>' ] ) <EOL> print '<STR_LIT>' % ( time . time ( ) - script_start_time , ) </s>
