Update cnn_ela_training.py
Browse files- cnn_ela_training.py +22 -23
cnn_ela_training.py
CHANGED
|
@@ -18,7 +18,7 @@ import tensorflow as tf
|
|
| 18 |
from sklearn.model_selection import train_test_split
|
| 19 |
from sklearn.metrics import confusion_matrix
|
| 20 |
import itertools
|
| 21 |
-
from tensorflow.keras.utils import to_categorical
|
| 22 |
from keras.models import Sequential
|
| 23 |
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
|
| 24 |
from tensorflow.keras.optimizers.legacy import RMSprop
|
|
@@ -57,6 +57,7 @@ def convert_to_ela_image(path, quality, output_dir, resize=(256, 256)):
|
|
| 57 |
|
| 58 |
return ela_im
|
| 59 |
|
|
|
|
| 60 |
def shuffle_and_split_data(dataframe, test_size=0.2, random_state=59):
|
| 61 |
# Shuffle the DataFrame
|
| 62 |
shuffled_df = dataframe.sample(frac=1, random_state=random_state).reset_index(drop=True)
|
|
@@ -84,10 +85,8 @@ def labeling(path_real, path_fake):
|
|
| 84 |
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
np.random.seed(22)
|
| 92 |
tf.random.set_seed(9)
|
| 93 |
|
|
@@ -95,34 +94,29 @@ if __name__ == "__main__":
|
|
| 95 |
traning_real_folder = 'datasets/training_set/real/'
|
| 96 |
|
| 97 |
|
| 98 |
-
|
| 99 |
traning_ela_output = 'datasets/training_set/ela_output/'
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
traning_set = labeling(traning_real_folder, traning_fake_folder)
|
| 107 |
|
| 108 |
|
| 109 |
X = []
|
| 110 |
Y = []
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
for index, row in traning_set.iterrows():
|
| 113 |
X.append(array(convert_to_ela_image(row[0], 90,traning_ela_output).resize((128, 128))).flatten() / 255.0)
|
| 114 |
Y.append(row[1])
|
| 115 |
|
| 116 |
-
|
| 117 |
X = np.array(X)
|
| 118 |
Y = to_categorical(Y, 2)
|
| 119 |
-
|
| 120 |
X = X.reshape(-1, 128, 128, 3)
|
| 121 |
-
|
| 122 |
X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size = 0.2, random_state=1,shuffle=True)
|
| 123 |
|
| 124 |
-
model = Sequential()
|
| 125 |
|
|
|
|
|
|
|
|
|
|
| 126 |
model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'valid',
|
| 127 |
activation ='relu', input_shape = (128,128,3)))
|
| 128 |
print("Input: ", model.input_shape)
|
|
@@ -146,9 +140,11 @@ if __name__ == "__main__":
|
|
| 146 |
|
| 147 |
model.summary()
|
| 148 |
|
|
|
|
| 149 |
optimizer = RMSprop(lr=0.0005, rho=0.9, epsilon=1e-08, decay=0.0)
|
|
|
|
| 150 |
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
|
| 151 |
-
|
| 152 |
early_stopping = EarlyStopping(monitor='val_acc',
|
| 153 |
min_delta=0,
|
| 154 |
patience=2,
|
|
@@ -157,11 +153,14 @@ if __name__ == "__main__":
|
|
| 157 |
|
| 158 |
epochs = 22
|
| 159 |
batch_size = 100
|
| 160 |
-
|
| 161 |
-
|
| 162 |
history = model.fit(X_train, Y_train, batch_size = batch_size, epochs = epochs,
|
| 163 |
validation_data = (X_val, Y_val), verbose = 2, callbacks=[early_stopping])
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
| 165 |
plt.plot(history.history['accuracy'])
|
| 166 |
plt.plot(history.history['val_accuracy'])
|
| 167 |
plt.title('Model accuracy')
|
|
@@ -179,8 +178,8 @@ if __name__ == "__main__":
|
|
| 179 |
plt.legend(['Train', 'Validation'], loc='upper left')
|
| 180 |
plt.show()
|
| 181 |
|
| 182 |
-
# every training can give different results ,
|
| 183 |
-
|
| 184 |
|
| 185 |
|
| 186 |
|
|
|
|
| 18 |
from sklearn.model_selection import train_test_split
|
| 19 |
from sklearn.metrics import confusion_matrix
|
| 20 |
import itertools
|
| 21 |
+
from tensorflow.keras.utils import to_categorical
|
| 22 |
from keras.models import Sequential
|
| 23 |
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
|
| 24 |
from tensorflow.keras.optimizers.legacy import RMSprop
|
|
|
|
| 57 |
|
| 58 |
return ela_im
|
| 59 |
|
| 60 |
+
|
| 61 |
def shuffle_and_split_data(dataframe, test_size=0.2, random_state=59):
|
| 62 |
# Shuffle the DataFrame
|
| 63 |
shuffled_df = dataframe.sample(frac=1, random_state=random_state).reset_index(drop=True)
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
+
##############################################################
|
| 89 |
+
# handling the dataset , set it and label it
|
|
|
|
|
|
|
| 90 |
np.random.seed(22)
|
| 91 |
tf.random.set_seed(9)
|
| 92 |
|
|
|
|
| 94 |
traning_real_folder = 'datasets/training_set/real/'
|
| 95 |
|
| 96 |
|
|
|
|
| 97 |
traning_ela_output = 'datasets/training_set/ela_output/'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
traning_set = labeling(traning_real_folder, traning_fake_folder)
|
| 99 |
|
| 100 |
|
| 101 |
X = []
|
| 102 |
Y = []
|
| 103 |
+
|
| 104 |
+
#################################################################
|
| 105 |
+
# preprocess the images using ELA method and storing the output.
|
| 106 |
+
|
| 107 |
for index, row in traning_set.iterrows():
|
| 108 |
X.append(array(convert_to_ela_image(row[0], 90,traning_ela_output).resize((128, 128))).flatten() / 255.0)
|
| 109 |
Y.append(row[1])
|
| 110 |
|
|
|
|
| 111 |
X = np.array(X)
|
| 112 |
Y = to_categorical(Y, 2)
|
|
|
|
| 113 |
X = X.reshape(-1, 128, 128, 3)
|
|
|
|
| 114 |
X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size = 0.2, random_state=1,shuffle=True)
|
| 115 |
|
|
|
|
| 116 |
|
| 117 |
+
################################################################################
|
| 118 |
+
# Cnn network creation
|
| 119 |
+
model = Sequential()
|
| 120 |
model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'valid',
|
| 121 |
activation ='relu', input_shape = (128,128,3)))
|
| 122 |
print("Input: ", model.input_shape)
|
|
|
|
| 140 |
|
| 141 |
model.summary()
|
| 142 |
|
| 143 |
+
#Define optimizer .
|
| 144 |
optimizer = RMSprop(lr=0.0005, rho=0.9, epsilon=1e-08, decay=0.0)
|
| 145 |
+
#setting the model , loss func , mertics , optimizer.
|
| 146 |
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
|
| 147 |
+
#setting early stopping to train faster.
|
| 148 |
early_stopping = EarlyStopping(monitor='val_acc',
|
| 149 |
min_delta=0,
|
| 150 |
patience=2,
|
|
|
|
| 153 |
|
| 154 |
epochs = 22
|
| 155 |
batch_size = 100
|
| 156 |
+
#####################################################
|
| 157 |
+
#running the model , adding the validation set
|
| 158 |
history = model.fit(X_train, Y_train, batch_size = batch_size, epochs = epochs,
|
| 159 |
validation_data = (X_val, Y_val), verbose = 2, callbacks=[early_stopping])
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
#####################################################
|
| 163 |
+
#plots and metrics
|
| 164 |
plt.plot(history.history['accuracy'])
|
| 165 |
plt.plot(history.history['val_accuracy'])
|
| 166 |
plt.title('Model accuracy')
|
|
|
|
| 178 |
plt.legend(['Train', 'Validation'], loc='upper left')
|
| 179 |
plt.show()
|
| 180 |
|
| 181 |
+
# every training can give different results , you can mark the next line as comment when you got the best result running the test set.
|
| 182 |
+
model.save('ELA_CNN_ART_V2.h5')
|
| 183 |
|
| 184 |
|
| 185 |
|