Esempio n. 1
0
def viterbi(observations, **kwargs):
    TRANSITION_PROBS = []
    radius = kwargs['radius'] if 'radius' in kwargs else RADIUS
    filename = kwargs['filename'] if 'filename' in kwargs else None
    window = kwargs['window'] if 'window' in kwargs else WINDOW
    n = kwargs['n'] if 'n' in kwargs else N
    return_gps = kwargs['return_gps'] if 'return_gps' in kwargs else False

    print 'Running viterbi. window size: {0}, max states {1}, max radius {2}'.format(window,n,radius)

    result_sequence = []
    segments_table = []
    probabilities_table = []
    segments, emission_probabilities, point = compute_emission_probabilities(observations[0],radius, n)
    for i, segment in enumerate(segments):
        segments[i]['previous'] = None 
        segments[i]['direction'] = None
    segments_table.append(segments)
    probabilities_table.append(emission_probabilities)
    for window_idx in range(len(observations) / window + 1):
        current_obs = observations[window_idx*window:(window_idx+1)*window]
        if (len(current_obs) == 0):
            break
        for t, obs in enumerate(current_obs):
            TRANSITION_PROBS.append([])
            if t == 0:
                continue
            previous_point = point
            segments, emission_probabilities, point = compute_emission_probabilities(obs, radius, n)
            transition_probabilities = compute_transition_probabilities_training(previous_point,
                                                                        point,
                                                                        segments_table[t-1],
                                                                        segments,
                                                                        window_idx*window+t,
                                                                        TRANSITION_PROBS)
            segments_table.append([])
            probabilities_table.append([])
            for i, emission_probability in enumerate(emission_probabilities):
                candidates = []
                for j, previous_probability in enumerate(probabilities_table[t-1]):
                    candidates.append(previous_probability * transition_probabilities[j][i] * emission_probability)
                idx, highest_probability = max(enumerate(candidates), key=lambda x: x[1])
                probabilities_table[t].append(highest_probability)
                segments[i]['previous'] = idx
                segments[i]['direction'] = utils.calculate_direction(segments_table[t-1][idx], segments[i])
                segments_table[t].append(segments[i])
        last_idx, last_val = max(enumerate(probabilities_table[t]), key=lambda x: x[1])
        idx = last_idx
        intermediate_result = []
        for _t in range(len(current_obs))[::-1]:
            cur =  segments_table[_t][idx]
            intermediate_result.append(cur)
            if _t != 0:
                idx = cur['previous']
        probabilities_table = [[1]]
        segments_table = [[segments_table[t][last_idx]]]
        result_sequence = result_sequence + intermediate_result[::-1]
    for t, cur in enumerate(result_sequence):
        if t == 0 or not TRANSITION_PROBS[t]:
            continue
        prev = result_sequence[t-1]
        prev_str = '{0},{1}'.format(prev['way_osm_id'], prev['index_in_way'])
        cur_str = '{0},{1}'.format(cur['way_osm_id'], cur['index_in_way'])
        TRANSITION_PROBS[t] = {prev_str : TRANSITION_PROBS[t][prev_str]}
        TRANSITION_PROBS[t][prev_str][cur_str][2] = 1

    return TRANSITION_PROBS
Esempio n. 2
0
    if counter - 8 is 0:
        print("a")
        for i in range(1, 3):
            crop_y_start = int(((i - 1) / 2) * size_y)
            crop_y_end = int((i / 2) * size_y)
            sliced_img = src_processed[crop_y_start:crop_y_end, 0:size_x]
            crop_img.append(sliced_img)
            img_sum_av.append(
                sig.savgol_filter(np.sum(sliced_img, axis=0), 101, 3))
            # img_sum_av.append(np.sum(sliced_img, axis=0))

        center_upper = calculate_center(img_sum_av[0])
        center_lower = calculate_center(img_sum_av[1])

        direction_upper, direction_lower = calculate_direction(
            [center_upper, center_lower], img_sum_av[0].size)

        mark_value = []

        plot.figure()
        plot.subplot(3, 1, 1)
        plot.title("threshold = {}".format(lowThreshold))
        plot.imshow(src_processed)
        plot.subplot(3, 1, 2)
        plot.title("Direction = {}".format(direction_upper))
        plot.plot(img_sum_av[0], "-D", markevery=[center_upper])
        plot.subplot(3, 1, 3)
        plot.title("Direction = {}".format(direction_lower))
        plot.plot(img_sum_av[1], "-D", markevery=[center_lower])
        plot.subplots_adjust(top=0.85)
        plot.show()
Esempio n. 3
0
def viterbi(observations, **kwargs):
    radius = kwargs['radius'] if 'radius' in kwargs else RADIUS
    filename = kwargs['filename'] if 'filename' in kwargs else None
    window = kwargs['window'] if 'window' in kwargs else WINDOW
    n = kwargs['n'] if 'n' in kwargs else N
    return_gps = kwargs['return_gps'] if 'return_gps' in kwargs else False

    print 'Running viterbi. window size: {0}, max states {1}, max radius {2}'.format(window,n,radius)

    result_sequence = []
    segments_table = []
    probabilities_table = []
    segments, emission_probabilities, point = compute_emission_probabilities(observations[0],radius, n)
    for i, segment in enumerate(segments):
        segments[i]['previous'] = None 
        segments[i]['direction'] = None
    segments_table.append(segments)
    probabilities_table.append(emission_probabilities)
    for window_idx in range(len(observations) / window + 1):
        current_obs = observations[window_idx*window:(window_idx+1)*window]
        if (len(current_obs) == 0):
            break
        for t, obs in enumerate(current_obs):
            if t == 0:
                continue
            previous_point = point
            segments, emission_probabilities, point = compute_emission_probabilities(obs, radius, n)
            transition_probabilities = compute_transition_probabilities(previous_point,
                                                                        point,
                                                                        segments_table[t-1],
                                                                        segments)
            segments_table.append([])
            probabilities_table.append([])
            for i, emission_probability in enumerate(emission_probabilities):
                candidates = []
                for j, previous_probability in enumerate(probabilities_table[t-1]):
                    candidates.append(previous_probability * transition_probabilities[j][i] * emission_probability)
                idx, highest_probability = max(enumerate(candidates), key=lambda x: x[1])
                probabilities_table[t].append(highest_probability)
                segments[i]['previous'] = idx
                segments[i]['direction'] = utils.calculate_direction(segments_table[t-1][idx], segments[i])
                segments_table[t].append(segments[i])
        last_idx, last_val = max(enumerate(probabilities_table[t]), key=lambda x: x[1])
        idx = last_idx
        intermediate_result = []
        for _t in range(len(current_obs))[::-1]:
            cur =  segments_table[_t][idx]
            intermediate_result.append(cur)
            if _t != 0:
                idx = cur['previous']
        probabilities_table = [[1]]
        segments_table = [[segments_table[t][last_idx]]]
        result_sequence = result_sequence + intermediate_result[::-1]
    if return_gps:
        node_gps = utils.get_node_gps_points(result_sequence)
        start_points = ['{0},{1}'.format(point[0][0], point[0][1]) for point in node_gps]
        end_points = ['{0},{1}'.format(point[1][0], point[1][1]) for point in node_gps]
        with open('result_nodes.csv', 'w') as resf:
            for i, point in enumerate(start_points):
                resf.write(point+'\n')
                resf.write(end_points[i]+'\n')
        return
    node_ids = utils.get_node_ids(result_sequence)
    if filename is not None:
        utils.write_to_file(node_ids, filename)
        return
    return node_ids