-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlog_utils.py
More file actions
188 lines (147 loc) · 5.6 KB
/
log_utils.py
File metadata and controls
188 lines (147 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import os
import tempfile
from datetime import datetime
import absl.flags as flags
import ml_collections
import numpy as np
import wandb
from PIL import Image, ImageEnhance
import glob
import shutil
import csv
def get_csv_header(path):
with open(path, 'r') as f:
# Use DictReader to automatically interpret the first row as headers
dict_reader = csv.DictReader(f)
return dict_reader.fieldnames
class CsvLogger:
"""CSV logger for logging metrics to a CSV file."""
def __init__(self, path):
self.path = path
self.header = None
self.file = None
self.disallowed_types = (wandb.Image, wandb.Video, wandb.Histogram)
def log(self, row, step):
row['step'] = step
if self.file is None:
self.file = open(self.path, 'w')
if self.header is None:
self.header = [k for k, v in row.items() if not isinstance(v, self.disallowed_types)]
self.file.write(','.join(self.header) + '\n')
filtered_row = {k: v for k, v in row.items() if not isinstance(v, self.disallowed_types)}
self.file.write(','.join([str(filtered_row.get(k, '')) for k in self.header]) + '\n')
else:
filtered_row = {k: v for k, v in row.items() if not isinstance(v, self.disallowed_types)}
self.file.write(','.join([str(filtered_row.get(k, '')) for k in self.header]) + '\n')
self.file.flush()
def close(self):
if self.file is not None:
self.file.close()
def save(self, dst_path):
if self.file is not None:
self.file.close()
src_path = self.path
try:
shutil.copyfile(src_path, dst_path)
except FileNotFoundError:
print(f"Error: '{src_path}' not found.")
except Exception as e:
print(f"An error occurred: {e}")
self.file = open(self.path, 'a')
def restore(self, src_path):
dst_path = self.path
try:
shutil.copyfile(src_path, dst_path)
except FileNotFoundError:
print(f"Error: '{src_path}' not found.")
except Exception as e:
print(f"An error occurred: {e}")
self.header = get_csv_header(self.path)
self.file = open(self.path, 'a')
def get_hash(s):
import hashlib
encoded_string = s.encode('utf-8')
sha256_hash = hashlib.sha256()
sha256_hash.update(encoded_string)
hex_digest = sha256_hash.hexdigest()
return hex_digest
def get_exp_name(flags):
"""Return the experiment name."""
s = flags.flags_into_string()
exp_name = s
return get_hash(exp_name)
def get_flag_dict():
"""Return the dictionary of flags."""
flag_dict = {k: getattr(flags.FLAGS, k) for k in flags.FLAGS if '.' not in k}
for k in flag_dict:
if isinstance(flag_dict[k], ml_collections.ConfigDict):
flag_dict[k] = flag_dict[k].to_dict()
return flag_dict
def setup_wandb(
entity=None,
project='project',
group=None,
tags=None,
name=None,
mode='online',
):
"""Set up Weights & Biases for logging."""
wandb_output_dir = tempfile.mkdtemp()
if tags is None:
tags = [group] if group is not None else None
init_kwargs = dict(
config=get_flag_dict(),
project=project,
entity=entity,
tags=tags,
group=group,
dir=wandb_output_dir,
name=name,
settings=wandb.Settings(
start_method='thread',
_disable_stats=False,
),
mode=mode,
)
run = wandb.init(**init_kwargs)
return run
def reshape_video(v, n_cols=None):
"""Helper function to reshape videos."""
if v.ndim == 4:
v = v[None,]
_, t, h, w, c = v.shape
if n_cols is None:
# Set n_cols to the square root of the number of videos.
n_cols = np.ceil(np.sqrt(v.shape[0])).astype(int)
if v.shape[0] % n_cols != 0:
len_addition = n_cols - v.shape[0] % n_cols
v = np.concatenate((v, np.zeros(shape=(len_addition, t, h, w, c))), axis=0)
n_rows = v.shape[0] // n_cols
v = np.reshape(v, newshape=(n_rows, n_cols, t, h, w, c))
v = np.transpose(v, axes=(2, 5, 0, 3, 1, 4))
v = np.reshape(v, newshape=(t, c, n_rows * h, n_cols * w))
return v
def get_wandb_video(renders=None, n_cols=None, fps=15):
"""Return a Weights & Biases video.
It takes a list of videos and reshapes them into a single video with the specified number of columns.
Args:
renders: List of videos. Each video should be a numpy array of shape (t, h, w, c).
n_cols: Number of columns for the reshaped video. If None, it is set to the square root of the number of videos.
"""
# Pad videos to the same length.
max_length = max([len(render) for render in renders])
for i, render in enumerate(renders):
assert render.dtype == np.uint8
# Decrease brightness of the padded frames.
final_frame = render[-1]
final_image = Image.fromarray(final_frame)
enhancer = ImageEnhance.Brightness(final_image)
final_image = enhancer.enhance(0.5)
final_frame = np.array(final_image)
pad = np.repeat(final_frame[np.newaxis, ...], max_length - len(render), axis=0)
renders[i] = np.concatenate([render, pad], axis=0)
# Add borders.
renders[i] = np.pad(renders[i], ((0, 0), (1, 1), (1, 1), (0, 0)), mode='constant', constant_values=0)
renders = np.array(renders) # (n, t, h, w, c)
renders = reshape_video(renders, n_cols) # (t, c, nr * h, nc * w)
return wandb.Video(renders, fps=fps, format='mp4')