| 6 | |
| 7 | |
| 8 | class CapsNet(object): |
| 9 | def __init__(self, is_training=True): |
| 10 | self.graph = tf.Graph() |
| 11 | with self.graph.as_default(): |
| 12 | if is_training: |
| 13 | self.X, self.Y = get_batch_data() |
| 14 | |
| 15 | self.build_arch() |
| 16 | self.loss() |
| 17 | |
| 18 | # t_vars = tf.trainable_variables() |
| 19 | self.optimizer = tf.train.AdamOptimizer() |
| 20 | self.global_step = tf.Variable(0, name='global_step', trainable=False) |
| 21 | self.train_op = self.optimizer.minimize(self.total_loss, global_step=self.global_step) # var_list=t_vars) |
| 22 | else: |
| 23 | self.X = tf.placeholder(tf.float32, |
| 24 | shape=(cfg.batch_size, 28, 28, 1)) |
| 25 | self.build_arch() |
| 26 | |
| 27 | tf.logging.info('Seting up the main structure') |
| 28 | |
| 29 | def build_arch(self): |
| 30 | with tf.variable_scope('Conv1_layer'): |
| 31 | # Conv1, [batch_size, 20, 20, 256] |
| 32 | conv1 = tf.contrib.layers.conv2d(self.X, num_outputs=256, |
| 33 | kernel_size=9, stride=1, |
| 34 | padding='VALID') |
| 35 | assert conv1.get_shape() == [cfg.batch_size, 20, 20, 256] |
| 36 | |
| 37 | # TODO: Rewrite the 'CapsConv' class as a function, the capsLay |
| 38 | # function should be encapsulated into tow function, one like conv2d |
| 39 | # and another is fully_connected in Tensorflow. |
| 40 | # Primary Capsules, [batch_size, 1152, 8, 1] |
| 41 | with tf.variable_scope('PrimaryCaps_layer'): |
| 42 | primaryCaps = CapsConv(num_units=8, with_routing=False) |
| 43 | caps1 = primaryCaps(conv1, num_outputs=32, kernel_size=9, stride=2) |
| 44 | assert caps1.get_shape() == [cfg.batch_size, 1152, 8, 1] |
| 45 | |
| 46 | # DigitCaps layer, [batch_size, 10, 16, 1] |
| 47 | with tf.variable_scope('DigitCaps_layer'): |
| 48 | digitCaps = CapsConv(num_units=16, with_routing=True) |
| 49 | self.caps2 = digitCaps(caps1, num_outputs=10) |
| 50 | |
| 51 | # Decoder structure in Fig. 2 |
| 52 | # 1. Do masking, how: |
| 53 | with tf.variable_scope('Masking'): |
| 54 | # a). calc ||v_c||, then do softmax(||v_c||) |
| 55 | # [batch_size, 10, 16, 1] => [batch_size, 10, 1, 1] |
| 56 | self.v_length = tf.sqrt(tf.reduce_sum(tf.square(self.caps2), |
| 57 | axis=2, keep_dims=True)) |
| 58 | self.softmax_v = tf.nn.softmax(self.v_length, dim=1) |
| 59 | assert self.softmax_v.get_shape() == [cfg.batch_size, 10, 1, 1] |
| 60 | |
| 61 | # b). pick out the index of max softmax val of the 10 caps |
| 62 | # [batch_size, 10, 1, 1] => [batch_size] (index) |
| 63 | argmax_idx = tf.argmax(self.softmax_v, axis=1, output_type=tf.int32) |
| 64 | assert argmax_idx.get_shape() == [cfg.batch_size, 1, 1] |
| 65 | |