-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
153 lines (128 loc) · 5.98 KB
/
run.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
import argparse
import torch
from tqdm import tqdm
import numpy as np
from torch.utils.data import DataLoader
from transformers import LayoutLMConfig, LayoutLMTokenizer
from transformers.optimization import AdamW, get_linear_schedule_with_warmup
from utils import set_seed
from datasets import load_metric
from model import LayoutLMForSequenceClassification
from evaluation import evaluate_ood
import wandb
wandb.init(project="ood", )
import warnings
from data import load
from sklearn.metrics import accuracy_score
warnings.filterwarnings("ignore")
# 2419435459658b249d1a54abb6760d498b974b47
task_to_labels = {
'rvl_cdip': 16,
}
def train(args, model, train_dataset, dev_dataset, test_dataset, benchmarks):
train_dataloader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, drop_last=True)
dev_dataloader = DataLoader(dev_dataset, batch_size=args.batch_size, drop_last=True)
total_steps = int(len(train_dataloader) * args.num_train_epochs)
warmup_steps = int(total_steps * args.warmup_ratio)
no_decay = ["LayerNorm.weight", "bias"]
optimizer_grouped_parameters = [
{
"params": [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
"weight_decay": args.weight_decay,
},
{"params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)], "weight_decay": 0.0},
]
optimizer = AdamW(optimizer_grouped_parameters, lr=args.learning_rate, eps=args.adam_epsilon)
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=warmup_steps, num_training_steps=total_steps)
def detect_ood():
model.prepare_ood(dev_dataloader)
for tag, ood_features in benchmarks:
results = evaluate_ood(args, model, test_dataset, ood_features, tag=tag)
wandb.log(results, step=num_steps)
num_steps = 0
for epoch in range(int(args.num_train_epochs)):
model.zero_grad()
for step, batch in enumerate(tqdm(train_dataloader)):
model.train()
batch = {key: value.to(args.device) for key, value in batch.items()}
outputs = model(**batch)
loss, cos_loss = outputs[0], outputs[1]
loss.backward()
num_steps += 1
optimizer.step()
scheduler.step()
model.zero_grad()
wandb.log({'loss': loss.item()}, step=num_steps)
wandb.log({'cos_loss': cos_loss.item()}, step=num_steps)
results = evaluate(args, model, dev_dataset, tag="dev")
wandb.log(results, step=num_steps)
results = evaluate(args, model, test_dataset, tag="test")
wandb.log(results, step=num_steps)
detect_ood()
def evaluate(args, model, eval_dataset, tag="train"):
def compute_metrics(preds, labels):
preds = np.argmax(preds, axis=1)
result = {}
acc_score = accuracy_score(y_true=preds, y_pred=labels)
# if len(result) > 1:
# result["score"] = np.mean(list(result.values())).item()
result["accuracy"] = acc_score
return result
dataloader = DataLoader(eval_dataset, batch_size=args.batch_size, drop_last=True)
label_list, logit_list = [], []
for step, batch in enumerate(tqdm(dataloader)):
model.eval()
labels = batch["label"].detach().cpu().numpy()
batch = {key: value.to(args.device) for key, value in batch.items()}
batch["label"] = None
outputs = model(**batch)
logits = outputs[0].detach().cpu().numpy()
label_list.append(labels)
logit_list.append(logits)
preds = np.concatenate(logit_list, axis=0)
labels = np.concatenate(label_list, axis=0)
results = compute_metrics(preds, labels)
results = {"{}_{}".format(tag, key): value for key, value in results.items()}
return results
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--model_name_or_path", default="microsoft/layoutlm-base-uncased", type=str)
parser.add_argument("--max_seq_length", default=512, type=int)
parser.add_argument("--task_name", default="rvl_cdip", type=str)
parser.add_argument("--batch_size", default=32, type=int)
parser.add_argument("--learning_rate", default=1e-5, type=float)
parser.add_argument("--adam_epsilon", default=1e-6, type=float)
parser.add_argument("--warmup_ratio", default=0.06, type=float)
parser.add_argument("--weight_decay", default=0.01, type=float)
parser.add_argument("--num_train_epochs", default=10.0, type=float)
parser.add_argument("--seed", type=int, default=42)
parser.add_argument("--project_name", type=str, default="ood")
parser.add_argument("--alpha", type=float, default=2.0)
parser.add_argument("--loss", type=str, default="margin")
args = parser.parse_args()
wandb.init(project=args.project_name, name=args.task_name + '-' + str(args.alpha) + "_" + args.loss)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
args.n_gpu = torch.cuda.device_count()
args.device = device
set_seed(args)
num_labels = task_to_labels[args.task_name]
config = LayoutLMConfig.from_pretrained(args.model_name_or_path, num_labels=num_labels)
config.gradient_checkpointing = True
config.alpha = args.alpha
config.loss = args.loss
tokenizer = LayoutLMTokenizer.from_pretrained(args.model_name_or_path)
model = LayoutLMForSequenceClassification.from_pretrained(
args.model_name_or_path, config=config,
)
model.to(0)
datasets = ['rvl_cdip', 'ood']
benchmarks = ()
for dataset in datasets:
if dataset == args.task_name:
train_dataset, dev_dataset, test_dataset = load(dataset, tokenizer, max_seq_length=args.max_seq_length, is_id=True)
else:
_, _, ood_dataset = load(dataset, tokenizer, max_seq_length=args.max_seq_length)
benchmarks = (('rvl_cdip_' + dataset, ood_dataset),) + benchmarks
train(args, model, train_dataset, dev_dataset, test_dataset, benchmarks)
if __name__ == "__main__":
main()