-
Notifications
You must be signed in to change notification settings - Fork 0
/
finetune.py
135 lines (98 loc) · 4.37 KB
/
finetune.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
import json
import os
from utils import ImageCaptioningDataset, LogValidationDistanceCallback, Pix2Struct
from pytorch_lightning.callbacks import LearningRateMonitor
import pytorch_lightning as pl
from pytorch_lightning.callbacks import ModelCheckpoint
import random
from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor
from datetime import datetime
repo_id = os.getenv("MODEL_PATH")
processor = Pix2StructProcessor.from_pretrained(repo_id)
model = Pix2StructForConditionalGeneration.from_pretrained(repo_id, is_encoder_decoder=True)
processor.image_processor.is_vqa = True
data_dir = os.getenv('DATA_DIR')
train_file_path = os.path.join(data_dir, 'train.json')
test_file_path = os.path.join(data_dir, 'test.json')
if not os.path.exists(train_file_path):
raise FileNotFoundError(f"File not found: {train_file_path}")
if not os.path.exists(test_file_path):
raise FileNotFoundError(f"File not found: {test_file_path}")
with open(train_file_path, 'r') as f:
train_json = json.load(f)
with open(test_file_path, 'r') as f:
test_json = json.load(f)
random.shuffle(train_json)
random.shuffle(test_json)
max_patches = int(os.getenv('MAX_PATCHES', 3584))
max_length = int(os.getenv('MAX_LENGTH', 256))
batch_size = int(os.getenv('BATCH_SIZE', 1))
num_gpus = int(os.getenv('NUM_GPUS', 1))
num_epochs = int(os.getenv('NUM_EPOCHS', 1))
lr = float(os.getenv('LR', 5e-5))
train_dataset = ImageCaptioningDataset(train_json, processor, model,
max_patches=max_patches, max_length=max_length)
val_dataset = ImageCaptioningDataset(test_json, processor, model,
max_patches=max_patches, max_length=max_length)
encoding, target_sequence = train_dataset[0]
print(encoding.keys())
print(processor.decode([id.item() for id in encoding["labels"] if id != -100]))
print(target_sequence)
print("Number of added tokens train:", len(train_dataset.added_tokens))
print(val_dataset.added_tokens)
print("Number of added tokens test:", len(train_dataset.added_tokens))
print(val_dataset.added_tokens)
len(processor.tokenizer)
from torch.utils.data import DataLoader
train_dataloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=8)
val_dataloader = DataLoader(val_dataset, batch_size=batch_size, num_workers=8)
batch = next(iter(train_dataloader))
encoding, target_sequences = batch
for k,v in encoding.items():
print(k,v.shape)
print(processor.batch_decode([id for id in encoding["labels"].squeeze().tolist() if id != -100]))
config = {
"num_warmup_epochs": 0,
"max_epochs": num_epochs,
"lr": lr,
"check_val_every_n_epoch": 1,
"gradient_clip_val": 1.0,
"warmup_steps": 0, # 800/8*30/10, 10%
"accumulate_grad_batches": 8,
"verbose": True,
}
pl_module = Pix2Struct(config, processor, model, train_dataloader, val_dataloader)
score_file_path = os.path.join(os.getenv("DATA_DIR"), "training", "scores.txt")
os.makedirs(os.path.dirname(score_file_path), exist_ok=True)
if not os.path.exists(score_file_path):
with open(score_file_path, "w") as f:
f.write("edit_distance\n")
validation_logger = LogValidationDistanceCallback(score_file_path)
import pytorch_lightning as pl
trainer = pl.Trainer(
accelerator="gpu",
devices=num_gpus,
max_epochs = config.get("max_epochs"),
check_val_every_n_epoch=config.get("check_val_every_n_epoch"),
gradient_clip_val=config.get("gradient_clip_val"), # use gradient clipping
accumulate_grad_batches=config.get("accumulate_grad_batches"), # use gradient accumulation
callbacks = [validation_logger],
log_every_n_steps = 2,
# precision = 16
)
trainer.fit(pl_module)
model_output_name = os.path.join(os.getenv("DATA_DIR"), "training", "model")
os.makedirs(model_output_name, exist_ok=True)
try:
pl_module.model.save_pretrained(model_output_name)
except Exception as e:
print("saving og")
model.save_pretrained(model_output_name)
processor.save_pretrained(model_output_name)
processor.tokenizer.save_pretrained(model_output_name)
with open(os.path.join(model_output_name, "meta.json"), "w") as f:
json.dump({
"max_patches": max_patches,
"max_length": max_length,
"train_date": str(datetime.now())
}, f, indent=4, ensure_ascii=False)