def inference(self, features): """Adds the inference graph ops. Builds the architecture of the neural net to drive logits from features. The inference graph includes a convolution layer, a primary capsule layer and a 10-capsule final layer. Optionally, it also adds the reconstruction network on top of the 10-capsule final layer. Args: features: Dictionary of batched feature tensors like images and labels. Returns: A model.Inferred named tuple of expected outputs of the model like 'logits' and 'recons' for the reconstructions. """ image_dim = features['height'] image_depth = features['depth'] image = features['images'] image_4d = tf.reshape(image, [-1, image_depth, image_dim, image_dim]) # ReLU Convolution (conv1 layer start) with tf.variable_scope('conv1') as scope: kernel = variables.weight_variable(shape=[9, 9, image_depth, 256], stddev=5e-2, verbose=self._hparams.verbose) biases = variables.bias_variable([256], verbose=self._hparams.verbose) conv1 = tf.nn.conv2d(image_4d, kernel, [1, 1, 1, 1], padding=self._hparams.padding, data_format='NCHW') pre_activation = tf.nn.bias_add(conv1, biases, data_format='NCHW') relu1 = tf.nn.relu(pre_activation, name=scope.name) if self._hparams.verbose: tf.summary.histogram('activation', relu1) # conv1 laeyr end, return [128, 256, 20, 20] in NCHW format. # Then expand dims to [128, 1, 256, 20, 20] hidden1 = tf.expand_dims(relu1, 1) # Capsules, including primary capsules layer and digit capsules layer. # The final output here is [batch_size, 10, 16] capsule_output = self._build_capsule(hidden1, features['num_classes']) # calculate the length of v using vector norm or 2-norm ||v||_2 # return [batch_size, 10] # equals to the sqrt(reduce_sum(square(v))) logits = tf.norm(capsule_output, axis=-1) # Reconstruction if self._hparams.remake: remake = self._remake(features, capsule_output) else: remake = None return model.Inferred(logits, remake)
def inference(self, features): """Adds the inference graph ops. Builds the architecture of the neural net to drive logits from features. The inference graph includes a convolution layer, a primary capsule layer and a 10-capsule final layer. Optionally, it also adds the reconstruction network on top of the 10-capsule final layer. Args: features: Dictionary of batched feature tensors like images and labels. Returns: A model.Inferred named tuple of expected outputs of the model like 'logits' and 'recons' for the reconstructions. """ image_height = features['height'] image_width = features['width'] image_depth = features['depth'] image = features['images'] image_4d = tf.reshape(image, [-1, image_depth, image_height, image_width]) # ReLU Convolution with tf.variable_scope('conv1') as scope: kernel = variables.weight_variable(shape=[9, 9, image_depth, 256], stddev=5e-2, verbose=self._hparams.verbose) biases = variables.bias_variable([256], verbose=self._hparams.verbose) conv1 = tf.nn.conv2d(image_4d, kernel, [1, 1, 1, 1], padding=self._hparams.padding, data_format='NCHW') pre_activation = tf.nn.bias_add(conv1, biases, data_format='NCHW') relu1 = tf.nn.relu(pre_activation, name=scope.name) if self._hparams.verbose: tf.summary.histogram('activation', relu1) hidden1 = tf.expand_dims(relu1, 1) # Capsules capsule_output = self._build_capsule(hidden1, features['num_classes']) logits = tf.norm(capsule_output, axis=-1) # Reconstruction if self._hparams.remake: remake = self._remake(features, capsule_output) else: remake = None return model.Inferred(logits, remake)
def inference(self, features): """Adds the inference graph ops. Builds the architecture of the neural net to drive logits from features. The inference graph includes a series of convolution and fully connected layers and outputs a [batch, 10] tensor as the logits. Args: features: Dictionary of batched feature tensors like images and labels. Returns: A model.Inferred named tuple of expected outputs of the model like 'logits' and 'remakes' for the reconstructions (to be added). """ image = features['images'] image_dim = features['height'] image_depth = features['depth'] image_4d = tf.reshape(image, [-1, image_depth, image_dim, image_dim]) conv = self._add_convs(image_4d, [image_depth, 512, 256]) hidden1 = tf.contrib.layers.flatten(conv) with tf.variable_scope('fc1') as scope: dim = hidden1.get_shape()[1].value weights = variables.weight_variable(shape=[dim, 1024], stddev=0.1, verbose=self._hparams.verbose) biases = variables.bias_variable(shape=[1024], verbose=self._hparams.verbose) pre_activation = tf.matmul(hidden1, weights) + biases hidden2 = tf.nn.relu(pre_activation, name=scope.name) with tf.variable_scope('softmax_layer') as scope: weights = variables.weight_variable( shape=[1024, features['num_classes']], stddev=0.1, verbose=self._hparams.verbose) biases = variables.bias_variable(shape=[features['num_classes']], verbose=self._hparams.verbose) logits = tf.matmul(hidden2, weights) + biases return model.Inferred(logits, None)
def build_replica(self, tower_idx): """Adds a replica graph ops. Builds the architecture of the neural net to derive logits from batched_dataset. The inference graph defined here should involve trainable variables otherwise the optimizer will raise a ValueError. Args: tower_idx: the index number for this tower. Each tower is named as tower_{tower_idx} and resides on gpu:{tower_idx}. Returns: Inferred namedtuple containing (logits, recons). """ # Image specs image_size = self._specs['image_size'] image_depth = self._specs['depth'] num_classes = self._specs['num_classes'] # Define input_tensor for input batched_images batched_images = tf.placeholder( tf.float32, shape=[None, image_depth, image_size, image_size], name='batched_images') # (?, 3, h, w) """visual""" tf.add_to_collection('tower_%d_batched_images' % tower_idx, batched_images) # declare the threshold placeholder for ensemble evaluation threshold = tf.placeholder(tf.float32, name='threshold') tf.add_to_collection('tower_%d_batched_threshold' % tower_idx, threshold) # ReLU Convolution with tf.variable_scope('conv1') as scope: kernel = variables.weight_variable(shape=[9, 9, image_depth, 256], stddev=5e-2, verbose=self._hparams.verbose) biases = variables.bias_variable([256], verbose=self._hparams.verbose) conv1 = tf.nn.conv2d(batched_images, kernel, strides=[1, 1, 1, 1], padding=self._hparams.padding, data_format='NCHW') pre_activation = tf.nn.bias_add(conv1, biases, data_format='NCHW', name='logits') """visual""" tf.add_to_collection('tower_%d_visual' % tower_idx, pre_activation) relu1 = tf.nn.relu(pre_activation, name=scope.name) if self._hparams.verbose: tf.summary.histogram(scope.name + '/activation', relu1) hidden1 = tf.expand_dims( relu1, 1) # (?, 1, 3, h, w) h,w are different from previous ones. # Capsules capsule_output = self._build_capsule(hidden1, num_classes, tower_idx) logits = tf.norm(capsule_output, axis=-1, name='logits') """visual""" tf.add_to_collection('tower_%d_visual' % tower_idx, logits) # Declare one-hot format placeholder for batched_labels batched_labels = tf.placeholder(tf.int32, shape=[None, num_classes], name='batched_labels') tf.add_to_collection('tower_%d_batched_labels' % tower_idx, batched_labels) # Reconstruction remake = None if self._hparams.remake: remake = self._remake(capsule_output, batched_images, batched_labels) tf.add_to_collection('tower_%d_recons' % tower_idx, remake) else: remake = None return model.Inferred(logits, remake)
def build_replica(self, tower_idx): """Adds a replica graph ops. Builds the architecture of the neural net to derive logits from batched_dataset. The inference graph defined here should involve trainable variables otherwise the optimizer will raise a ValueError. Args: tower_idx: the index number for this tower. Each tower is named as tower_{tower_idx} and resides on gpu:{tower_idx}. Returns: Inferred namedtuple containing (logits, None). """ # Image specs image_size = self._specs['image_size'] image_depth = self._specs['depth'] num_classes = self._specs['num_classes'] # Define input_tensor for input batched_images batched_images = tf.placeholder(tf.float32, shape=[None, image_depth, image_size, image_size], name='batched_images') """visual""" tf.add_to_collection('tower_%d_batched_images' % tower_idx, batched_images) # Add convolutional layers conv_out = self._add_convs(batched_images, [image_depth, 512, 256], tower_idx) hidden1 = tf.contrib.layers.flatten(conv_out) # flatten neurons, shape (?, rest) # Add fully connected layer 1, activation = relu with tf.variable_scope('fc1') as scope: dim = hidden1.get_shape()[1].value weights = variables.weight_variable(shape=[dim, 1024], stddev=0.1, verbose=self._hparams.verbose) biases = variables.bias_variable(shape=[1024], verbose=self._hparams.verbose) pre_activation = tf.add(tf.matmul(hidden1, weights), biases, name='logits') """visual""" tf.add_to_collection('tower_%d_visual' % tower_idx, pre_activation) hidden2 = tf.nn.relu(pre_activation, name=scope.name) # Add fully connected layer 2, activation = None with tf.variable_scope('softmax_layer') as scope: weights = variables.weight_variable( shape=[1024, num_classes], stddev=0.1, verbose=self._hparams.verbose) biases = variables.bias_variable( shape=[num_classes], verbose=self._hparams.verbose) logits = tf.add(tf.matmul(hidden2, weights), biases, name='logits') """visual""" tf.add_to_collection('tower_%d_visual' % tower_idx, logits) # Declare one-hot format placeholder for batched_labels batched_labels = tf.placeholder(tf.int32, shape=[None, num_classes], name='batched_labels') # 'tower_i/batched_labels:0' """visual""" tf.add_to_collection('tower_%d_batched_labels' % tower_idx, batched_labels) return model.Inferred(logits, None)
def inference(self, features): logits = variables.bias_variable([1, 3]) return model.Inferred(logits, None)
def inference(self, features): logits = tf.constant([[0.1, 0.2, 0.7]]) return model.Inferred(logits, None)