Skip to content
Snippets Groups Projects

WIP: Generic trainer

Closed Anjith GEORGE requested to merge generic_trainer into master
3 files
+ 263
0
Compare changes
  • Side-by-side
  • Inline
Files
3
 
 
 
# =============================================================================
 
# define instance of the preprocessor:
 
 
from bob.pad.face.preprocessor import VideoFaceCropAlignBlockPatch
 
 
from bob.pad.face.preprocessor import FaceCropAlign
 
 
from bob.bio.video.preprocessor import Wrapper
 
 
from bob.bio.video.utils import FrameSelector
 
 
from torchvision import transforms
 
 
import numpy as np
 
 
from bob.pad.face.database import BatlPadDatabase
 
 
from bob.learn.pytorch.datasets import ChannelSelect, RandomHorizontalFlipImage
 
 
 
FACE_SIZE = 224 # The size of the resulting face
 
RGB_OUTPUT_FLAG = True # RGB output
 
USE_FACE_ALIGNMENT = True # use annotations
 
MAX_IMAGE_SIZE = None # no limiting here
 
FACE_DETECTION_METHOD = 'mtcnn' # use annotations
 
MIN_FACE_SIZE = 50 # skip small faces
 
ALIGNMENT_TYPE='default'
 
 
 
_image_preprocessor = FaceCropAlign(face_size = FACE_SIZE,
 
rgb_output_flag = RGB_OUTPUT_FLAG,
 
use_face_alignment = USE_FACE_ALIGNMENT,
 
alignment_type=ALIGNMENT_TYPE,
 
max_image_size = MAX_IMAGE_SIZE,
 
face_detection_method = FACE_DETECTION_METHOD,
 
min_face_size = MIN_FACE_SIZE)
 
 
_frame_selector = FrameSelector(selection_style = "all")
 
 
preprocessor = Wrapper(preprocessor = _image_preprocessor,
 
frame_selector = _frame_selector)
 
 
 
 
 
#====================================================================================
 
# DeepPixBiS Extractor
 
 
 
from bob.learn.pytorch.extractor.image import GenericExtractor
 
 
from bob.learn.pytorch.architectures import DeepMSPAD
 
 
 
from bob.bio.video.extractor import Wrapper
 
 
MODEL_FILE='/idiap/temp/ageorge/Pytorch_WMCA/Generic-DeepMSPAD/model_0_0.pth'
 
####################################################################
 
 
SCORING_METHOD='pixel_mean' # 'pixel_mean','binary','combined'
 
 
SELECTED_CHANNELS = [0,1,2,3]
 
####################################################################
 
_img_transform = transforms.Compose([ChannelSelect(selected_channels = SELECTED_CHANNELS),transforms.ToPILImage(),transforms.Resize((224,224), interpolation=2),transforms.ToTensor()])
 
 
 
 
def extractor_function(output,kwargs):
 
 
output_pixel = output.data.numpy().flatten()
 
 
score=np.mean(output_pixel)
 
 
return score
 
 
network= DeepMSPAD(pretrained=True, num_channels=4)
 
 
 
_image_extracor=GenericExtractor(network=network,extractor_function=extractor_function,transforms=_img_transform, model_file=MODEL_FILE,scoring_method='pixel_mean')
 
 
_frame_selector = FrameSelector(selection_style = "all")
 
 
extractor = Wrapper(_image_extracor, frame_selector = _frame_selector)
 
 
#=======================================================================================
 
# define algorithm:
 
# Dummy algorithm
 
 
from bob.pad.base.algorithm import Algorithm
 
 
class DummyAlgorithm(Algorithm):
 
"""An algorithm that takes the precomputed predictions and uses them for
 
scoring."""
 
 
def __init__(self, **kwargs):
 
 
super(DummyAlgorithm, self).__init__(
 
**kwargs)
 
 
def project(self, feature):
 
# print("feature",feature.as_array())
 
return feature.as_array().reshape(-1,1)
 
 
 
def score_for_multiple_projections(self, predictions):
 
# one node at the output
 
 
return list(predictions)
 
 
def score(self, predictions):
 
return list(predictions)
 
algorithm = DummyAlgorithm(performs_projection=True, requires_projector_training=False)
 
 
Loading