-
Notifications
You must be signed in to change notification settings - Fork 0
/
LossPlot.py
147 lines (139 loc) · 5.51 KB
/
LossPlot.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
import os
import numpy as np
import matplotlib.pyplot as plt
# sliding smoothing
def slide_smooth(train_loss, window = 2):
new_loss = np.zeros(train_loss.shape)
for t in range(new_loss.shape[1]):
window_sum = train_loss[:,t:t+window]
#print(window_sum.shape[1])
new_loss[:,t] = np.sum(window_sum, axis = 1)/window_sum.shape[1]
#break
return new_loss
# tensorboard smoothing
def tensor_smooth(train_loss, weight = 0.2):
new_loss = np.zeros(train_loss.shape)
new_loss[:,0] = train_loss[:,0]
for t in range(1,new_loss.shape[1]):
new_loss[:,t] = train_loss[:,t-1] * weight + train_loss[:,t] * (1-weight)
return new_loss
# gaussian noise
def gaussian_smooth(train_loss, std_ratio = 1):
new_loss = np.zeros(train_loss.shape)
for t in range(new_loss.shape[1]):
current_std = np.std(train_loss[:,t])
#print(current_std)
layer_noise = np.random.normal(0, std_ratio * current_std, train_loss.shape[0])
#print(window_sum.shape[1])
new_loss[:,t] = train_loss[:,t] + layer_noise
#break
return new_loss
# original plots
TOT_SAMPLE = 1000
split_idx = int(TOT_SAMPLE/4*3)
dataset = 'CIFAR'
train_path = './data/lossplot/' + dataset + '/train/'
test_path = './data/lossplot/' + dataset + '/test/'
#train_path = './data/lossplot/' + dataset + '_waxis/train/'
#test_path = './data/lossplot/' + dataset + '_waxis/test/'
inference_targets = ['ResNet18', 'ResNet34', 'ResNet50', 'MobileV2', 'MobileV3L', 'DenseNet121']
plt_ymin = 0
plt_ymax = 3.2
epoch_limit = 25
test_limit = int(epoch_limit/5)
for model_name in inference_targets:
print(model_name)
train_save = train_path + model_name + '/'
test_save = test_path + model_name + '/'
if not os.path.exists(train_save):
os.makedirs(train_save)
if not os.path.exists(test_save):
os.makedirs(test_save)
train_loss = np.load('./data/loss/TrainLoss_'+dataset+'_'+model_name+'.npy')[:,:epoch_limit]
test_loss = np.load('./data/loss/TestLoss_'+dataset+'_'+model_name+'.npy')[:,:test_limit]
# train
for idx in range(split_idx):
ytrain = train_loss[idx]
xtrain = np.arange(ytrain.shape[0])
ytest = test_loss[idx]
xtest = np.arange(1,ytest.shape[0]+1) * (ytrain.shape[0]/ytest.shape[0]) - 1
plt.figure(figsize=(3,3))
#plt.ylim([plt_ymin, plt_ymax])
plt.yticks(np.arange(plt_ymin, plt_ymax, 0.5))
plt.xlim([-1, epoch_limit + 1])
plt.xticks(np.arange(0, epoch_limit + 1, 5))
plt.plot(xtrain,ytrain)
plt.plot(xtest,ytest)
plt.axis('off')
plt.savefig(train_save + model_name + '_' +str(idx)+'.png', dpi = 50)
plt.close()
#break
# test
for idx in range(split_idx, TOT_SAMPLE):
ytrain = train_loss[idx]
xtrain = np.arange(ytrain.shape[0])
ytest = test_loss[idx]
xtest = np.arange(1,ytest.shape[0]+1) * (ytrain.shape[0]/ytest.shape[0]) - 1
plt.figure(figsize=(3,3))
#plt.ylim([plt_ymin, plt_ymax])
plt.yticks(np.arange(plt_ymin, plt_ymax, 0.5))
plt.xlim([-1, epoch_limit + 1])
plt.xticks(np.arange(0, epoch_limit + 1, 5))
plt.plot(xtrain,ytrain)
plt.plot(xtest,ytest)
plt.axis('off')
plt.savefig(test_save + model_name + '_'+ str(idx)+'_.png', dpi = 50)
plt.close()
#break
# defense plots
TOT_SAMPLE = 500
split_idx = int(TOT_SAMPLE/4*3)
dataset = 'SVHN'
method = 'slide' #
defense_methods = {'slide': slide_smooth, 'tensor': tensor_smooth, 'gaussian': gaussian_smooth}
#train_path = './data/lossplot/defense/' + dataset + '/train/'
test_path = './data/lossplot/defense/' + dataset + '_waxis/' + method + '/'
#train_path = './data/lossplot/' + dataset + '_waxis/train/'
#test_path = './data/lossplot/' + dataset + '_waxis/test/'
plt_ymin = 0
plt_ymax = 3.2
epoch_limit = 25
test_limit = int(epoch_limit/5)
norms = []
for model_name in ['ResNet18', 'ResNet34', 'ResNet50', 'MobileV2', 'MobileV3L', 'DenseNet121']:
print(model_name)
#train_save = train_path + model_name + '/'
test_save = test_path + model_name + '/'
#if not os.path.exists(train_save):
# os.makedirs(train_save)
if not os.path.exists(test_save):
os.makedirs(test_save)
train_loss = np.load('./data/loss/TrainLoss_'+dataset+'_'+model_name+'.npy')[:,:epoch_limit]
test_loss = np.load('./data/loss/TestLoss_'+dataset+'_'+model_name+'.npy')[:,:test_limit]
new_loss = defense_methods[method](train_loss)
#new_loss = slide_smooth(train_loss,2)
#new_loss = tensor_smooth(train_loss,0.2)
#new_loss = gaussian_smooth(train_loss,1)
l2_norm = np.linalg.norm(train_loss - new_loss, axis = 1)
print(np.mean(l2_norm))
norms.append(np.mean(l2_norm))
train_loss = new_loss
# test
for idx in range(split_idx, TOT_SAMPLE):
ytrain = train_loss[idx]
xtrain = np.arange(ytrain.shape[0])
ytest = test_loss[idx]
xtest = np.arange(1,ytest.shape[0]+1) * (ytrain.shape[0]/ytest.shape[0]) - 1
plt.figure(figsize=(3,3))
#plt.ylim([plt_ymin, plt_ymax])
plt.yticks(np.arange(plt_ymin, plt_ymax, 0.5))
plt.xlim([-1, epoch_limit + 1])
plt.xticks(np.arange(0, epoch_limit + 1, 5))
plt.plot(xtrain,ytrain)
plt.plot(xtest,ytest)
#plt.axis('off')
plt.savefig(test_save + model_name + '_'+ str(idx)+'_.png', dpi = 50)
plt.close()
#break
np.save(dataset + '_' + method, np.asarray(norms))
print('AVG L2: ', np.mean(norms))