-
Notifications
You must be signed in to change notification settings - Fork 1
/
3-Pretraining_ImageNet.py
156 lines (120 loc) · 4.23 KB
/
3-Pretraining_ImageNet.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mar 25 2021
@author: Aurelien Callens
"""
import pickle
import numpy as np
from sklearn.utils import class_weight
from numpy.random import seed
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.callbacks import EarlyStopping, LearningRateScheduler
from scripts.Cnn_functions import *
from sklearn.utils import class_weight
from scripts.Make_graphs import make_train_graphs
from scripts.Cnn_functions import make_conf_mat
# Setting seeds for reproducibility
seed(1)
# tensorflow.random.set_random_seed(2)
# Gpu loading
"""
from tensorflow.keras.backend import set_session
config = tensorflow.ConfigProto()
config.gpu_options.allow_growth = True
# config.gpu_options.per_process_gpu_memory_fraction = 0.80
sess = tensorflow.Session(config=config)
set_session(sess)
"""
# Analysis
# # Generator initialization
batch_size = 32
train_generator, train_ov_generator, val_generator, test_generator = generator_init(
img_path='data/Keras_Btz_img', batch_size=batch_size)
# # Models set up
# # # General parameters
sgd_opti = SGD(lr=0.001, momentum=0.9, nesterov=True, decay=1e-6)
# # # Custom model
base_model = create_base_model(output=3)
base_model.compile(optimizer=sgd_opti,
loss='categorical_crossentropy',
metrics=['categorical_accuracy'])
# # # AlexNet model
alex_model = create_alex_model()
alex_model.compile(optimizer=sgd_opti,
loss='categorical_crossentropy',
metrics=['categorical_accuracy'])
# # # Inception v3 model
# Imagenet weights
model_incep = create_incep_model(output=3, weights_cnn='imagenet')
model_incep.compile(optimizer=sgd_opti, loss='categorical_crossentropy',
metrics=['categorical_accuracy'])
# # # VGG16 model
# Imagenet weights
model_vgg = create_vgg16_model(output=3, weights_cnn='imagenet')
model_vgg.compile(optimizer=sgd_opti, loss='categorical_crossentropy',
metrics=['categorical_accuracy'])
# # # Callbacks
earlystop = EarlyStopping(patience=10)
cb = TimingCallback()
schedule = StepDecay(initAlpha=0.001, factor=0.5, dropEvery=10)
callbacks = [cb, earlystop, LearningRateScheduler(schedule)]
# # # Model training and saving
# VGG16 model
cb = TimingCallback()
schedule = StepDecay(initAlpha=0.001, factor=0.5, dropEvery=10)
callbacks = [cb, earlystop, LearningRateScheduler(schedule)]
history_vgg = model_vgg.fit(
train_ov_generator,
epochs=100,
validation_data=val_generator,
validation_steps=val_generator.n//batch_size,
steps_per_epoch=train_ov_generator.n//batch_size,
callbacks=callbacks,
# class_weight = class_weights,
max_queue_size=100,
use_multiprocessing=True
)
make_train_graphs(history_vgg)
make_conf_mat(model_vgg,
test_gen=test_generator,
print=True)
mat_conf_vgg = make_conf_mat(model_vgg, print=False, test_gen=test_generator)
Res5 = [sum(cb.logs),
len(history_vgg.epoch),
mat_conf_vgg.ACC_Macro,
mat_conf_vgg.ACC,
mat_conf_vgg.F1_Macro,
mat_conf_vgg.F1,
mat_conf_vgg]
filehandler = open("Results_Btz/vgg_model_T_OV_34e_bs32", 'wb')
pickle.dump(Res5, filehandler)
model_vgg.save_weights("Results_Btz/vgg_model_T_OV_34e_bs32.h5")
# Inception v3 model
cb = TimingCallback()
schedule = StepDecay(initAlpha=0.001, factor=0.5, dropEvery=10)
callbacks = [cb, earlystop, LearningRateScheduler(schedule)]
history_incep = model_incep.fit(
train_ov_generator,
epochs=100,
validation_data=val_generator,
validation_steps=val_generator.n//batch_size,
steps_per_epoch=train_ov_generator.n//batch_size,
callbacks=callbacks,
# class_weight = class_weights,
max_queue_size=100,
use_multiprocessing=True
)
make_train_graphs(history_incep)
mat_conf_incep = make_conf_mat(model_incep, test_gen=test_generator,
print=False)
Res6 = [sum(cb.logs),
len(history_incep.epoch),
mat_conf_incep.ACC_Macro,
mat_conf_incep.ACC,
mat_conf_incep.F1_Macro,
mat_conf_incep.F1,
mat_conf_incep]
filehandler = open("Results/incep_model_T_OV_21e_bs32", 'wb')
pickle.dump(Res6, filehandler)
model_incep.save_weights("Results/incep_model_T_OV_21e_bs32.h5")