def printCategoryScores(scoreDict, instScoreDict, args):
    if (args.quiet):
        return
    print(args.bold + 'categories       IoU      nIoU' + args.nocol)
    print('--------------------------------')
    for categoryName in scoreDict:
        if all(label.ignoreInEval for label in category2labels[categoryName]):
            continue
        iouStr = getColorEntry(scoreDict[categoryName],
                               args) + '{val:>5.3f}'.format(
                                   val=scoreDict[categoryName]) + args.nocol
        niouStr = getColorEntry(
            instScoreDict[categoryName], args) + '{val:>5.3f}'.format(
                val=instScoreDict[categoryName]) + args.nocol
        print('{:<14}: '.format(categoryName) + iouStr + '    ' + niouStr)
def printClassScores(scoreList, instScoreList, args):
    if (args.quiet):
        return
    print(args.bold + 'classes          IoU      nIoU' + args.nocol)
    print('--------------------------------')
    for label in args.evalLabels:
        if (id2label[label].ignoreInEval):
            continue
        labelName = str(id2label[label].name)
        iouStr = getColorEntry(
            scoreList[labelName],
            args) + '{val:>5.3f}'.format(val=scoreList[labelName]) + args.nocol
        niouStr = getColorEntry(instScoreList[labelName],
                                args) + '{val:>5.3f}'.format(
                                    val=instScoreList[labelName]) + args.nocol
        print('{:<14}: '.format(labelName) + iouStr + '    ' + niouStr)
def evaluateImgLists(predictionImgList, groundTruthImgList, args):
    if len(predictionImgList) != len(groundTruthImgList):
        printError(
            'List of images for prediction and groundtruth are not of equal size.'
        )
    confMatrix = generateMatrix(args)
    instStats = generateInstanceStats(args)
    perImageStats = {}
    nbPixels = 0

    if not args.quiet:
        print('Evaluating {} pairs of images...'.format(
            len(predictionImgList)))

    # Evaluate all pairs of images and save them into a matrix
    for i in range(len(predictionImgList)):
        predictionImgFileName = predictionImgList[i]
        groundTruthImgFileName = groundTruthImgList[i]
        #print 'Evaluate ', predictionImgFileName, '<>', groundTruthImgFileName
        nbPixels += evaluatePair(predictionImgFileName, groundTruthImgFileName,
                                 confMatrix, instStats, perImageStats, args)

        # sanity check
        if confMatrix.sum() != nbPixels:
            printError(
                'Number of analyzed pixels and entries in confusion matrix disagree: contMatrix {}, pixels {}'
                .format(confMatrix.sum(), nbPixels))

        if not args.quiet:
            print('\rImages Processed: {}'.format(i + 1), end=' ')
            sys.stdout.flush()
    if not args.quiet:
        print('\n')

    # sanity check
    if confMatrix.sum() != nbPixels:
        printError(
            'Number of analyzed pixels and entries in confusion matrix disagree: contMatrix {}, pixels {}'
            .format(confMatrix.sum(), nbPixels))

    # print confusion matrix
    if (not args.quiet):
        printConfMatrix(confMatrix, args)

    # Calculate IOU scores on class level from matrix
    classScoreList = {}
    for label in args.evalLabels:
        labelName = id2label[label].name
        classScoreList[labelName] = getIouScoreForLabel(
            label, confMatrix, args)

    # Calculate instance IOU scores on class level from matrix
    classInstScoreList = {}
    for label in args.evalLabels:
        labelName = id2label[label].name
        classInstScoreList[labelName] = getInstanceIouScoreForLabel(
            label, confMatrix, instStats, args)

    # Print IOU scores
    if (not args.quiet):
        print('')
        print('')
        printClassScores(classScoreList, classInstScoreList, args)
        iouAvgStr = getColorEntry(getScoreAverage(
            classScoreList, args), args) + '{avg:5.3f}'.format(
                avg=getScoreAverage(classScoreList, args)) + args.nocol
        niouAvgStr = getColorEntry(getScoreAverage(
            classInstScoreList, args), args) + '{avg:5.3f}'.format(
                avg=getScoreAverage(classInstScoreList, args)) + args.nocol
        print('--------------------------------')
        print('Score Average : ' + iouAvgStr + '    ' + niouAvgStr)
        print('--------------------------------')
        print('')

    # Calculate IOU scores on category level from matrix
    categoryScoreList = {}
    for category in category2labels.keys():
        categoryScoreList[category] = getIouScoreForCategory(
            category, confMatrix, args)

    # Calculate instance IOU scores on category level from matrix
    categoryInstScoreList = {}
    for category in category2labels.keys():
        categoryInstScoreList[category] = getInstanceIouScoreForCategory(
            category, confMatrix, instStats, args)

    # Print IOU scores
    if (not args.quiet):
        print('')
        printCategoryScores(categoryScoreList, categoryInstScoreList, args)
        iouAvgStr = getColorEntry(getScoreAverage(
            categoryScoreList, args), args) + '{avg:5.3f}'.format(
                avg=getScoreAverage(categoryScoreList, args)) + args.nocol
        niouAvgStr = getColorEntry(
            getScoreAverage(categoryInstScoreList, args),
            args) + '{avg:5.3f}'.format(
                avg=getScoreAverage(categoryInstScoreList, args)) + args.nocol
        print('--------------------------------')
        print('Score Average : ' + iouAvgStr + '    ' + niouAvgStr)
        print('--------------------------------')
        print('')

    allResultsDict = createResultDict(confMatrix, classScoreList,
                                      classInstScoreList, categoryScoreList,
                                      categoryInstScoreList, perImageStats,
                                      args)
    # write result file
    if args.JSONOutput:
        writeJSONFile(allResultsDict, args)

    writeDict2Txt(allResultsDict, 'results.txt')

    # return confusion matrix
    return allResultsDict
def printConfMatrix(confMatrix, args):
    # print line
    print('\b{text:{fill}>{width}}'.format(width=15, fill='-', text=' '),
          end=' ')
    for label in args.evalLabels:
        print('\b{text:{fill}>{width}}'.format(width=args.printRow + 2,
                                               fill='-',
                                               text=' '),
              end=' ')
    print('\b{text:{fill}>{width}}'.format(width=args.printRow + 3,
                                           fill='-',
                                           text=' '))

    # print label names
    print('\b{text:>{width}} |'.format(width=13, text=''), end=' ')
    for label in args.evalLabels:
        print('\b{text:^{width}} |'.format(width=args.printRow,
                                           text=id2label[label].name[0]),
              end=' ')
    print('\b{text:>{width}} |'.format(width=6, text='Prior'))

    # print line
    print('\b{text:{fill}>{width}}'.format(width=15, fill='-', text=' '),
          end=' ')
    for label in args.evalLabels:
        print('\b{text:{fill}>{width}}'.format(width=args.printRow + 2,
                                               fill='-',
                                               text=' '),
              end=' ')
    print('\b{text:{fill}>{width}}'.format(width=args.printRow + 3,
                                           fill='-',
                                           text=' '))

    # print matrix
    for x in range(0, confMatrix.shape[0]):
        if (not x in args.evalLabels):
            continue
        # get prior of this label
        prior = getPrior(x, confMatrix)
        # skip if label does not exist in ground truth
        if prior < 1e-9:
            continue

        # print name
        name = id2label[x].name
        if len(name) > 13:
            name = name[:13]
        print('\b{text:>{width}} |'.format(width=13, text=name), end=' ')
        # print matrix content
        for y in range(0, len(confMatrix[x])):
            if (not y in args.evalLabels):
                continue
            matrixFieldValue = getMatrixFieldValue(confMatrix, x, y, args)
            print(getColorEntry(matrixFieldValue, args) +
                  '\b{text:>{width}.2f}  '.format(
                      width=args.printRow, text=matrixFieldValue) + args.nocol,
                  end=' ')
        # print prior
        print(
            getColorEntry(prior, args) +
            '\b{text:>{width}.4f} '.format(width=6, text=prior) + args.nocol)
    # print line
    print('\b{text:{fill}>{width}}'.format(width=15, fill='-', text=' '),
          end=' ')
    for label in args.evalLabels:
        print('\b{text:{fill}>{width}}'.format(width=args.printRow + 2,
                                               fill='-',
                                               text=' '),
              end=' ')
    print('\b{text:{fill}>{width}}'.format(width=args.printRow + 3,
                                           fill='-',
                                           text=' '),
          end=' ')