-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
270 lines (221 loc) · 11.6 KB
/
main.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import os
import tensorflow as tf
import numpy as np
import pandas as pd
from tqdm import tqdm
import geopandas as gpd
from shapely.geometry import Point
from scipy.spatial.distance import cdist
LAND_POLYGONS = gpd.read_file("./GSHHS_f_L3.shp")
LAND_MASK = LAND_POLYGONS.geometry.unary_union
# import matplotlib
# matplotlib.use("tkagg")
from matplotlib import pyplot as plt
import preprocessing_gsv as preprocessing
import models
def map_to_land(y_true, y_pred):
"""
maps all predictions to nearest land coordinate using known geometries
y_true : true image coordinates
y_pred : predicted image coordinates
"""
for i in range(y_pred.shape[0]):
point = Point(y_pred[i, 1], y_pred[i, 0])
if not point.within(LAND_MASK):
for polygon in LAND_MASK.geoms:
if polygon.intersects(point):
land_points = np.array(list(polygon.exterior.coords))
dists = cdist([point.coords[0]], land_points)
nearest_land_point = land_points[np.argmin(dists)]
y_pred[i] = np.array([nearest_land_point[1], nearest_land_point[0]])
return y_true, y_pred
def print_results(models, test_data, test_labels, metrics):
"""
prints the results of each model after training
models : list of models
test_data : model-specific test data
test_labels : model-specific test labels
metrics : table evaluation metrics
"""
table = []
for model, data in zip(models, test_data):
table.append([])
for metric in metrics:
y_true = test_labels[:data.shape[0]]
y_pred = model.call(data)
table[-1].append(metric(y_true, y_pred).numpy())
table_df = pd.DataFrame(
data=table,
index=[model.name for model in models],
columns=[metric.name for metric in metrics])
print()
print(table_df)
print()
def train_model(model, train_data, train_labels, test_data=[], test_labels=[], epochs=10, batch_size=16, summary=False, compile=True):
"""
train a model
model : model inheriting from tf.keras.Model
train_data :
train_labels:
test_data :
test_labels :
epochs : number of training epochs
batch_size : batch size for training
summary : print description of model
compile : False for pre-compiled models
"""
print("\ntraining", model.name, "...")
if compile:
model.compile(optimizer=model.optimizer, loss=model.loss, metrics=[])
model.build(train_data.shape)
if summary:
model.summary()
model.fit(train_data, train_labels, batch_size=batch_size, epochs=epochs, validation_data=(test_data, test_labels))
def train_distribution_model(model, train_data, train_labels, test_data=[], test_labels=[], epochs=10, batch_size=16, summary=False, downsampling=4, verbose=1, compile=True):
"""
[UNUSED] train a distribution model with mu, sigma outputs
model : model inheriting from tf.keras.Model
train_data :
train_labels:
test_data :
test_labels :
epochs : number of training epochs
batch_size : batch size for training
summary : print description of model
downsampling: factor for reducing training samples
verbose : print characteristic
compile : False for pre-compiled models
"""
print("\ntraining", model.name, "...")
if compile:
model.compile(optimizer=model.optimizer, loss=model.loss, metrics=[])
model.build(train_data.shape)
if summary:
model.summary()
train_length = train_data.shape[0]
test_length = test_data.shape[0]
indices = np.arange(train_length)
for epoch in range(epochs):
np.random.shuffle(indices)
train_loss = 0
if verbose >= 1: print("epoch", epoch + 1, "/", epochs)
for i in tqdm(range(0, train_length, batch_size * downsampling)):
batch_indices = indices[i:i + batch_size]
batch_data = train_data[batch_indices]
batch_labels = train_labels[batch_indices]
with tf.GradientTape() as tape:
mu = model(batch_data)
loss = model.loss.call(batch_labels, mu)
train_loss += loss.numpy() / (train_length // batch_size)
gradients = tape.gradient(loss, model.trainable_variables)
model.optimizer.apply_gradients(zip(gradients, model.trainable_variables))
if not len(test_data) == 0:
if verbose >= 1:
test_indices = np.arange(test_length)
np.random.shuffle(test_indices)
test_batch_indices = test_indices[:batch_size * 8]
test_batch_data = test_data[test_batch_indices]
test_batch_labels = test_labels[test_batch_indices]
test_mu = model(test_batch_data)
test_loss = model.loss.call(test_batch_labels, test_mu).numpy()
# train_acc = models.DistanceAccuracy().call(train_labels[:batch_size], model(train_data[:batch_size])[0])
test_acc = models.DistanceAccuracy().call(test_batch_labels, test_mu).numpy()
print("training loss :", train_loss, "testing loss :", test_loss, "testing accuracy :", test_acc)
if verbose >= 2:
test_mu_mu = tf.math.reduce_mean(test_mu, axis=0)
test_mu_std = tf.math.reduce_std(test_mu, axis=0)
test_labels_std = tf.math.reduce_std(test_batch_labels, axis=0)
print("testing mean :", test_mu_mu.numpy(), "testing std :", test_mu_std.numpy(), "labels std :", test_labels_std.numpy())
else:
if verbose >= 1: print("training loss :", train_loss)
if verbose == 0:
if not len(test_data) == 0:
test_indices = np.arange(test_length)
np.random.shuffle(test_indices)
test_batch_indices = test_indices[:batch_size * 8]
test_batch_data = test_data[test_batch_indices]
test_batch_labels = test_labels[test_batch_indices]
test_mu, test_sigma = model(test_batch_data)
test_loss = model.loss.call(test_batch_labels, test_mu, test_sigma).numpy()
print("training loss :", train_loss, "testing loss :", test_loss)
else:
print("training loss :", train_loss)
def main(save=True, load=False, train=True, load_model=False, save_model=True):
"""
main function for training
save : save loaded images/features to local directory
load : load images/features to local directory
load_model : load models from weights.h5 file
save_model : save models to weights.h5 file
"""
data_path = "data/"
features_path = "features/"
weights_path = "weights/"
if load:
images, labels, cities = preprocessing.load_data(data_path)
images, labels, cities = preprocessing.shuffle_data(images, labels, cities)
cities, city_labels = preprocessing.ohe_cities_labels(cities, np.copy(labels))
print("\nloading features from", features_path, "...")
features = np.load(features_path + "features.npy")
else:
images, labels, cities = preprocessing.load_random_data(num_per_city=400)
preprocessing.plot_points([labels], "world_image.jpeg", density_map=True, normalize_points=True)
images, labels, cities = preprocessing.shuffle_data(images, labels, cities)
images, labels, cities = preprocessing.uniform_geographic_distribution(images, labels, cities, radius=40, maximum=400)
preprocessing.plot_points([labels], "world_image.jpeg", density_map=True, normalize_points=True)
features = preprocessing.pass_through_VGG(images)
if save:
print("\nsaving data to", data_path, "...")
preprocessing.remove_files(data_path + "*")
preprocessing.save_data(images, labels, cities, data_path)
print("\nsaving features to", features_path, "...")
np.save(features_path + "features", features)
# data preprocessing
labels = preprocessing.normalize_labels(labels)
grouped_features, grouped_feature_labels, grouped_feature_cities = preprocessing.reshape_grouped_features(features, labels, cities)
features, feature_labels, feature_cities = preprocessing.reshape_features(features, labels, cities)
train_images, test_images = preprocessing.train_test_split(images)
train_labels, test_labels = preprocessing.train_test_split(labels)
train_cities, test_cities = preprocessing.train_test_split(cities)
train_features, test_features = preprocessing.train_test_split(features)
train_feature_labels, test_feature_labels = preprocessing.train_test_split(feature_labels)
train_feature_cities, test_feature_cities = preprocessing.train_test_split(feature_cities)
train_grouped_features, test_grouped_features = preprocessing.train_test_split(grouped_features)
grouped_feature_labels = preprocessing.expand_and_group_feature_labels(labels)
grouped_feature_cities = preprocessing.expand_and_group_feature_labels(cities)
train_grouped_labels, test_grouped_labels = preprocessing.train_test_split(grouped_feature_labels)
train_grouped_cities, test_grouped_cities = preprocessing.train_test_split(grouped_feature_cities)
# initial model classifier
city_model = models.VGGCityModel(input_shape=images.shape[1:], output_units=cities.shape[1], dropout=0.5)
city_model.compile(optimizer=city_model.optimizer, loss=city_model.loss, metrics=["accuracy"])
city_model.build(train_images.shape)
if load_model: city_model.load_weights("city_model_weights.h5")
city_model.summary()
city_model.fit(train_images, train_cities, batch_size=32, epochs=10, validation_data=(test_images, test_cities))
if save_model: city_model.save_weights("city_model_weights.h5")
# worldNET interpolation head from model classifier
worldNET_city = models.worldNETCity(city_model=city_model, city_labels=city_labels, output_units=cities.shape[1], units=64, layers=2)
worldNET_city.compile(optimizer=worldNET_city.optimizer, loss=worldNET_city.loss, metrics=[])
worldNET_city.build(train_images.shape)
if load_model: worldNET_city.load_weights("worldNET_weights.h5")
worldNET_city.summary()
worldNET_city.fit(train_images, train_labels, batch_size=32, epochs=10, validation_data=(test_images, test_labels))
if save_model: worldNET_city.save_weights("worldNET_weights.h5")
# view example worldNET predictions
y_pred = worldNET_city(test_images[:16])[0]
y_true = test_labels[:16]
y_true, y_pred = map_to_land(y_true, y_pred)
preprocessing.plot_points([y_pred, y_true], "world_image.jpeg", colors=['b', 'r'])
# control models
naive_vgg_model = models.NaiveVGG(units=512, output_units=2, layers=2)
train_model(naive_vgg_model, train_grouped_features, train_labels, test_grouped_features, test_labels, epochs=1, batch_size=16)
mean_model = models.MeanModel(train_labels=train_labels, loss_fn=models.MeanHaversineDistanceLoss())
guess_model = models.GuessModel(train_labels=train_labels, loss_fn=models.MeanHaversineDistanceLoss())
randomized_guess_model = models.RandomizedGuessModel()
# create table for final results
print_results([mean_model, guess_model, randomized_guess_model, naive_vgg_model, worldNET_city],
[test_images, test_images, test_images, test_images[:64], test_images[:64]],
test_labels, metrics=[tf.keras.losses.MeanSquaredError(), models.MeanHaversineDistanceLoss(), models.DistanceAccuracy()])
if __name__ == "__main__":
os.system("clear")
main(save=False, load=True, train=True, load_model=True, save_model=True)