-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot_surface.py
444 lines (382 loc) · 18.8 KB
/
plot_surface.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
"""
Calculate and visualize the loss surface.
Usage example:
>> python plot_surface.py --x=-1:1:101 --y=-1:1:101 --model resnet56 --cuda
"""
import argparse
import copy
import h5py
import time
import socket
import os
import sys
import numpy as np
from tensorflow import set_random_seed
import tensorflow.keras as keras
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras import backend as K
from tensorflow.keras.models import load_model
from numpy.random import seed
import tensorflow.keras as keras
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras import backend as K
from tensorflow.keras.models import load_model
from numpy.random import seed
from tensorflow import set_random_seed
import tensorflow.keras as keras
import time
import numpy as np
import torch
import os
import copy
import h5py
import net_plotter
import model_loader
import h5_util
from sklearn.decomposition import PCA
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt
from matplotlib import cm
import h5py
import argparse
import numpy as np
from os.path import exists
import seaborn as sns
from matplotlib import pyplot as pp
import h5py
import argparse
import numpy as np
import torchvision
import torch.nn as nn
import projection as proj
import net_plotter
import plot_2D
import plot_1D
import evaluation
import model_loader
import scheduler
def name_surface_file(args, dir_file):
# skip if surf_file is specified in args
if args.surf_file:
return args.surf_file
# use args.dir_file as the perfix
surf_file = dir_file
# resolution
surf_file += '_[%s,%s,%d]' % (str(args.xmin), str(args.xmax), int(args.xnum))
if args.y:
surf_file += 'x[%s,%s,%d]' % (str(args.ymin), str(args.ymax), int(args.ynum))
# dataloder parameters
if args.raw_data: # without data normalization
surf_file += '_rawdata'
if args.data_split > 1:
surf_file += '_datasplit=' + str(args.data_split) + '_splitidx=' + str(args.split_idx)
return surf_file + ".h5"
def get_data(folder):
"""
Load the data and labels from the given folder.
"""
image_list = [] # list of images
cell_label_list = [] # list of labels
for wbc_type in os.listdir(folder):
if not wbc_type.startswith('.'):
if wbc_type in ['galangal']:
cell_type_label = 1
elif wbc_type in ['ginger']:
cell_type_label = 2
else:
cell_type_label = 3
for image_filename in tqdm(os.listdir(folder + wbc_type)):
img_file = cv2.imread(folder + wbc_type + '/' + image_filename)
if img_file is not None:
img_arr = np.asarray(img_file)
image_list.append(img_arr)
cell_label_list.append(cell_type_label)
nucleus_list.append(nuclear_label)
return np.asarray(image_list), np.asarray(cell_label_list)
def setup_surface_file(args, surf_file, dir_file):
# skip if the direction file already exists
if os.path.exists(surf_file):
f = h5py.File(surf_file, 'r')
if (args.y and 'ycoordinates' in f.keys()) or 'xcoordinates' in f.keys():
f.close()
print ("%s is already set up" % surf_file)
return
f = h5py.File(surf_file, 'a')
f['dir_file'] = dir_file
# Create the coordinates(resolutions) at which the function is evaluated
xcoordinates = np.linspace(args.xmin, args.xmax, num=args.xnum)
f['xcoordinates'] = xcoordinates
if args.y:
ycoordinates = np.linspace(args.ymin, args.ymax, num=args.ynum)
f['ycoordinates'] = ycoordinates
f.close()
return surf_file
def reduce_max(comm, array, display_info=False):
if not comm:
return array
array = np.asarray(array, dtype='d')
total = np.zeros_like(array)
float_min = np.finfo(np.float).min
total.fill(float_min)
if display_info:
print ("(%d): sum=%f : size=%d"%(get_rank(comm), np.sum(array), array.nbytes))
rows = str(comm.gather(array.shape[0]))
cols = str(comm.gather(array.shape[1]))
print_once(comm, "reduce: %s, %s"%(rows, cols))
comm.Reduce(array, total, op=mpi4py.MPI.MAX, root=0)
return total
def crunch(surf_file, model, w, d, dataloader, loss_key, acc_key, comm, rank, args):
"""
Calculate the loss values and accuracies of modified models.
"""
f = h5py.File(surf_file, 'r+' if rank == 0 else 'r')
losses, accuracies = [], []
xcoordinates = f['xcoordinates'][:]
ycoordinates = f['ycoordinates'][:] if 'ycoordinates' in f.keys() else None
if loss_key not in f.keys():
shape = xcoordinates.shape if ycoordinates is None else (len(xcoordinates),len(ycoordinates))
losses = -np.ones(shape=shape)
accuracies = -np.ones(shape=shape)
if rank == 0:
f[loss_key] = losses
f[acc_key] = accuracies
else:
losses = f[loss_key][:]
accuracies = f[acc_key][:]
# Generate a list of indices of 'losses' that need to be filled in.
# The coordinates of each unfilled index (with respect to the direction vectors
# stored in 'd') are stored in 'coords'.
inds, coords, inds_nums = scheduler.get_job_indices(losses, xcoordinates, ycoordinates, comm)
print('Computing %d values for rank %d'% (len(inds), rank))
start_time = time.time()
total_sync = 0.0
criterion = tf.keras.metrics.categorical_crossentropy
if args.loss_name == 'mse':
criterion = tf.keras.losses.MeanSquaredError
# Loop over all uncalculated loss values
for count, ind in enumerate(inds):
# Get the coordinates of the loss value being calculated
coord = coords[count]
# Load the weights corresponding to those coordinates into the net
net_plotter.set_weights(model, w, d, coord)
# Record the time to compute the loss value
loss_start = time.time()
loss, acc = evaluation.eval_loss(model, dataloader)
loss_compute_time = time.time() - loss_start
# Record the result in the local array
losses.ravel()[ind] = loss
accuracies.ravel()[ind] = acc
# Send updated plot data to the master node
syc_start = time.time()
losses = reduce_max(comm, losses)
accuracies = reduce_max(comm, accuracies)
syc_time = time.time() - syc_start
total_sync += syc_time
# Only the master node writes to the file - this avoids write conflicts
if rank == 0:
f[loss_key][:] = losses
f[acc_key][:] = accuracies
f.flush()
print('Evaluating rank %d %d/%d (%.1f%%) coord=%s \t%s= %.3f \t%s=%.2f \ttime=%.2f \tsync=%.2f' % (
rank, count, len(inds), 100.0 * count/len(inds), str(coord), loss_key, loss,
acc_key, acc, loss_compute_time, syc_time))
# This is only needed to make MPI run smoothly. If this process has less work than
# the rank0 process, then we need to keep calling reduce so the rank0 process doesn't block
for i in range(max(inds_nums) - len(inds)):
losses = reduce_max(comm, losses)
accuracies = reduce_max(comm, accuracies)
total_time = time.time() - start_time
print('Rank %d done! Total time: %.2f Sync: %.2f' % (rank, total_time, total_sync))
f.close()
###############################################################
# MAIN
###############################################################
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='plotting loss surface')
parser.add_argument('--threads', default=2, type=int, help='number of threads')
parser.add_argument('--ngpu', type=int, default=1, help='number of GPUs to use for each rank, useful for data parallel evaluation')
parser.add_argument('--batch_size', default=128, type=int, help='minibatch size')
# data parameters
parser.add_argument('--dataset', default='cifar10', help='cifar10 | imagenet')
parser.add_argument('--datapath', default='cifar10/data', metavar='DIR', help='path to the dataset')
parser.add_argument('--raw_data', action='store_true', default=False, help='no data preprocessing')
parser.add_argument('--data_split', default=1, type=int, help='the number of splits for the dataloader')
parser.add_argument('--split_idx', default=0, type=int, help='the index of data splits for the dataloader')
parser.add_argument('--trainloader', default='', help='path to the dataloader with random labels')
parser.add_argument('--testloader', default='', help='path to the testloader with random labels')
# model parameters
parser.add_argument('--model', default='resnet56', help='model name')
parser.add_argument('--model_folder', default='', help='the common folder that contains model_file and model_file2')
parser.add_argument('--model_file', default='mnist_shallow_cnn.h5', help='path to the trained model file')
parser.add_argument('--model_file2', default='', help='use (model_file2 - model_file) as the xdirection')
parser.add_argument('--model_file3', default='', help='use (model_file3 - model_file) as the ydirection')
parser.add_argument('--loss_name', '-l', default='crossentropy', help='loss functions: crossentropy | mse')
# direction parameters
parser.add_argument('--dir_file', default='', help='specify the name of direction file, or the path to an eisting direction file')
parser.add_argument('--dir_type', default='weights', help='direction type: weights | states (including BN\'s running_mean/var)')
parser.add_argument('--x', default='-1:1:51', help='A string with format xmin:x_max:xnum')
parser.add_argument('--y', default='-1:1:51', help='A string with format ymin:ymax:ynum')
parser.add_argument('--xnorm', default='', help='direction normalization: filter | layer | weight')
parser.add_argument('--ynorm', default='', help='direction normalization: filter | layer | weight')
parser.add_argument('--xignore', default='', help='ignore bias and BN parameters: biasbn')
parser.add_argument('--yignore', default='', help='ignore bias and BN parameters: biasbn')
parser.add_argument('--same_dir', action='store_true', default=False, help='use the same random direction for both x-axis and y-axis')
parser.add_argument('--idx', default=0, type=int, help='the index for the repeatness experiment')
parser.add_argument('--surf_file', default='', help='customize the name of surface file, could be an existing file.')
# plot parameters
parser.add_argument('--proj_file', default='', help='the .h5 file contains projected optimization trajectory.')
parser.add_argument('--loss_max', default=5, type=float, help='Maximum value to show in 1D plot')
parser.add_argument('--vmax', default=10, type=float, help='Maximum value to map')
parser.add_argument('--vmin', default=0.1, type=float, help='Miminum value to map')
parser.add_argument('--vlevel', default=0.5, type=float, help='plot contours every vlevel')
parser.add_argument('--show', action='store_true', default=False, help='show plotted figures')
parser.add_argument('--log', action='store_true', default=False, help='use log scale for loss values')
parser.add_argument('--plot', action='store_true', default=False, help='plot figures after computation')
args = parser.parse_args()
seed(1)
set_random_seed(2)
#--------------------------------------------------------------------------
# Environment setup
#--------------------------------------------------------------------------
comm, rank, nproc = None, 0, 1
def session_options(enable_gpu_ram_resizing=True, enable_xla=True):
"""
Allowing the notebook to make use of GPUs if they're available.
XLA (Accelerated Linear Algebra) is a domain-specific compiler for linear
algebra that optimizes TensorFlow computations.
"""
config = tf.ConfigProto()
config.log_device_placement = True
if enable_gpu_ram_resizing:
# `allow_growth=True` makes it possible to connect multiple colabs to your
# GPU. Otherwise the colab malloc's all GPU ram.
config.gpu_options.allow_growth = True
if enable_xla:
# Enable on XLA. https://www.tensorflow.org/performance/xla/.
config.graph_options.optimizer_options.global_jit_level = (
tf.OptimizerOptions.ON_1)
return config
#--------------------------------------------------------------------------
# Check plotting resolution
#--------------------------------------------------------------------------
try:
args.xmin, args.xmax, args.xnum = [float(a) for a in args.x.split(':')]
args.ymin, args.ymax, args.ynum = (None, None, None)
if args.y:
args.ymin, args.ymax, args.ynum = [float(a) for a in args.y.split(':')]
assert args.ymin and args.ymax and args.ynum, 'You specified some arguments for the y axis, but not all'
except:
raise Exception('Improper format for x- or y-coordinates. Try something like -1:1:51')
#--------------------------------------------------------------------------
# Load models and extract parameters
#--------------------------------------------------------------------------
model = load_model(args.model_file)
w = net_plotter.get_weights(model) # initial parameters
#--------------------------------------------------------------------------
# Setup the direction file and the surface file
#--------------------------------------------------------------------------
dir_file = net_plotter.name_direction_file(args) # name the direction file
if rank == 0:
net_plotter.setup_direction(args, dir_file, model)
surf_file = name_surface_file(args, dir_file)
if rank == 0:
setup_surface_file(args, surf_file, dir_file)
# load directions
d = net_plotter.load_directions(dir_file)
# calculate the consine similarity of the two directions
if len(d) == 2 and rank == 0:
similarity = proj.cal_angle(proj.nplist_to_tensor(d[0]), proj.nplist_to_tensor(d[1]))
print('cosine similarity between x-axis and y-axis: %f' % similarity)
#--------------------------------------------------------------------------
# Setup dataloader
#--------------------------------------------------------------------------
if rank == 0 and args.dataset == 'cifar10':
num_classes = 10
# input image dimensions
img_rows, img_cols = 31, 31
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 3, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 3, img_rows, img_cols)
input_shape = (3, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 3)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 3)
input_shape = (img_rows, img_cols, 3)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
elif rank == 0 and args.dataset == 'mnist':
num_classes = 10
# input image dimensions
img_rows, img_cols = 28, 28
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
# convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
elif rank == 0 and args.dataset == 'ginger3':
# These are the in-memory representations of the training and test datasets
x_train, y_train = get_data('train')
x_test, y_test = get_data('test')
x_train = x_train / 255
x_test = x_test / 255
# Encode labels to hot vectors (ex : 2 -> [0,0,1,0,0,0,0,0,0,0])
y_train = keras.utils.to_categorical(y_train, num_classes = 3)
y_test = keras.utils.to_categorical(y_test, num_classes = 3)
elif rank == 0 and args.dataset == 'padova':
x_train = np.load('X_1_1k_train.py')
x_test = np.load('X_1_1k_train.py')
y_train = np.load('y_1_1k_test.py')
y_test = np.load('y_1_1k_test.py')
elif rank == 0 and args.dataset == 'gaitnet':
x_train = np.load('X_1_1k_train.py')
x_test = np.load('X_1_1k_train.py')
y_train = np.load('y_1_1k_test.py')
y_test = np.load('y_1_1k_test.py')
trainloader = {'x': x_train, 'y': y_train}
#--------------------------------------------------------------------------
# Start the computation
#--------------------------------------------------------------------------
crunch(surf_file=surf_file, model=model, w=w, d=d, dataloader=trainloader,
loss_key='train_loss', acc_key='train_acc',
comm=comm, rank=rank, args=args)
#--------------------------------------------------------------------------
# Plot figures
#--------------------------------------------------------------------------
if args.plot and rank == 0:
if args.y and args.proj_file:
plot_2D.plot_contour_trajectory(surf_file, dir_file, args.proj_file, 'train_loss', args.show)
elif args.y:
plot_2D.plot_2d_contour(surf_file, 'train_loss', args.vmin, args.vmax, args.vlevel, args.show)
else:
plot_1D.plot_1d_loss_err(surf_file, args.xmin, args.xmax, args.loss_max, args.log, args.show)