def __init__(self, classes=None, weights_file=False):
        super(Yolov2, self).__init__()
        if classes:
            self.num_classes = len(classes)

        darknet19 = Darknet19()

        if weights_file:
            print('load pretrained weight from {}'.format(weights_file))
            darknet19.load_weights(weights_file)
            print('pretrained weight loaded!')

        # darknet backbone
        self.conv1 = nn.Sequential(darknet19.layer0, darknet19.layer1,
                                   darknet19.layer2, darknet19.layer3,
                                   darknet19.layer4)

        self.conv2 = darknet19.layer5

        # detection layers
        self.conv3 = nn.Sequential(
            conv_bn_leaky(1024, 1024, kernel_size=3, return_module=True),
            conv_bn_leaky(1024, 1024, kernel_size=3, return_module=True))

        self.downsampler = conv_bn_leaky(512,
                                         64,
                                         kernel_size=1,
                                         return_module=True)

        self.conv4 = nn.Sequential(
            conv_bn_leaky(1280, 1024, kernel_size=3, return_module=True),
            nn.Conv2d(1024, (5 + self.num_classes) * self.num_anchors,
                      kernel_size=1))

        self.reorg = ReorgLayer()
def main():

    trained_model = cfg.trained_model
    thresh = 0.5
    image_dir = '/home/cory/cedl/vid/videos/vid04'

    net = Darknet19()
    net_utils.load_net(trained_model, net)
    net.eval()
    net.cuda()
    print('load model successfully')
    print(net)

    image_extensions = ['.jpg', '.JPG', '.png', '.PNG']
    image_abs_paths = sorted([
        os.path.join(image_dir, name) for name in os.listdir(image_dir)
        if name[-4:] in image_extensions
    ])

    t_det = Timer()
    t_total = Timer()

    for i, image_path in enumerate(image_abs_paths):
        t_total.tic()
        image, im_data = preprocess(image_path)
        im_data = net_utils.np_to_variable(im_data,
                                           is_cuda=True,
                                           volatile=True).permute(0, 3, 1, 2)
        t_det.tic()
        bbox_pred, iou_pred, prob_pred = net.forward(im_data)
        det_time = t_det.toc()
        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        # print bbox_pred.shape, iou_pred.shape, prob_pred.shape

        bboxes, scores, cls_inds = yolo_utils.postprocess(
            bbox_pred, iou_pred, prob_pred, image.shape, cfg, thresh)

        im2show = yolo_utils.draw_detection(image, bboxes, scores, cls_inds,
                                            cfg)

        if im2show.shape[0] > 1100:
            im2show = cv2.resize(im2show, (int(
                1000. * float(im2show.shape[1]) / im2show.shape[0]), 1000))
        cv2.imshow('test', im2show)

        total_time = t_total.toc()
        format_str = 'frame: %d, (detection: %.1f fps, %.1f ms) (total: %.1f fps, %.1f ms)'
        print(format_str % (i, 1. / det_time, det_time * 1000, 1. / total_time,
                            total_time * 1000))

        t_det.clear()
        t_total.clear()

        key = cv2.waitKey(1)
        if key == ord('q'):
            break
Exemple #3
0
 def __init__(self, channel=None, weights_file=False):
     super(HalfMirror, self).__init__()
     if channel:
         self.num_channel = channel
     darknet = Darknet19()
     seblock = SELayer(self.num_channel)
     self.conv0 = darknet.layer0
     self.max_1 = nn.MaxPool2d(kernel_size=2,
                               stride=2,
                               return_indices=False)
     self.conv2 = darknet.layer1
     self.max_3 = nn.MaxPool2d(kernel_size=2,
                               stride=2,
                               return_indices=False)
     self.conv4_6 = darknet.layer2
     self.max_7 = nn.MaxPool2d(kernel_size=2,
                               stride=2,
                               return_indices=False)
     ###############################################################
     darknet19 = de_Darknet19()
     # self.deconv = nn.Sequential(darknet19.layer1,darknet19.layer2,darknet19.layer3,)
     # self.reconstruct = nn.Sequential(darknet19.layer4)
     #self.unpool_7 = nn.MaxUnpool2d(2, stride=2)
     self.channel_select = seblock
     self.deconv4_6 = darknet19.layer2
     #self.unpool_3 = nn.MaxUnpool2d(2, stride=2)
     self.deconv2 = darknet19.layer3
     #self.unpool_1 = nn.MaxUnpool2d(2, stride=2)
     self.deconv0 = darknet19.layer4
Exemple #4
0
    def __init__(self):
        self.n_boxes = 5
        self.n_classes = 20
        super(YOLO_v2, self).__init__()

        darknet = Darknet19(pretrained=True).features

        self.feature = darknet
        self.layer1 = nn.Sequential(
            nn.Conv2d(in_channels=1024,
                      out_channels=1024,
                      kernel_size=(3, 3),
                      padding=1), nn.BatchNorm2d(1024),
            nn.LeakyReLU(negative_slope=0.1))
        self.layer2 = nn.Sequential(
            nn.Conv2d(in_channels=1024,
                      out_channels=1024,
                      kernel_size=(3, 3),
                      padding=1), nn.BatchNorm2d(1024),
            nn.LeakyReLU(negative_slope=0.1))
        self.layer3 = nn.Sequential(
            nn.Conv2d(in_channels=1024,
                      out_channels=1024,
                      kernel_size=(3, 3),
                      padding=1), nn.BatchNorm2d(1024),
            nn.LeakyReLU(negative_slope=0.1))
        self.layer4 = nn.Sequential(
            nn.Conv2d(in_channels=1024,
                      out_channels=(self.n_boxes * (5 + self.n_classes)),
                      kernel_size=(3, 3),
                      padding=1))
 def __init__(self, modelpath):
     self.thresh = 0.5
     self.label = ('car', 'truck', 'trailer', 'oil')
     self.net = Darknet19()
     net_utils.load_net(modelpath, self.net)
     self.net.cuda()
     self.net.eval()
     print('Yolo init Success')
Exemple #6
0
    def __init__(self, args):
        super(YOLOv2, self).__init__()

        ### YOLOv2 Config from VOC
        self.ANCHORS = ANCHORS
        self.BOX = len(ANCHORS) // 2
        self.LABELS = LABELS
        self.CLASS = len(LABELS)
        self.IMAGE_H = IMAGE_H
        self.IMAGE_W = IMAGE_W
        self.GRID_H = GRID_H
        self.GRID_W = GRID_W

        self.LAMBDA_OBJECT = LAMBDA_OBJECT
        self.LAMBDA_NO_OBJECT = LAMBDA_NO_OBJECT
        self.LAMBDA_COORD = LAMBDA_COORD
        self.LAMBDA_CLASS = LAMBDA_CLASS

        self.OBJ_THRESHOLD = OBJ_THRESHOLD
        self.NMS_THRESHOLD = NMS_THRESHOLD

        ### training config
        self.BATCH_SIZE = int(args.batch_size)
        self.DARKNET19_WEIGHTS = args.darknet19_weights

        ### create YOLO network components
        # helper
        self.best_anchor_finder = BestAnchorFinder(self.ANCHORS)
        self.darknet19 = Darknet19()

        # take some blocks from darknet 19
        self.conv1 = nn.Sequential(self.darknet19.layer0,
                                   self.darknet19.layer1,
                                   self.darknet19.layer2,
                                   self.darknet19.layer3,
                                   self.darknet19.layer4)
        self.conv2 = self.darknet19.layer5
        # detection layers
        self.conv3 = nn.Sequential(
            conv_bn_leaky(1024, 1024, kernel_size=3, return_module=True),
            conv_bn_leaky(1024, 1024, kernel_size=3, return_module=True))
        self.downsampler = conv_bn_leaky(512,
                                         64,
                                         kernel_size=1,
                                         return_module=True)
        self.conv4 = nn.Sequential(
            conv_bn_leaky(1280, 1024, kernel_size=3, return_module=True),
            nn.Conv2d(1024, (5 + self.CLASS) * self.BOX, kernel_size=1))
        # reorg
        self.reorg = ReorgLayer()

        return
Exemple #7
0
    def __init__(self, thresh=0.1, width=600, height=480):

        self.center = (width / 2, height / 2)
        self.camera = cv2.VideoCapture(0)
        self.camera.set(cv2.CAP_PROP_FRAME_WIDTH, width)
        self.camera.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

        self.max_area = width * height

        trained_model = cfg.trained_model
        self.thresh = thresh

        self.net = Darknet19()
        net_utils.load_net(trained_model, self.net)
        self.net.cuda()
        self.net.eval()
Exemple #8
0
def test_voc_ap(model):
    print(model)
    imdb = VOCDataset(imdb_name,
                      cfg.DATA_DIR,
                      cfg.batch_size,
                      yolo_utils.preprocess_test,
                      processes=4,
                      shuffle=False,
                      dst_size=cfg.inp_size)

    net = Darknet19()
    net_utils.load_net(model, net)

    net.cuda()
    net.eval()

    mAP = test_net(net, imdb, max_per_image, thresh, vis)

    imdb.close()
    return mAP
Exemple #9
0
def main():

    opt = parse_args()
    use_cuda, num_gpus = cuda_mode(opt)
    with open('config.yml', 'r') as f:
        config = yaml.load(f)
    class_names = config['class_names']

    data_dir = opt.data_dir
    anno_dir = os.path.join(data_dir, 'annotations_cache')
    test_file_path = os.path.join(anno_dir, '2007_test.txt')
    with open(test_file_path, 'r') as f:
        tmp_files = f.readlines()
        test_files = [item.rstrip() for item in tmp_files]

    model = Darknet19()
    model.load_trained_model(torch.load(opt.trained_model))
    if use_cuda:
        model.cuda()
    model.eval()

    init_width = 416
    init_height = 416
    test_dataset = dataset.listDataset(test_file_path,
                                       shape=(init_width, init_height),
                                       shuffle=False,
                                       transform=transforms.Compose(
                                           [transforms.ToTensor()]))
    test_bs = opt.bs
    assert test_bs > 1
    kwargs = {'num_workers': 4, 'pin_memory': True}
    test_loader = DataLoader(test_dataset,
                             batch_size=test_bs,
                             shuffle=False,
                             **kwargs)

    results = test(model, test_loader, test_files, config)
    evaluate(results, data_dir, class_names)
Exemple #10
0
    im_data = np.expand_dims(
        yolo_utils.preprocess_test((image, None, cfg.inp_size))[0], 0)
    return image, im_data, fname


# hyper-parameters
# npz_fname = 'models/yolo-voc.weights.npz'
# h5_fname = 'models/yolo-voc.weights.h5'
trained_model = cfg.trained_model
# trained_model = os.path.join(cfg.train_output_dir, 'darknet19_voc07trainval_exp3_158.h5')
thresh = 0.1
#im_path = 'demo'
dir_path = 'demo'
# ---

net = Darknet19()  #load model
net_utils.load_net(trained_model, net)  #load weights
# net.load_from_npz(npz_fname)
# net_utils.save_net(h5_fname, net)
net.cuda()  #use GPU
net.eval()
print('load model succ...')

t_det = Timer()
t_total = Timer()
# im_fnames = ['person.jpg']
for n, par in enumerate(sorted(os.listdir(dir_path))):
    print "hahahaha"
    im_path = os.path.join(dir_path, par)
    #im_fnames = sorted((fname for fname in os.listdir(im_path) if os.path.splitext(fname)[-1] == '.JPG'))#shuffle data
    im_fnames = sorted(fname for fname in os.listdir(im_path)
Exemple #11
0
def main():

    shutil.rmtree('output', ignore_errors=True)
    shutil.copytree('output_template', 'output')
    shutil.rmtree('kitti_det_output', ignore_errors=True)
    os.makedirs('kitti_det_output')

    trained_model = '/home/cory/yolo2-pytorch/models/training/kitti_new_2/kitti_new_2_100.h5'
    thresh = 0.5
    use_kitti = True
    image_dir = '/home/cory/KITTI_Dataset/data_object_image_2/training/image_2'

    net = Darknet19()
    net_utils.load_net(trained_model, net)
    net.eval()
    net.cuda()
    print('load model successfully')

    # print(net)

    def str_index(filename):
        if use_kitti:
            return filename
        begin_pos = filename.rfind('_') + 1
        end_pos = filename.rfind('.')
        str_v = filename[begin_pos:end_pos]
        return int(str_v)

    image_extensions = ['.jpg', '.JPG', '.png', '.PNG']
    img_files = open(
        '/home/cory/yolo2-pytorch/train_data/kitti/kitti_val_images.txt')
    image_abs_paths = img_files.readlines()
    image_abs_paths = [f.strip() for f in image_abs_paths]
    '''image_abs_paths = sorted([os.path.join(image_dir, name)
                              for name in os.listdir(image_dir)
                              if name[-4:] in image_extensions],
                             key=str_index)'''

    key_frame_path = ''
    detection_period = 5
    use_flow = False

    kitti_filename = 'yolo_flow_kitti_det.txt'
    try:
        os.remove(kitti_filename)
    except OSError:
        pass

    t_det = Timer()
    t_total = Timer()

    for i, image_path in enumerate(image_abs_paths):
        t_total.tic()
        image, im_data = preprocess(image_path)
        im_data = net_utils.np_to_variable(im_data,
                                           is_cuda=True,
                                           volatile=True).permute(0, 3, 1, 2)

        layer_of_flow = 'conv4'
        t_det.tic()
        bbox_pred, iou_pred, prob_pred = net.forward(im_data)

        det_time = t_det.toc()

        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        # print bbox_pred.shape, iou_pred.shape, prob_pred.shape

        bboxes, scores, cls_inds = yolo_utils.postprocess(
            bbox_pred, iou_pred, prob_pred, image.shape, cfg, thresh)
        det_obj = detection_objects(bboxes, scores, cls_inds)
        save_as_kitti_format(i, det_obj, kitti_filename, src_label='kitti')

        vis_enable = False
        if vis_enable:
            im2show = yolo_utils.draw_detection(image, bboxes, scores,
                                                cls_inds, cfg)

            cv2.imshow('detection', im2show)
            cv2.imwrite('output/detection/{:04d}.jpg'.format(i), im2show)

        total_time = t_total.toc()
        format_str = 'frame: %d, (detection: %.1f fps, %.1f ms) (total: %.1f fps, %.1f ms)'
        print(format_str % (i, 1. / det_time, det_time * 1000, 1. / total_time,
                            total_time * 1000))

        t_det.clear()
        t_total.clear()

        if vis_enable:
            key = cv2.waitKey(0)
            if key == ord('q'):
                break
Exemple #12
0
def main():

    opt = parse_args()
    use_cuda, num_gpus = cuda_mode(opt)
    with open('config.yml', 'r') as f:
        config = yaml.load(f)

    data_dir = opt.data_dir
    anno_dir = os.path.join(data_dir, 'annotations_cache')

    train_list = '2012_train.txt'
    test_list = '2007_test.txt'
    train_list_path = os.path.join(anno_dir, train_list)
    test_list_path = os.path.join(anno_dir, test_list)

    num_workers = config['train_num_workers']
    batch_size = opt.bs
    lr = opt.lr
    momentum = config['momentum']
    decay = config['decay']

    # Training parameters
    max_epochs = opt.epochs
    seed = int(time.time())
    save_interval = 10  # epochs

    save_dir = opt.out_dir
    print('Trained models will be save to', os.path.abspath(save_dir))
    if not os.path.exists(save_dir):
        os.makedirs(save_dir)

    torch.manual_seed(seed)

    pre_model = opt.pre_model
    model = Darknet19(pre_model)

    init_width = 416
    init_height = 416

    kwargs = {'num_workers': num_workers, 'pin_memory': True} \
        if use_cuda else {}

    test_loader = DataLoader(dataset.listDataset(
        test_list_path, shape=(init_width, init_height), shuffle=False,
        transform=transforms.Compose([transforms.ToTensor()]), train=False),
        batch_size=batch_size, shuffle=False, **kwargs)

    if use_cuda:
        if num_gpus > 1:
            model = torch.nn.DataParallel(model).cuda()
        else:
            model = model.cuda()

    params_dict = dict(model.named_parameters())
    params = []
    for key, value in params_dict.items():
        if key.find('.bn') >= 0 or key.find('.bias') >= 0:
            params += [{'params': [value], 'weight_decay': 0.}]
        else:
            params += [{'params': [value], 'weight_decay': decay * batch_size}]
    optimizer = optim.SGD(model.parameters(), lr=lr/batch_size,
                          momentum=momentum, dampening=0,
                          weight_decay=decay*batch_size)

    pro_bs = 0
    for epoch in range(max_epochs):
        train_loader = DataLoader(dataset.listDataset(
            train_list_path, shape=(init_width, init_height), shuffle=True,
            transform=transforms.Compose([transforms.ToTensor()]), train=True,
            seen=0, batch_size=batch_size, num_workers=num_workers),
            batch_size=batch_size, shuffle=False, **kwargs)
        pro_bs = train(model, epoch, train_loader, optimizer, opt, config,
                       pro_bs, save_dir, save_interval)
        test(model, test_loader, config)
Exemple #13
0
 def __init__(self):
     self.img_uploader = util.FTP_Uploader()
     self.face = util.Face_Detect()
     # =================================检索列表===============================
     #年龄列表'
     self.ageLists = {}
     fp_ages = open(AgeInfoDir, 'r')
     for ages_lists in fp_ages:
         age_num, age_lists = ages_lists[:-1].split(':')
         self.ageLists[age_num.encode('utf-8')] = age_lists
     fp_ages.close()
     #类别列表
     self.classLists = {}
     fp_classes = open(ClassInfoDir, 'r')
     for classes_lists in fp_classes:
         class_num, class_lists = classes_lists[:-1].split(':')
         #print class_num
         self.classLists[class_num.encode('utf-8')] = class_lists
     fp_classes.close()
     #print self.classLists
     #款式列表
     self.versionLists = {}
     fp_versions = open(VerInfoDir, 'r')
     for versions_lists in fp_versions:
         print versions_lists
         version_num, version_lists = versions_lists[:-1].split(':')
         self.versionLists[version_num.encode('utf-8')] = version_lists
     fp_versions.close()
     # 风格列表
     self.styleLists = {}
     fp_styles = open(StyInfoDir, 'r')
     for styles_lists in fp_styles:
         style_num, style_lists = styles_lists[:-1].split(':')
         self.styleLists[style_num.encode('utf-8')] = style_lists
     fp_styles.close()
     # 价钱列表
     self.princeLists = {}
     fp_princes = open(PrinceInfoDir, 'r')
     for princes_lists in fp_princes:
         prince_num, prince_value = princes_lists[:-1].split('\t')
         self.princeLists[prince_num.encode('utf-8')] = prince_value
     fp_princes.close()
     #print self.princeLists
     self.bboxes = []
     self.scores = []
     self.cls_inds = []
     self.cls_res = []
     self.cls_score = []
     self.cls_res_top5 = []
     self.cls_score_top5 = []
     #Init for resnet
     with open(ClassPath, 'r') as file:
         num_classes = []
         for eachline in file:
             numclass = eachline.split('\t')[-1][:-1]
             num_classes.append(numclass)
     self.num_classes = num_classes
     model_res = getmodel(restore=1)
     torch.nn.DataParallel(model_res).cuda()
     self.model_res = model_res
     self.trained_model = cfg.trained_model
     #Init for yolov2
     # trained_model = os.path.join(cfg.train_output_dir, 'darknet19_voc07trainval_exp3_158.h5')
     self.thresh = 0.5
     self.im_path = 'demo'
     # ---
     net = Darknet19()  # load model
     net_utils.load_net(self.trained_model, net)  # load weights
     # net.load_from_npz(npz_fname)
     # net_utils.save_net(h5_fname, net)
     net.cuda()  # use GPU
     net.eval()
     self.net = net
     print('load model succ...')
Exemple #14
0
def main():

    shutil.rmtree('output', ignore_errors=True)
    shutil.copytree('output_template', 'output')

    # trained_model = cfg.trained_model
    # trained_model = '/home/cory/yolo2-pytorch/models/training/kitti_new_2/kitti_new_2_60.h5'
    trained_model = '/home/cory/yolo2-pytorch/models/training/voc0712_obj_scale/voc0712_obj_scale_1.h5'
    # trained_model = '/home/cory/yolo2-pytorch/models/training/kitti_det_new_2/kitti_det_new_2_40.h5'
    # trained_model = '/home/cory/yolo2-pytorch/models/training/kitti_det_new_2/kitti_det_new_2_10.h5'
    thresh = 0.5
    use_kitti = True
    image_dir = '/home/cory/KITTI_Dataset/data_tracking_image_2/training/image_02/0013'
    # car = 1 5
    # pedestrian = 13 17

    net = Darknet19()
    net_utils.load_net(trained_model, net)
    net.eval()
    net.cuda()
    print('load model successfully')
    # print(net)

    def str_index(filename):
        if use_kitti:
            return filename
        begin_pos = filename.rfind('_') + 1
        end_pos = filename.rfind('.')
        str_v = filename[begin_pos: end_pos]
        return int(str_v)

    image_extensions = ['.jpg', '.JPG', '.png', '.PNG']
    image_abs_paths = sorted([os.path.join(image_dir, name)
                              for name in os.listdir(image_dir)
                              if name[-4:] in image_extensions],
                             key=str_index)

    key_frame_path = ''
    detection_period = 5
    use_flow = False

    kitti_filename = 'yolo_flow_kitti_det.txt'
    try:
        os.remove(kitti_filename)
    except OSError:
        pass

    t_det = Timer()
    t_total = Timer()

    for i, image_path in enumerate(image_abs_paths):
        t_total.tic()
        image, im_data = preprocess(image_path)
        im_data = net_utils.np_to_variable(im_data, is_cuda=True, volatile=True).permute(0, 3, 1, 2)

        layer_of_flow = 'conv4'
        # key frame
        if i % detection_period == 0 and use_flow:
            key_frame_path = image_path
            # conv5 feature map
            feature = net.get_feature_map(im_data=im_data, layer=layer_of_flow)
            feature = feature.data.cpu().numpy()
            feature_map_all = plot_feature_map(feature, resize_ratio=1)
            # cv2.imshow('feature_map', feature_map_all)
            cv2.imwrite('output/feature_map/{:04d}.jpg'.format(i), feature_map_all * 255)

        t_det.tic()
        if use_flow:
            t1 = time.time()
            conv5_shifted_gpu = detect_by_flow(i, feature, image, image_path, key_frame_path)
            t2 = time.time()
            print('detect_by_flow', t2 - t1)
            bbox_pred, iou_pred, prob_pred = net.feed_feature(Variable(conv5_shifted_gpu), layer=layer_of_flow)

        else:
            bbox_pred, iou_pred, prob_pred = net.forward(im_data)

        det_time = t_det.toc()

        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        # print bbox_pred.shape, iou_pred.shape, prob_pred.shape

        bboxes, scores, cls_inds = yolo_utils.postprocess(bbox_pred, iou_pred, prob_pred, image.shape, cfg, thresh)
        det_obj = detection_objects(bboxes, scores, cls_inds)
        save_as_kitti_format(i, det_obj, kitti_filename, src_label='kitti')

        im2show = yolo_utils.draw_detection(image, bboxes, scores, cls_inds, cfg)

        cv2.imshow('detection', im2show)
        cv2.imwrite('output/detection/{:04d}.jpg'.format(i), im2show)

        total_time = t_total.toc()
        format_str = 'frame: %d, (detection: %.1f fps, %.1f ms) (total: %.1f fps, %.1f ms)'
        print(format_str % (
            i, 1. / det_time, det_time * 1000, 1. / total_time, total_time * 1000))

        t_det.clear()
        t_total.clear()

        key = cv2.waitKey(1)
        if key == ord('q'):
            break
    # Use LMDB custom dataset or VOC-style
    if cfg.lmdb:
        dataset = dset(cfg.target_file, cfg.root_dir, cfg.multi_scale_inp_size)  # , cfg.transforms)
        dataloader = torch.utils.data.DataLoader(dataset, batch_size=args.batch, shuffle=True, num_workers=args.workers)

    else:

        image_data = torchvision.datasets.ImageFolder(args.path)
        data_loader = torch.utils.data.DataLoader(image_data, batch_size=args.batch, shuffle=True, num_workers=args.workers, multiscale=cfg.multi_scale_inp_size)

    # replace 4 with the number of classes in your custom dataset
    classes = 20 if args.transfer else cfg.num_classes

    # create the network
    net = Darknet(classes)

    # Load weights

    # Loads pretrained yolo VOC weights
    if args.transfer:
        net.load_from_npz(cfg.pretrained_model, num_conv=18)
        exp_name = str(int(time.time()))  # For tensorboard consistency on reloads
        start_epoch = 0
        j = 0
        lr = cfg.init_learning_rate

    # Loads from a latest saved checkpoint in case training takes place over multiple days.
    else:
        path_t = cfg.trained_model()
        if os.path.exists(path_t):
Exemple #16
0
                                          im2show.shape[0]), 1000))  # noqa
            cv2.imshow('test', im2show)
            cv2.waitKey(0)

    with open(det_file, 'wb') as f:
        pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)

    print('Evaluating detections')
    imdb.evaluate_detections(all_boxes, output_dir)


if __name__ == '__main__':
    # data loader
    imdb = VOCDataset(imdb_name,
                      cfg.DATA_DIR,
                      cfg.batch_size,
                      yolo_utils.preprocess_test,
                      processes=1,
                      shuffle=False,
                      dst_size=cfg.multi_scale_inp_size)

    net = Darknet19(training=False)
    net_utils.load_net(trained_model, net)

    net.cuda()
    net.eval()

    test_net(net, imdb, max_per_image, thresh, vis)

    imdb.close()
Exemple #17
0
def main():
    root_dir = '/home/cory/project/yolo2-pytorch'
    output_dir = root_dir + '/output'
    output_template_dir = root_dir + '/output_template'
    kitti_filename = root_dir + '/yolo_flow_kitti_det.txt'

    shutil.rmtree(output_dir, ignore_errors=True)
    shutil.copytree(output_template_dir, output_dir)

    # trained_model = cfg.trained_model
    # trained_model = '/home/cory/yolo2-pytorch/models/training/kitti_new_2/kitti_new_2_100.h5'
    # trained_model = '/home/cory/project/yolo2-pytorch/models/training/kitti_baseline_v3/kitti_baseline_v3_80.h5'
    trained_model = '/home/cory/project/yolo2-pytorch/models/training/kitti_new_2_flow_center_ft/kitti_new_2_flow_center_ft_50.h5'
    # trained_model = '/home/cory/yolo2-pytorch/models/training/kitti_new_2_flow_ft/kitti_new_2_flow_ft_2.h5'
    # trained_model = '/home/cory/yolo2-pytorch/models/training/voc0712_obj_scale/voc0712_obj_scale_1.h5'
    # trained_model = '/home/cory/yolo2-pytorch/models/training/kitti_det_new_2/kitti_det_new_2_40.h5'
    # trained_model = '/home/cory/yolo2-pytorch/models/training/kitti_det_new_2/kitti_det_new_2_10.h5'
    thresh = 0.5

    # car = 1 5
    # pedestrian = 13 17

    net = Darknet19()
    net_utils.load_net(trained_model, net)
    net.eval()
    net.cuda()
    print('load model successfully')
    # print(net)

    img_files = open(
        '/home/cory/project/yolo2-pytorch/train_data/kitti/kitti_val_images.txt'
    )
    # img_files = open('/home/cory/project/yolo2-pytorch/train_data/kitti/0001_images.txt')
    # img_files = open('/home/cory/yolo2-pytorch/train_data/ImageNetVID_test.txt')
    # img_files = open('/home/cory/yolo2-pytorch/train_data/vid04_images.txt')
    image_abs_paths = img_files.readlines()
    image_abs_paths = [f.strip() for f in image_abs_paths]
    image_abs_paths = image_abs_paths[500:]

    key_frame_path = ''
    detection_period = 1
    use_flow = False
    layer_of_flow = 'conv4'

    try:
        os.remove(kitti_filename)
    except OSError:
        pass

    t_det = Timer()
    t_total = Timer()

    for i, image_path in enumerate(image_abs_paths):
        t_total.tic()
        t0 = time.time()
        image, im_data = preprocess(image_path)
        im_data = net_utils.np_to_variable(im_data,
                                           is_cuda=True,
                                           volatile=True).permute(0, 3, 1, 2)
        t1 = time.time()
        print('t1', t1 - t0)

        # key frame
        if use_flow and i % detection_period == 0:
            key_frame_path = image_path
            # conv5 feature map
            feature = net.get_feature_map(im_data=im_data, layer=layer_of_flow)
            feature = feature.data.cpu().numpy()
            feature_map_all = plot_feature_map(feature, resize_ratio=1)
            # cv2.imshow('feature_map', feature_map_all)
            cv2.imwrite(output_dir + '/feature_map/{:04d}.jpg'.format(i),
                        feature_map_all * 255)

        t_det.tic()
        if use_flow:
            conv5_shifted_gpu = detect_by_flow(i, feature, image, image_path,
                                               key_frame_path, output_dir)
            bbox_pred, iou_pred, prob_pred = net.feed_feature(
                Variable(conv5_shifted_gpu), layer=layer_of_flow)

        else:
            bbox_pred, iou_pred, prob_pred = net.forward(im_data)

        det_time = t_det.toc()
        t2 = time.time()
        print('t2', t2 - t1)

        # to numpy
        bbox_pred = bbox_pred.data.cpu().numpy()
        iou_pred = iou_pred.data.cpu().numpy()
        prob_pred = prob_pred.data.cpu().numpy()

        t3 = time.time()
        print('t3', t3 - t2)

        # print bbox_pred.shape, iou_pred.shape, prob_pred.shape

        bboxes, scores, cls_inds = yolo_utils.postprocess(
            bbox_pred, iou_pred, prob_pred, image.shape, cfg, thresh)

        t4 = time.time()
        print('t4', t4 - t3)

        det_obj = detection_objects(bboxes, scores, cls_inds)
        save_as_kitti_format(i, det_obj, kitti_filename, src_label='kitti')
        im2show = yolo_utils.draw_detection(image, bboxes, scores, cls_inds,
                                            cfg)

        cv2.imshow('detection', im2show)
        cv2.imwrite(output_dir + '/detection/{:04d}.jpg'.format(i), im2show)

        total_time = t_total.toc()
        format_str = 'frame: %d, (detection: %.1f fps, %.1f ms) (total: %.1f fps, %.1f ms)'
        print(format_str % (i, 1. / det_time, det_time * 1000, 1. / total_time,
                            total_time * 1000))

        t5 = time.time()
        print('t5', t5 - t4)

        t_det.clear()
        t_total.clear()

        key = cv2.waitKey(1)
        if key == ord('q'):
            break
Exemple #18
0
    im_data = np.expand_dims(
        yolo_utils.preprocess_test((image, None, cfg.inp_size))[0], 0)
    return image, im_data


# hyper-parameters
# npz_fname = 'models/yolo-voc.weights.npz'
# h5_fname = 'models/yolo-voc.weights.h5'
trained_model = cfg.trained_model
# trained_model = os.path.join(
#     cfg.train_output_dir, 'darknet19_voc07trainval_exp3_158.h5')
thresh = 0.5
im_path = 'demo'
# ---

net = Darknet19()
net_utils.load_net(trained_model, net)
# net.load_from_npz(npz_fname)
# net_utils.save_net(h5_fname, net)
net.cuda()
net.eval()
print('load model succ...')

t_det = Timer()
t_total = Timer()
im_fnames = sorted((fname
                    for fname in os.listdir(im_path)
                    if os.path.splitext(fname)[-1] == '.jpg'))
im_fnames = (os.path.join(im_path, fname) for fname in im_fnames)
pool = Pool(processes=1)
Exemple #19
0
    def extractObjects(self, video_path):
        import os
        import cv2
        import torch
        import numpy as np
        from torch.multiprocessing import Pool

        from darknet import Darknet19
        import utils.yolo as yolo_utils
        import utils.network as net_utils
        from utils.timer import Timer
        import cfgs.config as cfg

        def preprocess(fname):
            # return fname
            image = cv2.imread(fname)
            im_data = np.expand_dims(
                yolo_utils.preprocess_test((image, None, cfg.inp_size))[0], 0)
            return image, im_data

        # hyper-parameters
        # npz_fname = 'models/yolo-voc.weights.npz'
        # h5_fname = 'models/yolo-voc.weights.h5'
        trained_model = cfg.trained_model
        # trained_model = os.path.join(cfg.train_output_dir, 'darknet19_voc07trainval_exp3_158.h5')
        thresh = 0.5
        im_path = video_path
        # ---

        net = Darknet19()
        net_utils.load_net(trained_model, net)
        # net.load_from_npz(npz_fname)
        # net_utils.save_net(h5_fname, net)
        net.cuda()
        net.eval()
        print('load model succ...')

        t_det = Timer()
        t_total = Timer()
        # im_fnames = ['person.jpg']
        im_fnames = sorted([
            fname for fname in sorted(os.listdir(im_path))
            if os.path.splitext(fname)[-1] == '.jpg'
        ])
        im_fnames = (os.path.join(im_path, fname) for fname in im_fnames)
        objectDetect = []
        for i, (image) in enumerate(im_fnames):
            t_total.tic()
            im_data = preprocess(image)
            image = im_data[0]
            im_data = im_data[1]
            im_data = net_utils.np_to_variable(im_data,
                                               is_cuda=True,
                                               volatile=True).permute(
                                                   0, 3, 1, 2)
            t_det.tic()
            bbox_pred, iou_pred, prob_pred = net(im_data)
            det_time = t_det.toc()
            # to numpy
            bbox_pred = bbox_pred.data.cpu().numpy()
            iou_pred = iou_pred.data.cpu().numpy()
            prob_pred = prob_pred.data.cpu().numpy()

            # print bbox_pred.shape, iou_pred.shape, prob_pred.shape
            bboxes, scores, cls_inds = yolo_utils.postprocess(
                bbox_pred, iou_pred, prob_pred, image.shape, cfg, thresh)
            objectDetect.append(','.join(
                set([cfg.label_names[i] for i in cls_inds])))
        return objectDetect