-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
162 lines (126 loc) · 6.2 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
151
152
153
154
155
156
157
158
159
160
161
162
from utils import *
import torch.nn as nn
from configs_7days import get_args
from functions import train, test
from read_npy import create_dataloaders
from skimage.transform import resize
from torch.utils.tensorboard import SummaryWriter
def check_size_for_reize(img_size, num_heads):
if img_size[0] % num_heads != 0:
img_size_new = [(int(img_size[0] / num_heads) + 1) * num_heads, img_size[1]]
print("New image size is", img_size_new)
return img_size_new
else:
return img_size
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def embed_dim_by_img(img, num_heads, emb_mult):
emb_dim = img * emb_mult
head_det = emb_dim % num_heads
if head_det != 0:
emb_dim = emb_dim - head_det + num_heads
return emb_dim
def count_patch_size(imgsize):
patch = imgsize ** 0.5
if imgsize % patch == 0:
return patch
else:
while imgsize % patch != 0:
patch = int(patch) - 1
return patch
# Loss f-n
loss_l1 = torch.nn.L1Loss()
accumulation_steps = 12
writer = SummaryWriter(f'writer/lstm_104_52_predtrain')
def setup(args):
path_to_mask = r'D:\Projects\test_cond\AAAI_code\Ice\coastline_masks'
path_to_sea = r'D:\Projects\test_cond\AAAI_code\Ice'
device = 'cuda' if torch.cuda.is_available() else 'cpu'
lr_max = 0.0005
lr_min = 0.00001
epochs = 90
predict_period = 52
in_period = 86
batch_size = 1
num_heads = 8
emb_mult = 4
place = 'kara'
from_ymd_train = [1979, 1, 1]
to_ymd_train = [2012, 1, 1]
from_ymd_test = [2012, 1, 2]
to_ymd_test = [2020, 1, 1]
stride = 7
mask = np.load(fr'{path_to_mask}\{place}_mask.npy')
resize_img = [64, 64]
if resize_img is not None:
resize_img = check_size_for_reize(resize_img, num_heads=num_heads)
mask = np.load(fr'{path_to_mask}\{place}_mask.npy')
mask = resize(mask, (resize_img[0], resize_img[1]), anti_aliasing=False)
dataloader_train, img_sizes = create_dataloaders(path_to_dir=f'{path_to_sea}/{place}',
batch_size=batch_size,
in_period=in_period,
predict_period=predict_period,
stride=stride,
test_end=None,
from_ymd=from_ymd_train,
to_ymd=to_ymd_train,
pad=False,
train_test_split=None,
resize_img=resize_img)
dataloader_test, img_sizes = create_dataloaders(path_to_dir=f'{path_to_sea}/{place}',
batch_size=1,
in_period=in_period,
predict_period=predict_period,
stride=stride,
test_end=None,
from_ymd=from_ymd_test,
to_ymd=to_ymd_test,
pad=False,
train_test_split=None,
resize_img=resize_img)
train_len = dataloader_train.__len__()
test_len = dataloader_test.__len__()
if img_sizes[1] > img_sizes[0]:
img_sizes = (img_sizes[1], img_sizes[0])
if img_sizes[1] != img_sizes[0]:
patch_size1 = count_patch_size(img_sizes[0]) # int(img_sizes[0]/(img_sizes[0]*2)**0.5)
patch_size2 = count_patch_size(img_sizes[1])
patch_size = [patch_size1, patch_size2] # int(img_sizes[1]/(img_sizes[1]*2)**0.5)
else:
patch_size = int(img_sizes[0] / (img_sizes[0] * 2) ** 0.5)
embed_dim = 128
if args.model == 'SwinLSTM-D':
from SwinLSTM_D import SwinLSTM
model = SwinLSTM(img_size=args.input_img_size, patch_size=args.patch_size,
in_chans=args.input_channels, embed_dim=embed_dim,
depths_downsample=args.depths_down, depths_upsample=args.depths_up,
num_heads=args.heads_number, window_size=args.window_size).to(args.device)
model.load_state_dict(torch.load(r'results_12_6\model\trained_model_state_dict_mse_80.86911010742188'))
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
criterion = nn.MSELoss()
return model, criterion, optimizer, dataloader_train, dataloader_test
def main(writer, accumulation_steps):
args = get_args()
set_seed(args.seed)
cache_dir, model_dir, log_dir = make_dir(args)
logger = init_logger(log_dir)
model, criterion, optimizer, train_loader, valid_loader = setup(args)
train_losses, valid_losses = [], []
best_metric = (0, float('inf'), float('inf'))
for epoch in range(args.epochs):
start_time = time.time()
train_loss = train(args, logger, epoch, model, train_loader, criterion, optimizer, writer,
accumulation_steps=accumulation_steps)
train_losses.append(train_loss)
plot_loss(train_losses, 'train', epoch, args.res_dir, 1)
if (epoch + 1) % args.epoch_valid == 0:
valid_loss, mse, ssim = test(args, logger, epoch, model, valid_loader, criterion, cache_dir, writer)
valid_losses.append(valid_loss)
plot_loss(valid_losses, 'valid', epoch, args.res_dir, args.epoch_valid)
if mse < best_metric[1]:
torch.save(model.state_dict(), f'{model_dir}/trained_model_state_dict_mse_{mse}')
best_metric = (epoch, mse, ssim)
logger.info(f'[Current Best] EP:{best_metric[0]:04d} MSE:{best_metric[1]:.4f} SSIM:{best_metric[2]:.4f}')
print(f'Time usage per epoch: {time.time() - start_time:.0f}s')
if __name__ == '__main__':
main(writer=writer, accumulation_steps=accumulation_steps)