This repository has been archived by the owner on Feb 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmain.py
159 lines (129 loc) · 5.71 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
import argparse
from keras.models import load_model
import dataset
import evaluation
import h5dataset
import networks
import visualize
################################################################################
parser = argparse.ArgumentParser()
parser.add_argument("--mode", action="store", dest="mode",
default="")
parser.add_argument("--data", action="store", dest="data",
default="data/")
parser.add_argument("--h5data", action="store", dest="h5data",
default="")
parser.add_argument("--model", action="store", dest="model",
default="models/model.h5")
parser.add_argument("--type", action="store", dest="model_type",
default="simple_conv")
parser.add_argument("--batch", action="store", dest="batch_size",
default=64, type=int)
parser.add_argument("--epochs", action="store", dest="epochs",
default=10, type=int)
parser.add_argument("--samples", action="store", dest="samples",
default=100000, type=int)
parser.add_argument("--samples_val", action="store", dest="samples_val",
default=10000, type=int)
parser.add_argument("--area", action="store", dest="area_size",
default=25, type=int)
parser.add_argument("--queue", action="store", dest="queue_size",
default=50, type=int)
parser.add_argument("--p", action="store", dest="p_train",
default=0.5, type=float)
parser.add_argument("--p_val", action="store", dest="p_val",
default=0.01, type=float)
parser.add_argument("--gpu", action="store", dest="gpu",
default=-1, type=int)
args = parser.parse_args()
args.steps_per_epoch = args.samples // args.batch_size
args.steps_per_val = args.samples_val // args.batch_size
################################################################################
def main_train_h5():
print("check for data.h5")
try:
open(args.h5data, "r")
except FileNotFoundError:
h5dataset.make_dataset(args.h5data)
print("load remaining data")
sat_images = dataset.load_sat_images(args.data)
alt, slp = dataset.load_static_data(args.data)
print("initialize training generator")
train_gen = h5dataset.patch_generator_from_h5(args.h5data, sat_images, alt, slp,
size=args.area_size,
batch_size=args.batch_size,
p=args.p_train)
print("initialize validation generator")
val_gen = h5dataset.patch_generator_from_h5(args.h5data, sat_images, alt, slp,
size=args.area_size,
batch_size=args.batch_size,
p=args.p_val)
print("get network")
model = networks.get_model_by_name(args.model_type)(args)
print("compile")
custom_metrics = list(evaluation.get_metrics().values())
model.compile(optimizer="adam",
loss="binary_crossentropy",
metrics=["accuracy"] + custom_metrics)
print(model.summary())
print("start training")
model.fit_generator(train_gen,
steps_per_epoch=args.steps_per_epoch,
epochs=args.epochs,
validation_data=val_gen,
validation_steps=args.steps_per_val,
verbose=True,
max_q_size=args.queue_size,
workers=1)
print("store model")
model.save(args.model)
def main_train():
print("load data into memory")
sat_images, pos, neg, alt, slp = dataset.make_small_dataset(args.data)
print("initialize training generator")
train_gen = dataset.patch_generator(sat_images, pos, neg, alt, slp,
size=args.area_size,
batch_size=args.batch_size,
p=args.p_train)
print("initialize validation generator")
val_gen = dataset.patch_generator(sat_images, pos, neg, alt, slp,
size=args.area_size,
batch_size=args.batch_size,
p=args.p_val)
print("get network")
model = networks.get_model_by_name(args.model_type)(args)
print("compile")
custom_metrics = evaluation.get_metric_functions()
model.compile(optimizer="adam",
loss="binary_crossentropy",
metrics=["accuracy"] + custom_metrics)
print(model.summary())
print("start training")
model.fit_generator(train_gen,
steps_per_epoch=args.steps_per_epoch,
epochs=args.epochs,
validation_data=val_gen,
validation_steps=args.steps_per_val,
verbose=True,
max_q_size=args.queue_size,
workers=1)
print("store model")
model.save(args.model)
def main_eval():
print("load specified model")
model = load_model(args.model, custom_objects=evaluation.get_metrics())
print("load evaluation image")
img = dataset.load_image_eval(args.data)
print("run evaluation on final year")
y_pred = evaluation.predict_image(model, img, args.area_size)
visualize.save_image_as(y_pred, "res/out.png")
if __name__ == "__main__":
if args.mode == "train":
if args.h5data:
main_train_h5()
else:
main_train()
elif args.mode == "eval":
main_eval()
else:
print("Invalid mode!")