Esempio n. 1
0
def crossValidate(train_y, train_X):
    """ Cross validate to get optimal parameters """
    # scale data
    min_max_scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1))
    X_scaledArr = min_max_scaler.fit_transform(train_X)
    X_scaled = X_scaledArr.tolist()

    # write to svm format file
    outputPath = '.\\cv'
    fileName = 'train_data'
    svmTool = SvmTool()
    svmTool.write2SVMFormat(outputPath, fileName, X_scaled, train_y)
Esempio n. 2
0
def main():
    actionDatabaseDir = config.actionDatabaseDir
    categories = config.actionCateogory
    
    ## Step.1 Data loading and features extraction 
    # Get features from training data over all categories
    print "Data loading and feature extraction ..."
    
    allFeatures = np.array([])
    trainActionSequence = []
    testActionSequence = []
    
    if os.path.exists('allFeatures.npy') \
        and os.path.exists('trainActionSequence.npy') \
        and os.path.exists('testActionSequence.npy'):
        
        allFeatures = np.load('allFeatures.npy')
        trainActionSequence = np.load('trainActionSequence.npy')
        testActionSequence = np.load('testActionSequence.npy')
    else:
        for category in categories:
            categoryPath = os.path.join(actionDatabaseDir, category)
            allData = os.listdir(categoryPath)
            
            
            # Train data and test data loading
            for data in allData:
                filePath = os.path.join(categoryPath, data)
                actionSequence = ActionSequence(filePath)
                actionSequence.extractStip()    # extract STIP
                
                subject = actionSequence.subject
                if subject in config.trainDataSubjects:
                    trainActionSequence.append(actionSequence)
                    
                    if allFeatures.size == 0:
                        allFeatures = actionSequence.stipFeatures
                    else:
                        allFeatures = np.vstack((allFeatures, 
                                                 actionSequence.stipFeatures))
                        
                elif subject in config.testDataSubjects:
                    testActionSequence.append(actionSequence)
            
        
        np.save('allFeatures', allFeatures)        
        np.save('trainActionSequence', trainActionSequence)
        np.save('testActionSequence', testActionSequence)
            
    ## Step.2 Codebook generation
    print "Codebook generation ..."
    bovw = BagOfWords(featureEncodingMethod = 'sparse-coding',
                      poolingMethod = 'max-pooling',
                      normalizationMethod = 'L2-norm')

#     bovw = BagOfWords(featureEncodingMethod = 'vector-quantization',
#                   poolingMethod = 'sum-pooling',
#                   normalizationMethod = 'L1-norm')
    
    if os.path.exists('codebook.npy'):
        codebook = np.load('codebook.npy')
        bovw.codebook = codebook
    else:
        bovw.generateCodebook(allFeatures)
        np.save('codebook', bovw.codebook)
        
    ## Step.3 Feature encoding for train data
    train_y = []
    train_X = []
    
    for actionSequence in trainActionSequence:
        # Feature encoding, pooling, normalization
        actionSequence.generateFinalFeatures(bovw)
        
        # Format train data
        train_y.append(actionSequence.categoryId)
        train_X.append(actionSequence.finalFeatures)
    
    # Cross validation    
    if config.is_cv:
        # cross validation
        crossValidate(train_y, train_X)
    
    ## Step.4 Classification
    # Learning using SVM
    svmTool = SvmTool()
    print "Model learning ..."
    svmTool.learnModel(train_y, train_X)
    
    # Feature encoding for test data and classify data using learned model
    print "Predicating ..."
    numCorrect = 0
    trueLabels = []
    testPredLabels = []
    removeall(config.predctDir)
    
    for actionSequence in testActionSequence:
        # Feature encoding, pooling, normalization
        actionSequence.generateFinalFeatures(bovw)
        
        # Format train data
        test_y = [actionSequence.categoryId]
        test_X = [actionSequence.finalFeatures]
        
        p_label, _ = svmTool.doPredication(test_y, test_X)
        predCagtegoryId = int(p_label[0])
        predCategoryName = categories[predCagtegoryId]
        
        # Write predicated action to predicated category
        predCategoryPath = os.path.join(config.predctDir, predCategoryName)
        if not os.path.exists(predCategoryPath):
            os.makedirs(predCategoryPath)
        
        predFilePath = os.path.join(predCategoryPath, actionSequence.filename)
        
        f = open(predFilePath, 'w')
        f.close()
        
        if predCagtegoryId == actionSequence.categoryId:
            numCorrect += 1 
            
        trueLabels.append(actionSequence.categoryName)
        testPredLabels.append(predCategoryName)
    
    # Calculate results
    accuracy = numCorrect / float(len(testActionSequence))
    print "accuracy: ", accuracy
    
    # Plot confusion matrix
    saveFilename = 'confusion_matrix.png'
    plotConfusionMatrix(trueLabels, testPredLabels, 
                        saveFilename, normalization = False)
    
    saveFilename = 'confusion_matrix_norm.png'
    plotConfusionMatrix(trueLabels, testPredLabels, 
                        saveFilename, normalization = True)