File size: 3,641 Bytes
bac2848
 
2819875
bac2848
2819875
 
 
 
 
 
bac2848
bfbff00
 
bac2848
 
 
 
 
bfbff00
 
bac2848
2819875
 
 
 
 
 
 
 
 
 
 
bac2848
2819875
 
 
bfbff00
bac2848
bfbff00
bac2848
 
 
 
2819875
 
b7dba8c
bac2848
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2819875
 
f4ffc3f
bac2848
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfbff00
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
import tensorflow as tf

# Custom conv2d Layer
class Conv2DBatchNMaxP(tf.keras.layers.Layer):
    # TAMBAHKAN **kwargs di sini
    def __init__(self, inp1, inp2, **kwargs):
        super(Conv2DBatchNMaxP, self).__init__(**kwargs) # Teruskan kwargs
        self.inp1 = inp1
        self.inp2 = inp2
        self.conv2d_main = tf.keras.layers.Conv2D(32, (inp1, inp2), padding='same', activation='relu')
        self.bn_main = tf.keras.layers.BatchNormalization()
        self.maxpool2d_main = tf.keras.layers.MaxPool2D((2,2))
        self.maxpool2d_shortcut = tf.keras.layers.MaxPool2D((2,2))

    def call(self, inputs):
        x = self.conv2d_main(inputs)
        x = self.bn_main(x)
        x = self.maxpool2d_main(x)
        shortcut = self.maxpool2d_shortcut(inputs)
        return x + shortcut

    # Tambahkan get_config agar layer bisa disimpan/dimuat dengan benar
    def get_config(self):
        config = super(Conv2DBatchNMaxP, self).get_config()
        config.update({
            "inp1": self.inp1,
            "inp2": self.inp2,
        })
        return config


# Model Subclassing
class Conv2DModel(tf.keras.Model):
    # TAMBAHKAN **kwargs di sini untuk menangani parameter 'trainable', 'dtype', dll.
    def __init__(self, **kwargs):
        super(Conv2DModel, self).__init__(**kwargs) # Teruskan kwargs
        self.ConvLayer1a = tf.keras.layers.Conv2D(32, (3,3), padding='same', activation='relu', input_shape=(150,150,1))
        self.ConvLayer1b = tf.keras.layers.BatchNormalization()
        self.ConvLayer1c = tf.keras.layers.MaxPool2D((2,2))
        self.ConvLayer2 = Conv2DBatchNMaxP(3, 3)
        self.ConvLayer3 = Conv2DBatchNMaxP(3, 3)
        self.ConvLayer4 = Conv2DBatchNMaxP(3, 3)
        self.flatten = tf.keras.layers.Flatten()
        
        # Dense layers
        self.dense1 = tf.keras.layers.Dense(256, activation='relu')
        self.dropout1 = tf.keras.layers.Dropout(0.5)
        self.dense2 = tf.keras.layers.Dense(128, activation='relu')
        self.dropout2 = tf.keras.layers.Dropout(0.5)
        self.dense3 = tf.keras.layers.Dense(128, activation='relu')
        self.dropout3 = tf.keras.layers.Dropout(0.5)
        self.dense4 = tf.keras.layers.Dense(128, activation='relu')
        self.dropout4 = tf.keras.layers.Dropout(0.5)
        self.dense5 = tf.keras.layers.Dense(128, activation='relu')
        self.dropout5 = tf.keras.layers.Dropout(0.5)
        self.dense6 = tf.keras.layers.Dense(128, activation='relu')
        self.dropout6 = tf.keras.layers.Dropout(0.5)
        self.dense7 = tf.keras.layers.Dense(128, activation='relu')
        self.dropout7 = tf.keras.layers.Dropout(0.5)
        self.dense8 = tf.keras.layers.Dense(128, activation='relu')
        self.dropout8 = tf.keras.layers.Dropout(0.5)
        
        # Output layer (Sesuai jumlah kelas dataset saat training!)
        self.dense9 = tf.keras.layers.Dense(31, activation='softmax') 

    def call(self, inputs):
        x = self.ConvLayer1a(inputs)
        x = self.ConvLayer1b(x)
        x = self.ConvLayer1c(x)
        x = self.ConvLayer2(x)
        x = self.ConvLayer3(x)
        x = self.ConvLayer4(x)
        x = self.flatten(x)
        x = self.dense1(x)
        x = self.dropout1(x)
        x = self.dense2(x)
        x = self.dropout2(x)
        x = self.dense3(x)
        x = self.dropout3(x)
        x = self.dense4(x)
        x = self.dropout4(x)
        x = self.dense5(x)
        x = self.dropout5(x)
        x = self.dense6(x)
        x = self.dropout6(x)
        x = self.dense7(x)
        x = self.dropout7(x)
        x = self.dense8(x)
        x = self.dropout8(x)
        return self.dense9(x)