-
Notifications
You must be signed in to change notification settings - Fork 1
/
LitModel.py
354 lines (296 loc) · 12.8 KB
/
LitModel.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
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import pandas as pd
import numpy as np
import pickle
import argparse
from collections import OrderedDict
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Optional, Generator, Union
import torch
import torch.nn.functional as F
from torch import optim
from torch.nn import Module
from torch.utils.data import DataLoader
import pytorch_lightning as pl
from pytorch_lightning import _logger as log
import random
from retriever import *
import SRNet
from pytorch_lightning.metrics.converters import _sync_ddp_if_available
class LitModel(pl.LightningModule):
"""Transfer Learning
"""
def __init__(self,
data_path: Union[str, Path],
backbone: str = 'OneHotSRNet',
batch_size: int = 16,
lr: float = 1e-3,
eps: float = 1e-8,
lr_scheduler_name: str = 'MultiStepLR',
qf: str = 'QF100',
optimizer_name: str = 'Adamax',
num_workers: int = 6,
epochs: int = 300,
milestone: int = 150,
gpus: int = 1,
weight_decay: float = 1e-4,
payload: str = '0.1_bpnzac',
stego_scheme: str = 'nsf5_simulation',
loss_weights: list = [1.0, 1.0, 1.0],
threshold: int = 5
,**kwargs) -> None:
super().__init__()
self.data_path = data_path
self.epochs = epochs
self.milestone = milestone
self.backbone = backbone
self.batch_size = batch_size
self.lr = lr
self.stego_scheme = stego_scheme
self.payload = payload
self.qf = qf
self.num_workers = num_workers
self.lr_scheduler_name = lr_scheduler_name
self.optimizer_name = optimizer_name
self.gpus = gpus
self.weight_decay = weight_decay
self.eps = eps
self.threshold = threshold
self.loss_weights = loss_weights
self.save_hyperparameters()
self.data_path = Path(self.data_path)/self.qf
self.__build_model()
def __build_model(self):
"""Define model layers & loss."""
self.net = getattr(SRNet, self.backbone)(1, 2, self.threshold)
self.loss_func = F.cross_entropy
def forward(self, x, x_dct):
"""Forward pass. Returns logits."""
logits = self.net(x, x_dct)
return logits
def loss(self, logits, labels, loss_weights):
labels = torch.flatten(labels)
losses = [self.loss_func(logits[i], labels) for i in range(3)]
loss = sum(l*w for l,w in zip(losses,loss_weights))
return losses+[loss] #torch.cat((losses,loss.reshape(1)))
@staticmethod
def __accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def training_step(self, batch, batch_idx):
# 1. Forward pass:
x, x_dct, y = batch
logits = self.forward(x, x_dct)
# 2. Compute loss & accuracy:
srnet_loss, oh_loss, fc_loss, train_loss = self.loss(logits, y, self.loss_weights)
#train_loss.requires_grad = True
losses = {'trn_SRNet_loss': srnet_loss,
'trn_OH_loss': oh_loss,
'trn_FC_loss': fc_loss}
metrics = {'trn_SRNet_acc': self.__accuracy(logits[0], y)[0],
'trn_OH_acc': self.__accuracy(logits[1], y)[0],
'trn_FC_acc': self.__accuracy(logits[2], y)[0]
}
# 3. Outputs:
output = OrderedDict({'loss': train_loss,
'progress_bar': losses,
'log': {**metrics, **losses}})
return output
def validation_step(self, batch, batch_idx):
# 1. Forward pass:
x, x_dct, y = batch
logits = self.forward(x, x_dct)
# 2. Compute loss & accuracy:
srnet_loss, oh_loss, fc_loss, val_loss = self.loss(logits, y, self.loss_weights)
losses = {'val_SRNet_loss': srnet_loss,
'val_OH_loss': oh_loss,
'val_FC_loss': fc_loss,
'val_loss': val_loss}
metrics = {'val_SRNet_acc': self.__accuracy(logits[0], y)[0],
'val_OH_acc': self.__accuracy(logits[1], y)[0],
'val_FC_acc': self.__accuracy(logits[2], y)[0]
}
return {**metrics, **losses}
def validation_epoch_end(self, outputs):
"""Compute and log training loss and accuracy at the epoch level."""
metrics = {}
for key in outputs[0].keys():
metrics[key] = torch.stack([output[key] for output in outputs]).mean()
metrics[key] = _sync_ddp_if_available(metrics[key], reduce_op='avg')
metrics['step'] = self.current_epoch
return {'log': metrics}
def configure_optimizers(self):
optimizer = getattr(torch.optim, self.optimizer_name)
optimizer = optimizer(self.parameters(),
lr=self.lr,
weight_decay=self.weight_decay,
eps=self.eps)
scheduler = getattr(torch.optim.lr_scheduler, self.lr_scheduler_name)
scheduler = scheduler(optimizer, milestones=[self.milestone])
interval = 'epoch'
return [optimizer], [{'scheduler': scheduler, 'interval': interval, 'name': 'lr'}]
def prepare_data(self):
"""Download images and prepare images datasets."""
print('Data Preparation is not part of this script, make sure your datasets are ready')
def setup(self, stage: str):
stego = self.stego_scheme+'_'+self.payload
cover_dir = self.data_path/'COVER'
IL_train = os.listdir(cover_dir/'TRN')
IL_val = os.listdir(cover_dir/'VAL')
dataset = []
for path in IL_train:
dataset.append({
'kind': ('COVER', stego),
'image_name': path,
'label': (0,1),
'fold': 'TRN',
})
for path in IL_val:
dataset.append({
'kind': ('COVER', stego),
'image_name': path,
'label': (0,1),
'fold': 'VAL',
})
random.shuffle(dataset)
dataset = pd.DataFrame(dataset)
self.train_dataset = TrainRetrieverPaired(
data_path=self.data_path,
kinds=dataset[dataset['fold'] != 'VAL'].kind.values,
folds=dataset[dataset['fold'] != 'VAL'].fold.values,
image_names=dataset[dataset['fold'] != 'VAL'].image_name.values,
labels=dataset[dataset['fold'] != 'VAL'].label.values,
transforms=True,
num_classes=2,
T=self.threshold,
)
self.valid_dataset = TrainRetrieverPaired(
data_path=self.data_path,
kinds=dataset[dataset['fold'] == 'VAL'].kind.values,
folds=dataset[dataset['fold'] == 'VAL'].fold.values,
image_names=dataset[dataset['fold'] == 'VAL'].image_name.values,
labels=dataset[dataset['fold'] == 'VAL'].label.values,
transforms=False,
num_classes=2,
T=self.threshold,
)
def __dataloader(self, train):
"""Train/validation loaders."""
_dataset = self.train_dataset if train else self.valid_dataset
def collate_fn(data):
images, images_dct, labels = zip(*data)
images = torch.cat(images)
labels = torch.cat(labels)
images_dct = torch.cat(images_dct)
return images, images_dct, labels
loader = DataLoader(dataset=_dataset,
batch_size=self.batch_size,
num_workers=self.num_workers,
collate_fn=collate_fn,
shuffle=True if train else False)
return loader
def train_dataloader(self):
log.info('Training data loaded.')
return self.__dataloader(train=True)
def val_dataloader(self):
log.info('Validation data loaded.')
return self.__dataloader(train=False)
@staticmethod
def add_model_specific_args(parent_parser):
parser = argparse.ArgumentParser(parents=[parent_parser])
parser.add_argument('--backbone',
default='OneHotSRNet',
type=str,
metavar='BK',
help='Name as in the SRNet.py file')
parser.add_argument('--data-path',
default='/media/ONEHOT-DCT/BOSS/JPEG_standard/',
type=str,
metavar='dp',
help='data_path')
parser.add_argument('--stego-scheme',
default='nsf5_simulation',
type=str,
help='Stego scheme')
parser.add_argument('--payload',
default='0.1_bpnzac',
type=str,
help='Payload')
parser.add_argument('--epochs',
default=300,
type=int,
metavar='N',
help='total number of epochs')
parser.add_argument('--milestone',
default=150,
type=int,
help='drop LR milestone')
parser.add_argument('--batch-size',
default=16,
type=int,
metavar='B',
help='batch size',
dest='batch_size')
parser.add_argument('--threshold',
type=int,
default=5,
help='DCT domain threshold')
parser.add_argument('--gpus',
type=int,
default=1,
help='number of gpus to use')
parser.add_argument('--lr',
'--learning-rate',
default=1e-3,
type=float,
metavar='LR',
help='initial learning rate',
dest='lr')
parser.add_argument('--loss-weights',
default=[1.0, 1.0, 1.0],
nargs='+',
type=float,
help='loss weights')
parser.add_argument('--eps',
default=1e-8,
type=float,
help='eps for adaptive optimizers',
dest='eps')
parser.add_argument('--num-workers',
default=6,
type=int,
metavar='W',
help='number of CPU workers',
dest='num_workers')
parser.add_argument('--lr-scheduler-name',
default='MultiStepLR',
type=str,
metavar='LRS',
help='Name of LR scheduler')
parser.add_argument('--optimizer-name',
default='Adamax',
type=str,
metavar='OPTI',
help='Name of optimizer')
parser.add_argument('--qf',
default='QF100',
type=str,
help='quality factor')
parser.add_argument('--weight-decay',
default=1e-4,
type=float,
metavar='wd',
help='Optimizer weight decay')
return parser