-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
55 lines (42 loc) · 2.07 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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import yaml
import os
import sys
from dataset.dataset import Dataset, DataLoader, check_dataset
from model.Model import Model
from utils import set_seed, split_dataset, wandb_init, mixup
from fit import *
if __name__ == "__main__":
set_seed(42)
# read config
with open('config.yaml') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
print(config)
os.makedirs(config['output_dir'], exist_ok=True)
wandb_run = wandb_init(config) if config['use_wandb'] else None
config['wandb_id'] = wandb_run.id if config['use_wandb'] else None
# make model
model = Model(config)
print(model)
if config['resume'] and config['checkpoint_path']:
model.load_model(config['checkpoint_path'], pretrained=True)
# make dataset
train_df, valid_df = split_dataset(config['data_dir'], validation_percentage=0.2)
print("Train: ", train_df.shape, "; Valid: ", valid_df.shape)
if config['debug']:
train_df = train_df.sample(frac=1).reset_index(drop=True)[:100]
valid_df = valid_df.sample(frac=1).reset_index(drop=True)[:100]
train_dataset = Dataset(config['data_dir'], train_df, label_col='digit', mode='train', config=config['augment'])
valid_dataset = Dataset(config['data_dir'], valid_df, label_col='digit', mode='valid', config=config['augment'])
check_dataset(train_dataset, valid_dataset, save_dir=config['output_dir'])
train_loader = DataLoader(train_dataset, batch_size=config['train_batch'], shuffle=True)
valid_loader = DataLoader(valid_dataset, batch_size=config['valid_batch'], shuffle=False)
if config['augment']['mixup']:
data = next(iter(train_loader))
images, labels = data[0], data[1]
images, labels = mixup(images, labels, num_classes=config['num_class'])
check_dataset([images, labels], valid_dataset, save_dir=config['output_dir'], from_mixup=True)
# train
fit_model(model, train_loader, valid_loader, config, wandb_run)