-
Notifications
You must be signed in to change notification settings - Fork 2
/
ldn_streethazards.py
134 lines (115 loc) · 4.19 KB
/
ldn_streethazards.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
import os
import torch
import argparse
from utils import Logger
from data import get_dataset, RandomHorizontalFlip, JitterRandomCrop
import torchvision.transforms as tf
from models import LadderDenseNet
from experiments import SemsegExperiment
from PIL import Image
import warnings
warnings.filterwarnings("ignore")
parser = argparse.ArgumentParser('Semseg training')
parser.add_argument('--dataroot',
help='dataroot',
type=str,
default='.')
parser.add_argument('--batch_size',
help='number of images in a mini-batch.',
type=int,
default=16)
parser.add_argument('--num_classes',
help='num classes of segmentator.',
type=int,
default=12)
parser.add_argument('--epochs',
help='maximum number of training epoches.',
type=int,
default=120)
parser.add_argument('--lr',
help='initial learning rate.',
type=float,
default=4e-4)
parser.add_argument('--lr_min',
help='min learning rate.',
type=float,
default=1e-7)
parser.add_argument('--momentum',
help='beta1 in Adam optimizer.',
type=float,
default=0.9)
parser.add_argument('--decay',
help='beta2 in Adam optimizer.',
type=float,
default=0.999)
parser.add_argument('--exp_name',
help='experiment name',
type=str,
required=True)
args = parser.parse_args()
class Args:
def __init__(self):
self.last_block_pooling = 0
def load_imagenet(segmentator):
from torch.utils.model_zoo import load_url as load_state_dict_from_url
state = load_state_dict_from_url('https://download.pytorch.org/models/densenet121-a639ec97.pth')
# state = load_state_dict_from_url('https://download.pytorch.org/models/densenet169-b2777c0a.pth')
ldn_state = {}
for k, v in state.items():
if 'transition' not in k:
k = k.replace('norm.', 'norm')
k = k.replace('conv.', 'conv')
ldn_state[k] = v
miss, unex = segmentator.backbone.load_state_dict(ldn_state, strict=False)
print('Missing:', len(miss), 'Unexpected:', len(unex))
return segmentator
def main(args):
device = 'cuda' if torch.cuda.is_available() else 'cpu'
exp_dir = f"./logs/{args.exp_name}"
if os.path.exists(exp_dir):
raise Exception('Directory exists!')
os.makedirs(exp_dir, exist_ok=True)
CROP_SIZE = 768
logger = Logger(f"{exp_dir}/log.txt")
logger.log(str(args))
train_transforms = {
'image': [
tf.ToTensor(),
],
'target': [
tf.ToTensor(),
],
'joint': [
JitterRandomCrop(size=CROP_SIZE, scale=(0.5, 2), ignore_id=args.num_classes, input_mean=(84, 88, 95)),
# streethazards mean
RandomHorizontalFlip()
]
}
val_transforms = {
'image': [
tf.ToTensor(),
],
'target': [
tf.ToTensor(),
],
'joint': None
}
loaders = get_dataset('street-hazards-full')(args.dataroot, args.batch_size, train_transforms, val_transforms)
model = LadderDenseNet(args=Args(), num_classes=args.num_classes, checkpointing=True).to(device)
model = load_imagenet(model)
backbone_params = list(model.backbone.parameters())
upsample_params = list(model.upsample.parameters()) \
+ list(model.spp.parameters()) \
+ list(model.logits.parameters())
lr_backbone = args.lr / 4.
optimizer = torch.optim.Adam([
{'params': backbone_params, 'lr': lr_backbone},
{'params': upsample_params}
], lr=args.lr, betas=(0.9, 0.999), eps=1e-7)
if device == 'cuda':
torch.backends.cudnn.benchmark = True
experiment = SemsegExperiment(
model, optimizer, loaders, args.epochs, logger, device, f"{exp_dir}/checkpoint.pt", args)
experiment.start()
if __name__ == '__main__':
main(args)