def __init__(self,
                 height=5,
                 nbands=4,
                 scale_factor=2,
                 extract_level=1,
                 visualize=False):
        '''Phase_Difference_Extractor: A class to do steerable pyramid computation, extract the phase and phase difference
        Usage: 
              build_pyramid(): build complex steerable pyramid coefficients
              extract(): extract phase differences
        Parameters:
            height: int, default 5
                The coefficients levels including low-pass and high-pass
            nbands: int, default 4
                The number of orientations of the bandpass filters
            scale_factor: int, default 2
                Spatial resolution reduction scale scale_factor
            extract_level: int, or list of int numbers, default 1
                If extract_level is an int number, build_pyramid() will only return the coefficients in one level;
                If extract_level is a list, build_pyramid() will only return the coefficients of multiple levels.
            visualize: bool, default False
               If true, the build_pyramid() and extract() will show the processed results.
        '''

        self.pyramid = SCFpyr_PyTorch(height=height,
                                      nbands=nbands,
                                      scale_factor=scale_factor,
                                      device=get_device())
        self.height = height
        self.nbands = nbands
        self.scale_factor = scale_factor
        self.extract_level = extract_level
        self.visualize = visualize
Example #2
0
if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument('--image_file',
                        type=str,
                        default='./assets/patagonia.jpg')
    parser.add_argument('--batch_size', type=int, default='32')
    parser.add_argument('--image_size', type=int, default='200')
    parser.add_argument('--pyr_nlevels', type=int, default='5')
    parser.add_argument('--pyr_nbands', type=int, default='4')
    parser.add_argument('--pyr_scale_factor', type=int, default='2')
    parser.add_argument('--device', type=str, default='cuda:0')
    parser.add_argument('--visualize', type=bool, default=True)
    config = parser.parse_args()

    device = utils.get_device(config.device)

    ############################################################################
    # Build the complex steerable pyramid

    pyr = SCFpyr_PyTorch(height=config.pyr_nlevels,
                         nbands=config.pyr_nbands,
                         scale_factor=config.pyr_scale_factor,
                         device=device)

    ############################################################################
    # Create a batch and feed-forward

    start_time = time.time()

    # Load Batch
Example #3
0
import os
import sys
#os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
#os.environ['CUDA_VISIBLE_DEVICES'] = str(0)
from sampler.image_sampler import Image_Sampler
import torch
from torch.nn import functional as F
from tqdm import tqdm
from utils.model_utils import load_model, compose_transforms
import numpy as np
from steerable.utils import get_device
device = get_device()


class Resnet50_Extractor(object):
    def __init__(self,
                 benchmark_dir='pytorch-benchmarks',
                 model_name='resnet50_ferplus_dag',
                 feature_layer='pool5_7x7_s1'):
        ''' Resnet50_Extractor: A feature extractor to extractor the final convolutional layer's 
         feature vector (2048 dimensional) and save those feature vectors to npy file in an 
         output directory.

        Parameters: 
            benchmark_dir: string, default 'pytorch-benchmarks'
                The pytorch-benchmarks is installed in benchmark_dir.
            model_name: string, default 'resnet50_ferplus_dag'
                The model name for resnet50 model.
            feature_layer: string, default is 'pool5_7x7_s1'
                The output feature layer for resnet50 model is the final convolutional layer named
                'pool5_7x7_s1'.
Example #4
0
    def __init__(
            self,
            # parameters for testing
            model_path,
            batch_size,
            device=None,
            workers=0,
            # parameters for the video processing
            save_size=112,
            nomask=True,
            grey=False,
            quiet=True,
            tracked_vid=False,
            noface_save=False,
            openface_exe='OpenFace/build/bin/FeatureExtraction',
            # parameters for deep feature extraction (Resnet50)
            benchmark_dir='pytorch-benchmarks',
            model_name='resnet50_ferplus_dag',
            feature_layer='pool5_7x7_s1',
            # parameters for snipper sampler
            num_phase=12,
            phase_size=48,
            length=64,
            stride=64,
            # parameters for phase difference extractor
            height=4,
            nbands=2,
            scale_factor=2,
            extract_level=[1, 2]):

        assert os.path.exists(model_path), \
            "Please, download the model checkpoint first."

        self.batch_size = batch_size
        self.workers = workers
        self.num_phase = num_phase
        self.phase_size = phase_size
        self.length = length
        self.stride = stride

        self.device = get_device(device)
        # Face detection and face alignment
        self.video_processor = VideoProcessor(save_size, nomask, grey, quiet,
                                              tracked_vid, noface_save,
                                              openface_exe)
        # From snippets to deep facial features
        self.resnet50_extractor = Resnet50Extractor(benchmark_dir, self.device,
                                                    model_name, feature_layer)
        # Phase and phase differences over time on faces
        self.pd_extractor = Phase_Difference_Extractor(height, nbands,
                                                       scale_factor,
                                                       extract_level,
                                                       self.device, not quiet)

        self.model = Two_Stream_RNN()  # model for FER
        checkpoint = torch.load(model_path, map_location=self.device)
        self.model.load_state_dict(checkpoint['state_dict'])
        self.model = self.model.eval()
        self.model.to(self.device)

        logger.info(f"Loaded checkpoint from {model_path},"
                    f"Epoch:{checkpoint['epoch']}")

        self.label_name = ['valence', 'arousal']  # model output format