def testBuildAndCheckAllEndPointsUptoPreAuxLogitsWithOutputStrideEight( self): batch_size = 5 height, width = 299, 299 inputs = tf.random_uniform((batch_size, height, width, 3)) _, end_points = inception_resnet_v2_base(inputs, final_endpoint='PreAuxLogits', output_stride=8) endpoints_shapes = { 'Conv2d_1a_3x3': [5, 149, 149, 32], 'Conv2d_2a_3x3': [5, 147, 147, 32], 'Conv2d_2b_3x3': [5, 147, 147, 64], 'MaxPool_3a_3x3': [5, 73, 73, 64], 'Conv2d_3b_1x1': [5, 73, 73, 80], 'Conv2d_4a_3x3': [5, 71, 71, 192], 'MaxPool_5a_3x3': [5, 35, 35, 192], 'Mixed_5b': [5, 35, 35, 320], 'Mixed_6a': [5, 33, 33, 1088], 'PreAuxLogits': [5, 33, 33, 1088] } self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys()) for endpoint_name in endpoints_shapes: expected_shape = endpoints_shapes[endpoint_name] self.assertTrue(endpoint_name in end_points) self.assertListEqual( end_points[endpoint_name].get_shape().as_list(), expected_shape)
def testBuildAndCheckAllEndPointsUptoPreAuxLogitsWithAlignedFeatureMaps( self): batch_size = 5 height, width = 299, 299 inputs = tf.random_uniform((batch_size, height, width, 3)) _, end_points = inception_resnet_v2_base(inputs, final_endpoint='PreAuxLogits', align_feature_maps=True) endpoints_shapes = { 'Conv2d_1a_3x3': [5, 150, 150, 32], 'Conv2d_2a_3x3': [5, 150, 150, 32], 'Conv2d_2b_3x3': [5, 150, 150, 64], 'MaxPool_3a_3x3': [5, 75, 75, 64], 'Conv2d_3b_1x1': [5, 75, 75, 80], 'Conv2d_4a_3x3': [5, 75, 75, 192], 'MaxPool_5a_3x3': [5, 38, 38, 192], 'Mixed_5b': [5, 38, 38, 320], 'Mixed_6a': [5, 19, 19, 1088], 'PreAuxLogits': [5, 19, 19, 1088] } self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys()) for endpoint_name in endpoints_shapes: expected_shape = endpoints_shapes[endpoint_name] self.assertTrue(endpoint_name in end_points) self.assertListEqual( end_points[endpoint_name].get_shape().as_list(), expected_shape)
def conv_tower_fn(self, images, is_training=True, reuse=None): """Computes convolutional features using the InceptionV3 model. Args: images: A tensor of shape [batch_size, height, width, channels]. is_training: whether is training or not. reuse: whether or not the network and its variables should be reused. To be able to reuse 'scope' must be given. Returns: A tensor of shape [batch_size, OH, OW, N], where OWxOH is resolution of output feature map and N is number of output features (depends on the network architecture). """ mparams = self._mparams['conv_tower_fn'] logging.debug('Using final_endpoint=%s', mparams.final_endpoint) with tf.variable_scope('conv_tower_fn/INCE'): if reuse: tf.get_variable_scope().reuse_variables() # with slim.arg_scope(inception.inception_v3_arg_scope()): with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()): with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=is_training): net, _ = inception_resnet_v2.inception_resnet_v2_base( images, final_endpoint=mparams.final_endpoint) # net, _ = inception.inception_v3_base( # images, final_endpoint=mparams.final_endpoint) return net
def testBuildBaseNetwork(self): batch_size = 5 height, width = 299, 299 inputs = tf.random_uniform((batch_size, height, width, 3)) net, end_points = inception_resnet_v2_base(inputs) self.assertTrue( net.op.name.startswith('InceptionResnetV2/Conv2d_7b_1x1')) self.assertListEqual(net.get_shape().as_list(), [batch_size, 8, 8, 1536]) expected_endpoints = [ 'Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 'MaxPool_3a_3x3', 'Conv2d_3b_1x1', 'Conv2d_4a_3x3', 'MaxPool_5a_3x3', 'Mixed_5b', 'Mixed_6a', 'PreAuxLogits', 'Mixed_7a', 'Conv2d_7b_1x1' ] self.assertItemsEqual(end_points.keys(), expected_endpoints)
def testBuildOnlyUptoFinalEndpoint(self): batch_size = 5 height, width = 299, 299 endpoints = [ 'Conv2d_1a_3x3', 'Conv2d_2a_3x3', 'Conv2d_2b_3x3', 'MaxPool_3a_3x3', 'Conv2d_3b_1x1', 'Conv2d_4a_3x3', 'MaxPool_5a_3x3', 'Mixed_5b', 'Mixed_6a', 'PreAuxLogits', 'Mixed_7a', 'Conv2d_7b_1x1' ] for index, endpoint in enumerate(endpoints): with tf.Graph().as_default(): inputs = tf.random_uniform((batch_size, height, width, 3)) out_tensor, end_points = inception_resnet_v2_base( inputs, final_endpoint=endpoint) if endpoint != 'PreAuxLogits': self.assertTrue( out_tensor.op.name.startswith('InceptionResnetV2/' + endpoint)) self.assertItemsEqual(endpoints[:index + 1], end_points.keys())
scaledInputBatchImages = tf.subtract(scaledInputBatchImages, 0.5) scaledInputBatchImages = tf.multiply(scaledInputBatchImages, 2.0) # Create model if options.modelName == "NASNet": arg_scope = nasnet.nasnet_large_arg_scope() with slim.arg_scope(arg_scope): logits, endPoints = nasnet.build_nasnet_large(scaledInputBatchImages, is_training=False, num_classes=options.numClasses) elif options.modelName == "IncResV2": arg_scope = inception_resnet_v2.inception_resnet_v2_arg_scope() with slim.arg_scope(arg_scope): # logits, endPoints = inception_resnet_v2.inception_resnet_v2(scaledInputBatchImages, is_training=False) with tf.variable_scope('InceptionResnetV2', 'InceptionResnetV2', [scaledInputBatchImages], reuse=None) as scope: with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=False): net, endPoints = inception_resnet_v2.inception_resnet_v2_base(scaledInputBatchImages, scope=scope, activation_fn=tf.nn.relu) variablesToRestore = slim.get_variables_to_restore(include=["InceptionResnetV2"]) else: print ("Error: Model not found!") exit (-1) # TODO: Attach the decoder to the encoder print (endPoints.keys()) if options.useSkipConnections: print ("Adding skip connections from the encoder to the decoder!") predictedLogits = attachDecoder(net, endPoints, tf.shape(scaledInputBatchImages)) predictedMask = tf.expand_dims(tf.argmax(predictedLogits, axis=-1), -1, name="predictedMasks") if options.tensorboardVisualization:
def inception_resnet_v2(images, trainable=True, is_training=True, weight_decay=0.00004, stddev=0.1, dropout_keep_prob=0.8, use_batch_norm=True, batch_norm_params=None, add_summaries=True, scope="InceptionResnetV2"): """Builds an Inception_resnet_V2 subgraph for image embeddings. Args: images: A float32 Tensor of shape [batch, height, width, channels]. trainable: Whether the inception submodel should be trainable or not. is_training: Boolean indicating training mode or not. weight_decay: Coefficient for weight regularization. stddev: The standard deviation of the trunctated normal weight initializer. dropout_keep_prob: Dropout keep probability. use_batch_norm: Whether to use batch normalization. batch_norm_params: Parameters for batch normalization. See tf.contrib.layers.batch_norm for details. add_summaries: Whether to add activation summaries. scope: Optional Variable scope. Returns: end_points: A dictionary of activations from inception_Resnet__v2 layers. """ # Only consider the inception model to be in training mode if it's trainable. is_inception_model_training = trainable and is_training if use_batch_norm: # Default parameters for batch normalization. if not batch_norm_params: batch_norm_params = { "is_training": is_inception_model_training, "trainable": trainable, # Decay for the moving averages. "decay": 0.9997, # Epsilon to prevent 0s in variance. "epsilon": 0.001, # Collection containing the moving mean and moving variance. "variables_collections": { "beta": None, "gamma": None, "moving_mean": ["moving_vars"], "moving_variance": ["moving_vars"], } } else: batch_norm_params = None if trainable: weights_regularizer = tf.contrib.layers.l2_regularizer(weight_decay) else: weights_regularizer = None with tf.variable_scope(scope, "InceptionResnetV2", [images]) as scope: with slim.arg_scope( [slim.conv2d, slim.fully_connected], weights_regularizer=weights_regularizer, trainable=trainable): with slim.arg_scope( [slim.conv2d], weights_initializer=tf.truncated_normal_initializer(stddev=stddev), activation_fn=tf.nn.relu, normalizer_fn=slim.batch_norm, normalizer_params=batch_norm_params): net, end_points = inception_resnet_v2_base(images, scope=scope) # Jay-modification: # Keep the spatial information of 2D images for attention (batch_size, 8, 8, 1536) '''with tf.variable_scope("logits"): shape = net.get_shape() net = slim.avg_pool2d(net, shape[1:3], padding="VALID", scope="pool") net = slim.dropout( net, keep_prob=dropout_keep_prob, is_training=is_inception_model_training, scope="dropout") net = slim.flatten(net, scope="flatten")''' # Add summaries. if add_summaries: for v in end_points.values(): tf.contrib.layers.summaries.summarize_activation(v) return net