def main(data_root, result_root, split, seed, feat_window_size):
    result_root += "-s-%d-%d" % (split, seed)

    ### read label2index mapping and index2label mapping ###########################
    label2index = dict()
    index2label = dict()
    with open(os.path.join(data_root, 'mapping.txt'), 'r') as f:
        content = f.read().split('\n')[0:-1]
        for line in content:
            label2index[line.split()[1]] = int(line.split()[0])
            index2label[int(line.split()[0])] = line.split()[1]

    ### read test data #############################################################
    #with open('data/split1.test', 'r') as f:
    with open(os.path.join(data_root, 'split%d.test' % split), 'r') as f:
        video_list = f.read().split('\n')[0:-1]
    dataset = Dataset(data_root, video_list, label2index, shuffle=False)

    # load prior, length model, grammar, and network
    load_iteration = NUM_ITERS
    log_prior = np.log(
        np.loadtxt('%s/prior.iter-' % result_root + str(load_iteration) +
                   '.txt'))
    grammar = PathGrammar('%s/grammar.txt' % result_root, label2index)
    length_model = PoissonModel('%s/lengths.iter-' % result_root +
                                str(load_iteration) + '.txt',
                                max_length=2000)
    forwarder = Forwarder(dataset.input_dimension,
                          dataset.n_classes,
                          feat_window_size=feat_window_size)
    forwarder.load_model('%s/network.iter-' % result_root +
                         str(load_iteration) + '.net')

    # parallelization
    n_threads = 4

    # Viterbi decoder
    viterbi_decoder = Viterbi(grammar,
                              length_model,
                              frame_sampling=30,
                              max_hypotheses=np.inf)
    # forward each video
    log_probs = dict()
    queue = mp.Queue()
    for i, data in enumerate(dataset):
        sequence, _ = data
        video = list(dataset.features.keys())[i]
        queue.put(video)
        log_probs[video] = forwarder.forward(sequence) - log_prior
        log_probs[video] = log_probs[video] - np.max(log_probs[video])
    # Viterbi decoding
    procs = []
    for i in range(n_threads):
        p = mp.Process(target=decode,
                       args=(queue, log_probs, viterbi_decoder, index2label,
                             result_root))
        procs.append(p)
        p.start()
    for p in procs:
        p.join()
Exemplo n.º 2
0
def infer(label2index, index2label, n_threads):
    # load models
    log_prior = np.log(np.loadtxt('results/prior'))
    grammar = PathGrammar('results/grammar', label2index)
    length_model = PoissonModel('results/mean_lengths', max_length=2000)
    forwarder = Forwarder('results/net.model')
    # Viterbi decoder (max_hypotheses = n: at each time step, prune all hypotheses worse than the top n)
    viterbi_decoder = Viterbi(grammar,
                              length_model,
                              frame_sampling=30,
                              max_hypotheses=50000)
    # create list of test videos
    with open('data/split1.test', 'r') as f:
        video_list = f.read().split('\n')[0:-1]
    # forward each video
    log_probs = dict()
    queue = mp.Queue()
    for video in video_list:
        queue.put(video)
        dataset = Dataset('data', [video], label2index)
        log_probs[video] = forwarder.forward(dataset) - log_prior
        log_probs[video] = log_probs[video] - np.max(log_probs[video])
    # Viterbi decoding
    procs = []
    for i in range(n_threads):
        p = mp.Process(target=decode,
                       args=(queue, log_probs, viterbi_decoder, index2label))
        procs.append(p)
        p.start()
    for p in procs:
        p.join()
Exemplo n.º 3
0
    content = f.read().split('\n')[0:-1]
    for line in content:
        label2index[line.split()[1]] = int(line.split()[0])
        index2label[int(line.split()[0])] = line.split()[1]

### read test data #############################################################
with open(args.decoded_path, 'r') as f:
    video_list = f.read().split('\n')[0:-1]
dataset = Dataset('data', video_list, label2index, shuffle=False)

# load prior, length model, grammar, and network
load_iteration = 10000
log_prior = np.log(
    np.loadtxt(args.result_path + 'prior.iter-' + str(load_iteration) +
               '.txt'))
grammar = PathGrammar(args.result_path + 'grammar.txt', label2index)
length_model = PoissonModel(args.result_path + 'lengths.iter-' +
                            str(load_iteration) + '.txt',
                            max_length=2000)
forwarder = Forwarder(dataset.input_dimension, dataset.n_classes)
forwarder.load_model(args.result_path + 'network.iter-' + str(load_iteration) +
                     '.net')

# parallelization
n_threads = 4

# Viterbi decoder
viterbi_decoder = Viterbi(grammar,
                          length_model,
                          frame_sampling=30,
                          max_hypotheses=np.inf)
Exemplo n.º 4
0
        label2index[line.split()[1]] = int(line.split()[0])
        index2label[int(line.split()[0])] = line.split()[1]

### read test data #############################################################
with open('/scratch/liju2/nn_viterbi/data/split1.test', 'r') as f:
    video_list = f.read().split('\n')[0:-1]
dataset = Dataset('/scratch/liju2/nn_viterbi/data',
                  video_list,
                  label2index,
                  shuffle=False)

# load prior, length model, grammar, and network
load_iteration = 100000
log_prior = np.log(
    np.loadtxt('results/prior.iter-' + str(load_iteration) + '.txt'))
grammar = PathGrammar('results/grammar.txt', label2index)
length_model = PoissonModel('results/lengths.iter-' + str(load_iteration) +
                            '.txt',
                            max_length=2000)
forwarder = Forwarder(dataset.input_dimension, dataset.n_classes)
forwarder.load_model('results/network.iter-' + str(load_iteration) + '.net')
window = 10
step = 5

# parallelization
n_threads = 4

# Viterbi decoder
viterbi_decoder = Viterbi(grammar, length_model, frame_sampling=30)
# forward each video
log_probs = dict()
Exemplo n.º 5
0
    # read label2index mapping and index2label mapping
    label2index = dict()
    index2label = dict()
    with open(file_dict, 'r') as f:
        content = f.read().split('\n')[0:-1]
        idx_count = 0
        for line in content:
            line = line.strip()
            label2index[line] = idx_count
            index2label[idx_count] = line
            idx_count = idx_count + 1

    # load grammar
    print('Running: ' + file_grammar)
    grammar = PathGrammar(file_grammar, label2index)

    # load lm
    length_model = PoissonModel(file_length, max_length=5000)

    # load your data here (must be in log space!!!):
    # Note that you might want to remove a prior first.
    log_probs = np.loadtxt(file_probs, dtype=np.float32)

    # sanity check
    print(np.max(log_probs))
    print(np.min(log_probs))

    # scale down if out of range
    if np.max(log_probs) > 0:
        log_probs = log_probs - (2 * np.max(log_probs))