-
Notifications
You must be signed in to change notification settings - Fork 3
/
MI_image.py
234 lines (188 loc) · 8.39 KB
/
MI_image.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
import os
import time
import numpy as np
from tqdm import tqdm
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset
import torchvision
import torchvision.transforms as transforms
import utils
import models
from PMI import PMI
class PMI_image(PMI):
def init_model_optimizer(self):
print('Initializing Model and Optimizer...')
self.compressor = models.__dict__['image_decoder_%d' % self.args.image_size](
nc=self.args.nc,
size=self.args.size,
dim=self.args.dim
)
self.critic_f = models.Estimator(
nc=self.args.image_nc,
size=self.args.image_size,
dim=self.args.dim,
hidden_dim=self.args.hidden_dim,
output_dim=self.args.output_dim,
n_layer=self.args.n_layer,
mode='conv'
)
self.logger.log_info('Compressor')
self.logger.log_info(self.compressor)
self.logger.log_info('Critic')
self.logger.log_info(self.critic_f)
self.compressor = self.compressor.cuda()
self.critic_f = self.critic_f.cuda()
self.optim = torch.optim.Adam(
list(self.critic_f.parameters()) +\
list(self.compressor.parameters()),
lr=self.args.lr,
betas=(self.args.beta1, 0.999)
)
def quantify(self, trace, secret):
self.compressor.eval()
self.critic_f.eval()
with torch.no_grad():
compressed = self.compressor(trace)
pos_scores = self.critic_f((recovered, secret))
pos_logits = F.sigmoid(pos_scores)
pmi = torch.log(pos_logits / (1 - pos_logits)) / np.log(2)
pmi /= (np.log(self.m_size) / np.log(2))
pmi = torch.clamp(pmi, 0, 1).mean()
return pmi
def process_grad(self, trace, secret):
self.zero_grad()
trace = trace.to(config.DEVICE)
secret = secret.to(config.DEVICE)
batch_size = trace.size(0)
grad_saver = utils.GradSaver()
compressed = self.compressor(trace)
pos_scores = self.critic_f((compressed, secret))
ones = torch.ones(pos_scores.size()).to(config.DEVICE)
pos_logits = F.sigmoid(pos_scores)
loss = self.bce_log(pos_scores, ones)
loss.backward()
gradient = grad_saver.grad.detach().abs().view(batch_size, -1)
return gradient
def fit(self, data_loader):
with torch.autograd.set_detect_anomaly(True):
self.epoch += 1
self.set_train()
record = utils.Record()
start_time = time.time()
for i, (trace, secret, name) in enumerate(tqdm(data_loader)):
self.zero_grad()
trace = trace.cuda()
secret = secret.cuda()
batch_size = trace.size(0)
random_index = torch.randperm(batch_size).long()
compressed = self.compressor(trace)
pos_scores = self.critic_f((compressed, secret))
neg_scores = self.critic_f((compressed, secret[random_index]))
ones = torch.ones(pos_scores.size()).cuda()
zeros = torch.zeros(neg_scores.size()).cuda()
pos_logits = F.sigmoid(pos_scores)
neg_logits = F.sigmoid(neg_scores)
loss = self.bce_log(pos_scores, ones) + self.bce_log(neg_scores, zeros)
with torch.no_grad():
MI = torch.log(pos_logits / (1 - pos_logits)) / np.log(2)
MI /= (np.log(batch_size) / np.log(2))
pmi = torch.clamp(MI, 0, 1).mean()
record.add(pmi.item())
loss.backward()
self.optim.step()
self.logger.log_info('----------------------------------------')
self.logger.log_info('Fitting Epoch: %d' % self.epoch)
self.logger.log_info('Costs Time: %.2f s' % (time.time() - start_time))
self.logger.log_info('Leakage Ratio: %f' % (record.mean()))
self.logger.log_info('----------------------------------------')
def validate(self, data_loader):
with torch.no_grad():
self.set_eval()
record = utils.Record()
start_time = time.time()
for i, (trace, secret, name) in enumerate(tqdm(data_loader)):
trace = trace.cuda()
secret = secret.cuda()
batch_size = trace.size(0)
m_size = self.args.batch_size
random_index = torch.randperm(batch_size).long()
compressed = self.compressor(trace)
pos_scores = self.critic_f((compressed, secret))
neg_scores = self.critic_f((compressed, secret[random_index]))
ones = torch.ones(pos_scores.size()).cuda()
zeros = torch.zeros(neg_scores.size()).cuda()
pos_logits = F.sigmoid(pos_scores)
neg_logits = F.sigmoid(neg_scores)
loss = self.bce_log(pos_scores, ones) + self.bce_log(neg_scores, zeros)
MI = torch.log(pos_logits / (1 - pos_logits)) / np.log(2)
MI /= (np.log(m_size) / np.log(2))
pmi = torch.clamp(MI, 0, 1).mean()
record.add(pmi.item())
self.logger.log_info('----------------------------------------')
self.logger.log_info('Validation.')
self.logger.log_info('Costs Time: %.2f s' % (time.time() - start_time))
self.logger.log_info('Leakage Ratio: %f' % (record.mean()))
self.logger.log_info('----------------------------------------')
if __name__ == '__main__':
import sys
import argparse
import random
import json
import torch
import torch.nn as nn
import torch.nn.functional as F
import utils
from data_loader import DataLoader
from dataset import *
from params import Params
args = Params().parse()
if len(args.side) > 0:
args.exp_name = 'mi-%s-%s-%s' % (args.software, args.setting, args.side)
else:
args.exp_name = 'mi-%s-%s' % (args.software, args.setting)
print(args.exp_name)
args.image_dir = config.image_dir
args.npz_dir = os.path.join(config.trace_dir, args.setting, args.software)
(args.size, args.nc) = PADLENGTH['%s-%s' % (args.software, args.setting)]
fp = open(os.path.join(config.output_dir, args.exp_name, 'log.txt'), 'a')
logger = utils.Logger(fp)
manual_seed = random.randint(1, 10000)
logger.log_info('Manual Seed: %d' % manual_seed)
random.seed(manual_seed)
torch.manual_seed(manual_seed)
torch.cuda.manual_seed_all(manual_seed)
utils.make_path(os.path.join(config.output_dir, args.exp_name))
args.ckpt_dir = os.path.join(config.output_dir, args.exp_name, 'ckpt')
utils.make_path(args.ckpt_dir)
with open(os.path.join(config.output_dir, args.exp_name, 'args.json'), 'a') as f:
json.dump(args.__dict__, f)
loader = utils.DataLoader(args)
fit_dataset = ImageDatasetMulti(
args,
image_dir=args.image_dir,
npz_dir=args.npz_dir,
image_split='fit',
npz_split_list=['1_fit', '2_fit', '3_fit', '4_fit']
)
val_dataset = ImageDatasetMulti(
args,
image_dir=args.image_dir,
npz_dir=args.npz_dir,
image_split='val',
npz_split_list=['1_val']
)
'''
Due to the generalizability consideration, we still do
validation for image cases if the traces are deterministic
'''
fit_loader = loader.get_loader(fit_dataset)
val_loader = loader.get_loader(val_dataset, False)
engine = PMI_image(args, logger)
for i in range(engine.epoch, args.num_epoch):
engine.fit(fit_loader)
if i % args.test_freq == 0:
engine.validate(val_loader)
engine.save_model((args.ckpt_dir + '%03d.pth') % (i + 1))
engine.save_model((args.ckpt_dir + 'final.pth'))