CelestialSurveyor
45 строк · 1.3 Кб
1from cryptography.fernet import Fernet
2from slow_fast_exp import SlowFast_body, bottleneck
3from tensorflow.keras.models import Model
4
5
6def build_model() -> Model:
7"""
8Builds and returns a SlowFast model with specific configurations.
9You can update this function with your own model.
10
11Returns:
12Model: The built SlowFast model.
13"""
14model = SlowFast_body([3, 4, 6, 3], bottleneck)
15return model
16
17
18def encrypt_model(model_name: str, key: bytes = b'J17tdv3zz2nemLNwd17DV33-sQbo52vFzl2EOYgtScw=') -> None:
19"""
20Encrypts the model weights and saves them to a binary file.
21Preliminary version of model decryption. It was done when I was thinking to make this project open source or not.
22
23Args:
24model_name: Name of the model file to be encrypted.
25key: Encryption key to be used. Default is a predefined key.
26
27Returns:
28None
29"""
30with open(f"{model_name}.h5", "rb") as file:
31model_bytes = file.read()
32
33# Use the provided key to create a cipher
34cipher = Fernet(key)
35
36# Encrypt the entire model
37encrypted_model = cipher.encrypt(model_bytes)
38
39# Save the encrypted weights to a file
40with open(f"{model_name}.bin", "wb") as file:
41file.write(encrypted_model)
42
43
44if __name__ == '__main__':
45build_model().summary()
46