-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_seq.py
249 lines (200 loc) · 10.3 KB
/
train_seq.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
'''
This is the main code
Ask the argument
Make the loaders
Make the train
'''
import argparse
from pathlib import Path
import torch
from torch import nn
from torch.optim import Adam
from torch.utils.data import DataLoader
from torchvision import transforms, datasets, models
import torch.backends.cudnn as cudnn
import torch.backends.cudnn
import json
from models import UNet11,UNet, AlbuNet34, SegNet
from dataset import WaterDataset
from torch.optim import lr_scheduler ####
import utilsTrain_seq
import torch.optim as optim
import numpy as np
import cv2
import glob ###
import os
from get_train_test_kfold import get_split_out, percent_split, get_split_in
from split_train_val import get_files_names
from scalarmeanstd import meanstd
from metrics_prediction_2 import find_metrics
from transformsdata import (DualCompose,
ImageOnly,
Normalize,
HorizontalFlip,
Rotate,
CenterCrop,
VerticalFlip)
def main():
parser = argparse.ArgumentParser()
arg = parser.add_argument
arg('--device-ids', type=str, default='0', help='For example 0,1 to run on two GPUs')
arg('--fold-out', type=int, help='fold train test', default=0)
arg('--fold-in', type=int, help='fold train val', default=0)
arg('--percent', type=float, help='percent of data', default=1)
arg('--root', default='runs/debug', help='checkpoint root')
arg('--batch-size', type=int, default=4)
arg('--limit', type=int, default=10000, help='number of images in epoch')
arg('--n-epochs', type=int, default=40)
arg('--n-steps', type=int, default=200)
arg('--lr', type=float, default=0.003)
arg('--modelVHR', type=str, default='UNet11', choices=['UNet11','UNet','AlbuNet34','SegNet'])
arg('--dataset-path-HR', type=str, default='data_HR', help='ain path of the HR dataset')
arg('--model-path-HR', type=str, default='logs_HR/mapping/model_40epoch_HR_UNet11.pth', help='path of the model of HR')
arg('--dataset-path-VHR', type=str, default='data_VHR', help='ain path of the VHR dataset')
arg('--name-file-HR', type=str, default='_HR', help='name file of HR dataset')
arg('--dataset-file', type=str, default='VHR', help='main dataset resolution,depend of this correspond a specific crop' )
arg('--out-file', type=str, default='seq', help='the file in which save the outputs')
arg('--train-val-file-HR', type=str, default='train_val_HR', help='name of the train-val file' )
arg('--test-file-HR', type=str, default='test_HR', help='name of the test file' )
arg('--train-val-file-VHR', type=str, default='train_val_850', help='name of the train-val file' )
arg('--test-file-VHR', type=str, default='test_850', help='name of the test file' )
args = parser.parse_args()
root = Path(args.root)
root.mkdir(exist_ok=True, parents=True)
num_classes = 1
input_channels=4
if args.modelVHR == 'UNet11':
model_VHR = UNet11(num_classes=num_classes, input_channels=input_channels)
elif args.modelVHR == 'UNet':
model_VHR = UNet(num_classes=num_classes, input_channels=input_channels)
elif args.modelVHR == 'AlbuNet34':
model_VHR =AlbuNet34(num_classes=num_classes, num_input_channels=input_channels, pretrained=False)
elif args.modelVHR == 'SegNet':
model_VHR = SegNet(num_classes=num_classes, num_input_channels=input_channels, pretrained=False)
else:
model_VHR = UNet11(num_classes=num_classes, input_channels=4)
if torch.cuda.is_available():
if args.device_ids:#
device_ids = list(map(int, args.device_ids.split(',')))
else:
device_ids = None
model_VHR = nn.DataParallel(model_VHR, device_ids=device_ids).cuda()
cudnn.benchmark = True
out_path = Path(('logs_{}/mapping/').format(args.out_file))
#Data-paths:--------------------------VHr-------------------------------------
data_path_VHR = Path(args.dataset_path_VHR)
print("data_path:",data_path_VHR)
name_file_VHR = '_'+ str(int(args.percent*100))+'_percent_'+args.out_file
data_all='data'
##--------------------------------------
############################
# NEstes cross validation K-fold train test
##train_val_file_names, test_file_names_HR = get_split_out(data_path_HR,data_all,args.fold_out)
############################
############################ Cross validation
train_val_file_names=np.array(sorted(glob.glob(str((data_path_VHR/args.train_val_file_VHR/'images'))+ "/*.npy")))
test_file_names_VHR = np.array(sorted(glob.glob(str((data_path_VHR/args.test_file_VHR/'images')) + "/*.npy")))
if args.percent !=1:
extra, train_val_file_names= percent_split(train_val_file_names, args.percent)
train_file_VHR_lab,val_file_VHR_lab = get_split_in(train_val_file_names,args.fold_in)
np.save(str(os.path.join(out_path,"train_files{}_{}_fold{}_{}.npy".format(name_file_VHR, args.modelVHR, args.fold_out, args.fold_in))), train_file_VHR_lab)
np.save(str(os.path.join(out_path,"val_files{}_{}_fold{}_{}.npy". format(name_file_VHR, args.modelVHR, args.fold_out, args.fold_in))), val_file_VHR_lab)
#Data-paths:--------------------------unlabeled VHR-------------------------------------
train_path_VHR_unlab= data_path_VHR/'unlabel'/'train'/'images'
val_path_VHR_unlab = data_path_VHR/'unlabel'/'val'/'images'
train_file_VHR_unlab = np.array(sorted(list(train_path_VHR_unlab.glob('*.npy'))))
val_file_VHR_unlab = np.array(sorted(list(val_path_VHR_unlab.glob('*.npy'))))
print('num train_lab = {}, num_val_lab = {}'.format(len(train_file_VHR_lab), len(val_file_VHR_lab)))
print('num train_unlab = {}, num_val_unlab = {}'.format(len(train_file_VHR_unlab), len(val_file_VHR_unlab)))
max_values_VHR, mean_values_VHR, std_values_VHR=meanstd(train_file_VHR_lab, val_file_VHR_lab,test_file_names_VHR,str(data_path_VHR),input_channels)
def make_loader(file_names, shuffle=False, transform=None,mode='train',batch_size=4, limit=None):
return DataLoader(
dataset=WaterDataset(file_names, transform=transform,mode=mode, limit=limit),
shuffle=shuffle,
batch_size=batch_size,
pin_memory=torch.cuda.is_available()
)
#transformations ---------------------------------------------------------------------------
train_transform_VHR = DualCompose([
CenterCrop(512),
HorizontalFlip(),
VerticalFlip(),
Rotate(),
ImageOnly(Normalize(mean=mean_values_VHR,std= std_values_VHR))
])
val_transform_VHR = DualCompose([
CenterCrop(512),
ImageOnly(Normalize(mean=mean_values_VHR, std=std_values_VHR))
])
#-------------------------------------------------------------------
mean_values_HR=(0.11952524, 0.1264638 , 0.13479991, 0.15017026)
std_values_HR=(0.08844988, 0.07304429, 0.06740904, 0.11003125)
train_transform_VHR_unlab = DualCompose([
CenterCrop(512),
HorizontalFlip(),
VerticalFlip(),
Rotate(),
ImageOnly(Normalize(mean=mean_values_HR,std= std_values_HR))
])
val_transform_VHR_unlab = DualCompose([
CenterCrop(512),
ImageOnly(Normalize(mean=mean_values_HR, std=std_values_HR))
])
######################## DATA-LOADERS ###########################################################49
train_loader_VHR_lab = make_loader(train_file_VHR_lab, shuffle=True, transform=train_transform_VHR , batch_size = 2, mode = "train")
valid_loader_VHR_lab = make_loader(val_file_VHR_lab, transform=val_transform_VHR, batch_size = 4, mode = "train")
train_loader_VHR_unlab = make_loader(train_file_VHR_unlab, shuffle=True, transform=train_transform_VHR, batch_size = 4, mode = "unlb_train")
valid_loader_VHR_unlab = make_loader(val_file_VHR_unlab, transform=val_transform_VHR, batch_size = 2, mode = "unlb_val")
dataloaders_VHR_lab= {
'train': train_loader_VHR_lab, 'val': valid_loader_VHR_lab
}
dataloaders_VHR_unlab= {
'train': train_loader_VHR_unlab, 'val': valid_loader_VHR_unlab
}
#----------------------------------------------
root.joinpath(('params_{}.json').format(args.out_file)).write_text(
json.dumps(vars(args), indent=True, sort_keys=True))
# Observe that all parameters are being optimized
optimizer_ft = optim.Adam(model_VHR.parameters(), lr= args.lr)
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=20, gamma=0.1)
#--------------------------model HR-------------------------------------
PATH_HR= args.model_path_HR
#Initialise the model
model_HR = UNet11(num_classes=num_classes)
model_HR.cuda()
model_HR.load_state_dict(torch.load(PATH_HR))
#---------------------------------------------------------------
model_VHR= utilsTrain_seq.train_model(
out_file=args.out_file,
name_file_VHR=name_file_VHR,
model_HR=model_HR,
model_VHR=model_VHR,
optimizer=optimizer_ft,
scheduler=exp_lr_scheduler,
dataloaders_VHR_lab=dataloaders_VHR_lab,
dataloaders_VHR_unlab=dataloaders_VHR_unlab,
fold_out=args.fold_out,
fold_in=args.fold_in,
name_model_VHR=args.modelVHR,
n_steps=args.n_steps,
num_epochs=args.n_epochs
)
torch.save(model_VHR.module.state_dict(), (str(out_path)+'/model{}_{}_foldout{}_foldin{}_{}epochs.pth').format(args.n_epochs,name_file_VHR,args.modelVHR, args.fold_out,args.fold_in,args.n_epochs))
print(args.modelVHR)
max_values_all_VHR=3521
find_metrics(train_file_names=train_file_VHR_lab,
val_file_names=val_file_VHR_lab,
test_file_names=test_file_names_VHR,
max_values=max_values_all_VHR,
mean_values=mean_values_VHR,
std_values=std_values_VHR,
model=model_VHR,
fold_out=args.fold_out,
fold_in=args.fold_in,
name_model=args.modelVHR,
epochs=args.n_epochs,
out_file=args.out_file,
dataset_file=args.dataset_file,
name_file=name_file_VHR)
if __name__ == '__main__':
main()