Example #1
0
def single_vid_eval(input_list):
    vid_file, det_file, image_set = input_list
    vid_proto = proto_load(vid_file)
    det_proto = proto_load(det_file)
    vid_name = vid_proto['video']
    assert vid_name == det_proto['video']

    # dets are [frame_idx, class_index, cls_score, x1, y1, x2, y2]
    dets = []
    for det in det_proto['detections']:
        local_idx = det['frame']
        image_name = image_name_at_frame(vid_proto, local_idx)
        frame_idx = image_set[image_name]
        bbox = map(lambda x: max(x, 0), det['bbox'])
        for cls_score in det['scores']:
            dets.append([
                int(frame_idx), cls_score['class_index'], cls_score['score'],
                bbox
            ])

    if len(dets) == 0:
        print "det_proto {} has no detections.".format(det_file)
        return dets

    nms_boxes = [[
        det[0],
    ] + det[-1] + [
        det[2],
    ] for det in dets]
    keep = vid_nms(np.asarray(nms_boxes).astype('float32'), args.nms_thres)

    kept_dets = [dets[i] for i in keep]
    kept_dets.sort(key=lambda x: (x[0], x[1], -x[2]))
    return kept_dets
def main(args):
    vid_proto = proto_load(args.vid_file)
    frame_to_det = load_frame_to_det(vid_proto, args.det_dir)

    if not os.path.isdir(args.save_dir):
        try:
            os.makedirs(args.save_dir)
        except:
            pass

    track_files = glob(osp.join(args.track_dir, '*.track*'))
    if len(track_files) == 0:
        print "Warning: {} has no tracks".format(args.track_dir)
        return

    for track_file in track_files:
        track_proto = proto_load(track_file)

        vid_name = vid_proto['video']
        assert vid_name == track_proto['video']
        cls_name = osp.basename(track_file).split('.')[1]
        cls_index = imagenet_vdet_class_idx[cls_name]

        # spatial max pooling
        score_proto = raw_dets_spatial_max_pooling(
            vid_proto,
            track_proto,
            frame_to_det,
            cls_index,
            overlap_thres=args.overlap_thres)

        # temporal max pooling
        temporal_max_score_proto = score_proto_temporal_maxpool(
            score_proto, args.window)

        # interpolation
        interpolated_score_proto = score_proto_interpolation(
            temporal_max_score_proto, vid_proto)

        # save score proto
        save_file = osp.join(args.save_dir,
                             '{}.{}.score'.format(vid_name, cls_name))
        if osp.isfile(save_file):
            print "{} already exists.".format(save_file)
            continue
        proto_dump(interpolated_score_proto, save_file)
        print "{} created.".format(save_file)
Example #3
0
def single_vid_raw_det_eval(input_list,
                            thresh=0.00,
                            max_per_image=100,
                            nms_thres=0.3,
                            num_classes=31):
    vid_file, det_folder, image_set = input_list
    vid_proto = proto_load(vid_file)
    print vid_proto['video']

    det_strings = []
    for frame in vid_proto['frames']:
        frame_name = os.path.splitext(frame['path'])[0]
        det_file = os.path.join(det_folder, "{}.mat".format(frame_name))
        local_idx = frame['frame']
        image_name = image_name_at_frame(vid_proto, local_idx)
        frame_idx = image_set[image_name]
        try:
            det = sio.loadmat(det_file)
        except Exception as error:
            print "Error {}: det_file {}.".format(error, det_file)
            continue
        boxes = det['boxes']
        scores = det['zs']
        cur_boxes = [[] for _ in xrange(num_classes)]
        for j in xrange(1, num_classes):
            inds = np.where(scores[:, j] > thresh)[0]
            if len(inds) == 0: continue
            cls_scores = scores[inds, j]
            cls_boxes = boxes[inds, j, :]
            cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \
                .astype(np.float32, copy=False)
            keep = nms(cls_dets, nms_thres)
            cls_dets = cls_dets[keep, :]
            cur_boxes[j] = cls_dets

        # Limit to max_per_image detections *over all classes*
        if max_per_image > 0:
            image_scores = np.hstack([cur_boxes[j][:, -1] \
                for j in xrange(1, num_classes)])
            if len(image_scores) > max_per_image:
                image_thresh = np.sort(image_scores)[-max_per_image]
                for j in xrange(1, num_classes):
                    keep = np.where(cur_boxes[j][:, -1] >= image_thresh)[0]
                    cur_boxes[j] = cur_boxes[j][keep, :]

        for class_index, cls_dets in enumerate(cur_boxes):
            if class_index == 0: continue
            for dets in cls_dets:
                det_strings.append(
                    '{} {} {:.06f} {:.2f} {:.2f} {:.2f} {:.2f}\n'.format(
                        frame_idx, class_index, dets[-1], dets[0], dets[1],
                        dets[2], dets[3]))

    return det_strings
def main(args):
    vid_proto = proto_load(args.vid_file)
    frame_to_det = load_frame_to_det(vid_proto, args.det_dir)

    if not os.path.isdir(args.save_dir):
        try:
            os.makedirs(args.save_dir)
        except:
            pass

    track_files = glob(osp.join(args.track_dir, '*.track*'))
    if len(track_files) == 0:
        print "Warning: {} has no tracks".format(args.track_dir)
        return

    for track_file in track_files:
        track_proto = proto_load(track_file)

        vid_name = vid_proto['video']
        assert vid_name == track_proto['video']
        cls_name = osp.basename(track_file).split('.')[1]
        cls_index = imagenet_vdet_class_idx[cls_name]

        # spatial max pooling
        score_proto = raw_dets_spatial_max_pooling(vid_proto, track_proto,
            frame_to_det, cls_index, overlap_thres=args.overlap_thres)

        # temporal max pooling
        temporal_max_score_proto = score_proto_temporal_maxpool(score_proto, args.window)

        # interpolation
        interpolated_score_proto = score_proto_interpolation(temporal_max_score_proto, vid_proto)

        # save score proto
        save_file = osp.join(args.save_dir,
                             '{}.{}.score'.format(vid_name, cls_name))
        if osp.isfile(save_file):
            print "{} already exists.".format(save_file)
            continue
        proto_dump(interpolated_score_proto, save_file)
        print "{} created.".format(save_file)
Example #5
0
def load_data(config, debug=False):
    if config['blacklist']:
        with open(config['blacklist']) as f:
            blacklist = [line.strip() for line in f]
    else:
        blacklist = []
    # check folder exists
    for folder in ['vid_dir', 'box_dir', 'annot_dir']:
        try:
            assert os.path.isdir(config[folder])
        except:
            raise ValueError("{} folder does not exist: {}".format(folder, config[folder]))

    # preprocess data
    with open(config['vid_list']) as f:
        vid_names = [line.strip().split('/')[-1] for line in f]
    vid_names = [vid_name for vid_name in vid_names if \
        vid_valid(vid_name, config['vid_dir'], config['box_dir'], config['annot_dir'],
                  blacklist)]
    random.shuffle(vid_names)

    if debug:
        vid_names = vid_names[:500]

    print "Loading data..."
    tot_data = {}
    for idx, vid_name in enumerate(vid_names, start=1):
        tot_data[vid_name] = {}
        for key in ['vid', 'box', 'annot']:
            tot_data[vid_name][key] = proto_load(
                osp.join(config[key + '_dir'], "{}.{}".format(vid_name, key)))
        if idx % 500 == 0:
            print "{} samples processed.".format(idx)
    if idx % 500 != 0:
        print "{} samples processed.".format(idx)
    return tot_data, vid_names
     raise ValueError('image name has {} components: {}'.format(
         len(parts), image_name))
 # start a new video
 if cur_vid_name != video_name:
     if cur_vid_name is not None:
         print "Saving {}...".format(cur_vid_name)
         save_if_not_exist(
             box_proto,
             os.path.join(args.save_root, cur_vid_name + '.box'))
     print "Processsing {}...".format(video_name)
     box_proto = {}
     box_proto['video'] = video_name
     box_proto['boxes'] = []
     cur_vid_name = video_name
     # read vid_proto
     vid_proto = proto_load(
         os.path.join(args.vid_root, cur_vid_name + '.vid'))
     if args.annot_root:
         annot_proto = proto_load(
             os.path.join(args.annot_root, cur_vid_name + '.annot'))
 # process boxes
 frame_idx = path_to_index(vid_proto, frame_name)
 if args.annot_root:
     annot_boxes = annot_boxes_at_frame(annot_proto, frame_idx)
 for box in boxes:
     bbox = box[0:4].tolist()
     if args.annot_root:
         # with GT
         if len(annot_boxes) == 0:
             overlaps = 0.
         else:
             overlaps = iou([bbox], annot_boxes)
Example #7
0
        default=None,
        help=
        'Annotation file if available to calculate gt overlaps for training.')
    parser.add_argument('--cls')
    parser.add_argument('--job', type=int, default=1)
    parser.add_argument('--sampling_num', type=int)
    parser.add_argument('--sampling_ratio', type=float)
    parser.add_argument('--save_feat', dest='save_feat', action='store_true')
    parser.set_defaults(save_feat=False)
    parser.add_argument('--save_all_sc',
                        dest='save_all_sc',
                        action='store_true')
    parser.set_defaults(save_all_sc=False)
    args = parser.parse_args()

    vid_proto = proto_load(args.vid_file)
    if args.annot_file is not None:
        annot_proto = proto_load(args.annot_file)
    else:
        annot_proto = None
    if os.path.isfile(args.save_file):
        print "{} already exists.".format(args.save_file)
        sys.exit(0)

    # classify ground truth tracks if no track_file provided
    if args.track_file == 'None':
        gt_classes = [annot['track'][0]['class'] \
            for annot in annot_proto['annotations']]
        if args.cls not in gt_classes:
            print "{} not in gt file {}".format(args.cls, args.annot_file)
            sys.exit(0)
    parser.add_argument("param_file")
    parser.add_argument("rcnn_model")
    parser.add_argument("save_file")
    parser.add_argument("--cls")
    parser.add_argument("--job", type=int)
    parser.add_argument(
        "--annot_file", default=None, help="Annotation file if available to calculate gt overlaps for training."
    )
    parser.add_argument("--overlap_thres", type=float, required=True)
    parser.add_argument("--save_feat", dest="save_feat", action="store_true")
    parser.set_defaults(save_feat=False)
    parser.add_argument("--save_all_sc", dest="save_all_sc", action="store_true")
    parser.set_defaults(save_all_sc=False)
    args = parser.parse_args()

    vid_proto = proto_load(args.vid_file)
    if args.annot_file is None:
        annot_proto = None
    else:
        annot_proto = proto_load(args.annot_file)
    det_proto = proto_load(args.det_file)
    if os.path.isfile(args.save_file):
        print "{} already exists.".format(args.save_file)
        sys.exit(0)

    # classify ground truth tracks if no track_file provided
    if args.track_file == "None":
        gt_classes = [annot["track"][0]["class"] for annot in annot_proto["annotations"]]
        if args.cls not in gt_classes:
            print "{} not in gt file {}".format(args.cls, args.annot_file)
            sys.exit(0)
    vid_name = vid_proto['video']
    for frame in vid_proto['frames']:
        if frame['frame'] == frame_idx:
            return os.path.join(vid_name, os.path.splitext(frame['path'])[0])


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('vid_file')
    parser.add_argument('score_file')
    parser.add_argument('det_file')
    parser.add_argument('image_set_file')
    parser.add_argument('--score_var')
    args = parser.parse_args()

    vid_proto = proto_load(args.vid_file)
    score_proto = proto_load(args.score_file)
    det_proto = proto_load(args.det_file)
    with open(args.image_set_file) as f:
        image_set = dict([line.strip().split() for line in f.readlines()])
    vid_name = vid_proto['video']
    assert vid_name == score_proto['video']
    assert vid_name == det_proto['video']
    assert len(score_proto['tubelets']) > 0

    dets = []
    class_index = score_proto['tubelets'][0]['class_index']

    # track detections
    for tubelet in score_proto['tubelets']:
        if tubelet['gt'] == 1:
sys.path.insert(1, '.')
from vdetlib.utils.protocol import proto_load, frame_path_at
from vdetlib.utils.cython_nms import vid_nms


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('vid_file')
    parser.add_argument('score_file')
    parser.add_argument('image_set_file')
    parser.add_argument('--varname')
    args = parser.parse_args()

    global_thres = -2.5

    vid_proto = proto_load(args.vid_file)
    score_proto = proto_load(args.score_file)
    with open(args.image_set_file) as f:
        image_set = dict([line.strip().split() for line in f.readlines()])
    vid_name = vid_proto['video']
    assert vid_name == score_proto['video']

    # build dict
    frame_to_image_name = {}
    for frame in vid_proto['frames']:
        frame_id = frame['frame']
        frame_to_image_name[frame_id] = os.path.join(
            vid_name, os.path.splitext(frame['path'])[0])

    # get image shape
    height, width = imread(frame_path_at(vid_proto, 1)).shape[:2]
Example #11
0
    args = parser.parse_args()

    # read image_list
    with open(args.image_list, 'r') as f:
        image_list = dict([line.strip().split() for line in f])

    num_classes = args.num_classes
    all_boxes = [[[] for _ in xrange(len(image_list))]
                 for _ in xrange(num_classes)]

    # process vid detections
    vids = sorted(glob.glob(osp.join(args.track_dir, '*')))
    for vid_path in vids:
        print vid_path
        vid_name = osp.split(vid_path)[-1].split('.')[0]
        vid_proto = proto_load(osp.join(args.vid_dir, vid_name + '.vid'))
        tracks = tpn_test_iterator(vid_path)
        for frame in vid_proto['frames']:
            frame_name = osp.join(vid_name, osp.splitext(frame['path'])[0])
            if frame_name not in image_list.keys(): continue

            frame_idx = frame['frame']
            global_idx = int(image_list[frame_name]) - 1
            start_time = time()
            scores, boxes = _frame_dets(tracks, frame_idx, args.score_key,
                                        args.box_key)
            boxes = boxes.reshape((boxes.shape[0], -1))

            for j in xrange(1, num_classes):
                inds = np.where(scores[:, j] > args.thres)[0]
                cls_scores = scores[inds, j]
import os.path as osp
import os
import argparse
this_dir = osp.dirname(__file__)
sys.path.insert(0, osp.join(this_dir, '../../external/'))
from vdetlib.utils.protocol import proto_load, frame_path_at, annots_at_frame
import shutil

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('vid_proto')
    parser.add_argument('annot_proto')
    parser.add_argument('save_dir')
    args = parser.parse_args()

    vid_proto = proto_load(args.vid_proto)
    annot_proto = proto_load(args.annot_proto)
    if not osp.isdir(args.save_dir):
        os.makedirs(args.save_dir)

    for frame in vid_proto['frames']:
        frame_id = frame['frame']
        image_path = frame_path_at(vid_proto, frame_id)
        annots = annots_at_frame(annot_proto, frame_id)
        cls_idx = [annot['class_index'] for annot in annots]
        uniq_cls = set(cls_idx)
        for cls in uniq_cls:
            save_dir = osp.join(args.save_dir,
                "{:02d}".format(cls))
            if not osp.isdir(save_dir):
                os.makedirs(save_dir)
Example #13
0
    parser.add_argument('track_file')
    parser.add_argument('annot_file')
    parser.add_argument('lstm_model')
    parser.add_argument('num_layers', type=int,
        help='Number of layers')
    parser.add_argument('input_size', type=int,
        help='Input size.')
    parser.add_argument('save_fig',
        help='Save figure.')
    parser.add_argument('--type', type=str, choices=['residual', 'basic'], default='residual',
        help='Type of LSTM cells. [residual]')

    args = parser.parse_args()

    tracks = tpn_test_iterator(args.track_file)
    annot_proto = proto_load(args.annot_file)

    config = TestConfig()
    config.num_layers = args.num_layers
    config.type = args.type
    config.hidden_size = config.input_size = args.input_size
    with tf.Graph().as_default(), tf.Session() as session:
        initializer = tf.random_uniform_initializer(
            -config.init_scale, config.init_scale)
        with tf.variable_scope("model", reuse=None, initializer=None):
            m = TPNModel(is_training=False, config = config)

        saver = tf.train.Saver()
        saver.restore(session, args.lstm_model)

        cls_losses = []
Example #14
0
#!/usr/bin/env python

import argparse
import os
import sys
sys.path.insert(1, '.')
from vdetlib.utils.protocol import merge_score_protos, proto_dump, proto_load

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('score_file1')
    parser.add_argument('score_file2')
    parser.add_argument('save_file')
    parser.add_argument('--scheme', required=True, choices=['combine', 'max'])
    args = parser.parse_args()
    if os.path.isfile(args.save_file):
        print '{} already exists.'.format(args.save_file)
        sys.exit(0)
    score_proto1 = proto_load(args.score_file1)
    score_proto2 = proto_load(args.score_file2)
    new_proto = merge_score_protos(score_proto1,
                                   score_proto2,
                                   scheme=args.scheme)
    save_dir = os.path.dirname(args.save_file)
    if save_dir is not '' and not os.path.isdir(save_dir):
        os.makedirs(save_dir)
    proto_dump(new_proto, args.save_file)
Example #15
0
    with open(args.vid_list) as f:
        vid_list = [line.strip() for line in f]
    for vid in vid_list:
        print "Processing {}".format(vid)
        try:
            save_file = osp.join(args.save_dir, vid+'.track')
            if args.zip:
                save_file += '.zip'
            else:
                save_file += '.pkl'
            if osp.isfile(save_file):
                print "{} already exists.".format(save_file)
                continue
            vid_file = osp.join(args.vid_dir, vid+'.vid')
            track_file = osp.join(args.track_dir, vid+'.track')
            vid_proto = proto_load(vid_file)
            track_proto = proto_load(track_file)

            new_track_proto = track_propagation(vid_proto, track_proto, net, im_detect,
                keep_feat=args.keep_feat, batch_size=args.boxes_num_per_batch)

            # add ground truth targets if annotation file is given
            if args.annot_dir is not None:
                annot_file = osp.join(args.annot_dir,
                    vid+'.annot')
                annot_proto = proto_load(annot_file)
                add_track_targets(track_proto, annot_proto)

            if args.zip:
                save_track_proto_to_zip(new_track_proto, save_file)
            else:
#!/usr/bin/env python

import argparse
import os
import sys
sys.path.insert(1, '.')
from vdetlib.utils.protocol import merge_score_protos, proto_dump, proto_load

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('score_file1')
    parser.add_argument('score_file2')
    parser.add_argument('save_file')
    parser.add_argument('--scheme', required=True, choices=['combine', 'max'])
    args = parser.parse_args()
    if os.path.isfile(args.save_file):
        print '{} already exists.'.format(args.save_file)
        sys.exit(0)
    score_proto1 = proto_load(args.score_file1)
    score_proto2 = proto_load(args.score_file2)
    new_proto = merge_score_protos(score_proto1, score_proto2, scheme=args.scheme)
    save_dir = os.path.dirname(args.save_file)
    if save_dir is not '' and not os.path.isdir(save_dir):
        os.makedirs(save_dir)
    proto_dump(new_proto, args.save_file)
Example #17
0
import argparse
import os
import sys

sys.path.insert(1, ".")
from vdetlib.utils.protocol import proto_load
import glob
import numpy as np


def max_det_score(det):
    return max([class_score["score"] for class_score in det["scores"]])


if __name__ == "__main__":
    parser = argparse.ArgumentParser("Calculate ratio of detections given a certain ratio.")
    parser.add_argument("det_dir")
    args = parser.parse_args()

    det_files = glob.glob(os.path.join(args.det_dir, "*.det*"))
    max_scores = []
    for det_file in det_files:
        print "Processing {}...".format(det_file)
        det_proto = proto_load(det_file)
        max_scores.extend([max_det_score(det) for det in det_proto["detections"]])

    sorted_scores = sorted(max_scores, reverse=True)
    for ratio in np.arange(0.01, 1, 0.01):
        num = int(ratio * len(sorted_scores))
        print "Thres {:.02f}: ratio {}".format(sorted_scores[num], ratio)
Example #18
0
import sys
sys.path.insert(1, '.')
from vdetlib.vdet.dataset import imagenet_vdet_classes
from vdetlib.utils.visual import unique_colors, add_bbox
from vdetlib.utils.common import imread, imwrite
from vdetlib.utils.protocol import proto_dump, proto_load, top_detections, frame_path_at, track_box_at_frame
import cv2

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('vid_file')
    parser.add_argument('annot_file')
    parser.add_argument('--save_dir', default=None)
    args = parser.parse_args()

    vid_proto = proto_load(args.vid_file)
    annot_proto = proto_load(args.annot_file)

    colors = unique_colors(len(annot_proto['annotations']))

    for frame in vid_proto['frames']:
        img = imread(frame_path_at(vid_proto, frame['frame']))
        boxes = [track_box_at_frame(tracklet, frame['frame']) \
                for tracklet in [annot['track'] for annot in annot_proto['annotations']]]
        tracked = add_bbox(img, boxes, None, 10)
        if args.save_dir:
            if not os.path.isdir(args.save_dir):
                os.makedirs(args.save_dir)
            imwrite(os.path.join(args.save_dir, "{:04d}.jpg".format(frame['frame'])),
                    tracked)
        else:
Example #19
0
import os.path as osp
from glob import glob
sys.path.insert(1, '.')
from vdetlib.utils.protocol import proto_load, proto_dump, track_proto_from_annot_proto
from vdetlib.vdet.dataset import imagenet_vdet_class_idx, imagenet_det_200_class_idx
from vdetlib.vdet.tubelet_cls import anchor_propagate, score_proto_interpolation

if __name__ == '__main__':
    parser = argparse.ArgumentParser('Max pooling on detections around tubelet boxes.')
    parser.add_argument('vid_file')
    parser.add_argument('track_dir')
    parser.add_argument('det_file')
    parser.add_argument('save_dir')
    args = parser.parse_args()

    vid_proto = proto_load(args.vid_file)
    det_proto = proto_load(args.det_file)

    if not os.path.isdir(args.save_dir):
        try:
            os.makedirs(args.save_dir)
        except:
            pass

    track_files = glob(osp.join(args.track_dir, '*.track*'))
    if len(track_files) == 0:
        print "Warning: {} has no tracks".format(args.track_dir)
        sys.exit(0)

    for track_file in track_files:
        track_proto = proto_load(track_file)
Example #20
0
    args = parser.parse_args()

    # read image_list
    with open(args.image_list, 'r') as f:
        image_list = dict([line.strip().split() for line in f])

    num_classes = args.num_classes
    all_boxes = [[[] for _ in xrange(len(image_list))]
                 for _ in xrange(num_classes)]

    # process vid detections
    tracks = sorted(glob.glob(osp.join(args.track_dir, '*')))
    for track_path in tracks:
        print track_path
        vid_name = osp.split(track_path)[-1].split('.')[0]
        vid_proto = proto_load(osp.join(args.vid_dir, vid_name + '.vid'))
        track_proto = proto_load(track_path)
        for frame in vid_proto['frames']:
            frame_name = osp.join(vid_name, osp.splitext(frame['path'])[0])
            if frame_name not in image_list.keys(): continue

            frame_idx = frame['frame']
            sub_idx = int(image_list[frame_name])
            global_idx = sub_idx - 1
            start_time = time()
            scores, boxes = _frame_dets(track_proto['tracks'], frame_idx,
                                        args.score_key, args.box_key)
            boxes = boxes.reshape((boxes.shape[0], -1))

            for j in xrange(1, num_classes):
                inds = np.where(scores[:, j] > args.thres)[0]
                        help='Tracking frame step. [1]')
    parser.add_argument('--thres', type=float, default=0.,
                        help='Threshold to terminate tracking. [0.]')
    parser.add_argument('--nms_thres', type=float, default=0.3,
                        help='Overlap threshold to start new anchors. [0.3]')
    parser.add_argument('--job', type=int, default=1,
                        help='job id. [1]')
    args = parser.parse_args()

    if not os.path.isdir(args.save_dir):
        try:
            os.makedirs(args.save_dir)
        except:
            pass

    vid_proto = proto_load(args.vid_file)
    vid_name = vid_proto['video']
    logging.info("Video: {}".format(vid_proto['video']))

    det_info = load_det_info(vid_proto, args.det_dir)

    #eng = matlab.engine.start_matlab('-nodisplay -nojvm -nosplash -nodesktop')
    eng = matlab.engine.start_matlab()
    opts = options({'engine': eng, 'max_tracks': args.num, 'thres': args.thres,
                   'gpu': args.job - 1, 'max_frames': args.max_frames,
                   'step': args.step, 'nms_thres': args.nms_thres})

    for cls_name in imagenet_vdet_class_idx:
        if cls_name == '__background__':
            continue
        save_name = os.path.join(args.save_dir, '.'.join([vid_name, cls_name, 'track.gz']))
Example #22
0
#!/usr/bin/env python

import argparse
import sys
import os
sys.path.insert(1, '.')
from vdetlib.utils.protocol import proto_load, proto_dump
from vdetlib.utils.common import caffe_net
from vdetlib.vdet.tubelet_cls import score_conv_cls
from vdetlib.utils.visual import plot_track_scores
import matplotlib.pyplot as plt

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('score_file')
    parser.add_argument('--save_dir', default=None)
    args = parser.parse_args()

    score_proto = proto_load(args.score_file)

    if args.save_dir is not None:
        plots = plot_track_scores(score_proto, True)
        if not os.path.isdir(args.save_dir):
            os.makedirs(args.save_dir)
        for ind, plot in enumerate(plots, start=1):
            plot.savefig(os.path.join(args.save_dir, "{}.png".format(ind)))

Example #23
0
    net = caffe.Net(args.prototxt, args.caffemodel, caffe.TEST)
    net.name = os.path.splitext(os.path.basename(args.caffemodel))[0]

    # apply bbox regression normalization on the net weights
    with open(args.bbox_mean, 'rb') as f:
        bbox_means = cPickle.load(f)
    with open(args.bbox_std, 'rb') as f:
        bbox_stds = cPickle.load(f)

    net.params[args.bbox_pred_layer][0].data[...] = \
        net.params[args.bbox_pred_layer][0].data * bbox_stds[:, np.newaxis]

    net.params[args.bbox_pred_layer][1].data[...] = \
        net.params[args.bbox_pred_layer][1].data * bbox_stds + bbox_means

    vid_proto = proto_load(args.vid_file)
    box_proto = proto_load(args.box_file)

    window = net.params[args.bbox_pred_layer][0].data.shape[0] / 4 + 1
    track_proto = sequence_roi_propagation(vid_proto,
                                           box_proto,
                                           net,
                                           sequence_im_detect,
                                           window=window,
                                           length=args.length,
                                           sample_rate=args.sample_rate,
                                           keep_feat=args.keep_feat,
                                           batch_size=args.boxes_num_per_batch)

    # add ground truth targets if annotation file is given
    if args.annot_file is not None:
Example #24
0
#!/usr/bin/env python

import argparse
import os
import sys
sys.path.insert(1, '.')
from vdetlib.utils.protocol import proto_load
import glob
import numpy as np

def max_det_score(det):
    return max([class_score['score'] for class_score in det['scores']])

if __name__ == '__main__':
    parser = argparse.ArgumentParser('Calculate ratio of detections given a certain ratio.')
    parser.add_argument('det_dir')
    args = parser.parse_args()

    det_files = glob.glob(os.path.join(args.det_dir, '*.det*'))
    max_scores = []
    for det_file in det_files:
        print "Processing {}...".format(det_file)
        det_proto = proto_load(det_file)
        max_scores.extend([max_det_score(det) for det in det_proto['detections']])

    sorted_scores = sorted(max_scores, reverse=True)
    for ratio in np.arange(0.01, 1, 0.01):
        num = int(ratio * len(sorted_scores))
        print "Thres {:.02f}: ratio {}".format(sorted_scores[num], ratio)
Example #25
0
    # load feature model
    caffe.set_mode_gpu()
    caffe.set_device(args.job_id - 1)
    feature_net = caffe.Net(args.def_file, args.param, caffe.TEST)
    print 'Loaded feature network from {:s}.'.format(args.def_file)

    return feature_net, rnn_net, session

if __name__ == '__main__':
    args = parse_args()

    if args.cfg_file is not None:
        cfg_from_file(args.cfg_file)
    cfg.DEDUP_BOXES = 0.0

    # Load models
    feature_net, rnn_net, session = load_models(args)

    # Load protocols
    vid_proto = proto_load(args.vid_file)
    box_proto = proto_load(args.box_file)

    # End-to-end testing
    track_proto = tpn_test(vid_proto, box_proto, feature_net, rnn_net,
        session, im_detect, scheme=args.scheme,
        length=args.length, sample_rate=args.sample_rate,
        offset=args.offset, batch_size=args.boxes_num_per_batch)

    proto_dump(track_proto, args.save_file)
Example #26
0
                        default=1,
                        help='Tracking frame step. [1]')
    parser.add_argument('--thres',
                        type=float,
                        default=0.,
                        help='Threshold to terminate tracking. [0.]')
    parser.add_argument('--nms_thres',
                        type=float,
                        default=0.3,
                        help='Overlap threshold to start new anchors. [0.3]')
    parser.add_argument('--job', type=int, default=1, help='job id. [1]')
    args = parser.parse_args()

    if not os.path.isdir(args.save_dir):
        os.makedirs(args.save_dir)
    vid_proto = proto_load(args.vid_file)
    det_proto = proto_load(args.det_file)
    vid_name = vid_proto['video']
    assert vid_name == det_proto['video']
    print "Video: {}".format(vid_proto['video'])

    eng = matlab.engine.start_matlab('-nodisplay -nojvm -nosplash -nodesktop')
    opts = options({
        'engine': eng,
        'max_tracks': args.num,
        'thres': args.thres,
        'gpu': args.job - 1,
        'max_frames': args.max_frames,
        'step': args.step,
        'nms_thres': args.nms_thres
    })
Example #27
0
from vdetlib.utils.protocol import proto_load, proto_dump
from vdetlib.utils.common import caffe_net
from vdetlib.vdet.tubelet_cls import score_conv_cls
from vdetlib.utils.visual import plot_track_scores
import matplotlib.pyplot as plt

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('score_file')
    parser.add_argument('net_file')
    parser.add_argument('param_file')
    parser.add_argument('save_file')
    parser.add_argument('--job', type=int, default=1)
    parser.add_argument('--save_dir', default=None)
    args = parser.parse_args()

    score_proto = proto_load(args.score_file)
    net = caffe_net(args.net_file, args.param_file, args.job - 1)
    new_score_proto = score_conv_cls(score_proto, net)
    save_root = os.path.dirname(args.save_file)
    if not os.path.isdir(save_root):
        os.makedirs(save_root)
    proto_dump(new_score_proto, args.save_file)

    if args.save_dir is not None:
        plots = plot_track_scores(new_score_proto)
        if not os.path.isdir(args.save_dir):
            os.makedirs(args.save_dir)
        for ind, plot in enumerate(plots, start=1):
            plot.savefig(os.path.join(args.save_dir, "{}.png".format(ind)))
Example #28
0
                        help='Number of detections to track. [10]')
    parser.add_argument('--max_frames', type=int, default=None,
                        help='Maximum track length. [Inf]')
    parser.add_argument('--step', type=int, default=1,
                        help='Tracking frame step. [1]')
    parser.add_argument('--thres', type=float, default=0.,
                        help='Threshold to terminate tracking. [0.]')
    parser.add_argument('--nms_thres', type=float, default=0.3,
                        help='Overlap threshold to start new anchors. [0.3]')
    parser.add_argument('--job', type=int, default=1,
                        help='job id. [1]')
    args = parser.parse_args()

    if not os.path.isdir(args.save_dir):
        os.makedirs(args.save_dir)
    vid_proto = proto_load(args.vid_file)
    det_proto = proto_load(args.det_file)
    vid_name = vid_proto['video']
    assert  vid_name == det_proto['video']
    print "Video: {}".format(vid_proto['video'])

    eng = matlab.engine.start_matlab('-nodisplay -nojvm -nosplash -nodesktop')
    opts = options({'engine': eng, 'max_tracks': args.num, 'thres': args.thres,
                   'gpu': args.job - 1, 'max_frames': args.max_frames,
                   'step': args.step, 'nms_thres': args.nms_thres})
    for cls_name in imagenet_vdet_class_idx:
        if cls_name == '__background__':
            continue
        save_name = os.path.join(args.save_dir, '.'.join([vid_name, cls_name, 'track.gz']))
        if os.path.isfile(save_name):
            print "{} already exists.".format(save_name)
Example #29
0
    # rpn model
    rpn_pt = os.path.join(model_dir, args.rpn, NETS[args.rpn][1])
    rpn_model = os.path.join(model_dir, args.rpn, NETS[args.rpn][2])

    # fast_rcnn model
    fast_rcnn_pt = os.path.join(model_dir, args.feature_net, NETS[args.feature_net][1])
    fast_rcnn_model = os.path.join(model_dir, args.feature_net, NETS[args.feature_net][2])

    if args.cpu_mode:
        caffe.set_mode_cpu()
    else:
        caffe.set_mode_gpu()
        gpu_id = args.job - 1;
        caffe.set_device(gpu_id)
        cfg.GPU_ID = gpu_id
    print '\n\nLoaded network {:s}'.format(rpn_model)
    rpn_net = caffe.Net(rpn_pt, rpn_model, caffe.TEST)
    print '\n\nLoaded network {:s}'.format(fast_rcnn_model)
    fast_rcnn_net = caffe.Net(fast_rcnn_pt, fast_rcnn_model, caffe.TEST)

    vid_proto = proto_load(args.vid_file)

    gen_rois = lambda net, im: im_proposals(net, im)[0]
    det_proto = rpn_fast_rcnn_det_vid(rpn_net, fast_rcnn_net, vid_proto, gen_rois,
        im_detect, CLS_IDX[args.feature_net])

    save_dir = os.path.dirname(args.save_file)
    if not os.path.isdir(save_dir):
        os.makedirs(save_dir)
    proto_dump(det_proto, args.save_file)
Example #30
0
#!/usr/bin/env python
import argparse

import sys
import os.path as osp
this_dir = osp.dirname(__file__)
sys.path.insert(0, osp.join(this_dir, '../../src/'))
sys.path.insert(0, osp.join(this_dir, '../../external/'))
from vdetlib.utils.protocol import proto_load
from tpn.data_io import save_track_proto_to_zip

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('proto_file')
    parser.add_argument('save_zip')
    args = parser.parse_args()

    proto = proto_load(args.proto_file)
    save_track_proto_to_zip(proto, args.save_zip)