Esempio n. 1
0
def testRBM(opts):
    """show how to use RBM to do classification"""
    # read data
    data = np.load(opts.feature)
    label = np.load(opts.label)

    # set the nodes of hidden layers
    nHid = 1000

    # shuffle data and label
    [data, label] = util.shuffle(data, label)

    # decide how many samples to be used as training set
    percent = float(opts.trainPercent)
    nCase = data.shape[0]

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

    # split data and label into  train dataset and test dataset
    trainData = data[0:nTrain, :]
    trainLabel = label[0:nTrain, :]
    example = data[nTrain:, :]
    testLabel = label[nTrain:, :]

    p = {"maxEpoch": opts.maxEpoch}

    m = rbmFit.rbmFit(trainData,
                      nHid,
                      trainLabel,
                      isSaveModel=True,
                      name=opts.model,
                      **p)

    [trainR, F1] = rbmPredict.rbmPredict(m, trainData)
    [testR, F2] = rbmPredict.rbmPredict(m, example)

    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 options.isSaveResult:
        result = shelve.open(options.resultName)
        result["nHid"] = nHid
        result["maxEpoch"] = options.maxEpoch
        result["trainAcc"] = 1 - trainK * 1.0 / nTrain
        result["testAcc"] = 1 - testK * 1.0 / nTest
        result.close()
Esempio n. 2
0
def testRBM(opts) :
    """show how to use RBM to do classification"""
    # read data
    data  = np.load(opts.feature)
    label = np.load(opts.label)

    # set the nodes of hidden layers
    nHid = 1000

    # shuffle data and label
    [data, label] = util.shuffle(data, label)

    # decide how many samples to be used as training set
    percent = float(opts.trainPercent)
    nCase   = data.shape[0]

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

    # split data and label into  train dataset and test dataset
    trainData  = data[0:nTrain, :]
    trainLabel = label[0:nTrain, :]
    example   = data[nTrain:, :]
    testLabel  = label[nTrain:, :]

    p = {"maxEpoch" : opts.maxEpoch}

    m = rbmFit.rbmFit(trainData, nHid, trainLabel, isSaveModel=True, name=opts.model, **p)
    
    [trainR, F1] = rbmPredict.rbmPredict(m, trainData)
    [testR, F2] = rbmPredict.rbmPredict(m, example)
	
    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 options.isSaveResult :
        result = shelve.open(options.resultName)
        result["nHid"]     = nHid
        result["maxEpoch"] = options.maxEpoch
        result["trainAcc"] = 1-trainK*1.0/nTrain
        result["testAcc"]  = 1-testK*1.0/nTest
        result.close()
Esempio n. 3
0
def multiModalityDBNPredict(models, X):
    """
    models : trained models including joint layer
    X : input data for all modalities"""
    N = len(X)

    if N != len(models) - 1:
        print "error"
        sys.exit()

    for index in range(N):
        X[index] = DBNPredict.DBNPredict(models[index],
                                         X[index],
                                         isSingleDBN=False)

    # concatenate all modalities data
    for index in range(N - 1):
        if index == 0:
            data = np.append(X[index], X[index + 1], axis=1)
        else:
            data = np.append(data, X[index + 1], axis=1)

    [prediction, F] = rbmPredict.rbmPredict(models[-1], data)

    return [prediction, F]
Esempio n. 4
0
def testRBM(opts):
    """show how to use RBM to do classification"""
    nHid = 1000

    # split data and label into  train dataset and test dataset
    trainData = np.load(opts.trainFeature)
    trainLabel = np.load(opts.trainLabel)
    testData = np.load(opts.testFeature)
    testLabel = np.load(opts.testLabel)

    nTrain = trainLabel.size
    nTest = testLabel.size

    p = {"maxEpoch": options.maxEpoch}

    m = rbmFit.rbmFit(trainData,
                      nHid,
                      trainLabel,
                      isSaveModel=True,
                      name=opts.model,
                      **p)

    [trainR, F1] = rbmPredict.rbmPredict(m, trainData)
    [testR, F2] = rbmPredict.rbmPredict(m, testData)

    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(options.resultName)
        result["nHid"] = nHid
        result["maxEpoch"] = options.maxEpoch
        result["trainAcc"] = 1 - trainK * 1.0 / nTrain
        result["testAcc"] = 1 - testK * 1.0 / nTest
        result.close()
Esempio n. 5
0
def testRBM(opts):
    """show how to use RBM to do classification"""
    nHid = 1000

    # split data and label into  train dataset and test dataset
    trainData = np.load(opts.trainFeature)
    trainLabel = np.load(opts.trainLabel)
    testData = np.load(opts.testFeature)
    testLabel = np.load(opts.testLabel)

    nTrain = trainLabel.size
    nTest = testLabel.size

    p = {"maxEpoch": options.maxEpoch}

    m = rbmFit.rbmFit(trainData, nHid, trainLabel, isSaveModel=True, name=opts.model, **p)

    [trainR, F1] = rbmPredict.rbmPredict(m, trainData)
    [testR, F2] = rbmPredict.rbmPredict(m, testData)

    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(options.resultName)
        result["nHid"] = nHid
        result["maxEpoch"] = options.maxEpoch
        result["trainAcc"] = 1 - trainK * 1.0 / nTrain
        result["testAcc"] = 1 - testK * 1.0 / nTest
        result.close()
Esempio n. 6
0
def DBNPredict(m, X, isSingleDBN = True) :
    """implement DBN predict
    m : models trained(save in np.array)
    X : data to predict"""

    H = len(m)

    if isSingleDBN :
        for index in range(H-1) :
            X = rbmVtoH.rbmVtoH(m[index], X)

        [prediction, F] = rbmPredict.rbmPredict(m[H-1], X)

        return [prediction, F]
    else :
        for index in range(H) :
            X = rbmVtoH.rbmVtoH(m[index], X)

        return X
def multiModalityDBNPredict(models, X) :
    """
    models : trained models including joint layer
    X : input data for all modalities"""
    N = len(X)

    if N != len(models)-1 :
        print "error"
        sys.exit()

    for index in range(N) :
        X[index] = DBNPredict.DBNPredict(models[index], X[index], isSingleDBN=False)

    # concatenate all modalities data
    for index in range(N-1) :
        if index == 0 :
            data = np.append(X[index], X[index+1], axis = 1)
        else :
            data = np.append(data, X[index+1], axis = 1)

    [prediction, F] = rbmPredict.rbmPredict(models[-1], data)

    return [prediction, F]
Esempio n. 8
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)
Esempio n. 9
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)