<s> '''<STR_LIT>''' <EOL> import pyvision as pv <EOL> import cv2 <EOL> import numpy as np <EOL> import tempfile <EOL> import shutil <EOL> import os <EOL> class StatsModelWrapper ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , model ) : <EOL> '''<STR_LIT>''' <EOL> self . model = model <EOL> def predict ( self , * args , ** kwarg ) : <EOL> '''<STR_LIT>''' <EOL> return self . model . predict ( * args , ** kwarg ) <EOL> def predict_prob ( self , * args , ** kwarg ) : <EOL> '''<STR_LIT>''' <EOL> return self . model . predict_prob ( * args , ** kwarg ) <EOL> def predict_all ( self , * args , ** kwarg ) : <EOL> '''<STR_LIT>''' <EOL> return self . model . predict_all ( * args , ** kwarg ) <EOL> def __getstate__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> state = { } <EOL> state [ '<STR_LIT>' ] = str ( self . model . __class__ ) . split ( "<STR_LIT:'>" ) [ - <NUM_LIT:2> ] <EOL> filename = tempfile . mktemp ( suffix = '<STR_LIT>' , prefix = '<STR_LIT>' ) <EOL> self . model . save ( filename ) <EOL> data = open ( filename , '<STR_LIT:rb>' ) . read ( ) <EOL> state [ '<STR_LIT>' ] = data <EOL> for key , value in self . __dict__ . iteritems ( ) : <EOL> if key != '<STR_LIT>' : <EOL> state [ key ] = value <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> '''<STR_LIT>''' <EOL> self . model = eval ( state [ '<STR_LIT>' ] + "<STR_LIT>" ) <EOL> filename = tempfile . mktemp ( suffix = '<STR_LIT>' , prefix = '<STR_LIT>' ) <EOL> open ( filename , '<STR_LIT:wb>' ) . write ( state [ '<STR_LIT>' ] ) <EOL> self . model . load ( filename ) <EOL> os . remove ( filename ) <EOL> for key , value in state . iteritems ( ) : <EOL> if key not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> setattr ( self , key , value ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> self . model . save ( * args , ** kwargs ) <EOL> def svc_rbf ( data , responses ) : <EOL> '''<STR_LIT>''' <EOL> data = np . float32 ( data ) <EOL> responses = np . float32 ( responses ) <EOL> params = dict ( kernel_type = cv2 . SVM_RBF , svm_type = cv2 . SVM_C_SVC ) <EOL> model = cv2 . SVM ( ) <EOL> model . train_auto ( data , responses , None , None , params ) <EOL> return StatsModelWrapper ( model ) <EOL> def svc_linear ( data , responses ) : <EOL> '''<STR_LIT>''' <EOL> np . float32 ( data ) <EOL> np . float32 ( responses ) <EOL> params = dict ( kernel_type = cv2 . SVM_LINEAR , svm_type = cv2 . SVM_C_SVC ) <EOL> model = cv2 . SVM ( ) <EOL> model . train_auto ( data , responses , None , None , params ) <EOL> return StatsModelWrapper ( model ) <EOL> def svr_rbf ( data , responses ) : <EOL> '''<STR_LIT>''' <EOL> np . float32 ( data ) <EOL> np . float32 ( responses ) <EOL> params = dict ( kernel_type = cv2 . SVM_RBF , svm_type = cv2 . SVM_EPS_SVR , p = <NUM_LIT:1.0> ) <EOL> model = cv2 . SVM ( ) <EOL> model . train_auto ( data , responses , None , None , params ) <EOL> return StatsModelWrapper ( model ) <EOL> def svr_linear ( data , responses ) : <EOL> '''<STR_LIT>''' <EOL> np . float32 ( data ) <EOL> np . float32 ( responses ) <EOL> params = dict ( kernel_type = cv2 . SVM_LINEAR , svm_type = cv2 . SVM_EPS_SVR , p = <NUM_LIT:1.0> ) <EOL> model = cv2 . SVM ( ) <EOL> model . train_auto ( data , responses , None , None , params ) <EOL> return StatsModelWrapper ( model ) <EOL> def random_forest ( data , responses , n_trees = <NUM_LIT:100> ) : <EOL> '''<STR_LIT>''' <EOL> np . float32 ( data ) <EOL> np . float32 ( responses ) <EOL> params = dict ( max_num_of_trees_in_the_forest = n_trees , termcrit_type = cv2 . TERM_CRITERIA_MAX_ITER ) <EOL> model = cv2 . RTrees ( ) <EOL> model . train ( data , cv2 . CV_ROW_SAMPLE , responses , params = params ) <EOL> return StatsModelWrapper ( model ) <EOL> def boost ( data , responses , weak_count = <NUM_LIT:100> , max_depth = <NUM_LIT:20> , boost_type = cv2 . BOOST_DISCRETE ) : <EOL> '''<STR_LIT>''' <EOL> np . float32 ( data ) <EOL> np . float32 ( responses ) <EOL> params = dict ( boost_type = boost_type , weak_count = weak_count , max_depth = max_depth ) <EOL> model = cv2 . Boost ( ) <EOL> model . train ( data , cv2 . CV_ROW_SAMPLE , responses , params = params ) <EOL> return StatsModelWrapper ( model ) <EOL> def gbtrees ( data , responses , n_trees = <NUM_LIT:100> ) : <EOL> '''<STR_LIT>''' <EOL> np . float32 ( data ) <EOL> np . float32 ( responses ) <EOL> params = dict ( max_num_of_trees_in_the_forest = n_trees , termcrit_type = cv2 . TERM_CRITERIA_MAX_ITER ) <EOL> model = cv2 . GBTrees ( ) <EOL> model . train ( data , cv2 . CV_ROW_SAMPLE , responses , params = params ) <EOL> return StatsModelWrapper ( model ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> labels = np . float32 ( ( pv . IRIS_LABELS == '<STR_LIT>' ) + <NUM_LIT:2> * ( pv . IRIS_LABELS == '<STR_LIT>' ) ) <EOL> model = svc_rbf ( pv . IRIS_DATA [ <NUM_LIT:0> : : <NUM_LIT:2> , : ] , labels [ <NUM_LIT:0> : : <NUM_LIT:2> ] ) <EOL> import cPickle as pkl <EOL> buf = pkl . dumps ( model ) <EOL> model = pkl . loads ( buf ) <EOL> print "<STR_LIT>" , np . float32 ( [ model . predict ( s ) for s in pv . IRIS_DATA [ <NUM_LIT:1> : : <NUM_LIT:2> , : ] ] ) <EOL> print "<STR_LIT>" , model . predict_all ( pv . IRIS_DATA [ <NUM_LIT:1> : : <NUM_LIT:2> , : ] ) <EOL> assert <NUM_LIT:0> </s>
