Exemplo 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)
Exemplo n.º 2
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)
Exemplo n.º 3
0
def main():
    imageDatasetDir = config.imageDatasetDir
    
    # Select part of the whole categories except 'BACKGROUND_Google'
    categories = os.listdir(imageDatasetDir)
    categories = categories[:config.numCategories]
    
    allFeatures = np.array([])
    
    ## Step.1 Data loading and _features extraction 
    # Get _features from training data over all categories
    print "Data loading and feature extraction ..."
    
    trainImageData = []
    testImageData = []
    
    for category in categories:
        categoryPath = os.path.join(imageDatasetDir, category)
        allData = os.listdir(categoryPath)
        numData = len(allData) # number of all data of the category
        numTrainData = int(numData * config.percentageTrainData)    # number of training data
        
        trainData = allData[:numTrainData]
        testData = allData[numTrainData:]
        
        # Train data loading
        for data in trainData:
            filePath = os.path.join(categoryPath, data)
            
            imageData = ImageData(filePath)
            imageData.extractFeatures()
            imageData.className = category
            imageData.classId = categories.index(category)
            
            trainImageData.append(imageData)
            
            if allFeatures.size == 0:
                allFeatures = imageData.features
            else:
                allFeatures = np.vstack((allFeatures, imageData.features))
        
        # Test data loading
        for data in testData:
            filePath = os.path.join(categoryPath, data)
            
            imageData = ImageData(filePath)
            imageData.extractFeatures()
            imageData.className = category
            imageData.classId = categories.index(category)
            
            testImageData.append(imageData)
    
    ## Step.2 Codebook generation
    print "Codebook generation ..."
    bovw = BagOfWords()
    if os.path.exists('codebook.npy'):
        codebook = np.load('codebook.npy')
        bovw.codebook = codebook
    else:
        bovw.generateCodebook(allFeatures)
    
    ## Step.3 Feature encoding for train data
    train_y = []
    train_X = []
    
    for imageData in trainImageData:
        # Feature encoding, pooling, normalization
        imageData.generateFinalFeatures(bovw)
        
        # Format train data
        train_y.append(imageData.classId)
        train_X.append(imageData._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 "Classifying ..."
    numCorrect = 0
    for imageData in testImageData:
        # Feature encoding, pooling, normalization
        imageData.generateFinalFeatures(bovw)
        
        # Format train data
        test_y = [imageData.classId]
        test_X = [imageData.finalFeatures]
        
        p_label, _ = svmTool.doPredication(test_y, test_X)
        predClassId = int(p_label[0])
        predClassName = categories[predClassId]
        
        # Write test image to predicated category
        predClassPath = os.path.join(config.predctDir, predClassName)
        if not os.path.exists(predClassPath):
            os.makedirs(predClassPath)
        
        imageName = imageData.className + '_' + os.path.basename(imageData.filepath)
        predFilePath = os.path.join(predClassPath, imageName)
        
        cv2.imwrite(predFilePath, imageData.image)
        
        if predClassId == imageData.classId:
            numCorrect += 1 
    
    # Calculate results
    accuracy = numCorrect / float(len(testImageData))
    print "accuracy: ", accuracy
Exemplo n.º 4
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)