BasicSR

Форк
0
/
plot_util.py 
83 строки · 2.5 Кб
1
import re
2

3

4
def read_data_from_tensorboard(log_path, tag):
5
    """Get raw data (steps and values) from tensorboard events.
6

7
    Args:
8
        log_path (str): Path to the tensorboard log.
9
        tag (str): tag to be read.
10
    """
11
    from tensorboard.backend.event_processing.event_accumulator import EventAccumulator
12

13
    # tensorboard event
14
    event_acc = EventAccumulator(log_path)
15
    event_acc.Reload()
16
    scalar_list = event_acc.Tags()['scalars']
17
    print('tag list: ', scalar_list)
18
    steps = [int(s.step) for s in event_acc.Scalars(tag)]
19
    values = [s.value for s in event_acc.Scalars(tag)]
20
    return steps, values
21

22

23
def read_data_from_txt_2v(path, pattern, step_one=False):
24
    """Read data from txt with 2 returned values (usually [step, value]).
25

26
    Args:
27
        path (str): path to the txt file.
28
        pattern (str): re (regular expression) pattern.
29
        step_one (bool): add 1 to steps. Default: False.
30
    """
31
    with open(path) as f:
32
        lines = f.readlines()
33
    lines = [line.strip() for line in lines]
34
    steps = []
35
    values = []
36

37
    pattern = re.compile(pattern)
38
    for line in lines:
39
        match = pattern.match(line)
40
        if match:
41
            steps.append(int(match.group(1)))
42
            values.append(float(match.group(2)))
43
    if step_one:
44
        steps = [v + 1 for v in steps]
45
    return steps, values
46

47

48
def read_data_from_txt_1v(path, pattern):
49
    """Read data from txt with 1 returned values.
50

51
    Args:
52
        path (str): path to the txt file.
53
        pattern (str): re (regular expression) pattern.
54
    """
55
    with open(path) as f:
56
        lines = f.readlines()
57
    lines = [line.strip() for line in lines]
58
    data = []
59

60
    pattern = re.compile(pattern)
61
    for line in lines:
62
        match = pattern.match(line)
63
        if match:
64
            data.append(float(match.group(1)))
65
    return data
66

67

68
def smooth_data(values, smooth_weight):
69
    """ Smooth data using 1st-order IIR low-pass filter (what tensorflow does).
70

71
    Reference: https://github.com/tensorflow/tensorboard/blob/f801ebf1f9fbfe2baee1ddd65714d0bccc640fb1/tensorboard/plugins/scalar/vz_line_chart/vz-line-chart.ts#L704  # noqa: E501
72

73
    Args:
74
        values (list): A list of values to be smoothed.
75
        smooth_weight (float): Smooth weight.
76
    """
77
    values_sm = []
78
    last_sm_value = values[0]
79
    for value in values:
80
        value_sm = last_sm_value * smooth_weight + (1 - smooth_weight) * value
81
        values_sm.append(value_sm)
82
        last_sm_value = value_sm
83
    return values_sm
84

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.