<s> from SimpleCV . base import * <EOL> from SimpleCV . ImageClass import Image , ImageSet <EOL> from SimpleCV . DrawingLayer import * <EOL> from SimpleCV . Features import FeatureExtractorBase <EOL> """<STR_LIT>""" <EOL> class TreeClassifier : <EOL> """<STR_LIT>""" <EOL> mClassNames = [ ] <EOL> mDataSetRaw = [ ] <EOL> mDataSetOrange = [ ] <EOL> mClassifier = None <EOL> mLearner = None <EOL> mTree = None <EOL> mFeatureExtractors = None <EOL> mOrangeDomain = None <EOL> mFlavorParams = None <EOL> mTreeTypeDict = { <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:2> , <EOL> "<STR_LIT>" : <NUM_LIT:3> <EOL> } <EOL> mforestFlavorDict = { <EOL> "<STR_LIT>" : <NUM_LIT:100> , <EOL> "<STR_LIT>" : None <EOL> } <EOL> mBoostedFlavorDict = { <EOL> "<STR_LIT>" : <NUM_LIT:10> , <EOL> } <EOL> mBaggedFlavorDict = { <EOL> "<STR_LIT>" : <NUM_LIT:10> , <EOL> } <EOL> def __init__ ( self , featureExtractors = [ ] , flavor = '<STR_LIT>' , flavorDict = None ) : <EOL> """<STR_LIT>""" <EOL> if not ORANGE_ENABLED : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> self . mClassNames = [ ] <EOL> self . mDataSetRaw = [ ] <EOL> self . mDataSetOrange = [ ] <EOL> self . mClassifier = None <EOL> self . mLearner = None <EOL> self . mTree = None <EOL> self . mFeatureExtractors = None <EOL> self . mOrangeDomain = None <EOL> self . mFlavorParams = None <EOL> self . mFlavor = self . mTreeTypeDict [ flavor ] <EOL> if ( flavorDict is None ) : <EOL> if ( self . mFlavor == self . mTreeTypeDict [ "<STR_LIT>" ] ) : <EOL> self . mFlavorParams = self . mBaggedFlavorDict <EOL> elif ( self . mFlavor == self . mTreeTypeDict [ "<STR_LIT>" ] ) : <EOL> self . mFlavorParams = self . mforestFlavorDict <EOL> elif ( self . mFlavor == self . mTreeTypeDict [ "<STR_LIT>" ] ) : <EOL> self . mFlavorParams = self . mBoostedFlavorDict <EOL> else : <EOL> self . mFlavorParams = flavorDict <EOL> self . mFeatureExtractors = featureExtractors <EOL> def load ( cls , fname ) : <EOL> """<STR_LIT>""" <EOL> return pickle . load ( file ( fname ) ) <EOL> load = classmethod ( load ) <EOL> def save ( self , fname ) : <EOL> """<STR_LIT>""" <EOL> output = open ( fname , '<STR_LIT:wb>' ) <EOL> pickle . dump ( self , output , <NUM_LIT:2> ) <EOL> output . close ( ) <EOL> def __getstate__ ( self ) : <EOL> mydict = self . __dict__ . copy ( ) <EOL> self . mDataSetOrange = None <EOL> del mydict [ '<STR_LIT>' ] <EOL> self . mOrangeDomain = None <EOL> del mydict [ '<STR_LIT>' ] <EOL> self . mLearner = None <EOL> del mydict [ '<STR_LIT>' ] <EOL> self . mTree = None <EOL> del mydict [ '<STR_LIT>' ] <EOL> return mydict <EOL> def __setstate__ ( self , mydict ) : <EOL> self . __dict__ = mydict <EOL> colNames = [ ] <EOL> for extractor in self . mFeatureExtractors : <EOL> colNames . extend ( extractor . getFieldNames ( ) ) <EOL> self . mOrangeDomain = orange . Domain ( map ( orange . FloatVariable , colNames ) , orange . EnumVariable ( "<STR_LIT:type>" , values = self . mClassNames ) ) <EOL> self . mDataSetOrange = orange . ExampleTable ( self . mOrangeDomain , self . mDataSetRaw ) <EOL> if ( self . mFlavor == <NUM_LIT:0> ) : <EOL> self . mLearner = orange . TreeLearner ( ) <EOL> self . mClassifier = self . mLearner ( self . mDataSetOrange ) <EOL> elif ( self . mFlavor == <NUM_LIT:1> ) : <EOL> self . mTree = orange . TreeLearner ( ) <EOL> self . mLearner = orngEnsemble . BaggedLearner ( self . mTree , t = self . mFlavorParams [ "<STR_LIT>" ] ) <EOL> self . mClassifier = self . mLearner ( self . mDataSetOrange ) <EOL> elif ( self . mFlavor == <NUM_LIT:2> ) : <EOL> self . mTree = orange . TreeLearner ( ) <EOL> self . mLearner = orngEnsemble . RandomForestLearner ( trees = self . mFlavorParams [ "<STR_LIT>" ] , attributes = self . mFlavorParams [ "<STR_LIT>" ] ) <EOL> self . mClassifier = self . mLearner ( self . mDataSetOrange ) <EOL> elif ( self . mFlavor == <NUM_LIT:3> ) : <EOL> self . mTree = orange . TreeLearner ( ) <EOL> self . mLearner = orngEnsemble . BoostedLearner ( self . mTree , t = self . mFlavorParams [ "<STR_LIT>" ] ) <EOL> self . mClassifier = self . mLearner ( self . mDataSetOrange ) <EOL> def classify ( self , image ) : <EOL> """<STR_LIT>""" <EOL> featureVector = [ ] <EOL> for extractor in self . mFeatureExtractors : <EOL> feats = extractor . extract ( image ) <EOL> if ( feats is not None ) : <EOL> featureVector . extend ( feats ) <EOL> featureVector . extend ( [ self . mClassNames [ <NUM_LIT:0> ] ] ) <EOL> test = orange . ExampleTable ( self . mOrangeDomain , [ featureVector ] ) <EOL> c = self . mClassifier ( test [ <NUM_LIT:0> ] ) <EOL> return str ( c ) <EOL> def setFeatureExtractors ( self , extractors ) : <EOL> """<STR_LIT>""" <EOL> self . mFeatureExtractors = extractors <EOL> return None <EOL> def _trainPath ( self , path , className , subset , disp , verbose ) : <EOL> count = <NUM_LIT:0> <EOL> files = [ ] <EOL> for ext in IMAGE_FORMATS : <EOL> files . extend ( glob . glob ( os . path . join ( path , ext ) ) ) <EOL> if ( subset > <NUM_LIT:0> ) : <EOL> nfiles = min ( subset , len ( files ) ) <EOL> else : <EOL> nfiles = len ( files ) <EOL> badFeat = False <EOL> for i in range ( nfiles ) : <EOL> infile = files [ i ] <EOL> if verbose : <EOL> print "<STR_LIT>" + infile <EOL> img = Image ( infile ) <EOL> featureVector = [ ] <EOL> for extractor in self . mFeatureExtractors : <EOL> feats = extractor . extract ( img ) <EOL> if ( feats is not None ) : <EOL> featureVector . extend ( feats ) <EOL> else : <EOL> badFeat = True <EOL> if ( badFeat ) : <EOL> badFeat = False <EOL> continue <EOL> featureVector . extend ( [ className ] ) <EOL> self . mDataSetRaw . append ( featureVector ) <EOL> text = '<STR_LIT>' + className <EOL> self . _WriteText ( disp , img , text , Color . WHITE ) <EOL> count = count + <NUM_LIT:1> <EOL> del img <EOL> return count <EOL> def _trainImageSet ( self , imageset , className , subset , disp , verbose ) : <EOL> count = <NUM_LIT:0> <EOL> badFeat = False <EOL> if ( subset > <NUM_LIT:0> ) : <EOL> imageset = imageset [ <NUM_LIT:0> : subset ] <EOL> for img in imageset : <EOL> if verbose : <EOL> print "<STR_LIT>" + img . filename <EOL> featureVector = [ ] <EOL> for extractor in self . mFeatureExtractors : <EOL> feats = extractor . extract ( img ) <EOL> if ( feats is not None ) : <EOL> featureVector . extend ( feats ) <EOL> else : <EOL> badFeat = True <EOL> if ( badFeat ) : <EOL> badFeat = False <EOL> continue <EOL> featureVector . extend ( [ className ] ) <EOL> self . mDataSetRaw . append ( featureVector ) <EOL> text = '<STR_LIT>' + className <EOL> self . _WriteText ( disp , img , text , Color . WHITE ) <EOL> count = count + <NUM_LIT:1> <EOL> del img <EOL> return count <EOL> def train ( self , images , classNames , disp = None , subset = - <NUM_LIT:1> , savedata = None , verbose = True ) : <EOL> """<STR_LIT>""" <EOL> count = <NUM_LIT:0> <EOL> self . mClassNames = classNames <EOL> for i in range ( len ( classNames ) ) : <EOL> if ( isinstance ( images [ i ] , str ) ) : <EOL> count = count + self . _trainPath ( images [ i ] , classNames [ i ] , subset , disp , verbose ) <EOL> else : <EOL> count = count + self . _trainImageSet ( images [ i ] , classNames [ i ] , subset , disp , verbose ) <EOL> colNames = [ ] <EOL> for extractor in self . mFeatureExtractors : <EOL> colNames . extend ( extractor . getFieldNames ( ) ) <EOL> if ( count <= <NUM_LIT:0> ) : <EOL> logger . warning ( "<STR_LIT>" ) <EOL> return None <EOL> self . mOrangeDomain = orange . Domain ( map ( orange . FloatVariable , colNames ) , orange . EnumVariable ( "<STR_LIT:type>" , values = self . mClassNames ) ) <EOL> self . mDataSetOrange = orange . ExampleTable ( self . mOrangeDomain , self . mDataSetRaw ) <EOL> if ( savedata is not None ) : <EOL> orange . saveTabDelimited ( savedata , self . mDataSetOrange ) <EOL> if ( self . mFlavor == <NUM_LIT:0> ) : <EOL> self . mLearner = orange . TreeLearner ( ) <EOL> self . mClassifier = self . mLearner ( self . mDataSetOrange ) <EOL> elif ( self . mFlavor == <NUM_LIT:1> ) : <EOL> self . mTree = orange . TreeLearner ( ) <EOL> self . mLearner = orngEnsemble . BaggedLearner ( self . mTree , t = self . mFlavorParams [ "<STR_LIT>" ] ) <EOL> self . mClassifier = self . mLearner ( self . mDataSetOrange ) <EOL> elif ( self . mFlavor == <NUM_LIT:2> ) : <EOL> self . mTree = orange . TreeLearner ( ) <EOL> self . mLearner = orngEnsemble . RandomForestLearner ( trees = self . mFlavorParams [ "<STR_LIT>" ] , attributes = self . mFlavorParams [ "<STR_LIT>" ] ) <EOL> self . mClassifier = self . mLearner ( self . mDataSetOrange ) <EOL> elif ( self . mFlavor == <NUM_LIT:3> ) : <EOL> self . mTree = orange . TreeLearner ( ) <EOL> self . mLearner = orngEnsemble . BoostedLearner ( self . mTree , t = self . mFlavorParams [ "<STR_LIT>" ] ) <EOL> self . mClassifier = self . mLearner ( self . mDataSetOrange ) <EOL> correct = <NUM_LIT:0> <EOL> incorrect = <NUM_LIT:0> <EOL> for i in range ( count ) : <EOL> c = self . mClassifier ( self . mDataSetOrange [ i ] ) <EOL> test = self . mDataSetOrange [ i ] . getclass ( ) <EOL> if verbose : <EOL> print "<STR_LIT>" , test , "<STR_LIT>" , c <EOL> if ( test == c ) : <EOL> correct = correct + <NUM_LIT:1> <EOL> else : <EOL> incorrect = incorrect + <NUM_LIT:1> <EOL> good = <NUM_LIT:100> * ( float ( correct ) / float ( count ) ) <EOL> bad = <NUM_LIT:100> * ( float ( incorrect ) / float ( count ) ) <EOL> confusion = <NUM_LIT:0> <EOL> if ( len ( self . mClassNames ) > <NUM_LIT:2> ) : <EOL> crossValidator = orngTest . learnAndTestOnLearnData ( [ self . mLearner ] , self . mDataSetOrange ) <EOL> confusion = orngStat . confusionMatrices ( crossValidator ) [ <NUM_LIT:0> ] <EOL> if verbose : <EOL> print ( "<STR_LIT>" + str ( good ) ) <EOL> print ( "<STR_LIT>" + str ( bad ) ) <EOL> if ( confusion != <NUM_LIT:0> ) : <EOL> classes = self . mDataSetOrange . domain . classVar . values <EOL> print "<STR_LIT:\t>" + "<STR_LIT:\t>" . join ( classes ) <EOL> for className , classConfusions in zip ( classes , confusion ) : <EOL> print ( "<STR_LIT:%s>" + ( "<STR_LIT>" * len ( classes ) ) ) % ( ( className , ) + tuple ( classConfusions ) ) <EOL> if ( self . mFlavor == <NUM_LIT:0> ) : <EOL> self . _PrintTree ( self . mClassifier ) <EOL> return [ good , bad , confusion ] <EOL> def test ( self , images , classNames , disp = None , subset = - <NUM_LIT:1> , savedata = None , verbose = True ) : <EOL> """<STR_LIT>""" <EOL> count = <NUM_LIT:0> <EOL> correct = <NUM_LIT:0> <EOL> self . mClassNames = classNames <EOL> colNames = [ ] <EOL> for extractor in self . mFeatureExtractors : <EOL> colNames . extend ( extractor . getFieldNames ( ) ) <EOL> if ( self . mOrangeDomain is None ) : <EOL> self . mOrangeDomain = orange . Domain ( map ( orange . FloatVariable , colNames ) , orange . EnumVariable ( "<STR_LIT:type>" , values = self . mClassNames ) ) <EOL> dataset = [ ] <EOL> for i in range ( len ( classNames ) ) : <EOL> if ( isinstance ( images [ i ] , str ) ) : <EOL> [ dataset , cnt , crct ] = self . _testPath ( images [ i ] , classNames [ i ] , dataset , subset , disp , verbose ) <EOL> count = count + cnt <EOL> correct = correct + crct <EOL> else : <EOL> [ dataset , cnt , crct ] = self . _testImageSet ( images [ i ] , classNames [ i ] , dataset , subset , disp , verbose ) <EOL> count = count + cnt <EOL> correct = correct + crct <EOL> testData = orange . ExampleTable ( self . mOrangeDomain , dataset ) <EOL> if savedata is not None : <EOL> orange . saveTabDelimited ( savedata , testData ) <EOL> confusion = <NUM_LIT:0> <EOL> if ( len ( self . mClassNames ) > <NUM_LIT:2> ) : <EOL> crossValidator = orngTest . learnAndTestOnTestData ( [ self . mLearner ] , self . mDataSetOrange , testData ) <EOL> confusion = orngStat . confusionMatrices ( crossValidator ) [ <NUM_LIT:0> ] <EOL> good = <NUM_LIT:100> * ( float ( correct ) / float ( count ) ) <EOL> bad = <NUM_LIT:100> * ( float ( count - correct ) / float ( count ) ) <EOL> if verbose : <EOL> print ( "<STR_LIT>" + str ( good ) ) <EOL> print ( "<STR_LIT>" + str ( bad ) ) <EOL> if ( confusion != <NUM_LIT:0> ) : <EOL> classes = self . mDataSetOrange . domain . classVar . values <EOL> print "<STR_LIT:\t>" + "<STR_LIT:\t>" . join ( classes ) <EOL> for className , classConfusions in zip ( classes , confusion ) : <EOL> print ( "<STR_LIT:%s>" + ( "<STR_LIT>" * len ( classes ) ) ) % ( ( className , ) + tuple ( classConfusions ) ) <EOL> return [ good , bad , confusion ] <EOL> def _testPath ( self , path , className , dataset , subset , disp , verbose ) : <EOL> count = <NUM_LIT:0> <EOL> correct = <NUM_LIT:0> <EOL> badFeat = False <EOL> files = [ ] <EOL> for ext in IMAGE_FORMATS : <EOL> files . extend ( glob . glob ( os . path . join ( path , ext ) ) ) <EOL> if ( subset > <NUM_LIT:0> ) : <EOL> nfiles = min ( subset , len ( files ) ) <EOL> else : <EOL> nfiles = len ( files ) <EOL> for i in range ( nfiles ) : <EOL> infile = files [ i ] <EOL> if verbose : <EOL> print "<STR_LIT>" + infile <EOL> img = Image ( infile ) <EOL> featureVector = [ ] <EOL> for extractor in self . mFeatureExtractors : <EOL> feats = extractor . extract ( img ) <EOL> if ( feats is not None ) : <EOL> featureVector . extend ( feats ) <EOL> else : <EOL> badFeat = True <EOL> if ( badFeat ) : <EOL> del img <EOL> badFeat = False <EOL> continue <EOL> featureVector . extend ( [ className ] ) <EOL> dataset . append ( featureVector ) <EOL> test = orange . ExampleTable ( self . mOrangeDomain , [ featureVector ] ) <EOL> c = self . mClassifier ( test [ <NUM_LIT:0> ] ) <EOL> testClass = test [ <NUM_LIT:0> ] . getclass ( ) <EOL> if ( testClass == c ) : <EOL> text = "<STR_LIT>" + str ( c ) <EOL> self . _WriteText ( disp , img , text , Color . GREEN ) <EOL> correct = correct + <NUM_LIT:1> <EOL> else : <EOL> text = "<STR_LIT>" + str ( c ) <EOL> self . _WriteText ( disp , img , text , Color . RED ) <EOL> count = count + <NUM_LIT:1> <EOL> del img <EOL> return ( [ dataset , count , correct ] ) <EOL> def _testImageSet ( self , imageset , className , dataset , subset , disp , verbose ) : <EOL> count = <NUM_LIT:0> <EOL> correct = <NUM_LIT:0> <EOL> badFeat = False <EOL> if ( subset > <NUM_LIT:0> ) : <EOL> imageset = imageset [ <NUM_LIT:0> : subset ] <EOL> for img in imageset : <EOL> if verbose : <EOL> print "<STR_LIT>" + img . filename <EOL> featureVector = [ ] <EOL> for extractor in self . mFeatureExtractors : <EOL> feats = extractor . extract ( img ) <EOL> if ( feats is not None ) : <EOL> featureVector . extend ( feats ) <EOL> else : <EOL> badFeat = True <EOL> if ( badFeat ) : <EOL> del img <EOL> badFeat = False <EOL> continue <EOL> featureVector . extend ( [ className ] ) <EOL> dataset . append ( featureVector ) <EOL> test = orange . ExampleTable ( self . mOrangeDomain , [ featureVector ] ) <EOL> c = self . mClassifier ( test [ <NUM_LIT:0> ] ) <EOL> testClass = test [ <NUM_LIT:0> ] . getclass ( ) <EOL> if ( testClass == c ) : <EOL> text = "<STR_LIT>" + str ( c ) <EOL> self . _WriteText ( disp , img , text , Color . GREEN ) <EOL> correct = correct + <NUM_LIT:1> <EOL> else : <EOL> text = "<STR_LIT>" + str ( c ) <EOL> self . _WriteText ( disp , img , text , Color . RED ) <EOL> count = count + <NUM_LIT:1> <EOL> del img <EOL> return ( [ dataset , count , correct ] ) <EOL> def _WriteText ( self , disp , img , txt , color ) : <EOL> if ( disp is not None ) : <EOL> txt = '<STR_LIT:U+0020>' + txt + '<STR_LIT:U+0020>' <EOL> img = img . adaptiveScale ( disp . resolution ) <EOL> layer = DrawingLayer ( ( img . width , img . height ) ) <EOL> layer . setFontSize ( <NUM_LIT> ) <EOL> layer . ezViewText ( txt , ( <NUM_LIT:20> , <NUM_LIT:20> ) , fgcolor = color ) <EOL> img . addDrawingLayer ( layer ) <EOL> img . applyLayers ( ) <EOL> img . save ( disp ) <EOL> def _PrintTree ( self , x ) : <EOL> if type ( x ) == orange . TreeClassifier : <EOL> self . _PrintTree0 ( x . tree , <NUM_LIT:0> ) <EOL> elif type ( x ) == orange . TreeNode : <EOL> self . _PrintTree0 ( x , <NUM_LIT:0> ) <EOL> else : <EOL> raise TypeError , "<STR_LIT>" <EOL> def _PrintTree0 ( self , node , level ) : <EOL> if not node : <EOL> print "<STR_LIT:U+0020>" * level + "<STR_LIT>" <EOL> return <EOL> if node . branchSelector : <EOL> nodeDesc = node . branchSelector . classVar . name <EOL> nodeCont = node . distribution <EOL> print "<STR_LIT:\n>" + "<STR_LIT:U+0020>" * level + "<STR_LIT>" % ( nodeDesc , nodeCont ) , <EOL> for i in range ( len ( node . branches ) ) : <EOL> print "<STR_LIT:\n>" + "<STR_LIT:U+0020>" * level + "<STR_LIT>" % node . branchDescriptions [ i ] , <EOL> self . _PrintTree0 ( node . branches [ i ] , level + <NUM_LIT:1> ) <EOL> else : <EOL> nodeCont = node . distribution <EOL> majorClass = node . nodeClassifier . defaultValue <EOL> print "<STR_LIT>" % ( majorClass , nodeCont ) </s>
