Esempio n. 1
0
    def __init__(self, train_from_scratch=False, eval_use_only=False):
        """
    age prediction model, provide APIs for public uses
    :param onlyTrainLastLayers: is set, freeze params of previous layers
    :param train_from_scratch: if set, do not load pretrained params
    :param eval_use_only: if set, model will not load training/testing data
    """
        self.from_scratch = train_from_scratch
        self.model = RetrainedResnetModel()
        self.use_gpu = torch.cuda.is_available()
        self.transformer = resnet_transformer()
        self.checkpoint_best = config.model + "best_torch.nn"
        self.checkpoint_last = config.model + "last_torch.nn"
        self.latest_weights = config.model + "latest_torch.nn"
        self.csv_path = config.model + "log.csv"

        self.batch_size = int(parser['TRAIN']['batch_size'])
        self.num_epochs = int(parser['TRAIN']['num_epochs'])
        self.loading_jobs = int(parser['TRAIN']['jobs_to_load_data'])
        self.max_no_reduce = int(parser['TRAIN']['max_no_reduce'])

        self.weight_decay = float(parser['TRAIN']['weight_decay'])
        self.age_divide = float(parser['DATA']['age_divide'])
        self.min_lr_rate = float(parser['TRAIN']['min_lr_rate'])
        self.lr_reduce_by = float(parser['TRAIN']['lr_reduce_by'])
        self.lr_rate = float(parser['TRAIN']['init_lr_rate'])

        self.loaded = False
        self.age_criterion = nn.L1Loss()
        self.gender_criterion = nn.NLLLoss()
        self.aligner = FaceAligner()

        if self.use_gpu:
            self.model = self.model.cuda()
            self.age_criterion = self.age_criterion.cuda()
            self.gender_criterion = self.gender_criterion.cuda()

        columns = [
            'Timstamp', 'Epoch', 'Phase', 'Gender Acc', 'Age MAE',
            'Best Gender Acc', 'Best Age MAE', 'Lr_rate'
        ]
        self.csv_checkpoint = pd.DataFrame(data=[], columns=columns)
        if not self.from_scratch and os.path.exists(self.csv_path):
            self.csv_checkpoint = pd.read_csv(self.csv_path)

        if not eval_use_only:
            self.load_data()
Esempio n. 2
0
import numpy as np
import time
import cv2
import utils
from align_faces import FaceAligner
from predict import FacePredict

net = cv2.dnn.readNetFromCaffe("deploy.prototxt.txt", "resnet10.caffemodel")
print("Model Loaded!")

cap = cv2.VideoCapture(0)
time.sleep(2.0)
print("Starting...")

faceAligner = FaceAligner("dlibModel.dat", 384)
userid = 1
counter = 0

# loop over the frames from the video stream
while True:
    # grab the frame from the threaded video stream and resize it
    # to have a maximum width of 400 pixels
    ret, frame = cap.read()
    frame = cv2.resize(frame, (640, 480))

    # grab the frame dimensions and convert it to a blob
    (h, w) = frame.shape[:2]
    blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0,
                                 (300, 300), (104.0, 177.0, 123.0))

    # pass the blob through the network and obtain the detections and
Esempio n. 3
0
    pwd = os.getcwd()
    if clean:
        clear_dir(config.named)
    os.chdir(config.aligned)
    for img in glob.glob("*.jpg"):
        name = re.findall(r'[^_]*_[^_]*_([\D]*)[0-9]*.jpg', img)
        if not len(name): continue
        name = name[0].lower()
        if not os.path.exists(config.named + name + '/'):
            os.mkdir(config.named + name + '/')
        copyfile(img, config.named + name + '/' + img)
    os.chdir(pwd)


# TODO: any other ways to get around this public variable?
FL = FaceAligner()


def sub_align_face(picname):
    """
  sub thread function to get and store aligned faces
  :param picname: pic names
  :return: 
  """
    aligned = FL.getAligns(picname)
    if len(aligned) == 0:
        return
        # copyfile(picname, config.aligned + picname)
    cv2.imwrite(config.aligned + picname, aligned[0])

Esempio n. 4
0
    def __init__(self,
                 load_best=True,
                 model_name='res18_cls70',
                 eval_use_only=False,
                 new_last_layer=False,
                 new_training_process=True):
        """
    :param load_best: if set, load the best weight, else load the latest weights,
                      usually load the best when doing evaluation, and latest when
                      doing training.
    :param model_name: name used for saving model weight and training info.
    :param eval_use_only: if set, model will not load training/testing data, and 
                          change the bahavior of some layers(dropout, batch norm).
    :param new_last_layer: if the model only changed last fully connected layers,
                            if set, only train last fully connected layers at first 
                            2 epochs.
    :param new_training_process: if set, create a new model and start training.
    """
        # init params
        self.model = AgeGenPredModel()
        self.model_name = model_name
        self.use_gpu = torch.cuda.is_available()
        self.transformer = image_transformer()
        self.load_best = load_best
        self.new_train = new_training_process
        self.new_last_layer = new_last_layer
        self.checkpoint_best = config.model + "{}_best.nn".format(
            model_name.lower())
        self.checkpoint_last = config.model + "{}_last.nn".format(
            model_name.lower())
        self.csv_path = config.model + self.model_name + ".csv"

        # training details
        self.batch_size = int(parser['TRAIN']['batch_size'])
        self.num_epochs = int(parser['TRAIN']['num_epochs'])
        self.loading_jobs = int(parser['TRAIN']['jobs_to_load_data'])
        self.max_no_reduce = int(parser['TRAIN']['max_no_reduce'])
        self.age_cls_unit = int(parser['RacNet']['age_cls_unit'])
        self.weight_decay = float(parser['TRAIN']['weight_decay'])
        self.age_divide = float(parser['DATA']['age_divide'])
        self.min_lr_rate = float(parser['TRAIN']['min_lr_rate'])
        self.lr_reduce_by = float(parser['TRAIN']['lr_reduce_by'])
        self.lr_rate = float(parser['TRAIN']['init_lr_rate'])

        # reduce loss on gender so the model focus on age pred
        self.reduce_gen_loss = float(parser['TRAIN']['reduce_gen_loss'])
        self.reduce_age_mae = float(parser['TRAIN']['reduce_age_mae'])

        self.weight_loaded = False
        self.age_cls_criterion = nn.BCELoss(weight=loss_weight)
        self.age_rgs_criterion = nn.L1Loss()
        self.gender_criterion = nn.CrossEntropyLoss()
        self.aligner = FaceAligner()

        if self.use_gpu:
            self.model = self.model.cuda()
            self.age_cls_criterion = self.age_cls_criterion.cuda()
            self.age_rgs_criterion = self.age_rgs_criterion.cuda()
            self.gender_criterion = self.gender_criterion.cuda()

        # csv checkpoint details
        columns = [
            'Timstamp', 'Epoch', 'Phase', 'AGE ACC', 'AGE MAE', 'GEN ACC',
            'BEST AGE ACC', 'BEST AGE MAE', 'BEST GEN ACC', 'Lr_rate'
        ]
        self.csv_checkpoint = pd.DataFrame(data=[], columns=columns)
        if not self.new_train and os.path.exists(self.csv_path):
            self.csv_checkpoint = pd.read_csv(self.csv_path)

        # load no training data when evaluation,
        if not eval_use_only:
            self.load_data()