-
Notifications
You must be signed in to change notification settings - Fork 0
/
11_bound_ae.py
125 lines (95 loc) · 2.98 KB
/
11_bound_ae.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
import matplotlib.pyplot as plt
from matplotlib.colors import LightSource
from matplotlib import cm
torch.manual_seed(1)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
#%%
n=128
boundl = 256
ts = terrain_set2.TerrainSet('data/USGS_1M_10_x43y465_OR_RogueSiskiyouNF_2019_B19.tif',
size=n, stride=8)
t,v = torch.utils.data.random_split(ts, [0.9, 0.1])
train = DataLoader(t, batch_size=256, shuffle=True,
num_workers=2, pin_memory=True, persistent_workers=True, prefetch_factor=4)
val = DataLoader(v, batch_size=256, shuffle=True,
num_workers=2, pin_memory=True, persistent_workers=True, prefetch_factor=4)
#%%
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
net = torch.load('models/08-full-ae-vl0.63')
decoder = net[27:]
#decoder.eval()
size = 128
for i, param in enumerate(decoder.parameters()):
param.requires_grad = False
print(decoder)
#%%
ch = 16
net = nn.Sequential(
nn.Linear(boundl, 1024),
nn.ReLU(True),
nn.Linear(1024, 256),
nn.ReLU(True),
decoder,
).to(device)
net(torch.Tensor([ts[0][0][0:boundl], ts[1][0][0:boundl]]).to(device)).shape
#%%
opt = optim.Adam(net.parameters())
lossfn = nn.MSELoss()
min_val_loss = 9999999999.0
early_stop_counter = 0
for epoch in range(999): # 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.unsqueeze(1).to(device))
loss.backward()
opt.step()
# print statistics
running_loss += loss.item()
if i % 20 == 19:
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.unsqueeze(1).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/11-%d'%boundl)
else:
early_stop_counter += 1
if early_stop_counter>=3:
break