-
Notifications
You must be signed in to change notification settings - Fork 0
/
10_explore_ae.py
161 lines (127 loc) · 3.65 KB
/
10_explore_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
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
#%%
import terrain_set
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 terrain_set
import matplotlib.pyplot as plt
from matplotlib.colors import LightSource
from matplotlib import cm
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
torch.manual_seed(1)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
#%%
n=128
ts = terrain_set.TerrainSet('data/USGS_1M_10_x43y465_OR_RogueSiskiyouNF_2019_B19.tif',
size=n, stride=8, local_norm=True, square_output=True)
#%%
net = torch.load('models/08-full-ae-vl1.36')
encoder = net[:27]
decoder = net[27:]
encoder.eval()
decoder.eval()
size = 128
#%%
def plot_surface(ax, data, cmap, alpha):
meshx, meshy = np.meshgrid(np.linspace(0, size, size), np.linspace(0, size, size))
ls = LightSource(270, 45)
rgb = ls.shade(data, cmap=cmap, vert_exag=0.1, blend_mode='soft')
_ = ax.plot_surface(meshx, meshy, data,
facecolors=rgb, linewidth=0, antialiased=False, shade=False, alpha=alpha)
def plot_boundary(ax, data):
ax.plot(
np.full(size, 0),
np.linspace(0, size-1, size),
data[:,0],
color="red", linewidth=2, zorder=100
)
ax.plot(
np.linspace(0, size-1, size),
np.full(size, 0),
data[0,:],
color="red", linewidth=2, zorder=100
)
ax.plot(
np.full(size, size),
np.linspace(0, size-1, size),
data[:, size-1],
color="purple", linewidth=2, zorder=100
)
ax.plot(
np.linspace(0, size-1, size),
np.full(size, size),
data[size-1, :],
color="purple", linewidth=2, zorder=100
)
def show(target, out, r=35):
_, ax = plt.subplots(2,2, subplot_kw=dict(projection='3d'), figsize=(10, 10))
ax1, ax2, ax3, ax4 = ax.flatten()
plot_surface(ax1, target, cm.gist_earth, 1.0)
plot_surface(ax2, out, cm.gist_earth, 1.0)
plot_boundary(ax1, target)
plot_boundary(ax2, target)
plot_surface(ax3, target, cm.gist_earth, 1.0)
plot_surface(ax4, out, cm.gist_earth, 1.0)
plot_boundary(ax3, target)
plot_boundary(ax4, target)
ax1.azim = 180+r
ax2.azim = 180+r
ax1.elev= 35
ax2.elev= 35
ax1.set_title('Truth')
ax2.set_title('Model')
ax3.azim = r
ax4.azim = r
ax3.elev= 35
ax4.elev= 35
ax3.set_title('Truth (back)')
ax4.set_title('Model (back)')
plt.show()
#%%
_,target = ts[25001]
inp = torch.Tensor([target]).unsqueeze(1).to(device)
noise = torch.randn(1, 1, 128, 128).to(device)
with torch.no_grad():
noisy_inp = inp+3*noise
v = encoder(noisy_inp)
out = decoder(v).cpu().squeeze(1)
show(noisy_inp[0][0].cpu().numpy(), out[0].numpy(), r=45)
#%%
dl = DataLoader(ts, batch_size=256, shuffle=False,
num_workers=2, pin_memory=True, persistent_workers=True, prefetch_factor=4)
vs = np.ndarray((0,256))
print(len(dl)*256)
with torch.no_grad():
for i, data in enumerate(dl, 0):
_, targets = data
v = encoder(targets.unsqueeze(1).to(device))
vs = np.concatenate((vs, v.cpu()))
print(len(vs))
vs.shape
#%%
df = pd.DataFrame({'v': vs.tolist()})
df
#%%
df.to_parquet('data/ea-embeds.parquet')
#%%
_,truth = ts[25001]
with torch.no_grad():
inp = torch.Tensor([df.loc[25001]['v']]).to(device)
out = decoder(inp).cpu().squeeze(1)
show(truth, out[0].numpy(), r=45)
#%%
pca = PCA(n_components=50)
p = pca.fit_transform(vs)
tsne = TSNE(n_components=2)
t = tsne.fit_transform(p)
#%%
tr = t.reshape((2,len(t)))
plt.scatter(tr[0], tr[1], cmap='hot')
plt.show()
#%%
# todo check loss on test dataset!