-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
199 lines (161 loc) · 5.74 KB
/
train.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
import torch
import torch.nn as nn
from torch.utils import data as torchdata
import numpy as np
from transformers import AutoProcessor, LayoutLMv3ForTokenClassification # type: ignore
from datasets import load_dataset
from datasets.features import ClassLabel
from datasets import Features, Sequence, ClassLabel, Value, Array2D, Array3D
import evaluate
import flor
from flor import MTK as Flor
# Device configuration
device = torch.device(flor.arg("device", "cuda" if torch.cuda.is_available() else "cpu"))
# Hyper-parameters
num_epochs = flor.arg("epochs", default=5)
batch_size = flor.arg("batch_size", 4)
learning_rate = flor.arg("lr", 1e-5)
# Data loader
dataset = load_dataset("nielsr/funsd-layoutlmv3")
print(str(dataset))
features = dataset["train"].features # type: ignore
column_names = dataset["train"].column_names # type: ignore
image_column_name = "image"
text_column_name = "tokens"
boxes_column_name = "bboxes"
label_column_name = "ner_tags"
# In the event the labels are not a `Sequence[ClassLabel]`, we will need to go through the dataset to get the
# unique labels.
def get_label_list(labels):
unique_labels = set()
for label in labels:
unique_labels = unique_labels | set(label)
label_list = list(unique_labels)
label_list.sort()
return label_list
if isinstance(features[label_column_name].feature, ClassLabel):
label_list = features[label_column_name].feature.names
# No need to convert the labels since they are already ints.
id2label = {k: v for k, v in enumerate(label_list)}
label2id = {v: k for k, v in enumerate(label_list)}
else:
label_list = get_label_list(dataset["train"][label_column_name]) # type: ignore
id2label = {k: v for k, v in enumerate(label_list)}
label2id = {v: k for k, v in enumerate(label_list)}
num_labels = len(label_list)
processor = AutoProcessor.from_pretrained("microsoft/layoutlmv3-base", apply_ocr=False)
model = LayoutLMv3ForTokenClassification.from_pretrained(
"microsoft/layoutlmv3-base", id2label=id2label, label2id=label2id
)
assert isinstance(model, LayoutLMv3ForTokenClassification)
model.to(device) # type: ignore
Flor.checkpoints(model)
def prepare_examples(examples):
images = examples[image_column_name]
words = examples[text_column_name]
boxes = examples[boxes_column_name]
word_labels = examples[label_column_name]
encoding = processor(
images,
words,
boxes=boxes,
word_labels=word_labels,
truncation=True,
padding="max_length",
)
return encoding
features = Features(
{
"pixel_values": Array3D(dtype="float32", shape=(3, 224, 224)),
"input_ids": Sequence(feature=Value(dtype="int64")),
"attention_mask": Sequence(Value(dtype="int64")),
"bbox": Array2D(dtype="int64", shape=(512, 4)),
"labels": Sequence(feature=Value(dtype="int64")),
} # type: ignore
)
train_dataset = dataset["train"].map( # type: ignore
prepare_examples,
batched=True,
remove_columns=column_names,
features=features,
)
train_dataset.set_format("torch")
eval_dataset = dataset["test"].map( # type: ignore
prepare_examples,
batched=True,
remove_columns=column_names,
features=features,
)
eval_dataset.set_format("torch")
train_loader = torchdata.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True, collate_fn=torchdata.default_collate) # type: ignore
test_loader = torchdata.DataLoader(
dataset=eval_dataset, # type: ignore
batch_size=batch_size,
shuffle=False,
collate_fn=torchdata.default_collate,
)
# Loss and optimizer
optimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate) # type: ignore
Flor.checkpoints(optimizer)
metric = evaluate.load("seqeval")
def compute_metrics(p):
predictions, labels = p
predictions = np.argmax(predictions, axis=2)
# Remove ignored index (special tokens)
true_predictions = [
[label_list[p] for (p, l) in zip(prediction, label) if l != -100]
for prediction, label in zip(predictions, labels)
]
true_labels = [
[label_list[l] for (p, l) in zip(prediction, label) if l != -100]
for prediction, label in zip(predictions, labels)
]
results = metric.compute(predictions=true_predictions, references=true_labels)
assert results is not None
return {
"precision": results["overall_precision"],
"recall": results["overall_recall"],
"f1": results["overall_f1"],
"accuracy": results["overall_accuracy"],
}
# Train the model
total_step = len(train_loader)
num_steps = 1000
for epoch in Flor.loop(range(num_epochs)):
model.train()
for i, batch in Flor.loop(enumerate(train_loader)):
# Move tensors to the configured device
for k in batch:
batch[k] = batch[k].to(device)
# Forward pass
outputs = model(**batch)
loss = outputs.loss
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
print(
"Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}".format(
epoch + 1, num_epochs, i, total_step, flor.log("loss", loss.item())
)
)
print("Model Validate", epoch + 1)
# Test the model
# In test phase, we don't need to compute gradients (for memory efficiency)
print("Model TEST")
model.eval()
with torch.no_grad():
preds = []
labels = []
for i, batch in enumerate(test_loader):
for k in batch:
batch[k] = batch[k].to(device)
# Forward pass
outputs = model(**batch)
preds.append(outputs.logits.cpu())
labels.append(batch["labels"].cpu())
# compute metrics
p = np.concatenate(preds)
l = np.concatenate(labels)
result = compute_metrics((p, l))
print(result)