Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

model.py 2.2 KB

You have to be logged in to leave a comment. Sign In
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
  1. import tensorflow as tf
  2. from tensorflow.keras import layers, Model
  3. class FEModel:
  4. def __init__(self, model_name="FaceExpressionModel"):
  5. super(FEModel, self).__init__()
  6. self.base_model = tf.keras.Sequential([layers.RandomFlip("horizontal"),layers.RandomRotation(0.1),layers.RandomZoom(0.2),])
  7. self.model = None
  8. self.build()
  9. def build(self):
  10. """ Builds the Keras model based """
  11. inputs = tf.keras.Input(shape=[48, 48, 1])
  12. x = self.base_model(inputs)
  13. x = layers.Conv2D(filters=16, kernel_size=3, activation='relu')(x)
  14. x = layers.MaxPooling2D(pool_size=2)(x)
  15. x = layers.Conv2D(filters=32, kernel_size=3, activation='relu')(x)
  16. x = layers.MaxPooling2D(pool_size=2)(x)
  17. x = layers.Conv2D(filters=64, kernel_size=3, activation='relu')(x)
  18. x = layers.MaxPooling2D(pool_size=2)(x)
  19. x = layers.Conv2D(filters=128, kernel_size=3, activation="relu")(x)
  20. x = layers.MaxPooling2D(pool_size=2)(x)
  21. x = layers.Flatten()(x)
  22. x = layers.Dense(1024, activation='relu')(x)
  23. x = layers.Dropout(0.5)(x)
  24. outputs = layers.Dense(7, activation="softmax")(x)
  25. self.model = Model(inputs=inputs, outputs=outputs)
  26. #print(self.model.summary())
  27. def compile(self, optimizer, loss, metrics):
  28. """ Compiles the model """
  29. return self.model.compile(optimizer=optimizer,
  30. loss=loss,
  31. metrics=metrics,
  32. weighted_metrics=None,
  33. run_eagerly=None,
  34. steps_per_execution=None,
  35. jit_compile=None)
  36. def train(self, train_dataset, batch_size=None, epochs=1, validation_data=None, callbacks=None):
  37. """ Trains the model """
  38. return self.model.fit(train_dataset, batch_size=batch_size, epochs=epochs, validation_data=validation_data, callbacks=callbacks)
  39. def evaluate(self, test_dataset):
  40. """Evaluates the model"""
  41. return self.model.evaluate(test_dataset)
  42. def save(self, file_path):
  43. """Saves the model"""
  44. self.model.save(file_path)
  45. if __name__ == '__main__':
  46. model = FEModel()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...