-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_experiment.py
140 lines (125 loc) · 4.7 KB
/
main_experiment.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
import sys
from matplotlib import pyplot as plt
import numpy as np
import tensorflow as tf
from FullImageDataGenerator import FullImageDataGenerator
from TileDataGenerator import TileDataGenerator
from BaseModel import BaseModel
from ShortSkipModel import ShortSkipModel
from LongSkipModel import LongSkipModel
from Evaluator import Evaluator
def train_experiment(experiment):
'''Train for an experiment, and compute metrics on training and validation set.
experiment is a dictionary with three required entries:
- datagen -> the DataGenerator class
- model -> the Model class
- clf_name -> name of the classifier, which will be used to set the saved file names'''
try:
DataGenerator = experiment['datagen']
Model = experiment['model']
clf_name = experiment['name']
except KeyError:
print("Experiment dictionary must be contain dataget, model and name keys.")
# optional parameters
lr = experiment.get('lr', 1e-4)
eps = experiment.get('eps', 1e-8)
overlap = experiment.get('overlap', 'minimum')
max_epochs = experiment.get('max_epochs', 200)
patience = experiment.get('patience', 15)
directory = experiment.get('directory', "D:/Adrien/dataset/GlaS/train")
image_size = experiment.get('image_size', (256,384))
batch_size = experiment.get('batch_size', 5)
validation_size = experiment.get('validation_size', 10)
min_area = experiment.get('min_area', 4000)
tf.keras.backend.clear_session()
# Training
generator = DataGenerator(batch_size, validation_size, directory, image_size)
model = Model(image_size, clf_name, lr=lr, eps=eps)
history = model.fit(max_epochs, generator, patience=patience)
plt.figure()
plt.subplot(2,1,1)
plt.plot(history.history['val_loss'], 'r-')
plt.plot(history.history['loss'], 'b-')
plt.subplot(2,1,2)
plt.plot(history.history['val_accuracy'], 'r-')
plt.plot(history.history['accuracy'], 'b-')
plt.savefig(f'{clf_name}_history.png')
# Compute & save metrics
tile = isinstance(generator, TileDataGenerator)
train_metrics = Evaluator.evaluate(model, generator, 'train', overlap, min_area)
val_metrics = Evaluator.evaluate(model, generator, 'val', overlap, min_area)
with open(f"{clf_name}_metrics.txt", 'w') as fp:
print("Training perfomance:", file=fp)
print("Precision\tRecall\tMCC", file=fp)
print(train_metrics.mean(axis=0), file=fp)
print(np.median(train_metrics,axis=0), file=fp)
print(" ---- ", file=fp)
print("Validation perfomance:", file=fp)
print("Precision\tRecall\tMCC", file=fp)
print(val_metrics.mean(axis=0), file=fp)
print(np.median(val_metrics,axis=0), file=fp)
np.save(f"{clf_name}_metrics_train.npy", train_metrics)
np.save(f"{clf_name}_metrics_val.npy", val_metrics)
def main():
try:
directory = sys.argv[1]
except:
raise ValueError("No directory provided. Usage: python main_experiment.py path_to_train_dataset")
experiments = [
{'datagen' : FullImageDataGenerator,
'model' : BaseModel,
'name' : 'base_model_full_image',
'lr' : 1e-3,
'eps': 1e-7,
'directory': directory,
'max_epochs': 1000,
'patience': 100},
{'datagen' : FullImageDataGenerator,
'model' : ShortSkipModel,
'name' : 'short_skip_model_full_image',
'lr' : 1e-3,
'eps': 1e-7,
'directory': directory,
'max_epochs': 1000,
'patience': 100},
{'datagen' : FullImageDataGenerator,
'model' : LongSkipModel,
'name' : 'long_skip_model_full_image',
'lr' : 1e-3,
'eps': 1e-7,
'directory': directory,
'max_epochs': 1000,
'patience': 100},
{'datagen' : TileDataGenerator,
'model' : BaseModel,
'name' : 'base_model_tile',
'lr' : 1e-4,
'eps': 1e-8,
'overlap': 'minimum',
'directory': directory,
'max_epochs': 1000,
'patience': 100},
{'datagen' : TileDataGenerator,
'model' : ShortSkipModel,
'name' : 'short_skip_model_tile',
'lr' : 1e-4,
'eps': 1e-8,
'overlap': 'minimum',
'directory': directory,
'max_epochs': 1000,
'patience': 100},
{'datagen' : TileDataGenerator,
'model' : LongSkipModel,
'name' : 'long_skip_model_tile',
'lr' : 1e-4,
'eps': 1e-8,
'overlap': 'minimum',
'directory': directory,
'max_epochs': 1000,
'patience': 100}
]
for experiment in experiments:
print(f"Training {experiment['name']}")
train_experiment(experiment)
if __name__ == '__main__':
main()