-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimhistory_ensemble.py
More file actions
269 lines (225 loc) · 11.5 KB
/
Copy pathimhistory_ensemble.py
File metadata and controls
269 lines (225 loc) · 11.5 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# -*- coding: utf-8 -*-"
# import folders
import sys
sys.path.append(r"D:\Imhistory\ImageHistory\utils")
import tensorflow as tf
from tensorflow import keras
from data_handling import *
from Network import *
from metric_utils3 import *
from Data_Generator import *
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, ModelCheckpoint,LearningRateScheduler,TensorBoard # Erken durdurma için
import multiprocessing
import math
from scipy.io import savemat
from functools import partial
# load and save model
from tensorflow.keras.models import Sequential, Model, load_model
from tensorflow.keras.models import model_from_json
import json
# =============================================================================
# Define variables
cc=1
num_classes = 5 # ["Adjustment", "Denoising","High-pass","Low-pass","Uniform"]
batch_size = 16
epochs =100
height=512
width=512
input_size=(height, width)
n_channels=3
input_shape=(height, width, 3)
color_mode='rgb'
model_tf_selection=1
tf_bool=2 #1: pretrained, 2: None
model_selection=4 ### 1: VIT, 2: SE-RESNET50, 3: SE-DENSENET121, 4: SE-INCEPTIONV3
qf=75 # QF=75,85,95
transformer_type=2 # 1-b16, 2-b32, 3-l16, 4-l32
lr=0.0002 # learning rate
weights_file = r'D:\Imhistory\ImageHistory\Imhistory_weights.hdf5' # kayıt edilecek yer
weights_file2 = r'D:\Imhistory\ImageHistory\Imhistory_weights.hdf5' # kayıt edilecek yer
path_file=r'D:\Imhistory\ImageHistory\Results' # dosyaların yerleri
dataset_path=r'D:\Imhistory\ImageHistory\Datasets\UCID'
# Compile parametreleri
optsecim=1
if optsecim==1: # SGD
optimizer= SGD(lr=lr, decay=1e-6, momentum=0.9, nesterov=True) # lr=1e-4
elif optsecim==2: #RMSprop
optimizer=RMSprop(lr=lr, rho=0.9)
elif optsecim==3: # Adam
optimizer=Adam(lr=lr, beta_1=0.9, beta_2=0.999, amsgrad=False)
elif optsecim==4: # Adam
optimizer=Adadelta(lr=lr, rho=0.95, epsilon=1e-07)
metric=["categorical_accuracy"]
loss="categorical_crossentropy"
dropbool=1 # Dropout
# Dataset
valbool=1
trainrate=0.9 # training rate
testrate=1.0 # testing rate
valrate=0.1 # validation rate
# ------------------------------
for ii in range(cc):
# initialize trainset and test set
x_train, y_train, x_test, y_test, x_val, y_val= [], [], [], [], [], []
# transfer train and test set data
imhistclasses = ["Adjustment", "Denoising","Highpass","Lowpass","Uniform"]
training_list, prediction_list, validation_list=[],[],[]
ytraining_list, yprediction_list, yvalidation_list=[],[],[]
for tclass in imhistclasses:
training, prediction, validation = get_files(dataset_path,tclass,trainrate,testrate,valrate,valbool,qf)
for item in training:
training_list.append(item)
ytraining_list.append(imhistclasses.index(tclass))
for item in prediction:
prediction_list.append(item)
yprediction_list.append(imhistclasses.index(tclass))
for item in validation:
validation_list.append(item)
yvalidation_list.append(imhistclasses.index(tclass))
# Adjust class weights
from sklearn.utils import class_weight
class_weight = class_weight.compute_class_weight('balanced'
,classes=np.unique(ytraining_list)
,y=ytraining_list)
class_weight_dict = dict(zip(np.unique(ytraining_list), class_weight))
print("Eğitim verisi örnek sayısı=",len(ytraining_list))
print("Test verisi örnek sayısı=",len(yprediction_list))
print("Validation örnek sayısı",len(yvalidation_list))
train_generator = Data_Generator(training_list, ytraining_list, batch_size,input_size,n_channels,num_classes,shuffle=True)
step_size_train = train_generator.batchlen #(train_generator.n/ train_generator.batch_size)
if valbool==1:
val_generator = Data_Generator(validation_list, yvalidation_list, batch_size,input_size,n_channels,num_classes,shuffle=False)
step_size_val = val_generator.batchlen #np.ceil(val_generator.n/ val_generator.batch_size)
test_generator =Data_Generator(prediction_list, yprediction_list, batch_size,input_size,n_channels,num_classes,shuffle=False)
step_size_test=test_generator.batchlen # np.ceil(test_generator.n/ test_generator.batch_size)
# Construct CNN structure
callbacks = [EarlyStopping(monitor='val_categorical_accuracy',
patience=15,
verbose=1,
min_delta=1e-5,
mode='max'),
ReduceLROnPlateau(monitor='val_loss',
factor=0.005,
patience=10,
verbose=1,
min_delta=1e-5,
mode='auto'),
ModelCheckpoint(monitor='val_categorical_accuracy',
filepath=weights_file,
verbose=1,
save_best_only=True,
save_weights_only=True,
mode='max',
period=1)]
model=get_Network(tf_bool,num_classes,input_shape,loss, optimizer, metric,height, width,dropbool,model_selection,transformer_type)
model.compile(loss=loss, optimizer=optimizer, metrics=[metric])
model.summary()
fit = False
if fit == True:
print("Training Started---------------------------")
if valbool==1:
history = model.fit(train_generator, validation_data=val_generator, validation_steps=step_size_val, callbacks=callbacks,
steps_per_epoch=step_size_train, epochs=epochs, class_weight=class_weight_dict, use_multiprocessing=False,shuffle=True)
else:
history = model.fit(train_generator, steps_per_epoch=step_size_train,
epochs=epochs, callbacks=callbacks, class_weight=class_weight_dict, use_multiprocessing=False,shuffle=True)
# Save model
fnm=path_file+"\\model"+str(ii)+".json"
model_json = model.to_json()
with open(fnm, "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
fnm1=path_file+"\\sonuc"+str(ii)+".h5"
model.save_weights(fnm1)
print("Saved model to disk")
print("Training Finished.................")
else:
model_input = Input(shape=input_shape)
# DENSENET121
model_selection=3
weights_file1 =r"D:\Imhistory\ImageHistory\Ensemble_Models\UCID\Ucid_75_Imhistory_weights1_SEDensenet.hdf5"
model1=get_Network(tf_bool,num_classes,input_shape,loss, optimizer, metric,height, width,dropbool,model_selection,transformer_type)
model1.load_weights(weights_file1)
model1.compile(loss=loss, optimizer=optimizer, metrics=[metric])
for f, layer in enumerate(model1.layers):
# layer.name = 'layer_' + str(i) <-- old way
layer._name = layer.name + 'sedense121'+str(f)
model1.summary()
# INCEPTIONV3
model_selection=4
weights_file1 =r"D:\Imhistory\ImageHistory\Ensemble_Models\UCID\Ucid_75_Imhistory_weights1_SEInception.hdf5"
model2=get_Network(tf_bool,num_classes,input_shape,loss, optimizer, metric,height, width,dropbool,model_selection,transformer_type)
model2.load_weights(weights_file1)
model2.compile(loss=loss, optimizer=optimizer, metrics=[metric])
for f, layer in enumerate(model2.layers):
# layer.name = 'layer_' + str(i) <-- old way
layer._name = layer.name + 'seincepv3'+str(f)
# RESNET50
model_selection=2
weights_file1 = r"D:\Imhistory\ImageHistory\Ensemble_Models\UCID\Ucid_75_Imhistory_weights1_SEResnet.hdf5"
model3=get_Network(tf_bool,num_classes,input_shape,loss, optimizer, metric,height, width,dropbool,model_selection,transformer_type)
model3.load_weights(weights_file1)
model3.compile(loss=loss, optimizer=optimizer, metrics=[metric])
for f, layer in enumerate(model3.layers):
# layer.name = 'layer_' + str(i) <-- old way
layer._name = layer.name + 'seresnet50'+str(f)
# TRANSFORMER
model_selection=1
weights_file1 = r"D:\Imhistory\ImageHistory\Ensemble_Models\UCID\Ucid_75_Imhistory_weights1_Transformer.hdf5"
model4=get_Network(tf_bool,num_classes,input_shape,loss, optimizer, metric,height, width,dropbool,model_selection,transformer_type)
model4.load_weights(weights_file1)
model4.compile(loss=loss, optimizer=optimizer, metrics=[metric])
for f, layer in enumerate(model4.layers):
# layer.name = 'layer_' + str(i) <-- old way
layer._name = layer.name + 'transformer'+str(f)
models=[model1,model2,model3,model4]
#models=ensemble(models,model_input)
## ------------------------------
train_score,test_score,test_pred=[],[],[]
model=get_Network(tf_bool,num_classes,input_shape,loss, optimizer, metric,height, width,dropbool,model_selection,transformer_type)
test_pred= [model.predict(test_generator,steps=step_size_test,verbose=1) for model in models]
test_pred=np.array(test_pred)#,workers=0, max_queue_size=0, use_multiprocessing=False)
test_pred=np.mean(test_pred,axis=0)
print("TEST TAHMİNİ BİTTİ")
y_test=keras.utils.to_categorical(yprediction_list, num_classes)
test_pred=test_pred[:len(y_test),:]
print("TEST TAHMİNİ BİTTİ")
val_score=0
# if valbool==1:
# val_score = model.evaluate(val_generator, steps=step_size_val, verbose=1)
# =====================ROC values ======================================
test_pred2=np.argmax(test_pred,axis=1)
test_pred2 = np.array(test_pred2, 'float32')
test_pred3 = keras.utils.to_categorical(test_pred2, num_classes)
history=[]
test_result=np.zeros((num_classes, 15))
test_result,test_result_mean,confusion=metric_all(path_file,1,train_score, test_score, valbool,val_score,history,num_classes,y_test,test_pred3)
if ii==0:
all_result=test_result
all_confusion=confusion
all_meanresult=test_result_mean
else:
all_result=np.append(all_result,test_result,axis=0)
all_confusion=np.append(all_confusion,confusion,axis=0)
all_meanresult=np.append(all_meanresult,test_result_mean,axis=0)
all_result=np.append(all_result,test_result,axis=0)
all_confusion=np.append(all_confusion,confusion,axis=0)
all_meanresult=np.append(all_meanresult,test_result_mean,axis=0)
# save test results
r_dict = {
'ytest': y_test,
'pred_test':test_pred3,
'prediction_list':yprediction_list,
'test_result':test_result,
'confusion':confusion
}
savemat(path_file+"/imhist_ucid_ensembleresult"+str(qf)+str(ii)+"1.mat", r_dict)
#---------------------------------------------------------------------------
def ensemble_predictions(members, testX):
yhats = [model.predict(testX) for model in members]
yhats = np.array(yhats)
# sum across ensemble members
summed = np.sum(yhats, axis=0)
# argmax across classes
result = np.argmax(summed, axis=1)
return result