Example #1
0
def load_model():
    if args.net_type == 'slim':
        model_path = "models/train-version-slim-new/slim_new-Epoch-150-Loss-2.590208204874552.pth"
        # model_path = "models/pretrained/version-slim-640.pth"
        net = create_mb_tiny_fd(len(class_names),
                                is_test=True,
                                device=test_device)
        predictor = create_mb_tiny_fd_predictor(
            net,
            model_path,
            candidate_size=args.candidate_size,
            device=test_device,
            fuse=True)
    elif args.net_type == 'RFB':
        model_path = "models/train-version-RFB-new/RFB-Epoch-160-Loss-2.547697603463169.pth"
        # model_path = "models/pretrained/version-RFB-640.pth"
        net = create_Mb_Tiny_RFB_fd(len(class_names),
                                    is_test=True,
                                    device=test_device)
        predictor = create_Mb_Tiny_RFB_fd_predictor(
            net,
            model_path,
            candidate_size=args.candidate_size,
            device=test_device,
            fuse=True)
    else:
        print("The net type is wrong!")
        sys.exit(1)
    return predictor
def load_model():
    if args.net_type == 'slim':
        model_path = "models/train-version-slim-new/slim-Epoch-5-Loss-3.3751535778045656.pth"
        # model_path = "models/pretrained/version-slim-640.pth"
        net = create_mb_tiny_fd(len(class_names),
                                is_test=True,
                                device=test_device)
        predictor = create_mb_tiny_fd_predictor(
            net,
            model_path,
            candidate_size=args.candidate_size,
            device=test_device,
            fuse=True)
    elif args.net_type == 'RFB':
        model_path = "models/train-version-RFB-640/RFB-Epoch-95-Loss-1.9533395563301288.pth"
        # model_path = "models/pretrained/version-RFB-640.pth"
        net = create_Mb_Tiny_RFB_fd(len(class_names),
                                    is_test=True,
                                    device=test_device)
        predictor = create_Mb_Tiny_RFB_fd_predictor(
            net,
            model_path,
            candidate_size=args.candidate_size,
            device=test_device,
            fuse=True)
    else:
        print("The net type is wrong!")
        sys.exit(1)

    return predictor
Example #3
0
 def create_predictor(param):
     """
     推論器の作成
     :param param:
     :return:
     """
     net = None
     model_path = None
     predictor = None
     label_path = MODEL_PATH + "voc-model-labels.txt"
     # must put define_img_size() before 'import create_mb_tiny_fd, create_mb_tiny_fd_predictor'
     define_img_size(param["InputSize"])
     class_names = [name.strip() for name in open(label_path).readlines()]
     if param["NetType"] == 'slim':
         model_path = MODEL_PATH + "pretrained/version-slim-320.pth"
         # model_path = "models/pretrained/version-slim-640.pth"
         net = create_mb_tiny_fd(len(class_names),
                                 is_test=True,
                                 device=param["TestDevice"])
         predictor = create_mb_tiny_fd_predictor(
             net,
             candidate_size=param["CandidateSize"],
             device=param["TestDevice"])
     elif param["NetType"] == 'RFB':
         model_path = MODEL_PATH + "pretrained/version-RFB-320.pth"
         # model_path = MODEL_PATH + "pretrained/version-RFB-640.pth"
         net = create_Mb_Tiny_RFB_fd(len(class_names),
                                     is_test=True,
                                     device=param["TestDevice"])
         predictor = create_Mb_Tiny_RFB_fd_predictor(
             net,
             candidate_size=param["CandidateSize"],
             device=param["TestDevice"])
     net.load(model_path)
     return predictor
Example #4
0
def fuse_model():
    input_img_size = 320
    define_img_size(input_img_size)
    net_type = "slim"  # inference faster,lower precision
    label_path = "./models/train-version-slim-480/voc-model-labels.txt"
    class_names = [name.strip() for name in open(label_path).readlines()]
    num_classes = len(class_names)
    if net_type == 'slim':
        model_path = "models/train-version-slim/slim-Epoch-95-Loss-2.1851983154029178.pth"
        # model_path = "models/pretrained/version-slim-320.pth"
        net = create_mb_tiny_fd(len(class_names), is_test=True, device='cpu')
    elif net_type == 'RFB':
        model_path = "models/pretrained/version-RFB-320.pth"
        # model_path = "models/pretrained/version-RFB-640.pth"
        net = create_Mb_Tiny_RFB_fd(len(class_names), is_test=True)
    else:
        print("unsupport network type.")
        sys.exit(1)
    # net.load(model_path)
    net.eval()
    net.to("cpu")
    print_size_of_model(net)
    dummy_input = torch.randn(1, 3, 240, 320).to("cpu")
    start = time.time()
    for i in range(0, 20):
        net(dummy_input)
    fps = 20 / (time.time() - start)
    print("FPS-320 {}".format(fps))

    dummy_input = torch.randn(1, 3, 480, 640).to("cpu")
    start = time.time()
    for i in range(0, 20):
        net(dummy_input)
    fps = 20 / (time.time() - start)
    print("FPS-640 {}".format(fps))

    net.fuse()
    print_size_of_model(net)
    dummy_input = torch.randn(1, 3, 240, 320).to("cpu")
    start = time.time()
    for i in range(0, 20):
        net(dummy_input)
    fps = 20 / (time.time() - start)
    print("FPS-320 {}".format(fps))

    dummy_input = torch.randn(1, 3, 480, 640).to("cpu")
    start = time.time()
    for i in range(0, 20):
        net(dummy_input)
    fps = 20 / (time.time() - start)
    print("FPS-640 {}".format(fps))
def detect_face(orig_image, net_type='slim', candidate_size=750, test_device='cuda', threshold=0.6):
    if net_type == 'slim':
        model_path = "models/pretrained/version-slim-320.pth"
        # model_path = "models/pretrained/version-slim-640.pth"
        net = create_mb_tiny_fd(len(class_names), is_test=True, device=test_device)
        predictor = create_mb_tiny_fd_predictor(net, candidate_size=args.candidate_size, device=test_device)
    elif args.net_type == 'RFB':
        model_path = "models/pretrained/version-RFB-320.pth"
        # model_path = "models/pretrained/version-RFB-640.pth"
        net = create_Mb_Tiny_RFB_fd(len(class_names), is_test=True, device=test_device)
        predictor = create_Mb_Tiny_RFB_fd_predictor(net, candidate_size=args.candidate_size, device=test_device)
    else:
        print("The net type is wrong!")
        sys.exit(1)
    net.load(model_path)
    image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB)
    return predictor.predict(image, candidate_size, threshold)
Example #6
0
    def load_model_ultra(self):
        from vision.ssd.config.fd_config import define_img_size
        net_type = 'RFB'  # The network architecture ,optional: RFB (higher precision) or slim (faster)
        input_size = 480  # help='define network input size,default optional value 128/160/320/480/640/1280')
        threshold = 0.7  # help='score threshold')
        candidate_size = 1000  # help='nms candidate size')

        test_device = "cuda:0"  # help='cuda:0 or cpu')

        define_img_size(
            input_size)  # must put define_img_size() before 'import create_mb_tiny_fd, create_mb_tiny_fd_predictor'

        from vision.ssd.mb_tiny_fd import create_mb_tiny_fd, create_mb_tiny_fd_predictor
        from vision.ssd.mb_tiny_RFB_fd import create_Mb_Tiny_RFB_fd, create_Mb_Tiny_RFB_fd_predictor
        from vision.utils.misc import Timer
        label_path = os.path.join(base_config.root_dir,r'core\face\faster_face\models\voc-model-labels.txt')


        # cap = cv2.VideoCapture(args.video_path)  # capture from video
        cap = cv2.VideoCapture(0)  # capture from camera

        class_names = [name.strip() for name in open(label_path).readlines()]
        num_classes = len(class_names)
        test_device = test_device

        candidate_size = candidate_size
        threshold = threshold

        if net_type == 'slim':
            model_path = os.path.join(base_config.root_dir,r'core\face\faster_face\models\pretrained\version-slim-320.pth')
            # model_path = "models/pretrained/version-slim-640.pth"
            net = create_mb_tiny_fd(len(class_names), is_test=True, device=test_device)
            predictor = create_mb_tiny_fd_predictor(net, candidate_size=candidate_size, device=test_device)
        elif net_type == 'RFB':
            model_path = os.path.join(base_config.root_dir,r'core\face\faster_face\models\pretrained\version-RFB-320.pth')
            # model_path = "models/pretrained/version-RFB-640.pth"
            net = create_Mb_Tiny_RFB_fd(len(class_names), is_test=True, device=test_device)
            predictor = create_Mb_Tiny_RFB_fd_predictor(net, candidate_size=candidate_size, device=test_device)
        else:
            print("The net type is wrong!")
            sys.exit(1)
        net.load(model_path)
        return predictor
Example #7
0
# net_type = "slim"  # inference faster,lower precision
net_type = "RFB"  # inference lower,higher precision

label_path = "models/voc-model-labels.txt"
class_names = [name.strip() for name in open(label_path).readlines()]
num_classes = len(class_names)

if net_type == 'slim':
    model_path = "models/pretrained/version-slim-320.pth"
    # model_path = "models/pretrained/version-slim-640.pth"
    net = create_mb_tiny_fd(len(class_names), is_test=True)
elif net_type == 'RFB':
    model_path = "models/pretrained/version-RFB-320.pth"
    # model_path = "models/pretrained/version-RFB-640.pth"
    net = create_Mb_Tiny_RFB_fd(len(class_names), is_test=False)

else:
    print("unsupport network type.")
    sys.exit(1)
net.load(model_path)
net.eval()
net.to("cuda")

model_name = model_path.split("/")[-1].split(".")[0]
model_path = f"models/onnx/base-model-new-320.onnx"

dummy_input = torch.randn(1, 3, 240, 320).to("cuda")
# dummy_input = torch.randn(1, 3, 480, 640).to("cuda") #if input size is 640*480
# NOTE change to a single output
#torch.onnx.export(net, dummy_input, model_path, verbose=False, input_names=['input'], output_names=['scores', 'boxes'])
from vision.ssd.mb_tiny_RFB_fd import create_Mb_Tiny_RFB_fd, create_Mb_Tiny_RFB_fd_predictor

result_path = "./detect_imgs_results"
label_path = "./models/voc-model-labels.txt"
test_device = args.test_device

class_names = [name.strip() for name in open(label_path).readlines()]
if args.net_type == 'slim':
    model_path = "models/pretrained/version-slim-320.pth"
    # model_path = "models/pretrained/version-slim-640.pth"
    net = create_mb_tiny_fd(len(class_names), is_test=True, device=test_device)
    predictor = create_mb_tiny_fd_predictor(net, candidate_size=args.candidate_size, device=test_device)
elif args.net_type == 'RFB':
    model_path = "models/pretrained/version-RFB-320.pth"
    # model_path = "models/pretrained/version-RFB-640.pth"
    net = create_Mb_Tiny_RFB_fd(len(class_names), is_test=True, device=test_device)
    predictor = create_Mb_Tiny_RFB_fd_predictor(net, candidate_size=args.candidate_size, device=test_device)
else:
    print("The net type is wrong!")
    sys.exit(1)
net.load(model_path)

"""
torchserve 模型导出
"""
example_input = torch.rand(1, 3, 640, 480)
net.eval()
inputs = example_input.to("cuda:0")
output = torch.jit.trace(net, inputs)
output.save("./version-RFB-320.pt")
Example #9
0
"""
import time

import torch
from torchstat import stat
from torchsummary import summary

from vision.ssd.mb_tiny_fd import create_mb_tiny_fd
from vision.ssd.mb_tiny_RFB_fd import create_Mb_Tiny_RFB_fd

device = "cpu"  # default cpu
width = 320
height = 240

# fd = create_mb_tiny_fd(2)
fd = create_Mb_Tiny_RFB_fd(2)

print(fd)
fd.eval()
fd.to(device)
x = torch.randn(1, 3, width, height).to(device)

summary(fd.to("cuda"), (3, width, height))

from ptflops import get_model_complexity_info

flops, params = get_model_complexity_info(fd.to(device), (3, width, height),
                                          print_per_layer_stat=True,
                                          as_strings=True)
print("FLOPS:", flops)
print("PARAMS:", params)
Example #10
0
import sys
import torch.onnx
from vision.ssd.config.fd_config import define_img_size

input_img_size = 320
define_img_size(input_img_size)
from vision.ssd.mb_tiny_RFB_fd import create_Mb_Tiny_RFB_fd
from vision.ssd.mb_tiny_fd import create_mb_tiny_fd

net_type = "RFB"
if net_type == 'slim':
    model_path = "models/pretrained/version-slim-320.pth"
    net = create_mb_tiny_fd(2, is_test=True)
elif net_type == 'RFB':
    model_path = "models/pretrained/version-RFB-320.pth"
    net = create_Mb_Tiny_RFB_fd(2, is_test=True)
else:
    print("unsupport network type.")
    sys.exit(1)

net.load(model_path)
net.eval()
net.to("cpu")

model_path = f"models/onnx/RFB-test.onnx"

dummy_input = torch.randn(1, 3, 240, 320)
torch.onnx.export(net,
                  dummy_input,
                  model_path,
                  verbose=False,
Example #11
0
label_path = "models/voc-model-labels.txt"
class_names = [name.strip() for name in open(label_path).readlines()]
num_classes = len(class_names)

if net_type == 'slim':
    # model_path = "models/pretrained/version-slim-320.pth"
    model_path = "models/pretrained/version-slim-640.pth"
    net = create_mb_tiny_fd(len(class_names),
                            is_test=True,
                            without_postprocessing=True)
elif net_type == 'RFB':
    # model_path = "models/pretrained/version-RFB-320.pth"
    model_path = "models/pretrained/ir_faces/RFB-640-best.pth"
    net = create_Mb_Tiny_RFB_fd(len(class_names),
                                is_test=True,
                                without_postprocessing=True)

else:
    print("unsupport network type.")
    sys.exit(1)
net.load(model_path)
net.eval()
net.to("cuda")

model_path = Path(model_path)
model_name = model_path.stem
model_path = model_path.parent / (model_name + '_wopp.onnx')

# dummy_input = torch.randn(1, 3, 240, 320).to("cuda")
dummy_input = torch.randn(1, 3, 480, 640).to("cuda")  #if input size is 640*480
def calc_mAP(weights,
             batch_size=16,
             img_size=640,
             iou_thres=0.5,
             conf_thres=0.001,
             nms_thres=0.5,
             save_json=False,
             model=None):
    label_path = "models/train-coco_person_face-0.0.3-RFB-160/coco-person-face-labels.txt"
    class_names = [name.strip() for name in open(label_path).readlines()]
    num_classes = len(class_names)
    if opt.net_type == 'RFB':
        net = create_Mb_Tiny_RFB_fd(len(class_names),
                                    is_test=True,
                                    device="cuda:0")
        predictor = create_Mb_Tiny_RFB_fd_predictor(net,
                                                    candidate_size=100,
                                                    device="cuda:0")
    elif opt.net_type == 'slim':
        net = create_mb_tiny_fd(len(class_names),
                                is_test=True,
                                device="cuda:0")
        predictor = create_mb_tiny_fd_predictor(net,
                                                candidate_size=100,
                                                device="cuda:0")

    net.load(weights)
    net.eval()
    device = "cuda:0"

    # target_transform = MatchPrior(config.priors, config.center_variance,
    #                               config.size_variance, 0.34999999404)
    #
    # test_transform = TestTransform(config.image_size, config.image_mean_test, config.image_std)
    # val_dataset = COCODataset("/media/test/data/coco", transform=test_transform,
    #                           target_transform=target_transform, is_test=True)
    # logging.info("validation dataset size: {}".format(len(val_dataset)))
    #
    # val_loader = DataLoader(val_dataset, batch_size,
    #                         num_workers=4,
    #                         shuffle=False)

    data_list = json.load(open("/media/test/data/coco/test_datalist.json"))

    all_correct = None
    all_p_prob = None
    all_p_label = None
    all_g_label = None
    seen = 0
    for data in tqdm(data_list):
        image_id = data['image_file']
        gt_boxes = np.array(data['boxes'], dtype=np.float32)
        gt_labels = np.array(data['labels'], dtype=np.int64)
        image = cv2.imread(image_id)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        p_boxes, p_labels, p_probs = predictor.predict(image)
        nl = gt_labels.shape[0]
        correct = np.array([0] * p_boxes.shape[0])
        for i, gt_box in enumerate(gt_boxes):
            seen += 1
            p_index = np.array(range(p_boxes.shape[0]))
            gt_label = gt_labels[i]
            valid_p_boxes = p_boxes[correct == 0]  # remove matched predic box
            valid_p_index = p_index[correct == 0]
            valid_p_probs = p_probs[correct == 0]
            valid_p_labels = p_labels[correct == 0]
            valid_p_boxes = valid_p_boxes[
                valid_p_labels == gt_label]  # select predict label == gt label
            valid_p_index = valid_p_index[valid_p_labels == gt_label]
            valid_p_probs = valid_p_probs[valid_p_labels == gt_label]
            if valid_p_boxes.shape[0] == 0:
                continue
            iou = iou_of(torch.tensor(valid_p_boxes),
                         torch.tensor(np.expand_dims(gt_box, axis=0)))
            max_val = torch.max(iou)
            if max_val.item() > iou_thres:
                correct[valid_p_index[torch.argmax(iou).item()]] = 1
        all_correct = np.concatenate(
            [all_correct, correct],
            axis=0) if all_correct is not None else correct
        all_p_prob = np.concatenate(
            [all_p_prob, p_probs],
            axis=0) if all_p_prob is not None else p_probs
        all_p_label = np.concatenate(
            [all_p_label, p_labels],
            axis=0) if all_p_label is not None else p_labels
        all_g_label = np.concatenate(
            [all_g_label, gt_labels],
            axis=0) if all_g_label is not None else gt_labels
    p, r, ap, f1, ap_class = ap_per_class(all_correct, all_p_prob, all_p_label,
                                          all_g_label)
    mp, mr, map, mf1 = p.mean(), r.mean(), ap.mean(), f1.mean()
    nt = np.bincount(all_g_label.astype(np.int64),
                     minlength=2)  # number of targets per class
    # Print results
    phead = '%30s' + '%10s' * 6
    print(phead % ('type', 'total', 'total', 'mp', 'mr', 'map', 'mf1'))
    pf = '%30s' + '%10.3g' * 6  # print format
    print(pf % ('all', seen, nt.sum(), mp, mr, map, mf1))

    # Print results per class
    names = ['BACKGROUND', 'person', 'face']
    for i, c in enumerate(ap_class):
        print(pf % (names[c], seen, nt[c], p[i], r[i], ap[i], f1[i]))