-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_weibo_text.py
454 lines (366 loc) · 16.3 KB
/
train_weibo_text.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
443
444
445
446
447
448
449
450
451
452
453
# coding=utf-8
#
#
import gc
import sys
import argparse
from tqdm import tqdm
import torch.optim as optim
from collections import defaultdict
from sklearn.model_selection import train_test_split, StratifiedKFold
import torch.nn as nn
import torch.backends.cudnn as cudnn
from torch.utils.data import DataLoader
from conf import model_config_bert as model_config
import os
import pandas as pd
import torch
import numpy as np
from helper import *
from crd.criterion import CRDLoss, CRDsoftmax
from torch.utils.data import Dataset
from transformers import BertTokenizer
from model.resnet import resnet50
from model.bert import Model
from conf import config
from PIL import Image
import torchvision.transforms as transforms
from PIL import ImageFile
torch.manual_seed(0)
torch.backends.cudnn.deterministic = False
torch.backends.cudnn.benchmark = True
ImageFile.LOAD_TRUNCATED_IMAGES = True
class MMRumor(object):
def __init__(self, root='data', **kwargs):
self.train_dir1 = os.path.join(root, 'train_rumor.txt')
self.train_dir2 = os.path.join(root, 'train_nonrumor.txt')
self.test_dir1 = os.path.join(root, 'test_rumor.txt')
self.test_dir2 = os.path.join(root, 'test_nonrumor.txt')
train_rumor = self.process_data(self.train_dir1, 0)
train_nonrumor = self.process_data(self.train_dir2, 1)
test_rumor = self.process_data(self.test_dir1, 0)
test_nonrumor = self.process_data(self.test_dir2, 1)
print("=> MMRumor loaded")
print("Dataset statistics:")
print(" ------------------------------")
print(" subset | # rumor | # nonrumor")
print(" ------------------------------")
print(" train | {:5d} | {:8d}".format(len(train_rumor), len(train_nonrumor)))
print(" test | {:5d} | {:8d}".format(len(test_rumor), len(test_nonrumor)))
print(" ------------------------------")
print(" total | {:5d} | {:8d}".format(len(train_rumor) + len(test_rumor),
len(train_nonrumor) + len(test_nonrumor)))
print(" ------------------------------")
self.train = train_rumor + train_nonrumor
self.test = test_rumor + test_nonrumor
def process_data(self, file_name, label):
with open('{}'.format(file_name), encoding="utf-8") as f:
lines = f.readlines()
count = 0
res, tmp, datas, dataset = [], [], [], []
for line in lines:
res.append(line)
for item in res:
if count < 3:
tmp += [item]
count += 1
if count == 3:
datas.append(tmp)
count = 0
tmp = []
for data in datas:
img_name = data[1].split('|')[0].split('/')[-1]
content = data[2].split('\n')[0]
if label == 0:
if os.path.exists('weibo_img/rumor_images/' + img_name):
dataset.append((img_name, content, 0))
if label == 1:
if os.path.exists('weibo_img/nonrumor_images/' + img_name):
dataset.append((img_name, content, 1))
return dataset
class RumorDataset(Dataset):
def __init__(self, dataset, mode='train', k=16384):
self.dataset = dataset
self.mode = mode
self.k = k
self.tokenizer = BertTokenizer.from_pretrained(model_config.pretrain_model_path)
self.pad_idx = self.tokenizer.pad_token_id
self.x_data = []
self.img_data = []
self.y_data = []
self.cls_positive = [[] for _ in range(2)]
self.cls_negative = [[] for _ in range(2)]
for i, data in enumerate(dataset):
img_name, content, label = data
x = self.row_to_tensor(self.tokenizer, content)
self.x_data.append(x)
self.img_data.append(self.read_img(img_name, label))
self.y_data.append(label)
self.cls_positive[label].append(i)
for i in range(2):
for j in range(2):
if j == i:
continue
self.cls_negative[i].extend(self.cls_positive[j])
self.cls_positive = [np.asarray(self.cls_positive[i]) for i in range(2)]
self.cls_negative = [np.asarray(self.cls_negative[i]) for i in range(2)]
self.cls_positive = np.asarray(self.cls_positive)
self.cls_negative = np.asarray(self.cls_negative)
def row_to_tensor(self, tokenizer, content):
x_encode = tokenizer.encode(content)
if len(x_encode) > config.max_seq_len:
text_len = int(config.max_seq_len / 2)
x_encode = x_encode[:text_len] + x_encode[-text_len:]
else:
padding = [0] * (config.max_seq_len - len(x_encode))
x_encode += padding
x_tensor = torch.tensor(x_encode, dtype=torch.long)
return x_tensor
def read_img(self, img_name, label):
if label == 0:
img_path = 'weibo_img/rumor_images/' + img_name
else:
img_path = 'weibo_img/nonrumor_images/' + img_name
img = Image.open(img_path).convert('RGB')
transform_train_list = [
# transforms.RandomResizedCrop(size=128, scale=(0.75,1.0), ratio=(0.75,1.3333), interpolation=3), #Image.BICUBIC)
transforms.Resize((256, 128), interpolation=3),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]
transform_test_list = [
transforms.Resize(size=(256, 128), interpolation=3), # Image.BICUBIC
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]
tran_trains = transforms.Compose(transform_train_list)
tran_tests = transforms.Compose(transform_test_list)
if self.mode == 'train':
img = tran_trains(img)
else:
img = tran_tests(img)
res_img = img.float()
return res_img
def __len__(self):
return len(self.dataset)
def __getitem__(self, index):
pos_idx = index
replace = True if self.k > len(self.cls_negative[self.y_data[index]]) else False
neg_idx = np.random.choice(self.cls_negative[self.y_data[index]], self.k, replace=replace)
sample_idx = np.hstack((np.asarray([pos_idx]), neg_idx))
return self.x_data[index], self.img_data[index], self.y_data[index], index, sample_idx
def parse_option():
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--operation", default='train', type=str, help="operation")
parser.add_argument("-bs", "--batch_size", default=16, type=int, help="batch size")
parser.add_argument("-e", "--epochs_num", default=30, type=int, help="epochs num")
parser.add_argument('--trial', type=str, default='1', help='trial id')
parser.add_argument('--distill', type=str, default='nst', choices=['kd', 'nst'])
parser.add_argument('--kd_T', type=float, default=4, help='temperature for KD distillation')
parser.add_argument('--feat_dim', default=128, type=int, help='feature dimension')
parser.add_argument('-r', '--gamma', type=float, default=1, help='weight for classification')
parser.add_argument('-a', '--alpha', type=float, default=0, help='weight balance for KD')
parser.add_argument('-b', '--beta', type=float, default=0.3, help='weight balance for other losses')
parser.add_argument('--nce_k', default=16384, type=int, help='number of negative samples for NCE')
parser.add_argument('--nce_t', default=0.07, type=float, help='temperature parameter for softmax')
parser.add_argument('--nce_m', default=0.5, type=float, help='momentum for non-parametric updates')
parser.add_argument('--crd_op', default=0, type=int, help='choice of crd or crd_softmax')
args = parser.parse_args()
# args.save_folder=config.save_folder
return args
#
def main():
best_acc = 0
best_text_acc = 0
best_prec_0, best_rec_0, best_f_0 = 0, 0, 0
best_prec_1, best_rec_1, best_f_1 = 0, 0, 0
full_metric = None
args = parse_option()
sys.stdout = Logger(os.path.join(config.save_folder, 'text.txt'))
print("==========\nArgs:{}\n==========".format(args))
dataset = MMRumor(root='data')
train_dataset = RumorDataset(dataset.train, 'train', args.nce_k)
args.n_data = len(train_dataset)
train_loader = DataLoader(dataset=train_dataset, batch_size=args.batch_size, shuffle=True)
val_dataset = RumorDataset(dataset.test, 'val', args.nce_k)
val_loader = DataLoader(dataset=val_dataset, batch_size=args.batch_size, shuffle=False)
model = Model()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=2e-5)
scheduler = optim.lr_scheduler.ExponentialLR(optimizer, gamma=0.1)
if torch.cuda.is_available():
model.cuda()
criterion.cuda()
cudnn.benchmark = True
#
# log_writer = LogWriter(config.save_folder, sync_cycle=10)
# with log_writer.mode("train") as logger:
# scalar_train_img_acc = logger.scalar("img_acc")
# scalar_train_text_acc = logger.scalar("text_acc")
# # scalar_train_loss = logger.scalar("loss")
# with log_writer.mode("test") as logger:
# # scalar_test_acc = logger.scalar("acc")
# # scalar_test_loss = logger.scalar("loss")
# scalar_test_img_acc = logger.scalar("img_acc")
# scalar_test_text_acc = logger.scalar("text_acc")
for epoch in range(1, args.epochs_num + 1):
print("==> training...")
time1 = time.time()
train(epoch, train_loader, model, criterion, optimizer)
time2 = time.time()
print('epoch {}, total time {:.2f}'.format(epoch, time2 - time1))
acc, prec_0, rec_0, f_0, prec_1, rec_1, f_1 = validate(val_loader, model, criterion)
if acc > best_acc:
best_acc = acc
full_metric = (acc, prec_0, rec_0, f_0, prec_1, rec_1, f_1)
print('best acc in epoch:{}'.format(epoch), best_acc)
print('full metric when best acc: prec_0, rec_0, f_0, prec_1, rec_1, f_1')
print(full_metric)
print('\n')
if prec_0 > best_prec_0:
best_prec_0 = prec_0
print('best prec_0 in epoch {}:'.format(epoch), best_prec_0)
if rec_0 > best_rec_0:
best_rec_0 = rec_0
print('best rec_0 in epoch {}:'.format(epoch), best_rec_0)
if f_0 > best_f_0:
best_f_0 = f_0
print('best f_0 in epoch {}:'.format(epoch), best_f_0)
if prec_1 > best_prec_1:
best_prec_1 = prec_1
print('best prec_1 in epoch {}:'.format(epoch), best_prec_1)
if rec_1 > best_rec_1:
best_rec_1 = rec_1
print('best rec_1 in epoch {}:'.format(epoch), best_rec_1)
if f_1 > best_f_1:
best_f_1 = f_1
print('best f_1 in epoch {}:'.format(epoch), best_f_1)
print('best accuracy :', best_acc)
print('full metric under best accuracy: prec, rec, F1, prec_fake, rec_fake, F1_fake')
print(full_metric)
print('best prec_0 :', best_prec_0)
print('best rec_0 :', best_rec_0)
print('best f_0 :', best_f_0)
print('best prec_1 :', best_prec_1)
print('best rec_1 :', best_rec_1)
print('best f_1 :', best_f_1)
# if test_text_acc > best_text_acc:
# best_text_acc = test_text_acc
# state = {
# 'epoch': epoch,
# 'model': model.state_dict(),
# 'best_acc': best_text_acc,
# }
# save_file = os.path.join(config.save_folder, 'text_best_baseline.pth')
# print('saving the best model for text!')
# torch.save(state, save_file)
# print('best accuracy for text model :', best_text_acc)
def train(epoch, train_loader, model, criterion, optimizer):
model.train()
losses = AverageMeter()
top1 = AverageMeter()
for idx, data in enumerate(train_loader):
batch_x, _, batch_y, _, _ = data
batch_x, batch_y = batch_x.cuda(), batch_y.cuda()
cls_output, logits_text = model(batch_x)
loss = criterion(logits_text, batch_y)
acc1_text = accuracy(logits_text, batch_y)
losses.update(loss.item(), batch_x.size(0))
top1.update(float(acc1_text[0]), batch_x.size(0))
optimizer.zero_grad()
loss.backward()
optimizer.step()
if idx % config.train_print_step == 0:
print('Text Model:')
print('Epoch: [{0}][{1}/{2}]\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Acc@1 {top1.val:.3f} ({top1.avg:.3f})\t'.format(
epoch, idx, len(train_loader),
loss=losses, top1=top1))
print(' Train text model : Acc@1 {top1.avg:.3f}'.format(top1=top1))
# sys.stdout.flush()
# return top1.avg, top1_text.avg
def validate(val_loader, model, criterion):
"""validation"""
batch_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
count_0_lists = []
correct_0_lists = []
target_0_lists = []
count_1_lists = []
correct_1_lists = []
target_1_lists = []
# switch to evaluate mode
# model.eval()
model.eval()
with torch.no_grad():
cur_step = 0
for batch_x, _, batch_y, _, _ in val_loader:
batch_x, batch_y = batch_x.cuda(), batch_y.cuda()
# compute output
cls_output, logits = model(batch_x)
loss = criterion(logits, batch_y)
# measure accuracy and record loss
acc1 = accuracy(logits, batch_y)
count_0, count_correct_0, count_target_0 = metric(logits, batch_y, for_fake=True)
count_1, count_correct_1, count_target_1 = metric(logits, batch_y, for_fake=False)
count_0_lists.append(count_0)
correct_0_lists.append(count_correct_0)
target_0_lists.append(count_target_0)
count_1_lists.append(count_1)
correct_1_lists.append(count_correct_1)
target_1_lists.append(count_target_1)
losses.update(loss.item(), batch_x.size(0))
top1.update(float(acc1[0]), batch_x.size(0))
cur_step += 1
if cur_step % config.train_print_step == 0:
print('test for text model: ')
print('test: [{0}/{1}]\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Acc@1 {top1.val:.3f} ({top1.avg:.3f})\t'.format(
cur_step, len(val_loader), loss=losses,
top1=top1))
epoch_count_0 = sum(count_0_lists)
epoch_correct_0 = sum(correct_0_lists)
epoch_target_0 = sum(target_0_lists)
epoch_count_1 = sum(count_1_lists)
epoch_correct_1 = sum(correct_1_lists)
epoch_target_1 = sum(target_1_lists)
try:
prec_0 = float(epoch_correct_0) * (100.0 / float(epoch_count_0))
except ZeroDivisionError:
prec_0 = 0
try:
rec_0 = float(epoch_correct_0) * (100.0 / float(epoch_target_0))
except ZeroDivisionError:
rec_0 = 0
try:
f_0 = 2 * (prec_0 * rec_0) / (prec_0 + rec_0)
except ZeroDivisionError:
f_0 = 0
try:
prec_1 = float(epoch_correct_1) * (100.0 / float(epoch_count_1))
except ZeroDivisionError:
prec_1 = 0
try:
rec_1 = float(epoch_correct_1) * (100.0 / float(epoch_target_1))
except ZeroDivisionError:
rec_1 = 0
try:
f_1 = 2 * (prec_1 * rec_1) / (prec_1 + rec_1)
except ZeroDivisionError:
f_1 = 0
# prec_1 = float(epoch_correct_1) * (100.0 / float(epoch_count_1))
# rec_1 = float(epoch_correct_1) * (100.0 / float(epoch_target_1))
# f_1 = 2 * (prec_1 * rec_1) / (prec_1 + rec_1)
print('val * Acc@1 {top1.avg:.3f} '.format(top1=top1))
print('metrics: prec_0, rec_0, f_0, prec_1, rec_1, f_1')
print(prec_0, rec_0, f_0, prec_1, rec_1, f_1)
return top1.avg, prec_0, rec_0, f_0, prec_1, rec_1, f_1
# print('val text model * Acc@1 {top1.avg:.3f} '.format(top1=top1))
#
# return top1.avg, losses.avg
if __name__ == '__main__':
main()