-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeep_learning.py
94 lines (75 loc) · 2.86 KB
/
deep_learning.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
import os
import sys
# TODO : Add python command lines in readme with path and model specifications
root_path = "/home/johann/git-repo/cloud-mask"
sys.path.insert(0, root_path)
sys.path.append("../../cloudmask/") #
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
import tensorflow as tf
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.compat.v1.Session(config=config)
from cloudmask.ml_task import convnet_models
from cloudmask.data_load.utils_exp import (
load_test_data,
load_train_data,
subset_data,
get_categorical_labels,
)
#####################################################################################################
ls_experiments = [
"experiments_tiles",
"experiments_countries",
"experiments_continents",
]
path_experiments = os.path.join(root_path, ls_experiments[0])
# Model configuration CNN
model_cfg_cnn = dict(
learning_rate=10e-4,
keep_prob=0.5,
keep_prob_conv=0.8,
nb_conv_filters=32,
nb_conv_stacks=3,
nb_fc_neurons=32,
nb_fc_stacks=1,
fc_activation="relu",
kernel_size=3,
padding="SAME",
emb_layer="GlobalAveragePooling1D",
enumerate=True,
str_inc=True,
n_classes=6,
kernel_regularizer=1e-8,
loss="cross_entropy", # "focal_loss", "cross_entropy"
metrics="precision",
)
model_compiled = convnet_models.CNN1D(model_cfg_cnn)
model_compiled.prepare()
###########################################################################################
###########################################################################################
# Train the model
test_folds = os.listdir(path_experiments)
n_val_folds = 4
factor_subset = 10
for test_folder in test_folds:
folder_test = os.path.join(path_experiments, test_folder)
x_test, y_test_ = load_test_data(folder_test)
for fold in range(n_val_folds):
path_val_fold = os.path.join(folder_test, f"fold_{str(fold+1)}")
x_train, y_train, x_val, y_val = load_train_data(path_val_fold)
x_train, y_train = subset_data(x_train, y_train, factor=factor_subset)
x_val, y_val = subset_data(x_val, y_val, factor=factor_subset)
# One-hot labels for deep learning methods
y_train, y_val, y_test = get_categorical_labels(y_train, y_val, y_test_)
model_compiled.fit(
train_dataset=(x_train.reshape(x_train.shape[0], 13, 1), y_train),
val_dataset=(x_val.reshape(x_val.shape[0], 13, 1), y_val),
batch_size=32,
num_epochs=50,
model_directory=os.path.join(
path_val_fold, f"cnn_1D_enc_task_{model_compiled.config['loss']}"
),
save_steps=2, # check if best model each 2 epoch
)
###########################################################################################
###########################################################################################