-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
320 lines (295 loc) · 8.76 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
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
from distutils.command.config import config
import random
from transformers import (
ViltProcessor,
DataCollatorForLanguageModeling,
ViltConfig,
ViltFeatureExtractor,
BertTokenizerFast,
TrainingArguments,
)
import torch
import pandas as pd
from datasets import load_metric
import numpy as np
from dataset import (
MimicCxrPretrainingDataset,
MimicCxrPretrainingDatasetAnyLabels,
MimicCxrPretrainingDatasetRandom,
)
from model import ViltForMaskedLMAndITM, ViltForImageTextMatching
from data_collator import ViltDataCollatorForPretraining, ViltDataCollatorForPretrainingOnlyITM
from transformers.utils import logging
from trainer import MemoryEfficientTrainer
import wandb
import argparse
logger = logging.get_logger(__name__)
# assegno a vari parametri i valori dati come argomento
argparser = argparse.ArgumentParser()
argparser.add_argument(
"--wandb_key",
type=str,
default="9ad1cd077e95967a0961345a858b3028d18c80f5",
help="Wandb key for logging",
)
argparser.add_argument(
"--wandb_project_name",
default="radiography",
type=str,
help="Project name for wandb logs",
)
argparser.add_argument(
"--wandb_entity", default="tesi-zanetti", type=str, help="Wandb entity"
)
# argparser.add_argument("N", type=int, help="defines the number of record in the training dataset (max 36896)")
argparser.add_argument(
"--neg_selection",
choices=["random", "any", "all"],
default="random",
help="approach used in selecting negative sampling (based on labels)",
)
argparser.add_argument(
"-t",
"--task",
choices=["mlm_and_itm", "itm"],
default="mlm_and_itm",
help="Defines the task on which the model is trained (options: itm or mlm and itm)",
)
argparser.add_argument(
"-bs",
"--batch_size",
type=int,
default=20,
help="defines the batch size for train and eval",
)
argparser.add_argument(
"-e", "--epochs", type=int, default=10, help="defines the number of epochs"
)
argparser.add_argument(
"-lr", "--learning_rate", type=float, default=2e-5, help="defines the learning rate"
)
argparser.add_argument(
"-wd", "--weight_decay", type=float, default=0.0, help="defines the weight decay"
)
argparser.add_argument(
"-b1",
"--adam_beta1",
type=float,
default=0.9,
help="defines the hyperparameter beta 1",
)
argparser.add_argument(
"-b2",
"--adam_beta2",
type=float,
default=0.999,
help="defines the hyperparameter beta 2",
)
argparser.add_argument(
"-st",
"--lr_scheduler_type",
choices=[
"linear",
"cosine",
"cosine_with_restarts",
"polynomial",
"constant",
"constant_with_warmup",
],
default="linear",
help="defines the learning rate scheduling type",
)
argparser.add_argument(
"-wr",
"--warmup_ratio",
type=float,
default=0.0,
help="defines the warmup ration of the training",
)
argparser.add_argument(
"-ws",
"--warmup_steps",
type=int,
default=0,
help="defines the number of warmup steps of the training",
)
argparser.add_argument(
"--mlm_prob", type=float, default=0.15, help="masked language model probability"
)
argparser.add_argument(
"-s",
"--seed",
type=int,
default=42,
help="defines the seed used for picking data and training the model",
)
argparser.add_argument(
"-en",
"--experiment_name",
default="train_checkpoint",
help="defines the directory where the training checkpoint are saved",
)
argparser.add_argument(
"-cd",
"--checkpoint_dir",
default=None,
help="defines the directory with the checkpoint from which the training starts",
)
argparser.add_argument(
"--dataset_path",
type=str,
default="/datasets/MIMIC-CXR",
help="Tokenizer used to tokenize texts",
)
argparser.add_argument(
"--max_position_embeddings",
default=512,
type=int,
help="Maximum number of position embeddings",
)
argparser.add_argument(
"--patch_size",
default=32,
type=int,
help="Patch size, where each patch is patch_size x patch_size",
)
args = argparser.parse_args()
# inizializzo wandb
wandb.login(key=args.wandb_key)
wandb.init(
project=args.wandb_project_name, entity=args.wandb_entity, name=args.experiment_name
)
# inizializzo valori di default
random.seed(args.seed)
torch.manual_seed(args.seed)
# test_size = 1000
# config modello
config = ViltConfig(
max_position_embeddings=args.max_position_embeddings, patch_size=args.patch_size
)
# modello pre addestrato
processor = ViltProcessor(
ViltFeatureExtractor(
resample=3,
image_mean=[0.5, 0.5, 0.5],
image_std=[0.5, 0.5, 0.5],
size_divisor=args.patch_size,
),
BertTokenizerFast.from_pretrained(
"bert-base-uncased", model_max_length=args.max_position_embeddings
),
)
tokenizer = processor.tokenizer
if args.task == "itm":
model = ViltForImageTextMatching.from_pretrained(
"dandelin/vilt-b32-mlm", config=config, ignore_mismatched_sizes=True
)
else:
model = ViltForMaskedLMAndITM.from_pretrained(
"dandelin/vilt-b32-mlm", config=config, ignore_mismatched_sizes=True
)
# passaggio del modello su gpu
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
torch.cuda.empty_cache()
print(device)
model = model.to(device)
# creo i dataset di train, test e validation
if args.neg_selection == "all":
train_dataset = MimicCxrPretrainingDataset(args.dataset_path, split="train")
val_dataset = MimicCxrPretrainingDataset(args.dataset_path, split="validate")
elif args.neg_selection == "any":
train_dataset = MimicCxrPretrainingDatasetAnyLabels(
args.dataset_path, split="train"
)
val_dataset = MimicCxrPretrainingDatasetAnyLabels(
args.dataset_path, split="validate"
)
else:
train_dataset = MimicCxrPretrainingDatasetRandom(args.dataset_path, split="train")
val_dataset = MimicCxrPretrainingDatasetRandom(args.dataset_path, split="validate")
# custom data collator
if args.task == "itm":
my_data_collator = ViltDataCollatorForPretrainingOnlyITM(processor)
else:
mlm_data_collator = DataCollatorForLanguageModeling(
tokenizer=tokenizer, mlm_probability=args.mlm_prob
)
my_data_collator = ViltDataCollatorForPretraining(processor, mlm_data_collator)
# metriche del training
metric = load_metric("accuracy")
if args.task == "itm":
def compute_metrics(eval_pred):
(mlm_predictions, itm_predictions, mlm_loss, itm_loss), (
mlm_labels,
itm_labels,
) = eval_pred
print(mlm_predictions.shape)
print(itm_predictions.shape)
print(mlm_loss.shape)
print(itm_loss.shape)
print(mlm_labels.shape)
print(itm_labels.shape)
# mlm_predictions = np.argmax(mlm_logits, axis=-1)
# itm_predictions = np.argmax(itm_logits, axis=-1)
return {
"itm_acc": metric.compute(
predictions=itm_predictions, references=itm_labels
),
"itm_loss": itm_loss.mean(),
}
else:
def compute_metrics(eval_pred):
(mlm_predictions, itm_predictions, mlm_loss, itm_loss), (
mlm_labels,
itm_labels,
) = eval_pred
# mlm_predictions = np.argmax(mlm_logits, axis=-1)
# itm_predictions = np.argmax(itm_logits, axis=-1)
return {
"mlm_acc": metric.compute(
predictions=mlm_predictions.flatten(), references=mlm_labels.flatten()
),
"itm_acc": metric.compute(
predictions=itm_predictions, references=itm_labels
),
"mlm_loss": mlm_loss.mean(),
"itm_loss": itm_loss.mean(),
}
training_args = TrainingArguments(
output_dir=args.experiment_name,
evaluation_strategy="steps", #epoch
eval_steps=1,
eval_accumulation_steps=1,
learning_rate=args.learning_rate,
weight_decay=args.weight_decay,
adam_beta1=args.adam_beta1,
adam_beta2=args.adam_beta2,
lr_scheduler_type=args.lr_scheduler_type,
warmup_ratio=args.warmup_ratio,
warmup_steps=args.warmup_steps,
seed=args.seed,
data_seed=args.seed,
logging_steps=100,
remove_unused_columns=False,
num_train_epochs=args.epochs,
per_device_train_batch_size=args.batch_size,
per_device_eval_batch_size=args.batch_size,
save_total_limit=2,
report_to="wandb",
)
# training
trainer = MemoryEfficientTrainer(
model=model,
args=training_args,
compute_metrics=compute_metrics,
train_dataset=train_dataset,
eval_dataset=val_dataset,
data_collator=my_data_collator,
)
trainer.train(resume_from_checkpoint=args.checkpoint_dir)
print(
"\n\n CHECK FOR ERROR:\n",
" THERE WAS 0 MISS (PROBABLY AN ERROR)"
if train_dataset.checkerror()
else " THERE WERE SOME MISSES (PROBABLY OKAY)",
)