-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
47 lines (38 loc) · 1.98 KB
/
main.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
from src.setup import setup_model_and_processor
from src.utils import load_config, collate_fn
from src.dataset import ImgDescriptionDataset
from torch.utils.data import DataLoader
from src.train import train_model
from datetime import datetime
import functools
def main():
config = load_config('config.yaml')
model, processor, device = setup_model_and_processor()
train_dataset = ImgDescriptionDataset(config['paths']['dataset_csv'],
config['paths']['image_dir'],
split="train")
val_dataset = ImgDescriptionDataset(config['paths']['dataset_csv'],
config['paths']['image_dir'],
split="valid")
train_loader = DataLoader(train_dataset,
batch_size=config['training']['batch_size'],
collate_fn=functools.partial(collate_fn, processor=processor),
num_workers=config['training']['num_workers'],
shuffle=True)
val_loader = DataLoader(val_dataset,
batch_size=config['training']['batch_size'],
collate_fn=functools.partial(collate_fn, processor=processor),
num_workers=config['training']['num_workers'])
train_model(train_loader, val_loader,
model, processor,
int(config['training']['epochs']), float(config['training']['learning_rate']),
int(config['training']['validation_interval']),
config['paths']['checkpoint_dir'],device=device)
current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
push_model_name = config['model']['name_template'].format(timestamp=current_time)
push_processor_name = f"{push_model_name}-processor"
model.push_to_hub(push_model_name)
processor.push_to_hub(push_processor_name)
print('Success')
if __name__ == "__main__":
main()