Repository navigation
Expand file tree
/
Copy pathrun_aug.py
More file actions
343 lines (255 loc) · 9.9 KB
/
Copy pathrun_aug.py
File metadata and controls
343 lines (255 loc) · 9.9 KB
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
from logging import Logger
import os
from threading import Timer
from lookahead import Lookahead
import numpy as np
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
from sklearn import metrics
from model import *
from dataset import *
import torch.cuda.amp as amp
is_amp = True #True #False
import torch
from torch.nn.parallel.data_parallel import data_parallel
from matplotlib import pyplot as plt
from utils import *
import time
from tqdm import tqdm
from sklearn.model_selection import StratifiedGroupKFold, KFold # Sklearn
#################################################################################################
torch.backends.cudnn.enabled = False
def do_valid(net, valid_loader):
valid_num = 0
valid_loss = 0
valid_probability = []
valid_truth = []
net = net.eval()
start_timer = time.time()
for t, batch in enumerate(tqdm(valid_loader)):
net.output_type = ['loss', 'inference']
with torch.no_grad():
with amp.autocast(enabled = is_amp):
batch_size = len(batch['index'])
for k in ['image', 'label' ]: batch[k] = batch[k].cuda()
output = data_parallel(net, batch) #net(input)#
loss0 = output['bce_loss'].mean()
# loss1 = output['focal_loss'].mean()
valid_num += 1
valid_loss += loss0.item()
valid_truth.append(batch['label'].data.cpu().numpy())
valid_probability.append(output['label'].data.cpu().numpy())
#---
# print('\r %8d / %d %s'%(valid_num, len(valid_loader),time_to_str(time.time() - start_timer,'sec')),end='',flush=True)
#if valid_num==200*4: break
# pdb.set_trace()
#assert(valid_num == len(valid_loader.dataset))
truth = np.concatenate(valid_truth)
probability = np.concatenate(valid_probability)
pred_tampers = probability[truth == 1]
pred_untampers = probability[truth == 0]
thres = np.percentile(pred_tampers, np.arange(90, 100, 1))
recall = np.mean(np.greater(pred_tampers[:, np.newaxis], thres).mean(axis=0))
#------
loss = valid_loss/valid_num
score = recall * 100
pf1 = pfbeta(truth, probability, beta=1)
AUC = metrics.roc_auc_score(truth, probability)
return [loss, pf1, AUC, score, thres[0]]
##----------------
def run_train():
tampered_img_paths = "dataset/train/tampered/ensemble"
untampered_img_paths = "dataset/train/untampered/"
col_name = ['img_name', 'img_path', 'img_label']
imgs_info = [] # img_name, img_path, img_label
for img_name in os.listdir(tampered_img_paths):
if img_name.endswith('.jpg'): # pass other files
imgs_info.append(["p_"+img_name, os.path.join(tampered_img_paths, img_name), np.ones(1).astype(np.float32)])
for img_name in os.listdir(untampered_img_paths):
if img_name.endswith('.jpg'): # pass other files
imgs_info.append(["n_"+img_name, os.path.join(untampered_img_paths, img_name), np.zeros(1).astype(np.float32)])
imgs_info_array = np.array(imgs_info)
df = pd.DataFrame(imgs_info_array, columns=col_name)
root_dir = '/hpc/data/home/bme/zhangzb1/Kaggle/Tianchi/classification/'
out_dir = root_dir + '/result/0306/effb6-1024'
initial_checkpoint = None
n_fold = 5
kf = KFold(n_splits=n_fold, shuffle=True, random_state=4307)
for fold, (train_idx, val_idx) in enumerate(kf.split(df)):
df.loc[val_idx, 'fold'] = fold
for fold in range(n_fold):
fold_dir = out_dir + f'/fold-{fold}'
print(f'#'*40, flush=True)
print(f'###### Fold: {fold}', flush=True)
print(f'#'*40, flush=True)
train_df, valid_df = make_fold(df, fold)
train_dataset = DTTDataset(train_df, augment=train_augment_v00)
valid_dataset = DTTDataset(valid_df, augment=None)
# pdb.set_trace()
start_lr = 1e-4 #0.0001
batch_size = 12 #32 #32
train_loader = DataLoader(train_dataset,
sampler = BalanceSampler(train_dataset, batch_size//1),
batch_size = batch_size,
drop_last = False,
num_workers = 8,
pin_memory = False,
worker_init_fn = lambda id: np.random.seed(torch.initial_seed() // 2 ** 32 + id),
collate_fn = null_collate,
)
valid_loader = DataLoader(valid_dataset,
sampler = SequentialSampler(valid_dataset),
batch_size = batch_size * 2,
drop_last = False,
num_workers = 8,
pin_memory = False,
collate_fn = null_collate,
)
## setup ----------------------------------------
for f in ['checkpoint','train','valid','backup'] : os.makedirs(fold_dir +'/'+f, exist_ok=True)
log = open(fold_dir+'/log_train.txt',mode='a')
log.write(f'\tfold_dir = {fold_dir}\n' )
log.write('\n')
## dataset ----------------------------------------
log.write('** dataset setting **\n')
log.write(f'fold = {fold}\n')
log.write(f'train_dataset : \n{train_dataset}\n')
log.write(f'valid_dataset : \n{valid_dataset}\n')
log.write('\n')
## net ----------------------------------------
log.write('** net setting **\n')
scaler = amp.GradScaler(enabled = is_amp)
net = Net().cuda()
if initial_checkpoint is not None:
f = torch.load(initial_checkpoint, map_location=lambda storage, loc: storage)
start_iteration = f['iteration']
start_epoch = f['epoch']
# start_iteration = 0
# start_epoch = 0
state_dict = f['state_dict']
net.load_state_dict(state_dict,strict=False) #True
else:
start_iteration = 0
start_epoch = 0
net.load_pretrain()
log.write('\tinitial_checkpoint = %s\n' % initial_checkpoint)
log.write('\n')
# optimizer = Lookahead(torch.optim.RAdam(filter(lambda p: p.requires_grad, net.parameters()),lr=start_lr))
optimizer = torch.optim.RAdam(filter(lambda p: p.requires_grad, net.parameters()), lr=start_lr, betas=(0.9, 0.999))
scheduler = get_scheduler(optimizer)
# optimizer = Lookahead(RAdam(filter(lambda p: p.requires_grad, net.parameters()),lr=start_lr), alpha=0.5, k=5)
log.write('optimizer\n %s\n'%(optimizer))
log.write('\n')
num_iteration = 20*len(train_loader)
iter_log = int(len(train_loader)*1) #479
iter_valid = iter_log
iter_save = iter_log
## start training here! ##############################################
log.write('** start training here! **\n')
log.write(' batch_size = %d \n'%(batch_size))
log.write(' experiment = %s\n' % str(__file__.split('/')[-2:]))
log.write(' |-------------- VALID---------|---- TRAIN/BATCH ----------------\n')
log.write('rate iter epoch | loss pF1 AUC score thres | loss | time \n')
log.write('-------------------------------------------------------------------------------------\n')
def message(mode='print'):
asterisk = ' '
if mode==('print'):
loss = batch_loss
if mode==('log'):
loss = train_loss
if (iteration % iter_save == 0): asterisk = '*'
text = \
('%0.2e %08d%s %6.2f | '%(rate, iteration, asterisk, epoch,)).replace('e-0','e-').replace('e+0','e+') + \
'%4.4f %4.4f %4.4f %4.4f %4.4f | '%(*valid_loss,) + \
'%4.4f %4.4f %4.3f | ' % (*loss,) + \
'%s' % (time_to_str(time.time() - start_timer,'min'))
return text
#----
valid_loss = np.zeros(5,np.float32)
# valid_train_loss = np.zeros(3,np.float32)
train_loss = np.zeros(3,np.float32)
batch_loss = np.zeros_like(train_loss)
sum_train_loss = np.zeros_like(train_loss)
sum_train = 0
start_timer = time.time()
iteration = start_iteration
epoch = start_epoch
rate = 0
while iteration < num_iteration:
for t, batch in enumerate((train_loader)):
# if ( iteration%iter_valid==0):
# # pdb.set_trace()
# valid_loss = do_valid(net, valid_loader)
# valid_train_loss = do_valid(net, train_loader)
# pass
if iteration%iter_save==0 and iteration != start_iteration:
torch.save({
'state_dict': net.state_dict(),
'iteration': iteration,
'epoch': epoch,
}, f'{fold_dir}/checkpoint/{epoch}.model.pth')
pass
if (iteration > 0 and iteration%iter_valid==0):
# pdb.set_trace()
valid_loss = do_valid(net, valid_loader)
# valid_train_loss = do_valid(net, train_loader)
pass
if (iteration%iter_log==0) or (iteration%iter_valid==0):
print('\r', end='', flush=True)
log.write(message(mode='log') + '\n')
rate = get_learning_rate(optimizer)
# one iteration update -------------
batch_size = len(batch['index'])
# print(batch_size)
for k in ['image', 'label' ]: batch[k] = batch[k].cuda()
net.train()
net.output_type = ['loss', 'inference']
if 1:
with amp.autocast(enabled = is_amp):
output = data_parallel(net,batch)
loss0 = output['bce_loss'].mean()
# loss1 = output['focal_loss'].mean()
optimizer.zero_grad()
scaler.scale(loss0).backward()
scaler.unscale_(optimizer)
scaler.step(optimizer)
scaler.update()
# print statistics --------
batch_loss[:3] = [loss0.item(), 0, 0]
sum_train_loss += batch_loss
sum_train += 1
if t % 100 == 0:
train_loss = sum_train_loss / (sum_train + 1e-12)
sum_train_loss[...] = 0
sum_train = 0
print('\r', end='', flush=True)
print(message(mode='print'), end='', flush=True)
epoch += 1 / len(train_loader)
iteration += 1
# debug --------
if 0:
image = batch['image'].float().data.cpu().numpy()
truth = batch['mask'].long().data.cpu().numpy()
predict = output['mask'].float().data.cpu().numpy()
image = np.ascontiguousarray(image.transpose(0,2,3,1))
predict = np.ascontiguousarray(predict.transpose(0,2,3,1))
for b in range(batch_size):
m = image[b]
p = predict[b]
t = truth[b,0]
onehot = np.ascontiguousarray(np.eye(4)[t][...,1:])
p = np.ascontiguousarray(p[..., 1:])
# image_show_norm('image', m, min=0, max=1, )
# image_show_norm('truth', onehot, min=0, max=1, )
# image_show_norm('predict', p, min=0, max=1, )
# cv2.waitKey(1)
# zz=0
# pdb.set_trace()
scheduler.step()
torch.cuda.empty_cache()
log.write('\n')
# main #################################################################
if __name__ == '__main__':
run_train()
'''
'''