Пример #1
0
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
Пример #2
0
def test():
    im_file = 'data/demo/004545.jpg'
    # im_file = 'data/VOCdevkit2007/VOC2007/JPEGImages/009036.jpg'
    # im_file = '/media/longc/Data/data/2DMOT2015/test/ETH-Crossing/img1/000100.jpg'
    image = cv2.imread(im_file)

    model_file = 'data/imagenet_models/VGGnet_fast_rcnn_iter_70000.h5'
    # model_file = '/media/longc/Data/models/faster_rcnn_pytorch3/faster_rcnn_100000.h5'
    # model_file = '/media/longc/Data/models/faster_rcnn_pytorch2/faster_rcnn_2000.h5'
    detector = FasterRCNN()
    network.load_net(model_file, detector)
    detector.cuda()
    detector.eval()
    print('load model successfully!')

    # network.save_net(r'/media/longc/Data/models/VGGnet_fast_rcnn_iter_70000.h5', detector)
    # print('save model succ')

    t = Timer()
    t.tic()
    # image = np.zeros(shape=[600, 800, 3], dtype=np.uint8) + 255
    dets, scores, classes = detector.detect(image, 0.7)
    runtime = t.toc()
    print('total spend: {}s'.format(runtime))

    im2show = np.copy(image)
    for i, det in enumerate(dets):
        det = tuple(int(x) for x in det)
        cv2.rectangle(im2show, det[0:2], det[2:4], (255, 205, 51), 2)
        cv2.putText(im2show, '%s: %.3f' % (classes[i], scores[i]), (det[0], det[1] + 15), cv2.FONT_HERSHEY_PLAIN,
                    1.0, (0, 0, 255), thickness=1)
    cv2.imwrite(os.path.join('demo', 'out.jpg'), im2show)
    cv2.imshow('demo', im2show)
    cv2.waitKey(0)
Пример #3
0
 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')
Пример #4
0
def init_network():
    dataset_yaml = os.path.join(base_dir, 'cfgs/config_kitti_demo.yaml')
    cfg = load_cfg_yamls([dataset_yaml])

    model = os.path.join(
        base_dir, 'models/training/kitti_baseline_v3/kitti_baseline_v3_100.h5')
    net = Darknet19(cfg)
    net_utils.load_net(model, net)
    net.eval()
    net.cuda()
    print('load model successfully')
    return net, cfg
Пример #5
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()
Пример #6
0
def voc_ap(model, cfg):
    imdb_name = 'voc_2007_test'
    output_dir = 'models/testing/' + cfg['exp_name']
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    imdb = VOCDataset(imdb_name, '../data', cfg['batch_size'],
                      yolo_utils.preprocess_test, processes=4, shuffle=False, dst_size=cfg['inp_size'])

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

    net.cuda()
    net.eval()

    mAP = eval_net(net, cfg, imdb, max_per_image=300, thresh=0.001, output_dir=output_dir, vis=False)

    imdb.close()
    return mAP
Пример #7
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
Пример #8
0
        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)

for i, (image, im_data) in enumerate(pool.imap(
Пример #9
0
print('imdb load data succeeded')
net = Darknet19()

# CUDA_VISIBLE_DEVICES=1
os.environ['CUDA_VISIBLE_DEVICES'] = '1'

os.makedirs(cfg.train_output_dir, exist_ok=True)
try:
    ckp = open(cfg.check_point_file)
    ckp_epoch = int(ckp.readlines()[0])
    use_model = os.path.join(cfg.train_output_dir, cfg.exp_name + '_' + str(ckp_epoch) + '.h5')
except IOError:
    ckp_epoch = 0
    use_model = cfg.pretrained_model

net_utils.load_net(use_model, net)

net.cuda()
net.train()
print('load net succeeded')

start_epoch = ckp_epoch
imdb.epoch = start_epoch

optimizer = get_optimizer(cfg, net, start_epoch)

# show training parameters
print('-------------------------------')
print('gpu_id', os.environ.get('CUDA_VISIBLE_DEVICES'))
print('use_model', use_model)
print('exp_name', cfg.exp_name)
Пример #10
0
        origin_time = max_ctime
        print('start_write_and_test')
        print('newfile_time', max_ctime)
        print('file', file_dict[max_ctime])

        test_f = open('test_ap_newmodel.txt', mode='a+')
        test_f.writelines(['MODELS', '\n', file_dict[max_ctime], '\n'])
        test_f.close()

        new_mdoels_path = os.path.join(file_dir, file_dict[max_ctime])
        imdb_map_newf = 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()
        net_utils.load_net(new_mdoels_path, net)
        net.cuda()
        net.eval()

        test_net(net, imdb_map_newf, max_per_image, thresh, vis=False)
        print('test_this_models_done.....')

        imdb_map_newf.close()
        torch.cuda.empty_cache()
        gc.collect()
    time.sleep(1200)
#    print(file_dict[max_ctime]) #打印出最新文件名
#    print(max_ctime)
    # 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):
            j, exp_name, start_epoch, lr = net_utils.load_net(path_t, net)
            j, exp_name, start_epoch, lr = int(j), str(int(exp_name)), int(start_epoch), float(lr)
            print('lr is {} and its type is {}'.format(lr, type(lr)))
        else:
            e = 'no checkpoint to load from\n'
            sys.exit(e)

    # To keep the tensorflow logs consistent in case training takes multiple days with a lot of start and stops
    path = os.path.join(cfg.TRAIN_DIR, 'runs', str(exp_name))
    if not os.path.exists(path):
        os.makedirs(path)

    # Transfer learning
    # Freeze all the parameters
    for params in net.parameters():
        params.requires_grad = False
Пример #12
0
def main():

    output_dir = '../output'
    output_template_dir = '../output_template'
    kitti_output_dir = '../kitti_det_output'
    input_file_list = '/home/cory/project/yolo2-pytorch/train_data/kitti/kitti_val_images.txt'
    # input_file_list = '/home/cory/project/yolo2-pytorch/flow/w01_imgs.txt'
    vis_enable = False
    thresh = 0.5

    trained_model = '/home/cory/project/yolo2-pytorch/models/training/kitti_new_2_flow_center_ft_flownet2_joint/' \
                    'kitti_new_2_flow_center_ft_flownet2_joint_30.h5'

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

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

    img_files = open(input_file_list)
    image_abs_paths = img_files.readlines()
    image_abs_paths = [f.strip() for f in image_abs_paths]

    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()

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

        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_output_dir, src_label='kitti')

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

        t_det.clear()
        t_total.clear()

        if vis_enable:
            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)
            key = cv2.waitKey(0)
            if key == ord('q'):
                break
Пример #13
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
Пример #14
0
        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)
                       if os.path.splitext(fname)[-1] == '.JPG'
Пример #15
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
Пример #16
0
                                                cfg,
                                                thr=0.1)
            if im2show.shape[0] > 1100:
                im2show = cv2.resize(im2show,
                                     (int(1000. * float(im2show.shape[1]) / 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()
    net_utils.load_net(trained_model, net)

    net.cuda()
    net.eval()

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

    imdb.close()
Пример #17
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
Пример #18
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
Пример #19
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...')
Пример #20
0
                  yolo_utils.preprocess_train,
                  processes=2,
                  shuffle=True,
                  dst_size=cfg.multi_scale_inp_size)
# dst_size=cfg.inp_size)
print('load data succ...')

net = Darknet19()
# net_utils.load_net(cfg.trained_model, net)
#pretrained_model = os.path.join(cfg.train_output_dir,
#                  'darknet19_voc07trainval_exp3_79.h5')
pretrained_model = 'yolo-voc.weights.h5'

# training from scratch: with_patch=False
# continue training or testing: with_patch=True
net_utils.load_net(pretrained_model, net, with_patch=False)
#net.load_from_npz(cfg.pretrained_model, num_conv=18)
net.cuda()
net.train()
print('load net succ...')

# optimizer
start_epoch = 0
lr = 1e-2
optimizer = torch.optim.SGD([net.patch],
                            lr=lr,
                            momentum=cfg.momentum,
                            weight_decay=cfg.weight_decay)

# tensorboad
use_tensorboard = cfg.use_tensorboard and SummaryWriter is not None