-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
150 lines (128 loc) · 5.54 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
import os
import argparse
from dataset import TreeDataset
from torch.utils.tensorboard import SummaryWriter
from tqdm.autonotebook import tqdm
import shutil
import torch
from torch.utils.data import DataLoader
from torchvision.models.detection import fasterrcnn_mobilenet_v3_large_320_fpn, FasterRCNN_MobileNet_V3_Large_320_FPN_Weights
from torchvision.transforms import Compose, ToTensor, Resize, ColorJitter, Normalize
from torchmetrics.detection.mean_ap import MeanAveragePrecision
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--data_path', type=str, default='Tree detection')
parser.add_argument('-e', '--epochs', type=int, default=50)
parser.add_argument('-b', '--batch_size', type=int, default=16)
parser.add_argument('-l', '--learning_rate', type=float, default=0.001)
parser.add_argument('-s', '--save_path', type=str, default='trained_models')
parser.add_argument('-c', '--checkpoint_path', type=str, default='trained_models/last.pt')
parser.add_argument('-t', '--tensorboard_path', type=str, default='tensorboard')
args = parser.parse_args()
return args
def collate(batch):
images, labels = zip(*batch)
return list(images), list(labels)
def train(args):
# device and model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = fasterrcnn_mobilenet_v3_large_320_fpn(weights=FasterRCNN_MobileNet_V3_Large_320_FPN_Weights, trainable_backbone_layers=5).to(device)
# Define data transformations for training and validation datasets
training_transform = Compose([
ColorJitter(brightness=0.125, contrast=0.5, saturation=0.5, hue=0.05),
ToTensor(),
Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
val_transform = Compose([
ToTensor(),
Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
])
# Create training and validation datasets
trainset = TreeDataset(args.data_path, train=True, transform=training_transform)
valset = TreeDataset(args.data_path, train=False, transform=val_transform)
# Create training and validation dataloaders
training_dataloader = DataLoader(
dataset=trainset,
batch_size=args.batch_size,
num_workers=6,
shuffle=True,
collate_fn=collate
)
val_dataloader = DataLoader(
dataset=valset,
batch_size=args.batch_size,
shuffle=False,
num_workers=6,
collate_fn=collate
)
# Define optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
# load if model existed
if args.checkpoint_path and os.path.isfile(args.checkpoint_path):
checkpoint = torch.load(args.checkpoint_path)
start_epoch = checkpoint['epoch']
model.load_state_dict(checkpoint['model'])
optimizer.load_state_dict(checkpoint['optimizer'])
best_map = checkpoint['best_map']
else:
start_epoch = 0
best_map = 0
# Create a checkpoint directory for training
if not os.path.isdir(args.save_path):
os.mkdir(args.save_path)
# Create a directory for Tensorboard logs
if os.path.isdir(args.tensorboard_path):
shutil.rmtree(args.tensorboard_path)
os.mkdir(args.tensorboard_path)
# Set up Tensorboard writer
writer = SummaryWriter(args.tensorboard_path)
num_iters = len(training_dataloader)
# Loop through each epoch
for epoch in range(start_epoch, args.epochs):
model.train()
progress_bar = tqdm(training_dataloader, colour='cyan')
# Loop through each batch
for i, (images, targets) in enumerate(progress_bar):
# put images, targets to device
images = [image.to(device) for image in images]
list_targets = []
for target in targets:
list_targets.append({key: value.to(device) for key, value in target.items()})
loss_components = model(images, list_targets)
losses = sum(loss for loss in loss_components.values())
progress_bar.set_description("Epoch {}/{}. Loss {:0.4f}".format(epoch+1, args.epochs, losses))
writer.add_scalar("Train/loss", losses, epoch * len(training_dataloader) + i)
# optimize
optimizer.zero_grad()
losses.backward()
optimizer.step()
model.eval()
metric = MeanAveragePrecision(iou_type='bbox')
progress_bar = tqdm(val_dataloader, colour='green')
for i, (images, targets) in enumerate(progress_bar):
images = [image.to(device) for image in images]
with torch.no_grad():
predictions = model(images)
list_targets = []
for target in targets:
list_targets.append({key: value.to(device) for key, value in target.items()})
metric.update(predictions, list_targets)
map = metric.compute()
writer.add_scalar("Val/mAP", map["map"], epoch)
writer.add_scalar("Val/mAP50", map["map_50"], epoch)
writer.add_scalar("Val/mAP75", map["map_75"], epoch)
checkpoint = {
'model' : model.state_dict(),
'optimizer' : optimizer.state_dict(),
'epoch' : epoch+1,
'best_map': map["map"]
}
torch.save(checkpoint, os.path.join(args.save_path, 'last.pt'))
if map["map"] > best_map:
torch.save(checkpoint, os.path.join(args.save_path, "best.pt"))
best_map = map["map"]
if __name__ == '__main__':
args = parse_args()
train(args)