forked from YunseokJANG/l2l-da
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckpoints.py
311 lines (236 loc) · 11.6 KB
/
checkpoints.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
""" Code for saving/loading pytorch models and batches of adversarial images
CHECKPOINT NAMING CONVENTIONS:
<unique_experiment_name>.<architecture_abbreviation>.<6 digits of epoch number>path.tar
e.g.
fgsm_def.resnet32.20180301.120000.path.tar
All checkpoints are stored in CHECKPOINT_DIR
Checkpoints are state dicts only!!!
"""
import torch
import math
import os
import re
import glob
import config
import numpy as np
import utils.pytorch_utils as utils
import random
CHECKPOINT_DIR = config.MODEL_PATH
OUTPUT_IMAGE_DIR = config.OUTPUT_IMAGE_PATH
##############################################################################
# #
# CHECKPOINTING MODELS #
# #
##############################################################################
def clear_experiment(experiment_name, architecture):
""" Deletes all saved state dicts for an experiment/architecture pair """
for filename in params_to_filename(experiment_name, architecture):
full_path = os.path.join(*[CHECKPOINT_DIR, filename])
os.remove(full_path) if os.path.exists(full_path) else None
def list_saved_epochs(experiment_name, architecture):
""" Returns a list of int epochs we've checkpointed for this
experiment name and architecture
"""
extract_epoch = lambda f: int(f.split('.')[-3])
filename_list = params_to_filename(experiment_name, architecture)
return [extract_epoch(f) for f in filename_list]
def params_to_filename(experiment_name, architecture, epoch_val=None):
""" Outputs string name of file.
ARGS:
experiment_name : string - name of experiment we're saving
architecture : string - abbreviation for model architecture
epoch_val : int/(intLo, intHi)/None -
- if int we return this int exactly
- if (intLo, intHi) we return all existing filenames with
highest epoch in range (intLo, intHi), in sorted order
- if None, we return all existing filenames with params
in ascending epoch-sorted order
RETURNS:
filenames: string or (possibly empty) string[] of just the base name
of saved models
"""
if isinstance(epoch_val, int):
return '.'.join([experiment_name, architecture, '%06d' % epoch_val,
'path', 'tar'])
glob_prefix = os.path.join(*[CHECKPOINT_DIR,
'%s.%s.*' % (experiment_name, architecture)])
re_prefix = '%s\.%s\.' % (experiment_name, architecture)
re_suffix = r'\.path\.tar'
valid_name = lambda f: bool(re.match(re_prefix + r'\d{6}' + re_suffix,f))
select_epoch = lambda f: int(re.sub(re_prefix, '',
re.sub(re_suffix, '', f)))
valid_epoch = lambda e: (e >= (epoch_val or (0, 0))[0] and
e <= (epoch_val or (0, float('inf')))[1])
filename_epoch_pairs = []
for full_path in glob.glob(glob_prefix):
filename = os.path.basename(full_path)
if not valid_name(filename):
continue
epoch = select_epoch(filename)
if valid_epoch(epoch):
filename_epoch_pairs.append((filename, epoch))
return [_[0] for _ in sorted(filename_epoch_pairs, key=lambda el: el[1])]
def save_state_dict(experiment_name, architecture, epoch_val, model,
k_highest=10):
""" Saves the state dict of a model with the given parameters.
ARGS:
experiment_name : string - name of experiment we're saving
architecture : string - abbreviation for model architecture
epoch_val : int - which epoch we're saving
model : model - object we're saving the state dict of
k_higest : int - if not None, we make sure to not include more than
k state_dicts for (experiment_name, architecture) pair,
keeping the k-most recent if we overflow
RETURNS:
The model we saved
"""
# First resolve THIS filename
this_filename = params_to_filename(experiment_name, architecture, epoch_val)
# Next clear up memory if too many state dicts
current_filenames = params_to_filename(experiment_name, architecture)
delete_els = []
if k_highest is not None:
num_to_delete = len(current_filenames) - k_highest + 1
if num_to_delete > 0:
delete_els = sorted(current_filenames)[:num_to_delete]
for delete_el in delete_els:
full_path = os.path.join(*[CHECKPOINT_DIR, delete_el])
os.remove(full_path) if os.path.exists(full_path) else None
# Finally save the state dict
torch.save(model.state_dict(), os.path.join(*[CHECKPOINT_DIR,
this_filename]))
return model
def load_state_dict_from_filename(filename, model):
""" Skips the whole parameter argument thing and just loads the whole
state dict from a filename.
ARGS:
filename : string - filename without directories
model : nn.Module - has 'load_state_dict' method
RETURNS:
the model loaded with the weights contained in the file
"""
assert len(glob.glob(os.path.join(*[CHECKPOINT_DIR, filename]))) == 1
# LOAD FILENAME
# If state_dict in keys, use that as the loader
right_dict = lambda d: d.get('state_dict', d)
model.load_state_dict(right_dict(torch.load(
os.path.join(*[CHECKPOINT_DIR, filename]))))
return model
def load_state_dict(experiment_name, architecture, epoch, model):
""" Loads a checkpoint that was previously saved
experiment_name : string - name of experiment we're saving
architecture : string - abbreviation for model architecture
epoch_val : int - which epoch we're loading
"""
filename = params_to_filename(experiment_name, architecture, epoch)
return load_state_dict_from_filename(filename, model)
###############################################################################
# #
# CHECKPOINTING DATA #
# #
###############################################################################
"""
This is a hacky fix to save batches of adversarial images along with their
labels.
"""
class CustomDataSaver(object):
# TODO: make this more pytorch compliant
def __init__(self, image_subdirectory):
self.image_subdirectory = image_subdirectory
# make this folder if it doesn't exist yet
def save_minibatch(self, examples, labels):
""" Assigns a random name to this minibatch and saves the examples and
labels in two separate files:
<random_name>.examples.npy and <random_name>.labels.npy
ARGS:
examples: Variable or Tensor (NxCxHxW) - examples to be saved
labels : Variable or Tensor (N) - labels matching the examples
"""
# First make both examples and labels into numpy arrays
examples = examples.cpu().numpy()
labels = labels.cpu().numpy()
# Make a name for the files
random_string = str(random.random())[2:] # DO THIS BETTER WHEN I HAVE INTERNET
# Save both files
example_file = '%s.examples.npy' % random_string
example_path = os.path.join(OUTPUT_IMAGE_DIR, self.image_subdirectory,
example_file)
np.save(example_path, examples)
label_file = '%s.labels.npy' % random_string
label_path = os.path.join(OUTPUT_IMAGE_DIR, self.image_subdirectory,
label_file)
np.save(label_path, labels)
class CustomDataLoader(object):
# TODO: make this more pytorch compliant
def __init__(self, image_subdirectory, batch_size=128, to_tensor=True,
use_gpu=False):
super(CustomDataLoader, self).__init__()
self.image_subdirectory = image_subdirectory
self.batch_size = batch_size
assert to_tensor >= use_gpu
self.to_tensor = to_tensor
self.use_gpu = use_gpu
def _prepare_data(self, examples, labels):
""" Takes in numpy examples and labels and tensor-ifies and cuda's them
if necessary
"""
if self.to_tensor:
examples = torch.Tensor(examples)
labels = torch.Tensor(labels)
if self.use_gpu:
examples = examples.cuda()
labels = labels.cuda()
return (examples, labels)
def _base_loader(self, prefix, which):
assert which in ['examples', 'labels']
filename = '%s.%s.npy' % (prefix, which)
full_path = os.path.join(OUTPUT_IMAGE_DIR, self.image_subdirectory,
filename)
return np.load(full_path)
def _example_loader(self, prefix):
""" Loads the numpy array of examples given the random 'prefix' """
return self._base_loader(prefix, 'examples')
def _label_loader(self, prefix):
""" Loads the numpy array of labels given the random 'prefix' """
return self._base_loader(prefix, 'labels')
def __iter__(self):
# First collect all the filenames:
glob_prefix = os.path.join(OUTPUT_IMAGE_DIR, self.image_subdirectory,
'*')
files = glob.glob(glob_prefix)
valid_random_names = set(os.path.basename(_).split('.')[0]
for _ in files)
# Now loop through filenames and yield out minibatches of correct size
running_examples, running_labels = [], []
running_size = 0
for random_name in valid_random_names:
# Load data from files and append to 'running' lists
loaded_examples = self._example_loader(random_name)
loaded_labels = self._label_loader(random_name)
running_examples.append(loaded_examples)
running_labels.append(loaded_labels)
running_size += loaded_examples.shape[0]
if running_size < self.batch_size:
# Load enough data to populate one minibatch, which might
# take multiple files
continue
# Concatenate all images together
merged_examples = np.concatenate(running_examples, axis=0)
merged_labels = np.concatenate(running_labels, axis=0)
# Make minibatches out of concatenated things,
for batch_no in range(running_size // self.batch_size):
index_lo = batch_no * self.batch_size
index_hi = index_lo + self.batch_size
example_batch = merged_examples[index_lo:index_hi]
label_batch = merged_labels[index_lo:index_hi]
yield self._prepare_data(example_batch, label_batch)
# Handle any remainder for remaining files
remainder_idx = (running_size // self.batch_size) * self.batch_size
running_examples = [merged_examples[remainder_idx:]]
running_labels = [merged_labels[remainder_idx:]]
running_size = running_size - remainder_idx
# If we're out of files, yield this last sub-minibatch of data
if running_size > 0:
merged_examples = np.concatenate(running_examples, axis=0)
merged_labels = np.concatenate(running_labels, axis=0)
yield self._prepare_data(merged_examples, merged_labels)