-
Notifications
You must be signed in to change notification settings - Fork 1
/
segmentation.py
73 lines (55 loc) · 2.84 KB
/
segmentation.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
# segmentation.py
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from absl import app
from absl import flags
from absl import logging
from datasets.segmentation_dataset import SegmentationDataset
from loggers.logger import Logger
from models.unet import UNet
from optimizers.lookahead import Lookahead
from optimizers.radam import RectifiedAdam
from trainers.segmentation_trainer import SegmentationTrainer
from utils.config import process_config
import tensorflow as tf
# Network entries
flags.DEFINE_float("learning_rate", 2e-4, "Initial learning rate for the chosen optimizer")
flags.DEFINE_integer("batch_size", 16, "The size of the batch to use while training the network.", lower_bound=1)
flags.DEFINE_integer("filters", 8, "A parameter that scales the depth of the neural network.", lower_bound=1)
flags.DEFINE_integer("num_epochs", 100, "Number of epochs to train the network for.", lower_bound=1)
# Data entries
flags.DEFINE_list("input_shape", [256, 256, 3], "The shape of the data to input in the neural network.")
flags.DEFINE_list("satellite_shape", [256, 256, 3], "The shape of the satellite image to input in the network.")
flags.DEFINE_list("output_shape", [256, 256, 3], "The shape of the data that will be output from the neural network.")
def main(argv) -> None:
del argv
# Process the configuration from flags.
config = process_config()
if config.mode != "evaluate":
# Define the datasets.
train_dataset = SegmentationDataset(batch_size=config.batch_size,
folder="datasets/segmentation_aracati/train",
x_shape=config.input_shape,
y_shape=config.output_shape)
valid_dataset = SegmentationDataset(batch_size=config.batch_size,
folder="datasets/segmentation_aracati/validation",
x_shape=config.input_shape,
y_shape=config.output_shape)
# Define the model.
loss = tf.keras.losses.CategoricalCrossentropy()
ranger = Lookahead(RectifiedAdam(learning_rate=config.learning_rate), sync_period=6, slow_step_size=0.5)
model = UNet(filters=config.filters, loss=loss, optimizer=ranger)
# Define the logger.
logger = Logger()
# Define the trainer.
trainer = SegmentationTrainer(model=model, logger=logger, train_dataset=train_dataset,
valid_dataset=valid_dataset)
if config.mode == "restore":
trainer.load_checkpoint()
trainer.train()
else:
logging.fatal("Evaluation not implemented yet.")
if __name__ == '__main__':
logging.set_verbosity(logging.INFO)
app.run(main)