-
Notifications
You must be signed in to change notification settings - Fork 6
/
model.py
104 lines (88 loc) · 2.95 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
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
#!/usr/bin/env python3
"""
@author: Jithin Sasikumar
Defines and create model for training and evaluation.
`CNN-LSTM` is used for this project with 1D convolutional layers
followed by LSTM layers with self-attention and fully connected
layers. This script provides the flexibility to add any other
models by inheriting Model(ABC).
"""
from dataclasses import dataclass
from typing import Tuple
from abc import ABC, abstractmethod
from tensorflow import keras
from keras.models import Sequential
from keras_self_attention import SeqSelfAttention
from keras.layers import Conv1D, MaxPooling1D, LSTM
from keras.layers import Input, Dropout, BatchNormalization, Dense
class Models(ABC):
"""
Abstract base class that defines and creates model.
"""
@abstractmethod
def define_model(self):
pass
@abstractmethod
def create_model(self):
pass
@dataclass
class CNN_LSTM_Model(Models):
"""
Dataclass to create CNN-LSTM model that inherits Models class.
"""
input_shape: Tuple[int, int]
num_classes: int
def define_model(self) -> Sequential:
"""
Method to define model that can be used for training
and inference. This existing model can also be tweaked
by changing parameters, based on the requirements.
Parameters
----------
None.
Returns
-------
Sequential
"""
return Sequential(
[
Input(shape=self.input_shape),
BatchNormalization(),
#1D Convolutional layers
Conv1D(32, kernel_size=3, strides=1, padding='same'),
BatchNormalization(),
MaxPooling1D(pool_size = 3),
Conv1D(64, kernel_size=3, strides=1, padding='same'),
BatchNormalization(),
MaxPooling1D(pool_size = 3),
Conv1D(128, kernel_size=3, strides=1, padding='same'),
BatchNormalization(),
MaxPooling1D(pool_size = 3, padding='same'),
Dropout(0.30),
#LSTM layers
LSTM(units = 128, return_sequences=True),
SeqSelfAttention(attention_activation='tanh'),
LSTM(units = 128, return_sequences=False),
BatchNormalization(),
Dropout(0.30),
#Dense layers
Dense(256, activation='relu'),
Dense(64, activation='relu'),
Dropout(0.30),
Dense(self.num_classes, activation='softmax')
]
)
def create_model(self) -> Sequential:
"""
Method to create the model defined by define_model() method
and prints the model summary.
Parameters
----------
None.
Returns
-------
model: Sequential
"""
model: Sequential = self.define_model()
model.summary()
return model