-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
300 lines (234 loc) · 11.5 KB
/
Copy pathmodel.py
File metadata and controls
300 lines (234 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import os
from typing import Union
from keras.src.optimizers import Adam
from numpy import set_printoptions as np_set_print_opts
from tensorflow import expand_dims as tf_expand_dims
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.math import exp as tf_exp
import feature_extraction as fe
import pre_processing as pp
""" This module contains functions to create the model and there utility functions. """
from tensorflow.keras.layers import Conv2D, Conv2DTranspose, MaxPooling2D, UpSampling2D, Flatten, Dense, BatchNormalization
def callbacks(lr_decay_rate=-0.1):
# Callbacks
def exponential_decay(epoch, lr):
if epoch < 2:
return lr
else:
return float(lr * tf_exp(lr_decay_rate))
callback = keras.callbacks.LearningRateScheduler(exponential_decay)
if not os.path.exists(os.path.join(os.getcwd(), 'model')):
os.mkdir(os.path.join(os.getcwd(), 'model'))
if not os.path.exists(os.path.join(os.getcwd(), 'model', 'tmp')):
os.mkdir(os.path.join(os.getcwd(), 'model', 'tmp'))
file_path = os.path.join(os.getcwd(), 'model', 'tmp', "{epoch:02d}-{val_accuracy:.3f}-{val_loss:.2f}.keras")
checkpoint = keras.callbacks.ModelCheckpoint(filepath=file_path,
monitor="val_accuracy", verbose=1,
save_best_only=True,
save_weights_only=False,
mode='auto', save_freq="epoch")
return callback, checkpoint
def cnn_autoencoder(input_shape, learning_rate=0.001):
"""
Creates a CNN encoder-decoder model for audio signal reconstruction.
:param learning_rate: learning rate provided to the model
:param input_shape: A tuple representing the shape of the input STFT (128, 376, 1).
Returns:
A compiled Keras model.
"""
# Encoder
inputs = keras.Input(shape=input_shape)
# Encoder Block
def encoder_block(x, filters, kernel_size=(3, 3), activation=layers.LeakyReLU(negative_slope=0.1), padding="same"):
shortcut = x
x = Conv2D(filters=filters, kernel_size=kernel_size, activation=activation, padding=padding)(x)
x = BatchNormalization()(x)
# x = Conv2D(filters=filters, kernel_size=kernel_size, activation=None, padding=padding)(x)
# x = layers.add([x, shortcut]) # Removed for this version
# x = layers.Activation(activation)(x)
x = layers.Dropout(0.2)(x)
return x
x = encoder_block(inputs, filters=16)
x = MaxPooling2D(pool_size=(2, 2), padding="same")(x)
x = encoder_block(x, filters=32)
x = MaxPooling2D(pool_size=(2, 2), padding="same")(x)
x = encoder_block(x, filters=64)
x = MaxPooling2D(pool_size=(2, 2), padding="same")(x)
encoded = Flatten()(x)
# Decoder
# x = Dense(units=640, activation=layers.LeakyReLU(negative_slope=0.1))(encoded)
# x = Dense(units=640, activation=layers.LeakyReLU(negative_slope=0.1))(x)
# x = layers.Reshape((10, 12, 16))(x)
# Decoder Block
def decoder_block(x, filters, kernel_size=(3, 3), activation=layers.LeakyReLU(negative_slope=0.1), padding="same"):
shortcut = Conv2DTranspose(filters=filters, kernel_size=kernel_size, strides=(1, 1), activation=None,
padding=padding)(x)
x = Conv2DTranspose(filters=filters, kernel_size=kernel_size, activation=activation, padding=padding)(x)
x = BatchNormalization()(x)
# x = Conv2DTranspose(filters=filters, kernel_size=kernel_size, activation=None, padding=padding)(x)
# Removed for this version: layers.add([x, shortcut])
# x = layers.Activation(activation)(x)
x = layers.Dropout(0.2)(x)
return x
x = decoder_block(x, filters=64)
x = UpSampling2D(size=(2, 2))(x)
x = decoder_block(x, filters=32)
x = UpSampling2D(size=(2, 2))(x)
x = decoder_block(x, filters=16)
flatten = Flatten()(x)
dense = Dense(units=64, activation="relu")(flatten)
dense = Dense(units=32, activation="relu")(dense)
outputs = Dense(units=2, activation="sigmoid")(dense)
# Model
model = keras.Model(inputs=inputs, outputs=outputs)
optimizer = Adam(learning_rate=learning_rate)
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=["accuracy"])
model.summary()
return model, callbacks()
# A complex cnn model.
def complex_cnn_model(input_shape, learning_rate=0.001):
model = keras.Sequential([
layers.InputLayer(shape=input_shape),
# First conv block
layers.Conv2D(filters=32, kernel_size=5, strides=1, padding="Same", name="1b1Conv",
kernel_initializer='he_normal',
activation=layers.LeakyReLU(negative_slope=0.1), ),
layers.BatchNormalization(),
layers.Conv2D(filters=32, kernel_size=3, strides=1, padding="Same", name="1b2Conv",
kernel_initializer='he_normal',
activation=layers.LeakyReLU(negative_slope=0.1), ),
layers.GaussianDropout(0.5),
layers.BatchNormalization(),
# Second conv block
layers.Conv2D(filters=64, kernel_size=3, strides=2, padding="same", name="2b1Conv",
kernel_initializer='he_normal',
activation=layers.LeakyReLU(negative_slope=0.1), ),
layers.BatchNormalization(),
layers.Conv2D(filters=64, kernel_size=3, strides=1, padding="same", name="2b2Conv",
kernel_initializer='he_normal',
activation=layers.LeakyReLU(negative_slope=0.1), ),
layers.GaussianDropout(0.5),
layers.BatchNormalization(),
# Third conv block
layers.Conv2D(filters=128, kernel_size=3, strides=2, padding="same", name="3b1Conv",
kernel_initializer='he_normal',
activation=layers.LeakyReLU(negative_slope=0.1), ),
layers.BatchNormalization(),
layers.Conv2D(filters=128, kernel_size=3, strides=1, padding="same", name="3b2Conv",
kernel_initializer='he_normal',
activation=layers.LeakyReLU(negative_slope=0.1), ),
layers.GaussianDropout(0.5),
layers.BatchNormalization(),
# Fourth conv block
layers.Conv2D(filters=256, kernel_size=3, strides=2, padding="same", name="4b1Conv",
kernel_initializer='he_normal',
activation=layers.LeakyReLU(negative_slope=0.1), ),
layers.BatchNormalization(),
layers.Conv2D(filters=256, kernel_size=3, strides=1, padding="same", name="4b2Conv",
kernel_initializer='he_normal',
activation=layers.LeakyReLU(negative_slope=0.1), ),
layers.BatchNormalization(),
layers.GaussianDropout(0.5),
# 5th
layers.Conv2D(filters=256, kernel_size=3, strides=2, padding="same", name="5b1Conv",
kernel_initializer='he_normal',
activation=layers.LeakyReLU(negative_slope=0.1), ),
layers.BatchNormalization(),
layers.Conv2D(filters=256, kernel_size=3, strides=1, padding="same", name="5b2Conv",
kernel_initializer='he_normal',
activation=layers.LeakyReLU(negative_slope=0.1), ),
layers.BatchNormalization(),
layers.GaussianDropout(0.5),
# Flattening and then classifying
layers.Flatten(),
layers.Dense(units=64, name="1FC", activation=layers.LeakyReLU(negative_slope=0.1),
kernel_initializer='he_uniform', ),
layers.Dense(units=64, name="2FC", activation=layers.LeakyReLU(negative_slope=0.1),
kernel_initializer='he_uniform', ),
layers.GaussianDropout(0.6),
layers.Dense(units=64, name="3FC", activation=layers.LeakyReLU(negative_slope=0.1),
kernel_initializer='he_uniform', ),
layers.Dense(units=32, name="4FC", activation=layers.LeakyReLU(negative_slope=0.1),
kernel_initializer='he_uniform', ),
layers.Dense(units=2, name="Output", activation='sigmoid', kernel_initializer='glorot_uniform', ),
])
# Compiling model
optimizer = Adam(learning_rate=learning_rate)
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=["accuracy"])
model.summary()
return model, callbacks()
# convert audio to wav or flac format
def verify(audio, model: keras.Model) -> Union[tuple[str, bool], str]:
# extracting feature from audio
feature_extractor = fe.ExtractSTFTDB(n_fft=512, hop_length=128)
normalizer = pp.TFMinMaxNormalize(new_min=-1, new_max=1)
feature = feature_extractor.extract(audio)
print("\nFeatures:\n", feature, feature.shape)
norm_feature = normalizer.normalize(feature)
print("\nNormalized Features:\n", norm_feature, norm_feature.shape)
reshaped_feature = tf_expand_dims(norm_feature, axis=0)
reshaped_feature = tf_expand_dims(reshaped_feature, axis=-1)
print("\nShape for predicting: ", reshaped_feature.shape)
np_set_print_opts(precision=2)
# predicting
prediction = model.predict(reshaped_feature)
print(prediction)
if prediction[0, 0] > 0.5:
return (f'This audio is {prediction[0, 0] * 100:.2f}% is likely to be fake.\n'
f'This audio is {prediction[0, 1] * 100:.2f}% is likely to be real.'), False
elif prediction[0, 1] > 0.5:
return (f'This audio is {prediction[0, 1] * 100:.2f}% is likely to be real.\n'
f'This audio is {prediction[0, 0] * 100:.2f}% is likely to be fake.'), True
else:
return 'Sorry! The model is not working properly.'
# deprecated
def load_model(model_path):
try:
model = keras.models.browse_model(model_path)
if model:
print(f'loaded model: {model_path}')
return model
except Exception as e:
print('Tried loading the model: Failed')
print('trying to load the model with custom function LeakyReLU(negative_slope=0.1)')
try:
model = keras.models.load_model(model_path,
custom_objects={'LeakyReLU': keras.layers.LeakyReLU(negative_slope=0.1)})
if model:
print(f'loaded model: {model_path}')
return model
except Exception as e:
raise f"Tried loading the model with custom function LeakyReLU(negative_slope=0.1): Failed with: {e}"
# loading video
import io
from moviepy.editor import VideoFileClip
from soundfile import write
from librosa import load
def convert_to_wav_in_memory(video_path):
"""
Extracts audio from video, converts it to WAV in-memory, and returns it.
Args:
video_path: Path to the video file.
Returns:
A byte string containing the WAV-encoded audio data or None on error.
"""
# Extract audio using moviepy (same as previous example)
clip = VideoFileClip(video_path)
audio_data = clip.audio.to_ndarray()
clip.close()
# Convert audio data to WAV format in-memory
wav_data = io.BytesIO()
write(wav_data, audio_data, samplerate=clip.audio.fps, format="wav")
# Return the WAV-encoded audio data
return wav_data.getvalue()
def load_video(video_path):
# Example usage
video_path = video_path
wav_data = convert_to_wav_in_memory(video_path)
if wav_data:
# Now you can use wav_data as the source for librosa.load
y, sr = load(io.BytesIO(wav_data), sr=None)
# Process the loaded audio data using librosa
else:
print("Error converting audio to WAV format.")