-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrain_Efusion_MLP.py
268 lines (225 loc) · 10.6 KB
/
train_Efusion_MLP.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import os
import copy
import argparse
import torch
import random
import numpy as np
import torch.nn.functional as F
from torch.utils.data import DataLoader
from datasets.dataset_dfc import DFC2020
from networks.propnets import E_Fusion
from networks.linear_eva import MLP
import utils.metrics as metrics
from utils.util import adjust_learning_rate, AverageMeter, accuracy, seed_torch, visualization
def parse_option():
parser = argparse.ArgumentParser('argument for training')
# 1600
parser.add_argument('--batch_size', type=int, default=10, help='batch_size')
parser.add_argument('--crop_size', type=int, default=64, help='crop_size')
parser.add_argument('--num_workers', type=int, default=0, help='num of workers to use')
parser.add_argument('--epochs', type=int, default=50, help='number of training epochs')
parser.add_argument('--in_dim', type=int, default=256, help='dim of feat for inner product')
parser.add_argument('--feat_dim', type=int, default=256, help='dim of feat for inner product')
# resume path
parser.add_argument('--resume', action='store_true', default=False, help='path to latest checkpoint (default: none)')
parser.add_argument('--valid', action='store_true', default=True, help='for validation')
# input/output
parser.add_argument('--use_s2hr', action='store_true', default=True, help='use sentinel-2 high-resolution (10 m) bands')
parser.add_argument('--use_s2mr', action='store_true', default=False, help='use sentinel-2 medium-resolution (20 m) bands')
parser.add_argument('--use_s2lr', action='store_true', default=False, help='use sentinel-2 low-resolution (60 m) bands')
parser.add_argument('--use_s1', action='store_true', default=True, help='use sentinel-1 data') #True for OSCD False for DFC2020
parser.add_argument('--no_savanna', action='store_true', default=False, help='ignore class savanna')
# add new views
#'/workplace/OSCD'
#'/R740-75T/Chenyx/Workplace/OSCD'
parser.add_argument('--data_dir_train', type=str, default='/workplace/DFC2020', help='path to training dataset')
parser.add_argument('--model_path', type=str, default='./save', help='path to save model')
parser.add_argument('--save', type=str, default='./mlp_Efusion1000', help='path to save linear classifier')
parser.add_argument('--save_freq', type=int, default=50, help='number of training epochs')
parser.add_argument('--preview_dir', type=str, default='./preview_Efusion',
help='path to preview dir (default: no previews)')
opt = parser.parse_args()
# set up saving name
if not os.path.isdir(opt.save):
os.makedirs(opt.save)
if (opt.data_dir_train is None) or (opt.model_path is None):
raise ValueError('one or more of the folders is None: data_folder | model_path | tb_path')
if not os.path.isdir(opt.data_dir_train):
raise ValueError('data path not exist: {}'.format(opt.data_dir_train))
return opt
def get_train_loader(args):
# load datasets
train_set = DFC2020(args.data_dir_train,
subset="train",
no_savanna=args.no_savanna,
use_s2hr=args.use_s2hr,
use_s2mr=args.use_s2mr,
use_s2lr=args.use_s2lr,
use_s1=args.use_s1,
transform=False,
unlabeled=False,
train_index='./utils/train1000.npy')
n_classes = train_set.n_classes
n_inputs = train_set.n_inputs
args.no_savanna = train_set.no_savanna
args.display_channels = train_set.display_channels
args.brightness_factor = train_set.brightness_factor
valid_set = DFC2020(args.data_dir_train,
subset="train",
no_savanna=args.no_savanna,
use_s2hr=args.use_s2hr,
use_s2mr=args.use_s2mr,
use_s2lr=args.use_s2lr,
use_s1=args.use_s1,
transform=False,
unlabeled=False,
train_index='./utils/vali5114.npy')
# set up dataloaders
train_loader = DataLoader(train_set,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers,
pin_memory=True,
drop_last=False)
valid_loader = DataLoader(valid_set,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers,
pin_memory=True,
drop_last=False)
return train_loader, valid_loader, n_inputs, n_classes
def unet_encoder_factory(args, pretrained=True):
"""Constructs a ResNet-50 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
input_channels: the number of output channels
"""
# build model
model = E_Fusion(width=1, in_channel=6, in_dim=args.in_dim, feat_dim=args.feat_dim).to(args.device)
if pretrained:
# load pre-trained model
print('==> loading pre-trained model')
ckpt = torch.load('./save/Efusion/twins_epoch_699_10.309292793273926.pth')
pretrained_dict = ckpt['online_network_state_dict']
model_dict = model.state_dict()
# filter out unnecessary keys
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
# 2. overwrite entries in the existing state dict
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
return model
def train(epoch, train_loader, online_network, classifier, criterion, optimizer, args):
"""
one epoch training
"""
# set model to train mode
online_network.eval()
classifier.train()
for idx, (batch) in enumerate(train_loader):
# unpack sample
image, target = batch['image'], batch['label']
if args.use_gpu:
image, target = image.cuda(), target.cuda()
# ===================forward=====================
with torch.no_grad():
feature1 = online_network(image, None, mode=1)
prediction = classifier(feature1)
loss = criterion(prediction, target)
# ===================backward=====================
# reset gradients
optimizer.zero_grad()
loss.backward(retain_graph=True)
optimizer.step()
# print info
print(f'\rtrain loss : {loss.item():.5f}| step :{idx}/{len(train_loader)}|{epoch}', end='', flush=True)
def validate(val_loader, online_network, classifier, criterion, args):
"""
evaluation
"""
# switch to evaluate mode
online_network.eval()
classifier.eval()
# main validation loop
loss = 0
conf_mat = metrics.ConfMatrix(args.n_classes)
with torch.no_grad():
for idx, (batch) in enumerate(val_loader):
# unpack sample
image, target = batch['image'], batch['label']
if args.use_gpu:
image, target = image.cuda(), target.cuda()
# ===================forward=====================
feature1 = online_network(image, None, mode=1)
prediction = classifier(feature1)
loss += criterion(prediction, target).cpu().item()
# calculate error metrics
conf_mat.add_batch(target, prediction.max(1)[1])
if args.visual:
visualization(prediction, target, batch['id'], image, args)
#print("[Val] AA: {:.2f}%".format(conf_mat.get_aa() * 100))
#print("[Val] mIoU: ", conf_mat.get_mIoU())
if args.valid:
print("[Val] AA: {:.2f}%".format(conf_mat.get_aa() * 100))
print("[Val] SA: ", conf_mat.get_sa())
print("[Val] mIoU: ", conf_mat.get_mIoU())
print("[Val] IoU: ", conf_mat.get_cf())
def main():
# set seed
seed_torch(seed=0)
# parse the args
args = parse_option()
# set flags for GPU processing if available
args.device = 'cuda' if torch.cuda.is_available() else 'cpu'
if torch.cuda.is_available():
args.use_gpu = True
else:
args.use_gpu = False
# set the data loader
train_loader, valid_loader, n_inputs, n_classes = get_train_loader(args)
args.n_inputs = n_inputs
args.n_classes = n_classes
# set the model
online_network = unet_encoder_factory(args, pretrained=True)
# predictor network
classifier = MLP(256, n_classes).to(args.device)
args.visual = True
if args.valid:
try:
print('==>loading pretrained Linear model')
checkpoints_folder = os.path.join('.', args.save)
# load pre-trained parameters
load_params = torch.load(os.path.join(os.path.join(checkpoints_folder, 'ckpt_epoch_50.pth')),
map_location=args.device)
classifier.load_state_dict(load_params['classifier'])
except FileNotFoundError:
print("Pre-trained weights not found. Please to check.")
# target encoder
optimizer = torch.optim.SGD(classifier.parameters(), lr=0.01, momentum=0.9, weight_decay=1e-4)
criterion = torch.nn.CrossEntropyLoss(ignore_index=255, reduction='mean')
# train and valid
args.start_epoch = 1
for epoch in range(args.start_epoch, args.epochs + 1):
if not args.valid:
# adjust_learning_rate(epoch, args, optimizer)
train(epoch, train_loader, online_network, classifier, criterion, optimizer, args)
if epoch % 50 == 0:
validate(valid_loader, online_network, classifier, criterion, args)
else:
validate(valid_loader, online_network, classifier, criterion, args)
break
# save model
if epoch % args.save_freq == 0:
print('==> Saving...')
state = {
'opt': args,
'epoch': epoch,
'classifier': classifier.state_dict(),
'optimizer': optimizer.state_dict(),
}
save_name = 'ckpt_epoch_{epoch}.pth'.format(epoch=epoch)
save_name = os.path.join(args.save, save_name)
print('saving regular model!')
torch.save(state, save_name)
if __name__ == '__main__':
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
main()