-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoencoders.py
95 lines (79 loc) · 4.29 KB
/
autoencoders.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
import tensorflow as tf
import keras
import keras.layers as L
class HeadAutoEncoder:
"""
autoencoder for head representation
"""
def __init__(self,input_shape=(88, 88, 3),code_size=128):
self.input_shape=input_shape
self.code_size=code_size
def model_func(self):
encoder = keras.models.Sequential()
encoder.add(L.InputLayer(self.input_shape))
encoder.add(L.Conv2D(32, kernel_size=(3, 3), strides=1, padding='same', activation='relu'))
encoder.add(L.MaxPool2D(pool_size=(2, 2)))
encoder.add(L.Conv2D(64, kernel_size=(3, 3), strides=1, padding='same', activation='relu'))
encoder.add(L.MaxPool2D(pool_size=(2, 2)))
encoder.add(L.Conv2D(128, kernel_size=(3, 3), strides=1, padding='same', activation='relu'))
encoder.add(L.MaxPool2D(pool_size=(2, 2)))
encoder.add(L.Conv2D(256, kernel_size=(3, 3), strides=1, padding='same', activation='relu'))
encoder.add(L.MaxPool2D(pool_size=(2, 2)))
encoder.add(L.Flatten())
encoder.add(L.Dense(self.code_size))
# decoder
decoder = keras.models.Sequential()
decoder.add(L.InputLayer((self.code_size,)))
decoder.add(L.Dense(1024))
decoder.add(L.Reshape((2, 2, 256)))
decoder.add(L.Conv2DTranspose(filters=128, kernel_size=(3, 3), strides=2, activation='relu', padding='valid'))
decoder.add(L.Conv2DTranspose(filters=64, kernel_size=(3, 3), strides=2, activation='relu', padding='valid'))
decoder.add(L.Conv2DTranspose(filters=32, kernel_size=(3, 3), strides=2, activation='relu', padding='same'))
decoder.add(L.Conv2DTranspose(filters=8, kernel_size=(3, 3), strides=2, activation='relu', padding='same'))
decoder.add(L.Conv2DTranspose(filters=3, kernel_size=(3, 3), strides=2, activation=None, padding='same'))
inp = L.Input(self.input_shape)
code = encoder(inp)
reconstruction = decoder(code)
autoencoder = keras.models.Model(inp, reconstruction)
autoencoder.compile('adamax', 'mse')
return encoder, decoder,autoencoder
class BodyAutoEncoder:
"""
autoencoder for head representation
"""
def __init__(self,input_shape=(128, 128, 3),code_size=256):
self.input_shape=input_shape
self.code_size=code_size
def model_func(self):
encoder = keras.models.Sequential()
encoder.add(L.InputLayer(self.input_shape))
encoder.add(L.Conv2D(32, kernel_size=(3, 3), strides=1, padding='same', activation='relu'))
encoder.add(L.MaxPool2D(pool_size=(2, 2)))
encoder.add(L.Conv2D(64, kernel_size=(3, 3), strides=1, padding='same', activation='relu'))
encoder.add(L.MaxPool2D(pool_size=(2, 2)))
encoder.add(L.Conv2D(128, kernel_size=(3, 3), strides=1, padding='same', activation='relu'))
encoder.add(L.MaxPool2D(pool_size=(2, 2)))
encoder.add(L.Conv2D(256, kernel_size=(3, 3), strides=1, padding='same', activation='relu'))
encoder.add(L.MaxPool2D(pool_size=(2, 2)))
encoder.add(L.Flatten())
encoder.add(L.Dense(self.code_size))
# decoder
decoder = keras.models.Sequential()
decoder.add(L.InputLayer((self.code_size,)))
decoder.add(L.Dense(1024))
decoder.add(L.Reshape((2, 2, 256)))
decoder.add(L.Conv2DTranspose(filters=128, kernel_size=(3, 3), strides=2, activation='relu', padding='same'))
decoder.add(L.Conv2DTranspose(filters=64, kernel_size=(3, 3), strides=2, activation='relu', padding='same'))
decoder.add(L.Conv2DTranspose(filters=32, kernel_size=(3, 3), strides=2, activation='relu', padding='same'))
decoder.add(L.Conv2DTranspose(filters=16, kernel_size=(3, 3), strides=2, activation='relu', padding='same'))
decoder.add(L.Conv2DTranspose(filters=8, kernel_size=(3, 3), strides=2, activation='relu', padding='same'))
decoder.add(L.Conv2DTranspose(filters=3, kernel_size=(3, 3), strides=2, activation=None, padding='same'))
inp = L.Input(self.input_shape)
code = encoder(inp)
reconstruction = decoder(code)
autoencoder = keras.models.Model(inp, reconstruction)
autoencoder.compile('adamax', 'mse')
return encoder, decoder,autoencoder
if __name__ == '__main__':
head_coder=HeadAutoEncoder()
head_coder.model_func()