<s> import tensorflow as tf <EOL> import numpy as np <EOL> import os <EOL> import argparse <EOL> import cPickle as pkl <EOL> from tensorpack . train import TrainConfig , start_train <EOL> from tensorpack . predict import PredictConfig , get_predict_func <EOL> from tensorpack . models import * <EOL> from tensorpack . utils import * <EOL> from tensorpack . tfutils import * <EOL> from tensorpack . tfutils . symbolic_functions import * <EOL> from tensorpack . tfutils . summary import * <EOL> from tensorpack . callbacks import * <EOL> from tensorpack . dataflow import * <EOL> from tensorpack . dataflow . dataset import ILSVRCMeta <EOL> """<STR_LIT>""" <EOL> class Model ( ModelDesc ) : <EOL> def _get_input_vars ( self ) : <EOL> return [ InputVar ( tf . float32 , ( None , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:3> ) , '<STR_LIT:input>' ) , <EOL> InputVar ( tf . int32 , ( None , ) , '<STR_LIT:label>' ) ] <EOL> def _get_cost ( self , inputs , is_training ) : <EOL> is_training = bool ( is_training ) <EOL> keep_prob = tf . constant ( <NUM_LIT:0.5> if is_training else <NUM_LIT:1.0> ) <EOL> image , label = inputs <EOL> with argscope ( Conv2D , kernel_shape = <NUM_LIT:3> ) : <EOL> l = Conv2D ( '<STR_LIT>' , image , <NUM_LIT:64> ) <EOL> l = Conv2D ( '<STR_LIT>' , l , <NUM_LIT:64> ) <EOL> l = MaxPooling ( '<STR_LIT>' , l , <NUM_LIT:2> , stride = <NUM_LIT:2> , padding = '<STR_LIT>' ) <EOL> l = Conv2D ( '<STR_LIT>' , l , <NUM_LIT> ) <EOL> l = Conv2D ( '<STR_LIT>' , l , <NUM_LIT> ) <EOL> l = MaxPooling ( '<STR_LIT>' , l , <NUM_LIT:2> , stride = <NUM_LIT:2> , padding = '<STR_LIT>' ) <EOL> l = Conv2D ( '<STR_LIT>' , l , <NUM_LIT> ) <EOL> l = Conv2D ( '<STR_LIT>' , l , <NUM_LIT> ) <EOL> l = Conv2D ( '<STR_LIT>' , l , <NUM_LIT> ) <EOL> l = MaxPooling ( '<STR_LIT>' , l , <NUM_LIT:2> , stride = <NUM_LIT:2> , padding = '<STR_LIT>' ) <EOL> l = Conv2D ( '<STR_LIT>' , l , <NUM_LIT> ) <EOL> l = Conv2D ( '<STR_LIT>' , l , <NUM_LIT> ) <EOL> l = Conv2D ( '<STR_LIT>' , l , <NUM_LIT> ) <EOL> l = MaxPooling ( '<STR_LIT>' , l , <NUM_LIT:2> , stride = <NUM_LIT:2> , padding = '<STR_LIT>' ) <EOL> l = Conv2D ( '<STR_LIT>' , l , <NUM_LIT> ) <EOL> l = Conv2D ( '<STR_LIT>' , l , <NUM_LIT> ) <EOL> l = Conv2D ( '<STR_LIT>' , l , <NUM_LIT> ) <EOL> l = MaxPooling ( '<STR_LIT>' , l , <NUM_LIT:2> , stride = <NUM_LIT:2> , padding = '<STR_LIT>' ) <EOL> l = FullyConnected ( '<STR_LIT>' , l , <NUM_LIT> ) <EOL> l = tf . nn . dropout ( l , keep_prob ) <EOL> l = FullyConnected ( '<STR_LIT>' , l , <NUM_LIT> ) <EOL> l = tf . nn . dropout ( l , keep_prob ) <EOL> logits = FullyConnected ( '<STR_LIT>' , l , out_dim = <NUM_LIT:1000> , nl = tf . identity ) <EOL> prob = tf . nn . softmax ( logits , name = '<STR_LIT>' ) <EOL> cost = tf . nn . sparse_softmax_cross_entropy_with_logits ( logits , label ) <EOL> cost = tf . reduce_mean ( cost , name = '<STR_LIT>' ) <EOL> return cost <EOL> def run_test ( path , input ) : <EOL> param_dict = np . load ( path ) . item ( ) <EOL> pred_config = PredictConfig ( <EOL> model = Model ( ) , <EOL> input_data_mapping = [ <NUM_LIT:0> ] , <EOL> session_init = ParamRestore ( param_dict ) , <EOL> output_var_names = [ '<STR_LIT>' ] <EOL> ) <EOL> predict_func = get_predict_func ( pred_config ) <EOL> import cv2 <EOL> im = cv2 . imread ( input ) <EOL> assert im is not None <EOL> im = cv2 . cvtColor ( im , cv2 . COLOR_BGR2RGB ) <EOL> im = cv2 . resize ( im , ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> im = np . reshape ( im , ( <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:3> ) ) . astype ( '<STR_LIT>' ) <EOL> im = im - <NUM_LIT> <EOL> outputs = predict_func ( [ im ] ) [ <NUM_LIT:0> ] <EOL> prob = outputs [ <NUM_LIT:0> ] <EOL> ret = prob . argsort ( ) [ - <NUM_LIT:10> : ] [ : : - <NUM_LIT:1> ] <EOL> print ret <EOL> meta = ILSVRCMeta ( ) . get_synset_words_1000 ( ) <EOL> print [ meta [ k ] for k in ret ] <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT:0>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , required = True , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , help = '<STR_LIT>' , required = True ) <EOL> args = parser . parse_args ( ) <EOL> if args . gpu : <EOL> os . environ [ '<STR_LIT>' ] = args . gpu <EOL> run_test ( args . load , args . input ) </s>
