-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrain_tf_sinr.py
More file actions
178 lines (147 loc) · 5.65 KB
/
Copy pathtrain_tf_sinr.py
File metadata and controls
178 lines (147 loc) · 5.65 KB
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
import json
import math
import os
from pathlib import Path
import click
import numpy as np
import pandas as pd
import tensorflow as tf
import yaml
import tqdm
import wandb
from lib.models.geo_model_net import make_geo_model_net
from utils import make_rand_samples_tf
from lib.models.encoders import CoordEncoder
from sinr_loss import sinr_loss
from lib.data.data_loader import (
make_subsampled_dataset,
load_inat_dataset_from_parquet,
load_inat_dataset_from_parquet_h3,
load_sinr_dataset_from_parquet,
load_sinr_dataset_from_parquet_h3,
)
@tf.function
def apply_gradient(optimizer, model, x, ys, fake_x, pos_weight):
with tf.GradientTape() as tape:
# make predictions for the true training data
yhat = model(x, training=True)
# make predictions for the fake/bg training data
fake_yhat = model(fake_x, training=True)
loss = sinr_loss(ys, yhat, fake_yhat, pos_weight, 1.0)
# do the neural network bits, calculate the gradients and update
# the model weights
gradients = tape.gradient(loss, model.trainable_weights)
optimizer.apply_gradients(zip(gradients, model.trainable_weights))
return loss
@click.command()
@click.option("--config_file", required=True)
def train_model(config_file):
with open(config_file, "r") as f:
config = yaml.safe_load(f)
wandb.init(project="geomodel_tf_sinr", config=config)
if config["sampling"]["discretized_sampling"]:
if config["dataset_type"] == "sinr":
(locs, class_ids, unique_taxa, h3_idx) = load_sinr_dataset_from_parquet_h3(
config["sinr_dataset"]["train_data"],
h3_resolution=config["sampling"]["h3_resolution"]
)
else:
(locs, class_ids, unique_taxa, h3_idx) = load_inat_dataset_from_parquet_h3(
config["inat_dataset"]["spatial_data"],
h3_resolution=config["sampling"]["h3_resolution"]
)
else:
if config["dataset_type"] == "sinr":
(locs, class_ids, unique_taxa) = load_sinr_dataset_from_parquet(
config["sinr_dataset"]["train_data"]
)
elif config["dataset_type"] == "inat":
(locs, class_ids, unique_taxa) = load_inat_dataset_from_parquet(
config["inat_dataset"]["spatial_data"]
)
if config["inputs"]["covariates"] == "env":
raster = np.load(config["bioclim_data"]).astype(np.float32)
elif config["inputs"]["covariates"] == "elev":
raster = np.load(config["elev_data"]).astype(np.float32)
else:
raster = None
encoder = CoordEncoder(config["inputs"]["loc_feat_encoding"], raster)
encoded_locs = encoder.encode(locs)
num_classes = len(unique_taxa)
fcnet = make_geo_model_net(
num_classes=num_classes, num_input_feats=encoder.num_input_feats()
)
losses = []
if config["sampling"]["discretized_sampling"]:
ds, num_train_steps_per_epoch = make_subsampled_dataset(
config["hard_cap"],
encoded_locs,
class_ids,
config["batch_size"],
config["shuffle_buffer_size"],
h3_idx=h3_idx
)
else:
ds, num_train_steps_per_epoch = make_subsampled_dataset(
config["hard_cap"],
encoded_locs,
class_ids,
config["batch_size"],
config["shuffle_buffer_size"],
h3_idx=None
)
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate=config["initial_lr"],
decay_rate=config["lr_decay"],
decay_steps=num_train_steps_per_epoch,
staircase=True,
)
optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
pos_weight = tf.constant(config["sinr_hyperparams"]["pos_weight"], dtype=tf.float32)
for epoch in range(config["num_epochs"]):
print(f"Epoch {epoch+1}")
epoch_losses = []
print(f" optimizer lr is {optimizer.learning_rate.numpy()}")
if config["subsample_each_epoch"] and epoch != 0:
print(" re subsampling dataset")
ds, _ = make_subsampled_dataset(
config["hard_cap"],
encoded_locs,
class_ids,
config["batch_size"],
config["shuffle_buffer_size"],
)
pbar = tqdm.tqdm(total=num_train_steps_per_epoch, dynamic_ncols=True)
for step, (x_batch_train, y_batch_train) in enumerate(ds):
pbar.update()
# make random samples, comes out as list of x,y pairs where x and y are in -1, 1
rand_loc = make_rand_samples_tf(
# make same size fake data as real data
len(x_batch_train)
)
rand_loc = encoder.encode(rand_loc, normalize=False)
loss = apply_gradient(
optimizer,
fcnet,
x_batch_train,
y_batch_train,
rand_loc,
pos_weight
)
global_step = (epoch * num_train_steps_per_epoch) + step
if step % 10 == 0:
log_entry = {
"batch_loss": loss,
"learning_rate": optimizer.learning_rate.numpy(),
}
wandb.log(log_entry, step=global_step, commit=True)
pbar.set_description(f" Loss {loss:.4f}")
epoch_losses.append(loss.numpy())
pbar.close()
epoch_loss = np.mean(epoch_losses)
losses.append(epoch_loss)
print(f" Epoch mean loss: {epoch_loss:.4f}")
fcnet.save(config["model_save_name"])
wandb.finish()
if __name__ == "__main__":
train_model()