-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stage3_CAENR.py
111 lines (84 loc) · 3.24 KB
/
Stage3_CAENR.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
import os
import csv
import sys
import numpy as np
from PIL import Image
import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.layers import Input
from scipy.stats import skew
def Load_File_Name(passed_dir):
file_list = []
for (root, directories, files) in os.walk(passed_dir):
for file in files:
file_path = os.path.join(root, file)
file_list.append(file_path)
return file_list
def Stackin_Data(img_file_list, CP_file_list):
img_file_num = len(img_file_list)
# CP_file_num = len(CP_file_list)
img_stack = []
CP_stack = []
count = 0
for img, CP in zip(img_file_list, CP_file_list):
# Stack image
np_img = np.asarray(Image.open(img)) / 255.
img_stack.append(np_img)
# Stack collapse percentage csv
reader = list(csv.reader(open(CP)))
now_b = np.squeeze(reader)
now_b = [float(now_b)]
now_b = np.transpose(np.reshape(np.asarray(now_b), -1))
CP_stack.append(now_b)
count += 1
print(str(count) + " / " + str(img_file_num) + " Image & CP Stack Finished ...")
# Debugging code
# if count == 1000:
# break
return img_stack, CP_stack
if __name__ == '__main__':
latent_dim = 10
## Stacking a dataset ##
img_path_dir = 'your image path'
img_file_list = Load_File_Name(img_path_dir)
img_data_num = len(img_file_list)
CP_path_dir = 'your regression path'
CP_file_list = Load_File_Name(CP_path_dir)
CP_data_num = len(CP_file_list)
if img_data_num == CP_data_num:
img_stacking, CP_stacking = Stackin_Data(img_file_list, CP_file_list)
else:
sys.stderr.write("Data numbers are not equal! Try again ...")
exit(1)
encoder_path = 'your encoder path.h5'
if (os.path.exists(encoder_path)):
encoder = tf.keras.models.load_model(encoder_path, compile=False)
# encoder.summary()
print("Encoder model exist & loaded ...")
MLP_path = 'your regression model path.h5'
if (os.path.exists(MLP_path)):
MLP = tf.keras.models.load_model(MLP_path, compile=True)
# encoder.summary()
print("MLP model exist & loaded ...")
img_stacking = np.expand_dims(img_stacking, -1)
CP_stacking = np.squeeze(CP_stacking)
CAENR_input = Input(shape=(128, 128, 1), name='CAE_input')
CAENR_encoder_output = encoder(CAENR_input)
CAEMLP_output = MLP(CAENR_encoder_output)
CAENR = Model(CAENR_input, CAENR_output, name='CAE')
CAENR.summary()
# CAEMLP.compile(optimizer='adam', loss='mse', metric=['mse'])
# CAEMLP_test_scores = CAEMLP.evaluate(img_stacking, CP_stacking, verbose=1)
# print("Test Loss : ", CAEMLP_test_scores[0])
CP_predict = CAENR.predict(img_stacking)
CP_predict = np.squeeze(CP_predict)
diff = []
for CAENR_result, CP in zip(CP_predict, CP_stacking):
diff.append(abs(CP-CAENR_result))
# print(diff)
for CP, now_CP_result, now_diff, now_file in zip(CP_stacking, CP_predict, diff, img_file_list):
print(now_file + ' : ' + str(CP) + ' / ' + str(now_CP_result) + ' / ' + str(now_diff))
print(np.mean(diff))
print(np.std(diff))
print(skew(diff))
# You can save CAENR model using save method