Exemplo n.º 1
0
def train():
    # Get the dataset
    for phase in phases:
        if phase == 'train':
            if dataset_name == 'hollywood':
                train_data_list_path = os.path.join(
                    opt.hollywood_dataset_root_path, 'hollywood_train.idl')
                train_data_list = utils.get_phase_data_list(
                    train_data_list_path, dataset_name)
            if dataset_name == 'brainwash':
                train_data_list_path = os.path.join(
                    opt.brainwash_dataset_root_path, 'brainwash_train.idl')
                train_data_list = utils.get_phase_data_list(
                    train_data_list_path, dataset_name)
        elif phase == 'val':
            if dataset_name == 'hollywood':
                val_data_list_path = os.path.join(
                    opt.hollywood_dataset_root_path, 'hollywood_val.idl')
                val_data_list = utils.get_phase_data_list(
                    val_data_list_path, dataset_name)
            if dataset_name == 'brainwash':
                val_data_list_path = os.path.join(
                    opt.brainwash_dataset_root_path, 'brainwash_val.idl')
                val_data_list = utils.get_phase_data_list(
                    val_data_list_path, dataset_name)
        elif phase == 'test':
            if dataset_name == 'hollywood':
                test_data_list_path = os.path.join(
                    opt.hollywood_dataset_root_path, 'hollywood_test.idl')
                test_data_list = utils.get_phase_data_list(
                    test_data_list_path, dataset_name)
            if dataset_name == 'brainwash':
                test_data_list_path = os.path.join(
                    opt.brainwash_dataset_root_path, 'brainwash_test.idl')
                test_data_list = utils.get_phase_data_list(
                    test_data_list_path, dataset_name)

    print("Number of images for training: %s" % (len(train_data_list)))
    print("Number of images for val: %s" % (len(val_data_list)))
    print("Number of images for test: %s" % (len(test_data_list)))

    if data_check_flag:
        utils.check_loaded_data(train_data_list[random.randint(
            1, len(train_data_list))])
        utils.check_loaded_data(val_data_list[random.randint(
            1, len(val_data_list))])
        utils.check_loaded_data(test_data_list[random.randint(
            1, len(test_data_list))])

    # Load the train dataset
    train_dataset = Dataset(train_data_list)
    test_dataset = Dataset(val_data_list)
    print("Load data.")

    train_dataloader = data_.DataLoader(train_dataset,
                                        batch_size=1,
                                        shuffle=True,
                                        num_workers=1)
    test_dataloader = data_.DataLoader(test_dataset,
                                       batch_size=1,
                                       shuffle=True,
                                       num_workers=1)
    # Initialize the head detector.
    head_detector_vgg16 = Head_Detector_VGG16(ratios=[1],
                                              anchor_scales=[8, 16])
    print("model construct completed")
    trainer = Head_Detector_Trainer(head_detector_vgg16).cuda()
    lr_ = opt.lr
    for epoch in range(opt.epoch):
        trainer.reset_meters()
        for ii, (img, bbox_, scale) in enumerate(train_dataloader):
            scale = at.scalar(scale)
            img, bbox = img.cuda().float(), bbox_.cuda()
            img, bbox = Variable(img), Variable(bbox)
            _, _, _ = trainer.train_step(img, bbox, scale)
            print("Forward and backward pass done.")
            if (ii + 1) % opt.plot_every == 0:
                trainer.vis.plot_many(trainer.get_meter_data())
                ori_img_ = inverse_normalize(at.tonumpy(img[0]))
                gt_img = visdom_bbox(ori_img_, at.tonumpy(bbox_[0]))
                trainer.vis.img('gt_img', gt_img)
                rois, _ = trainer.head_detector.predict(img,
                                                        scale=scale,
                                                        mode='visualize')
                pred_img = visdom_bbox(ori_img_, at.tonumpy(rois))
                trainer.vis.img('pred_img', pred_img)
                trainer.vis.text(str(trainer.rpn_cm.value().tolist()),
                                 win='rpn_cm')

        avg_test_CorrLoc = eval(test_dataloader, head_detector_vgg16)

        print("Epoch {} of {}.".format(epoch + 1, opt.epoch))
        print("  test average corrLoc accuracy:\t\t{:.3f}".format(
            avg_test_CorrLoc))

        model_save_path = trainer.save(best_map=avg_test_CorrLoc)

        if epoch == 8:
            trainer.load(model_save_path)
            trainer.head_detector.scale_lr(opt.lr_decay)
            lr_ = lr_ * opt.lr_decay
 def __init__(self, model_path="/home/ubuntu/suraj/package/FCHD/checkpoints/head_detector_final"):
     self.head_detector = Head_Detector_VGG16(ratios=[1], anchor_scales=[2,4])
     trainer = Head_Detector_Trainer(self.head_detector).cuda()
     trainer.load(model_path)
Exemplo n.º 3
0
from __future__ import division

import cv2
from src.head_detector_vgg16 import Head_Detector_VGG16
from trainer import Head_Detector_Trainer
from PIL import Image
import numpy as np
from data.dataset import preprocess
import src.array_tool as at

THRESH = 0.0075
IM_RESIZE = True

model_path = "/home/zhang/FCHD-Fully-Convolutional-Head-Detector/checkpoints/head_detector_final"
head_detector = Head_Detector_VGG16(ratios=[1], anchor_scales=[2, 4])
trainer = Head_Detector_Trainer(head_detector).cuda()
trainer.load(model_path)


def read_img(path):
    f = Image.fromarray(path)
    if IM_RESIZE:
        f = f.resize((640, 480), Image.ANTIALIAS)

    f.convert('RGB')
    img_raw = np.asarray(f, dtype=np.uint8)
    img_raw_final = img_raw.copy()
    img = np.asarray(f, dtype=np.float32)
    _, H, W = img.shape
    img = img.transpose((2, 0, 1))
    img = preprocess(img)
Exemplo n.º 4
0
from PIL import Image
import numpy as np
from data.dataset import preprocess
import matplotlib.pyplot as plt 
import src.array_tool as at
from src.vis_tool import visdom_bbox
import argparse
import src.utils as utils
from src.config import opt
#import simplejson as json


os.environ["CUDA_VISIBLE_DEVICES"]="0,1"

head_detector = Head_Detector_VGG16(ratios=[1], anchor_scales=[2,4])
trainer = Head_Detector_Trainer(head_detector).cuda()
trainer.load('./checkpoints/head_detector_final')



#data = {"taskId":"1", "roomId":"1", "liveUrl":"3.mp4", "recognitionDuration":30,"verifyCode":"xxxx"}
class IndexHandler(tornado.web.RequestHandler):
    executor = ThreadPoolExecutor(200)
    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def post(self):
        try:
            #sends= json_decode(self.request.body)
            sends = self.request.body
            data = json.loads(self.request.body)
            liveUrl = data['liveUrl']