<s> import sys <EOL> import os <EOL> import nn <EOL> import numpy as np <EOL> import imageqa_test as it <EOL> import imageqa_render as ir <EOL> import imageqa_ensemble as ie <EOL> nameList = [ '<STR_LIT:object>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:location>' ] <EOL> def getCatName ( i ) : <EOL> return nameList [ i ] <EOL> def getBinName ( n ) : <EOL> bin = [ ] <EOL> for k in range ( numModels ) : <EOL> bin . append ( str ( n >> ( numModels - k - <NUM_LIT:1> ) ) ) <EOL> n = n & ( ~ ( <NUM_LIT:1> << ( numModels - k - <NUM_LIT:1> ) ) ) <EOL> return '<STR_LIT>' . join ( bin ) <EOL> def getName ( catName , binName ) : <EOL> return catName + '<STR_LIT:->' + binName <EOL> def renderIndex ( modelNames , numCategories , bins ) : <EOL> htmlList = [ ] <EOL> htmlList . append ( '<STR_LIT>' % '<STR_LIT>' ) <EOL> numModels = len ( modelNames ) <EOL> numCorrect = <NUM_LIT:1> << numModels <EOL> htmlList . append ( '<STR_LIT>' ) <EOL> htmlList . append ( '<STR_LIT>' ) <EOL> htmlList . append ( '<STR_LIT>' ) <EOL> for i in range ( numCategories ) : <EOL> htmlList . append ( '<STR_LIT>' % getCatName ( i ) ) <EOL> htmlList . append ( '<STR_LIT>' ) <EOL> for j in range ( numCorrect ) : <EOL> htmlList . append ( '<STR_LIT>' ) <EOL> binId = numCorrect * i + j <EOL> for k , c in enumerate ( getBinName ( j ) ) : <EOL> htmlList . append ( '<STR_LIT>' ) <EOL> if c == '<STR_LIT:1>' : <EOL> htmlList . append ( <EOL> '<STR_LIT>' % modelNames [ k ] ) <EOL> elif c == '<STR_LIT:0>' : <EOL> htmlList . append ( <EOL> '<STR_LIT>' % modelNames [ k ] ) <EOL> htmlList . append ( '<STR_LIT>' ) <EOL> htmlList . append ( '<STR_LIT>' % len ( bins [ binId ] ) ) <EOL> htmlList . append ( '<STR_LIT>' % getName ( getCatName ( i ) , getBinName ( j ) ) ) <EOL> htmlList . append ( '<STR_LIT>' ) <EOL> htmlList . append ( '<STR_LIT>' ) <EOL> htmlList . append ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' . join ( htmlList ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> """<STR_LIT>""" <EOL> params = ir . parseComparativeParams ( sys . argv ) <EOL> urlDict = ir . loadImgUrl ( params [ '<STR_LIT>' ] , params [ '<STR_LIT>' ] ) <EOL> data = it . loadDataset ( params [ '<STR_LIT>' ] ) <EOL> print ( '<STR_LIT>' ) <EOL> inputTest = data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> targetTest = data [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> questionTypeArray = data [ '<STR_LIT>' ] <EOL> modelOutputs = ie . runAllModels ( <EOL> inputTest , <EOL> questionTypeArray , <EOL> params [ '<STR_LIT>' ] , <EOL> params [ '<STR_LIT>' ] , <EOL> params [ '<STR_LIT>' ] , <EOL> params [ '<STR_LIT>' ] ) <EOL> print ( '<STR_LIT>' ) <EOL> numCategories = np . max ( questionTypeArray ) + <NUM_LIT:1> <EOL> numModels = len ( params [ '<STR_LIT>' ] ) <EOL> numCorrect = <NUM_LIT:1> << numModels <EOL> numBins = numCategories * numCorrect <EOL> modelAnswers = np . zeros ( ( numModels , inputTest . shape [ <NUM_LIT:0> ] ) , dtype = '<STR_LIT:int>' ) <EOL> bins = [ None ] * numBins <EOL> names = [ ] <EOL> for i in range ( numCategories ) : <EOL> catName = getCatName ( i ) <EOL> for j in range ( numCorrect ) : <EOL> binName = getBinName ( j ) <EOL> names . append ( getName ( catName , binName ) ) <EOL> for i in range ( numModels ) : <EOL> modelAnswers [ i ] = np . argmax ( modelOutputs [ i ] , axis = - <NUM_LIT:1> ) <EOL> for n in range ( inputTest . shape [ <NUM_LIT:0> ] ) : <EOL> correct = targetTest [ n , <NUM_LIT:0> ] <EOL> bintmp = <NUM_LIT:0> <EOL> for i in range ( numModels ) : <EOL> if modelAnswers [ i , n ] == correct : <EOL> bintmp += <NUM_LIT:1> << ( numModels - i - <NUM_LIT:1> ) <EOL> category = questionTypeArray [ n ] <EOL> binNum = category * numCorrect + bintmp <EOL> if bins [ binNum ] == None : <EOL> bins [ binNum ] = [ n ] <EOL> else : <EOL> bins [ binNum ] . append ( n ) <EOL> for i , bin in enumerate ( bins ) : <EOL> if bin is None : <EOL> bins [ i ] = [ ] <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> outputFolder = params [ '<STR_LIT>' ] <EOL> if not os . path . exists ( outputFolder ) : <EOL> os . makedirs ( outputFolder ) <EOL> with open ( os . path . join ( outputFolder , '<STR_LIT>' ) , '<STR_LIT:w>' ) as f : <EOL> f . write ( renderIndex ( <EOL> ir . getModelNames ( params [ '<STR_LIT>' ] ) , numCategories , bins ) ) <EOL> for i in range ( numBins ) : <EOL> if bins [ i ] is not None : <EOL> print '<STR_LIT>' % names [ i ] <EOL> outputSubFolder = os . path . join ( outputFolder , names [ i ] ) <EOL> idx = np . array ( bins [ i ] , dtype = '<STR_LIT:int>' ) <EOL> inputTestSubset = inputTest [ idx ] <EOL> targetTestSubset = targetTest [ idx ] <EOL> modelOutputsSubset = [ ] <EOL> for j in range ( numModels ) : <EOL> modelOutputsSubset . append ( modelOutputs [ j ] [ idx ] ) <EOL> if not os . path . exists ( outputSubFolder ) : <EOL> os . makedirs ( outputSubFolder ) <EOL> htmlHyperLink = '<STR_LIT>' <EOL> pages = ir . renderHtml ( <EOL> inputTestSubset , <EOL> targetTestSubset , <EOL> data [ '<STR_LIT>' ] , <EOL> data [ '<STR_LIT>' ] , <EOL> urlDict , <EOL> topK = params [ '<STR_LIT>' ] , <EOL> modelOutputs = modelOutputsSubset , <EOL> modelNames = ir . getModelNames ( params [ '<STR_LIT>' ] ) , <EOL> questionIds = idx ) <EOL> for j , page in enumerate ( pages ) : <EOL> with open ( os . path . join ( outputSubFolder , <EOL> htmlHyperLink % j ) , '<STR_LIT:w>' ) as f : <EOL> f . write ( page ) </s>
