-
Notifications
You must be signed in to change notification settings - Fork 48
/
deepcaps.py
208 lines (159 loc) · 9.24 KB
/
deepcaps.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from keras import backend as K
from keras import layers, models, optimizers
from keras.layers import Layer
from keras.layers import Input, Conv2D, Activation, Dense, Dropout, Lambda, Reshape, Concatenate
from keras.layers import BatchNormalization, MaxPooling2D, Flatten, Conv1D, Deconvolution2D, Conv2DTranspose
from keras.callbacks import Callback, ModelCheckpoint, TensorBoard
from keras.utils import plot_model
from keras.layers.convolutional import UpSampling2D
import numpy as np
import tensorflow as tf
import os
from PIL import Image
from capslayers import Conv2DCaps, ConvCapsuleLayer3D, CapsuleLayer, CapsToScalars, Mask_CID, Mask, ConvertToCaps, FlattenCaps
# To limit the GPU usage
# config = tf.ConfigProto()
# config.gpu_options.allow_growth=True
# sess = tf.Session(config=config)
# K.set_session(sess)
def DeepCapsNet(input_shape, n_class, routings):
# assemble encoder
x = Input(shape=input_shape)
l = x
l = Conv2D(128, (3, 3), strides=(1, 1), activation='relu', padding="same")(l) # common conv layer
l = BatchNormalization()(l)
l = ConvertToCaps()(l)
l = Conv2DCaps(32, 4, kernel_size=(3, 3), strides=(2, 2), r_num=1, b_alphas=[1, 1, 1])(l)
l_skip = Conv2DCaps(32, 4, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = Conv2DCaps(32, 4, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = Conv2DCaps(32, 4, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = layers.Add()([l, l_skip])
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(2, 2), r_num=1, b_alphas=[1, 1, 1])(l)
l_skip = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = layers.Add()([l, l_skip])
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(2, 2), r_num=1, b_alphas=[1, 1, 1])(l)
l_skip = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = layers.Add()([l, l_skip])
l1 = l
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(2, 2), r_num=1, b_alphas=[1, 1, 1])(l)
l_skip = ConvCapsuleLayer3D(kernel_size=3, num_capsule=32, num_atoms=8, strides=1, padding='same', routings=3)(l)
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = layers.Add()([l, l_skip])
l2 = l
la = FlattenCaps()(l2)
lb = FlattenCaps()(l1)
l = layers.Concatenate(axis=-2)([la, lb])
# l = Dropout(0.4)(l)
digits_caps = CapsuleLayer(num_capsule=n_class, dim_capsule=32, routings=routings, channels=0, name='digit_caps')(l)
l = CapsToScalars(name='capsnet')(digits_caps)
m_capsnet = models.Model(inputs=x, outputs=l, name='capsnet_model')
y = Input(shape=(n_class,))
masked_by_y = Mask_CID()([digits_caps, y])
masked = Mask_CID()(digits_caps)
# Decoder Network
decoder = models.Sequential(name='decoder')
decoder.add(Dense(input_dim=32, activation="relu", output_dim=8 * 8 * 16))
decoder.add(Reshape((8, 8, 16)))
decoder.add(BatchNormalization(momentum=0.8))
decoder.add(Deconvolution2D(64, 3, 3, subsample=(1, 1), border_mode='same'))
decoder.add(Deconvolution2D(32, 3, 3, subsample=(2, 2), border_mode='same'))
decoder.add(Deconvolution2D(16, 3, 3, subsample=(2, 2), border_mode='same'))
decoder.add(Deconvolution2D(8, 3, 3, subsample=(2, 2), border_mode='same'))
decoder.add(Deconvolution2D(3, 3, 3, subsample=(1, 1), border_mode='same'))
decoder.add(Activation("relu"))
decoder.add(Reshape(target_shape=(64, 64, 3), name='out_recon'))
train_model = models.Model([x, y], [m_capsnet.output, decoder(masked_by_y)])
eval_model = models.Model(x, [m_capsnet.output, decoder(masked)])
train_model.summary()
return train_model, eval_model
def DeepCapsNet28(input_shape, n_class, routings):
# assemble encoder
x = Input(shape=input_shape)
l = x
l = Conv2D(128, (3, 3), strides=(1, 1), activation='relu', padding="same")(l) # common conv layer
l = BatchNormalization()(l)
l = ConvertToCaps()(l)
l = Conv2DCaps(32, 4, kernel_size=(3, 3), strides=(2, 2), r_num=1, b_alphas=[1, 1, 1])(l)
l_skip = Conv2DCaps(32, 4, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = Conv2DCaps(32, 4, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = Conv2DCaps(32, 4, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = layers.Add()([l, l_skip])
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(2, 2), r_num=1, b_alphas=[1, 1, 1])(l)
l_skip = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = layers.Add()([l, l_skip])
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(2, 2), r_num=1, b_alphas=[1, 1, 1])(l)
l_skip = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = layers.Add()([l, l_skip])
l1 = l
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(2, 2), r_num=1, b_alphas=[1, 1, 1])(l)
l_skip = ConvCapsuleLayer3D(kernel_size=3, num_capsule=32, num_atoms=8, strides=1, padding='same', routings=3)(l)
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = Conv2DCaps(32, 8, kernel_size=(3, 3), strides=(1, 1), r_num=1, b_alphas=[1, 1, 1])(l)
l = layers.Add()([l, l_skip])
l2 = l
la = FlattenCaps()(l2)
lb = FlattenCaps()(l1)
l = layers.Concatenate(axis=-2)([la, lb])
# l = Dropout(0.4)(l)
digits_caps = CapsuleLayer(num_capsule=n_class, dim_capsule=32, routings=routings, channels=0, name='digit_caps')(l)
l = CapsToScalars(name='capsnet')(digits_caps)
m_capsnet = models.Model(inputs=x, outputs=l, name='capsnet_model')
y = Input(shape=(n_class,))
masked_by_y = Mask_CID()([digits_caps, y])
masked = Mask_CID()(digits_caps)
# Decoder Network
decoder = models.Sequential(name='decoder')
decoder.add(Dense(input_dim=32, activation="relu", output_dim=7 * 7 * 16))
decoder.add(Reshape((7, 7, 16)))
decoder.add(BatchNormalization(momentum=0.8))
decoder.add(Deconvolution2D(64, 3, 3, subsample=(1, 1), border_mode='same'))
decoder.add(Deconvolution2D(32, 3, 3, subsample=(2, 2), border_mode='same'))
decoder.add(Deconvolution2D(16, 3, 3, subsample=(2, 2), border_mode='same'))
decoder.add(Deconvolution2D(1, 3, 3, subsample=(1, 1), border_mode='same'))
decoder.add(Activation("relu"))
decoder.add(Reshape(target_shape=(28, 28, 1), name='out_recon'))
train_model = models.Model([x, y], [m_capsnet.output, decoder(masked_by_y)])
eval_model = models.Model(x, [m_capsnet.output, decoder(masked)])
train_model.summary()
return train_model, eval_model
def BaseCapsNet(input_shape, n_class, routings):
# assemble encoder
x = Input(shape=input_shape)
l = x
l = Conv2D(256, (9, 9), strides=(2, 2), activation='relu', padding="same")(l)
l = BatchNormalization()(l)
l = Conv2D(256, (9, 9), strides=(2, 2), activation='relu', padding="same")(l)
l = BatchNormalization()(l)
l = ConvertToCaps()(l)
l = Conv2DCaps(16, 6, kernel_size=(3, 3), strides=(2, 2), r_num=1, b_alphas=[1, 1, 1])(l)
l = FlattenCaps()(l)
digits_caps = CapsuleLayer(num_capsule=10, dim_capsule=8, routings=routings, channels=0, name='digit_caps')(l)
l = CapsToScalars(name='capsnet')(digits_caps)
m_capsnet = models.Model(inputs=x, outputs=l, name='capsnet_model')
y = layers.Input(shape=(n_class,))
masked_by_y = Mask()([digits_caps, y]) # The true label is used to mask the output of capsule layer. For training
masked = Mask()(digits_caps)
# Decoder Network
decoder = models.Sequential(name='decoder')
decoder.add(Dense(input_dim=80, activation="relu", output_dim=8 * 8 * 16))
decoder.add(Reshape((8, 8, 16)))
decoder.add(BatchNormalization(momentum=0.8))
decoder.add(layers.Deconvolution2D(64, 3, 3, subsample=(1, 1), border_mode='same'))
decoder.add(layers.Deconvolution2D(32, 3, 3, subsample=(2, 2), border_mode='same'))
decoder.add(layers.Deconvolution2D(16, 3, 3, subsample=(2, 2), border_mode='same'))
decoder.add(layers.Deconvolution2D(3, 3, 3, subsample=(1, 1), border_mode='same'))
decoder.add(Activation("relu"))
decoder.add(layers.Reshape(target_shape=(32, 32, 3), name='out_recon'))
train_model = models.Model([x, y], [m_capsnet.output, decoder(masked_by_y)])
eval_model = models.Model(x, [m_capsnet.output, decoder(masked)])
train_model.summary()
return train_model, eval_model