예제 #1
0
def testEvaluation(modelType="multi-modality", modelsFile = None, viewBasedDataFile = None,
                   shapeBasedDataFile = None, labelFile = None,\
                   isSaveFig = True, figName = None, evaluationFile = None) :
    """modelsFile : multi-modality model file name
    viewBasedDataFile :
    shapeBasedDataFile :
    labelFile :
    isSaveFig : whether save the recall-precision curve or not
    figName : the recall-precision name, only works when isSaveFig is True
    evaluationFile : where to save the retrieval result """

    if modelsFile == None :
        modelsFile = "data/model/multi-modalityDBN.npy"
    if viewBasedDataFile == None :
        viewBasedDataFile = "data/shrec2007/SHREC_2007_BOW_1000_viewBased.npy"
    if shapeBasedDataFile == None :
        shapeBasedDataFile = "data/shrec2007/SHREC_2007_BOW_100_shapeBased.npy"
    if labelFile == None :
        labelFile = "data/shrec2007/label.npy"
    if figName == None :
        figName = "data/shrec2007/RP.eps"
    if evaluationFile == None :
        evaluationFile = "data/shrec2007/evaluation.shelve"

    models = np.load(modelsFile)
    viewBasedData = np.load(viewBasedDataFile)
    shapeBasedData = np.load(shapeBasedDataFile)
    label = np.load(labelFile)

    if modelType == "multi-modality" :
        [y, F] = multiModalityDBNPredict.multiModalityDBNPredict(models, [viewBasedData, shapeBasedData])

        simM = calSimMatrix(F)

        nQuery = [20, 40, 60, 80]
        evalShape = shapeEvaluation(simM, label, nQuery = nQuery)
        evalShape.drawPR(isSaveFig=isSaveFig, figName = figName)
        evalShape.saveEval(fileName=evaluationFile)
    elif modelType == "DBN_view" :
        [yView, FView] = DBNPredict.DBNPredict(models, viewBasedData)

        # process view
        simMView = calSimMatrix(FView)

        nQuery = [20, 40, 60, 80]
        evalShapeView = shapeEvaluation(simMView, label, nQuery = nQuery)

#        figName = figName+".DBN.view"
#        evaluationFile = evaluationFile+".DBN.view"
        evalShapeView.drawPR(isSaveFig=isSaveFig, figName = figName)
        evalShapeView.saveEval(fileName=evaluationFile)

    elif modelType == "DBN_shape" :
        [yShape, FShape] = DBNPredict.DBNPredict(models, shapeBasedData)
        # process shape
        simMShape = calSimMatrix(FShape)

        nQuery = [20, 40, 60, 80]
        evalShapeShape = shapeEvaluation(simMShape, label, nQuery = nQuery)

#        figName = figName+".DBN.shape"
#        evaluationFile = evaluationFile+".DBN.shape"
        evalShapeShape.drawPR(isSaveFig=isSaveFig, figName = figName)
        evalShapeShape.saveEval(fileName=evaluationFile)

    elif modelType == "rbm_view" :
        [yView, FView] = rbmPredict.rbmPredict(models[0], viewBasedData)

        # process view
        simMView = calSimMatrix(FView)

        nQuery = [20, 40, 60, 80]
        evalShapeView = shapeEvaluation(simMView, label, nQuery = nQuery)

#        figName = figName+".rbm.view"
#        evaluationFile = evaluationFile+".rbm.view"
        evalShapeView.drawPR(isSaveFig=isSaveFig, figName = figName)
        evalShapeView.saveEval(fileName=evaluationFile)

    elif modelType == "rbm_shape" :
        [yShape, FShape] = rbmPredict.rbmPredict(models[0], shapeBasedData)
        # process shape
        simMShape = calSimMatrix(FShape)

        nQuery = [20, 40, 60, 80]
        evalShapeShape = shapeEvaluation(simMShape, label, nQuery = nQuery)

#        figName = figName+".rbm.shape"
#        evaluationFile = evaluationFile+".rbm.shape"
        evalShapeShape.drawPR(isSaveFig=isSaveFig, figName = figName)
        evalShapeShape.saveEval(fileName=evaluationFile)
예제 #2
0
def testEvaluation(modelType="multi-modality", modelsFile = None, viewBasedDataFile = None,
                   shapeBasedDataFile = None, labelFile = None,\
                   isSaveFig = True, figName = None, evaluationFile = None) :
    """modelsFile : multi-modality model file name
    viewBasedDataFile :
    shapeBasedDataFile :
    labelFile :
    isSaveFig : whether save the recall-precision curve or not
    figName : the recall-precision name, only works when isSaveFig is True
    evaluationFile : where to save the retrieval result """

    if modelsFile == None:
        modelsFile = "data/model/multi-modalityDBN.npy"
    if viewBasedDataFile == None:
        viewBasedDataFile = "data/shrec2007/SHREC_2007_BOW_1000_viewBased.npy"
    if shapeBasedDataFile == None:
        shapeBasedDataFile = "data/shrec2007/SHREC_2007_BOW_100_shapeBased.npy"
    if labelFile == None:
        labelFile = "data/shrec2007/label.npy"
    if figName == None:
        figName = "data/shrec2007/RP.eps"
    if evaluationFile == None:
        evaluationFile = "data/shrec2007/evaluation.shelve"

    models = np.load(modelsFile)
    viewBasedData = np.load(viewBasedDataFile)
    shapeBasedData = np.load(shapeBasedDataFile)
    label = np.load(labelFile)

    if modelType == "multi-modality":
        [y, F] = multiModalityDBNPredict.multiModalityDBNPredict(
            models, [viewBasedData, shapeBasedData])

        simM = calSimMatrix(F)

        nQuery = [20, 40, 60, 80]
        evalShape = shapeEvaluation(simM, label, nQuery=nQuery)
        evalShape.drawPR(isSaveFig=isSaveFig, figName=figName)
        evalShape.saveEval(fileName=evaluationFile)
    elif modelType == "DBN_view":
        [yView, FView] = DBNPredict.DBNPredict(models, viewBasedData)

        # process view
        simMView = calSimMatrix(FView)

        nQuery = [20, 40, 60, 80]
        evalShapeView = shapeEvaluation(simMView, label, nQuery=nQuery)

        #        figName = figName+".DBN.view"
        #        evaluationFile = evaluationFile+".DBN.view"
        evalShapeView.drawPR(isSaveFig=isSaveFig, figName=figName)
        evalShapeView.saveEval(fileName=evaluationFile)

    elif modelType == "DBN_shape":
        [yShape, FShape] = DBNPredict.DBNPredict(models, shapeBasedData)
        # process shape
        simMShape = calSimMatrix(FShape)

        nQuery = [20, 40, 60, 80]
        evalShapeShape = shapeEvaluation(simMShape, label, nQuery=nQuery)

        #        figName = figName+".DBN.shape"
        #        evaluationFile = evaluationFile+".DBN.shape"
        evalShapeShape.drawPR(isSaveFig=isSaveFig, figName=figName)
        evalShapeShape.saveEval(fileName=evaluationFile)

    elif modelType == "rbm_view":
        [yView, FView] = rbmPredict.rbmPredict(models[0], viewBasedData)

        # process view
        simMView = calSimMatrix(FView)

        nQuery = [20, 40, 60, 80]
        evalShapeView = shapeEvaluation(simMView, label, nQuery=nQuery)

        #        figName = figName+".rbm.view"
        #        evaluationFile = evaluationFile+".rbm.view"
        evalShapeView.drawPR(isSaveFig=isSaveFig, figName=figName)
        evalShapeView.saveEval(fileName=evaluationFile)

    elif modelType == "rbm_shape":
        [yShape, FShape] = rbmPredict.rbmPredict(models[0], shapeBasedData)
        # process shape
        simMShape = calSimMatrix(FShape)

        nQuery = [20, 40, 60, 80]
        evalShapeShape = shapeEvaluation(simMShape, label, nQuery=nQuery)

        #        figName = figName+".rbm.shape"
        #        evaluationFile = evaluationFile+".rbm.shape"
        evalShapeShape.drawPR(isSaveFig=isSaveFig, figName=figName)
        evalShapeShape.saveEval(fileName=evaluationFile)
예제 #3
0
def testMultiModalityDBN(opts):
    """show how to use multi-modality DBN to do classification
    test multi-modality DBN
    In this example, I just use two modalities, but you can extend this code segment to let it work for more modalities"""
    # load data

    nHid = [[2000, 1000], [5000, 2000]]
    nJoint = 3000

    trainViewBasedData = np.load(opts.trainViewBasedFeature)
    trainShapeBasedData = np.load(opts.trainShapeBasedFeature)
    trainLabel = np.load(opts.trainLabel)

    testViewBasedData = np.load(opts.testViewBasedFeature)
    testShapeBasedData = np.load(opts.testShapeBasedFeature)
    testLabel = np.load(opts.testLabel)

    nTrain = trainLabel.size
    nTest = testLabel.size

    # set parameters for each layer
    # view based layer1
    pV1 = {"maxEpoch": opts.maxEpoch, "modelType": "BB"}

    # view based layer2
    pV2 = {"maxEpoch": opts.maxEpoch}

    p1 = {"layer1": pV1, "layer2": pV2}

    # shape based layer1
    pS1 = {"maxEpoch": opts.maxEpoch, "modelType": "BB"}

    # shape based layer2
    pS2 = {"maxEpoch": opts.maxEpoch}

    p2 = {"layer1": pS1, "layer2": pS2}

    # joint layer
    pJ = {"maxEpoch": opts.maxEpoch}

    p = {"modality1": p1, "modality2": p2, "joint": pJ}

    # train the multi-modality model
    model = multiModalityDBNFit.multiModalityDBNFit([trainViewBasedData, trainShapeBasedData],\
                                                    trainLabel, nHid, nJoint, isSaveModel=True, modelName = opts.model, **p)

    # do prediction for training set and testing set
    [trainR, F1] = multiModalityDBNPredict.multiModalityDBNPredict(
        model, [trainViewBasedData, trainShapeBasedData])
    [testR, F2] = multiModalityDBNPredict.multiModalityDBNPredict(
        model, [testViewBasedData, testShapeBasedData])

    # calculate the classification accuracy
    trainK = 0
    for x in range(nTrain):
        if trainLabel[x] != trainR[x]:
            trainK = trainK + 1

    testK = 0
    for x in range(nTest):
        if testLabel[x] != testR[x]:
            testK = testK + 1

    print "---------------------------------------"
    print "train classification rate : %f " % (1 - trainK * 1.0 / nTrain)
    print "test  classification rate : %f " % (1 - testK * 1.0 / nTest)
    print "---------------------------------------"

    if opts.isSaveResult:
        result = shelve.open(opts.resultName)
        result["nHid"] = nHid
        result["nJoint"] = nJoint
        result["maxEpoch"] = opts.maxEpoch
        result["trainAcc"] = 1 - trainK * 1.0 / nTrain
        result["testAcc"] = 1 - testK * 1.0 / nTest
        result.close()
예제 #4
0
def testMultiModalityDBN(opts) :
    """show how to use multi-modality DBN to do classification
    test multi-modality DBN
    In this example, I just use two modalities, but you can extend this code segment to let it work for more modalities"""
    # load data
    viewBasedData = np.load(opts.viewBasedFeature)
    shapeBasedData = np.load(opts.shapeBasedFeature)
    label = np.load(opts.label)

    if viewBasedData.shape[0] != shapeBasedData.shape[0] :
        print "different modalities must have the same number of samples"
        sys.exit()

    nHid = [[1000, 800], [5000, 2000]]
    nJoint = 2800

    # shuffle all data and label
    [viewBasedData, shapeBasedData, label] = util.shuffleMore(viewBasedData, shapeBasedData, label)
    
    percent = opts.trainPercent
    nCase = viewBasedData.shape[0]

    nTrain = int(nCase*percent)
    nTest  = nCase - nTrain

    trainViewBasedData = viewBasedData[0:nTrain, :]
    trainShapeBasedData = shapeBasedData[0:nTrain, :]
    trainLabel = label[0:nTrain, :]

    testViewBasedData  = viewBasedData[nTrain:, :]
    testShapeBasedData  = shapeBasedData[nTrain:, :]
    testLabel = label[nTrain:, :]

    # set parameters for each layer
    # view based layer1
    pV1 = {"maxEpoch" : opts.maxEpoch, "modelType" : "BB"}

    # view based layer2
    pV2 = {"maxEpoch" : opts.maxEpoch}

    p1 = {"layer1" : pV1, "layer2" : pV2}

    # shape based layer1
    pS1 = {"maxEpoch" : opts.maxEpoch, "modelType" : "BB"}

    # shape based layer2
    pS2 = {"maxEpoch" : opts.maxEpoch}

    p2 = {"layer1" : pS1, "layer2" : pS2}

    # joint layer
    pJ = {"maxEpoch" : opts.maxEpoch}

    p = {"modality1" : p1, "modality2" : p2, "joint" : pJ}

    # train the multi-modality model
    model = multiModalityDBNFit.multiModalityDBNFit([trainViewBasedData, trainShapeBasedData],\
                                                    trainLabel, nHid, nJoint, isSaveModel=True, modelName = opts.model, **p)

    # do prediction for training set and testing set
    [trainR, F1] = multiModalityDBNPredict.multiModalityDBNPredict(model, [trainViewBasedData, trainShapeBasedData])
    [testR, F2] = multiModalityDBNPredict.multiModalityDBNPredict(model, [testViewBasedData, testShapeBasedData])

    # calculate the classification accuracy
    trainK = 0
    for x in range(nTrain) :
        if trainLabel[x] != trainR[x] :
            trainK = trainK+1

    testK = 0
    for x in range(nTest) :
        if testLabel[x] != testR[x] :
            testK = testK+1

    print "---------------------------------------"
    print "train classification rate : %f " % (1-trainK*1.0/nTrain)
    print "test  classification rate : %f " % (1-testK*1.0/nTest)
    print "---------------------------------------"

    if opts.isSaveResult :
        result = shelve.open(opts.resultName)
        result["nHid"]     = nHid
        result["nJoint"]   = nJoint
        result["maxEpoch"] = opts.maxEpoch
        result["trainPercent"] = opts.trainPercent
        result["trainAcc"] = 1-trainK*1.0/nTrain
        result["testAcc"]  = 1-testK*1.0/nTest
        result["trainLabel"] = trainLabel
        result["trainR"] = trainR
        result["testLabel"] = testLabel
        result["testR"] = testR
        result.close()
예제 #5
0
def testMultiModalityDBN(opts) :
    """show how to use multi-modality DBN to do classification
    test multi-modality DBN
    In this example, I just use two modalities, but you can extend this code segment to let it work for more modalities"""
    # load data

    nHid = [[2000, 1000], [5000, 2000]]
    nJoint = 3000

    trainViewBasedData = np.load(opts.trainViewBasedFeature)
    trainShapeBasedData = np.load(opts.trainShapeBasedFeature)
    trainLabel = np.load(opts.trainLabel)

    testViewBasedData  = np.load(opts.testViewBasedFeature)
    testShapeBasedData  = np.load(opts.testShapeBasedFeature)
    testLabel = np.load(opts.testLabel)

    nTrain = trainLabel.size
    nTest  = testLabel.size

    # set parameters for each layer
    # view based layer1
    pV1 = {"maxEpoch" : opts.maxEpoch, "modelType" : "BB"}

    # view based layer2
    pV2 = {"maxEpoch" : opts.maxEpoch}

    p1 = {"layer1" : pV1, "layer2" : pV2}

    # shape based layer1
    pS1 = {"maxEpoch" : opts.maxEpoch, "modelType" : "BB"}

    # shape based layer2
    pS2 = {"maxEpoch" : opts.maxEpoch}

    p2 = {"layer1" : pS1, "layer2" : pS2}

    # joint layer
    pJ = {"maxEpoch" : opts.maxEpoch}

    p = {"modality1" : p1, "modality2" : p2, "joint" : pJ}

    # train the multi-modality model
    model = multiModalityDBNFit.multiModalityDBNFit([trainViewBasedData, trainShapeBasedData],\
                                                    trainLabel, nHid, nJoint, isSaveModel=True, modelName = opts.model, **p)

    # do prediction for training set and testing set
    [trainR, F1] = multiModalityDBNPredict.multiModalityDBNPredict(model, [trainViewBasedData, trainShapeBasedData])
    [testR, F2] = multiModalityDBNPredict.multiModalityDBNPredict(model, [testViewBasedData, testShapeBasedData])

    # calculate the classification accuracy
    trainK = 0
    for x in range(nTrain) :
        if trainLabel[x] != trainR[x] :
            trainK = trainK+1

    testK = 0
    for x in range(nTest) :
        if testLabel[x] != testR[x] :
            testK = testK+1

    print "---------------------------------------"
    print "train classification rate : %f " % (1-trainK*1.0/nTrain)
    print "test  classification rate : %f " % (1-testK*1.0/nTest)
    print "---------------------------------------"

    if opts.isSaveResult :
        result = shelve.open(opts.resultName)
        result["nHid"]     = nHid
        result["nJoint"]   = nJoint
        result["maxEpoch"] = opts.maxEpoch
        result["trainAcc"] = 1-trainK*1.0/nTrain
        result["testAcc"]  = 1-testK*1.0/nTest
        result.close()