-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
105 lines (82 loc) · 3.35 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
#
## Loading Annotations
from Annotations.load_labels import Annotation_to_Numpy, load_pickle
annotation_training = load_pickle(
'Annotations/annotation_training.pkl')
_, annotation_training = Annotation_to_Numpy(annotation_training)
annotation_validation = load_pickle(
'Annotations/annotation_validation.pkl')
_, annotation_validation = Annotation_to_Numpy(annotation_validation)
"""## Loading Dataset"""
from DataLoader import DataGenerator
# Dataloader:
Variables = {'num_labels': 5,
'batch_size': 8,
'dim': (256, 256),
'n_channels': 3,
'shuffle': True,
'mode': 'torch',
'dtype': 'float16',
'reshape_size': 256,
'number_of_split': 10,
'number_of_frames': 1,
'min_neighbors': 10,
'scalefactor': 1.2
}
path_train = 'Dataset/Train'
training_data = DataGenerator(from_dir=path_train, labels=annotation_training, name='Train', **Variables)
path_val = 'Dataset/Validation'
validation_data = DataGenerator(from_dir=path_val, labels=annotation_validation, name='Val', **Variables)
"""## Building the Model"""
from CustomModel import DisplayNetwork,CustomModel
from tensorflow.keras.layers import Layer
from tensorflow.python.keras.utils.vis_utils import plot_model
model = CustomModel()
model._layers = [layer for layer in model._layers if isinstance(layer, Layer)]
plot_model(model)
print(model.summary())
"""## Compiling the Model
### Callbacks
"""
import tensorflow as tf
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping, ModelCheckpoint
import datetime
#Checkpoint Callback
checkpoint_filepath = '/content/drive/MyDrive/First_Impression/Checkpoint/model.{epoch:02d}-{val_loss:.2f}.ckpt'
model_checkpoint_callback = ModelCheckpoint(
filepath=checkpoint_filepath,
save_weights_only=True,
monitor='val_EvalMetric',
mode='auto',
verbose=1,
save_best_only=False)
reduce_lr = ReduceLROnPlateau(monitor='val_loss',
patience=3,
verbose=1,
factor=0.2,
min_lr=5e-7)
# early stopping:
model_earlystopping_callback = EarlyStopping(patience=5)
"""### compile + new_metric"""
import tensorflow.keras.backend as k
import numpy as np
def EvalMetric(y_true,y_pred):
r2 = 1 - tf.reduce_mean(tf.abs(y_true-y_pred))
return r2
adam = Adam(lr=1e-4)
model.compile(optimizer=Adam(), loss='mse', metrics=['mae',tf.keras.metrics.CosineSimilarity(), EvalMetric])
"""## Training"""
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
result = model.fit(training_data, validation_data=validation_data, steps_per_epoch=30, validation_steps=8, epochs=300, callbacks=[model_earlystopping_callback,reduce_lr, model_checkpoint_callback], verbose=1)
a=list(result.history.keys())
a['loss' in a]
from matplotlib import pyplot as plt
for key in list(result.history.keys()):
plt.plot(result.history[key])
plt.xlabel("Epoch")
plt.ylabel(key)
plt.show()
print(key)
print('The end')