-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_train.py
177 lines (136 loc) · 4.91 KB
/
model_train.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import tensorflow as tf
import pandas as pd
import numpy as np
import os
import cv2
import os
currdir = os.path.abspath(os.getcwd())
images_path = currdir + '/real_and_fake_face'
# CONSTANTS
FOLDERS = ["training_real", "training_fake"]
IMG_SIZE=64
TRAINING_PROPORTION = 1
VALIDATION_PROPORTION = 0.1
MAX_USE = 1
EPOCHS = 50
RUN_NAME = str(IMG_SIZE) + "px Detector"
# Functions to Load Data
def loadTrainingData():
X = []
Y = []
for folder in FOLDERS:
path = os.path.join(images_path, folder)
class_num = FOLDERS.index(folder)
max_index = TRAINING_PROPORTION*len(os.listdir(path)) *MAX_USE * (1 - VALIDATION_PROPORTION)
for img in (os.listdir(path))[:int(max_index)]:
try:
img_array = cv2.imread(os.path.join(path,img))
new_array = cv2.resize(img_array,(IMG_SIZE,IMG_SIZE))
new_array = np.array(new_array)
new_array = new_array.astype('float32')
new_array /= 255
X.append(new_array)
Y.append(class_num)
except Exception as e:
continue
return X, Y
def loadValidationData():
X = []
Y = []
for folder in FOLDERS:
path = os.path.join(images_path, folder)
class_num = FOLDERS.index(folder)
min_index = TRAINING_PROPORTION*len(os.listdir(path)) *MAX_USE * (1 - VALIDATION_PROPORTION)
max_index = TRAINING_PROPORTION*len(os.listdir(path)) *MAX_USE
for img in (os.listdir(path))[int(min_index):int(max_index)]:
try:
img_array = cv2.imread(os.path.join(path,img))
new_array = cv2.resize(img_array,(IMG_SIZE,IMG_SIZE))
new_array = np.array(new_array)
new_array = new_array.astype('float32')
new_array /= 255
X.append(new_array)
Y.append(class_num)
except Exception as e:
continue
return X, Y
def loadTestingData():
X = []
Y = []
for folder in FOLDERS:
path = os.path.join(images_path, folder)
class_num = FOLDERS.index(folder)
min_index = TRAINING_PROPORTION*len(os.listdir(path)) *MAX_USE
max_index = len(os.listdir(path)) *MAX_USE
# print()
# print(int(min_index),int(max_index))
for img in os.listdir(path)[int(min_index):int(max_index)]:
try:
img_array = cv2.imread(os.path.join(path,img))
new_array = cv2.resize(img_array,(IMG_SIZE,IMG_SIZE))
new_array = np.array(new_array)
new_array = new_array.astype('float32')
new_array /= 255
X.append(new_array)
Y.append(class_num)
except Exception as e:
pass
return X, Y
# Load data
X, Y = loadTrainingData()
XVal, YVal = loadValidationData()
XTest, YTest = loadTestingData()
# Loading Model
# model = tf.keras.load_model('/content/drive/MyDrive/HCDS Project/trained_model.h5')
# Defining Model 256 by 256 by
model = tf.keras.Sequential(
[
tf.keras.layers.InputLayer(input_shape=(IMG_SIZE,IMG_SIZE, 3), name="Input"),
tf.keras.layers.Conv2D(filters=32, kernel_size=5, strides=(2, 2), activation='relu', name="1st_Conv2D_Layer",
kernel_regularizer=tf.keras.regularizers.l2(0.0005)
),
tf.keras.layers.MaxPool2D(pool_size=(2,2)),
tf.keras.layers.Conv2D(filters=64, kernel_size=3, strides=(2, 2), activation='relu', name="2nd_Conv2D_Layer",
kernel_regularizer=tf.keras.regularizers.l2(0.0005)
),
tf.keras.layers.Flatten(name="4th_Flatten_Layer"),
tf.keras.layers.Dense(1, name="out", activation="sigmoid"),
])
# Compiling and Attaching Logger
model.compile(
optimizer = tf.keras.optimizers.Adam(),
loss = "binary_crossentropy",
metrics=[
'accuracy',
tf.keras.metrics.Precision(),
tf.keras.metrics.Recall()
]
)
logger = tf.keras.callbacks.TensorBoard(
log_dir='./log/' + RUN_NAME,
write_graph=True,
histogram_freq=1
)
# Train Model
history = model.fit(
x=tf.cast(np.array(X), tf.float64),
y=tf.cast(list(map(int,Y)),tf.int32),
epochs=EPOCHS,
shuffle=True,
callbacks = [logger],
# validation_split = 0.1,
validation_data = (tf.cast(np.array(XVal), tf.float64), tf.cast(list(map(int,YVal)),tf.int32),)
# batch_size = 10,
)
# Test Model
# print("EVALUATION:")
# error_rate = model.evaluate(
# x=tf.cast(np.array(XTest), tf.float64),
# y=tf.cast(list(map(int,YTest)),tf.int32),
# callbacks = [logger]
# )
# print(error_rate)
# fileName = currdir + '/' + RUN_NAME + 'Precision=' + str(round(error_rate[2],3)) + 'Recall=' + str(round(error_rate[3],3)) + '.h5'4
fileName = currdir + '/' + RUN_NAME + '.h5'
# Saving Model
model.save(fileName)