-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
87 lines (68 loc) · 3.13 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
import time
import os
import sys
import torch
from model.lanenet.train_lanenet import train_model
from dataloader.data_loaders import TusimpleSet
from dataloader.transformers import Rescale
from model.lanenet.LaneNet import LaneNet
from torch.utils.data import DataLoader
from torch.autograd import Variable
from torchvision import transforms
from model.utils.cli_helper import parse_args
from model.eval_function import Eval_Score
import numpy as np
import pandas as pd
import cv2
DEVICE = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
def train():
args = parse_args()
save_path = args.save
if not os.path.isdir(save_path):
os.makedirs(save_path)
train_dataset_file = os.path.join(args.dataset, 'train.txt')
val_dataset_file = os.path.join(args.dataset, 'val.txt')
resize_height = args.height
resize_width = args.width
data_transforms = {
'train': transforms.Compose([
transforms.Resize((resize_height, resize_width)),
transforms.ColorJitter(brightness=0.1, contrast=0.1, saturation=0.1, hue=0.1),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
'val': transforms.Compose([
transforms.Resize((resize_height, resize_width)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}
target_transforms = transforms.Compose([
Rescale((resize_width, resize_height)),
])
train_dataset = TusimpleSet(train_dataset_file, transform=data_transforms['train'], target_transform=target_transforms)
train_loader = DataLoader(train_dataset, batch_size=args.bs, shuffle=True)
val_dataset = TusimpleSet(val_dataset_file, transform=data_transforms['val'], target_transform=target_transforms)
val_loader = DataLoader(val_dataset, batch_size=args.bs, shuffle=True)
dataloaders = {
'train' : train_loader,
'val' : val_loader
}
dataset_sizes = {'train': len(train_loader.dataset), 'val' : len(val_loader.dataset)}
model = LaneNet(arch=args.model_type)
model.to(DEVICE)
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
print(f"{args.epochs} epochs {len(train_dataset)} training samples\n")
model, log = train_model(model, optimizer, scheduler=None, dataloaders=dataloaders, dataset_sizes=dataset_sizes, device=DEVICE, loss_type=args.loss_type, num_epochs=args.epochs)
df=pd.DataFrame({'epoch':[],'training_loss':[],'val_loss':[]})
df['epoch'] = log['epoch']
df['training_loss'] = log['training_loss']
df['val_loss'] = log['val_loss']
train_log_save_filename = os.path.join(save_path, 'training_log.csv')
df.to_csv(train_log_save_filename, columns=['epoch','training_loss','val_loss'], header=True,index=False,encoding='utf-8')
print("training log is saved: {}".format(train_log_save_filename))
model_save_filename = os.path.join(save_path, 'best_model.pth')
torch.save(model.state_dict(), model_save_filename)
print("model is saved: {}".format(model_save_filename))
if __name__ == '__main__':
train()