-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
75 lines (54 loc) · 2.47 KB
/
model.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
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
from keras.models import Model as KerasModel
from keras.layers import Input, Dense,Flatten, Conv2D, MaxPooling2D, BatchNormalization, Dropout, Reshape, Concatenate, LeakyReLU
from keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
IMGWIDTH = 256
class Classifier:
def __init__(self):
self.model = 0
def predict(self, x):
return self.model.predict(x)
def fit(self, x, y):
return self.model.train_on_batch(x, y)
def get_accuracy(self, x, y):
return self.model.test_on_batch(x, y)
def load(self, path):
self.model.load_weights(path)
class Meso4(Classifier):
def __init__(self, learning_rate=0.001):
self.model = self.init_model()
optimizer = Adam(learning_rate = learning_rate)
self.model.compile(optimizer = optimizer, loss='mean_squared_error', metrics=['accuracy'])
def init_model(self):
x = Input(shape=(IMGWIDTH, IMGWIDTH, 3))
x1 = Conv2D(8, (3,3), padding='same', activation='relu')(x)
x1 = BatchNormalization()(x1)
x1 = MaxPooling2D(pool_size=(2,2), padding='same')(x1)
x2 = Conv2D(8,(5,5), padding='same', activation='relu')(x1)
x2 = BatchNormalization()(x2)
x2 = MaxPooling2D(pool_size=(2,2), padding='same')(x2)
x3 = Conv2D(16, (5,5), padding='same', activation='relu')(x2)
x3 = BatchNormalization()(x3)
x3 = MaxPooling2D(pool_size=(2,2), padding='same')(x3)
x4 = Conv2D(16,(5,5), padding='same', activation='relu')(x3)
x4 = BatchNormalization()(x4)
x4 = MaxPooling2D(pool_size=(4,4), padding='same')(x4)
y = Flatten()(x4)
y = Dropout(0.5)(y)
y = Dense(16)(y)
y = LeakyReLU(negative_slope=0.1)(y)
y = Dropout(0.5)(y)
y = Dense(1, activation='sigmoid')(y)
return KerasModel(inputs=x, outputs=y)
def model_sprint():
MesoNet_classifier = Meso4()
MesoNet_classifier.load("Meso4_DF.h5")
image_data_generator = ImageDataGenerator(rescale=1.0/255)
data_generator = image_data_generator.flow_from_directory("./", classes=["test_images"])
num_to_label = {1:"real", 0:"deepfake"}
X, y = next(iter(data_generator))
probabilistic_predictions = MesoNet_classifier.predict(X)
predictions = num_to_label[round(probabilistic_predictions[0][0])]
return predictions