-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_train.py
122 lines (99 loc) · 3.04 KB
/
02_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
#%%
import terrain_set2
from torch.utils.data import DataLoader
import numpy as np
import pandas as pd
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
#%%
n=128
boundl=256
rescale=1
batch=256//rescale
ts = terrain_set2.TerrainSet('data/USGS_1M_10_x43y465_OR_RogueSiskiyouNF_2019_B19.tif',
size=n, stride=8, rescale=rescale)
t,v = torch.utils.data.random_split(ts, [0.90, 0.10])
train = DataLoader(t, batch_size=batch, shuffle=True,
num_workers=2, pin_memory=True, persistent_workers=True, prefetch_factor=4)
val = DataLoader(v, batch_size=batch, shuffle=True,
num_workers=2, pin_memory=True, persistent_workers=True, prefetch_factor=4)
#%%
print("%d,%d" % (len(train), len(val)))
#%%
class View(nn.Module):
def __init__(self, dim, shape):
super(View, self).__init__()
self.dim = dim
self.shape = shape
def forward(self, input):
new_shape = list(input.shape)[:self.dim] + list(self.shape) + list(input.shape)[self.dim+1:]
return input.view(*new_shape)
# https://github.com/pytorch/pytorch/issues/49538
nn.Unflatten = View
if rescale==4:
net = nn.Sequential(
nn.Linear(boundl,2048),
nn.ReLU(True),
nn.Dropout(p=0.5),
nn.Linear(2048, n*n),
nn.ReLU(True),
nn.Unflatten(1, (128, 128)),
)
else:
net = nn.Sequential(
nn.Linear(boundl,1024),
nn.ReLU(True),
# For 2 bounds:
#nn.Dropout(p=0.1),
# For 1 bound?
nn.Dropout(p=0.5),
nn.Linear(1024, n*n),
nn.ReLU(True),
nn.Unflatten(1, (128, 128)),
)
net = net.to(device)
opt = optim.Adam(net.parameters())
lossfn = nn.MSELoss()
min_val_loss = 9999999999.0
early_stop_counter = 0
for epoch in range(9999): # loop over the dataset multiple times
running_loss = 0.0
net.train()
for i, data in enumerate(train, 0):
inputs, targets = data
inputs = inputs[:,0:boundl]
# zero the parameter gradients
opt.zero_grad()
# forward + backward + optimize
outputs = net(inputs.to(device))
loss = lossfn(outputs, targets.to(device))
loss.backward()
opt.step()
# print statistics
running_loss += loss.item()
if i % 10 == 9:
print("train: %.2f" % (running_loss/10.0))
running_loss = 0.0
running_loss = 0.0
net.eval()
with torch.no_grad():
for i,data in enumerate(val, 0):
inputs, targets = data
inputs = inputs[:,0:boundl]
outputs = net(inputs.to(device))
loss = lossfn(outputs, targets.to(device))
running_loss += loss.item()
vl = running_loss/len(val)
print("val: %.2f" % (vl))
if vl<min_val_loss:
min_val_loss = vl
early_stop_counter = 0
print('saving...')
torch.save(net, 'models/02-%d-%d'%(boundl, rescale))
else:
early_stop_counter += 1
if early_stop_counter>=3:
break