def main(unused_argv): # Model definition. g = ops.Graph() with g.as_default(): images = array_ops.placeholder( dtypes.float32, shape=(1, None, None, 3), name='input_image') inception.inception_resnet_v2_base(images) graph_io.write_graph(g.as_graph_def(), cmd_args.graph_dir, cmd_args.graph_filename)
def testBuildAndCheckAllEndPointsUptoPreAuxLogitsWithOutputStrideEight( self): batch_size = 5 height, width = 299, 299 inputs = tf.random_uniform((batch_size, height, width, 3)) _, end_points = inception.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.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 testBuildAndCheckAllEndPointsUptoPreAuxLogitsWithOutputStrideEight(self): batch_size = 5 height, width = 299, 299 inputs = tf.random_uniform((batch_size, height, width, 3)) _, end_points = inception.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.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 __call__(self, image_input, training=False, keep_prob=1.0): """ Runs the CNN producing the embeddings and the gradients. :param image_input: Image input to produce embeddings for. [batch_size, image_size, image_size, 1] :param training: A flag indicating training or evaluation :param keep_prob: A tf placeholder of type tf.float32 indicating the amount of dropout applied :return: Embeddings of size [batch_size, 2048] """ weight_decay = FLAGS.weight_decay activation_fn = tf.nn.relu end_points = {} with slim.arg_scope( inception.inception_resnet_v2_arg_scope( weight_decay=FLAGS.weight_decay)): with tf.variable_scope("", reuse=self.reuse): with tf.variable_scope(None, 'InceptionResnetV2', [image_input]) as scope: with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=training): net, end_points = inception.inception_resnet_v2_base( image_input, scope=scope, activation_fn=activation_fn) feature_map = end_points['PreAuxLogits'] self.reuse = True return feature_map
def testBuildBaseNetwork(self): batch_size = 5 height, width = 299, 299 inputs = tf.random_uniform((batch_size, height, width, 3)) net, end_points = inception.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.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())
def _construct_model(model_type='resnet_v1_50'): """Constructs model for the desired type of CNN. Args: model_type: Type of model to be used. Returns: end_points: A dictionary from components of the network to the corresponding activations. Raises: ValueError: If the model_type is not supported. """ # Placeholder input. images = array_ops.placeholder( dtypes.float32, shape=(1, None, None, 3), name=_INPUT_NODE) # Construct model. if model_type == 'inception_resnet_v2': _, end_points = inception.inception_resnet_v2_base(images) elif model_type == 'inception_resnet_v2-same': _, end_points = inception.inception_resnet_v2_base( images, align_feature_maps=True) elif model_type == 'inception_v2': _, end_points = inception.inception_v2_base(images) elif model_type == 'inception_v2-no-separable-conv': _, end_points = inception.inception_v2_base( images, use_separable_conv=False) elif model_type == 'inception_v3': _, end_points = inception.inception_v3_base(images) elif model_type == 'inception_v4': _, end_points = inception.inception_v4_base(images) elif model_type == 'alexnet_v2': _, end_points = alexnet.alexnet_v2(images) elif model_type == 'vgg_a': _, end_points = vgg.vgg_a(images) elif model_type == 'vgg_16': _, end_points = vgg.vgg_16(images) elif model_type == 'mobilenet_v1': _, end_points = mobilenet_v1.mobilenet_v1_base(images) elif model_type == 'mobilenet_v1_075': _, end_points = mobilenet_v1.mobilenet_v1_base( images, depth_multiplier=0.75) elif model_type == 'resnet_v1_50': _, end_points = resnet_v1.resnet_v1_50( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v1_101': _, end_points = resnet_v1.resnet_v1_101( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v1_152': _, end_points = resnet_v1.resnet_v1_152( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v1_200': _, end_points = resnet_v1.resnet_v1_200( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_50': _, end_points = resnet_v2.resnet_v2_50( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_101': _, end_points = resnet_v2.resnet_v2_101( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_152': _, end_points = resnet_v2.resnet_v2_152( images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_200': _, end_points = resnet_v2.resnet_v2_200( images, num_classes=None, is_training=False, global_pool=False) else: raise ValueError('Unsupported model_type %s.' % model_type) return end_points
def _construct_model(model_type='resnet_v1_50'): """Constructs model for the desired type of CNN. Args: model_type: Type of model to be used. Returns: end_points: A dictionary from components of the network to the corresponding activations. Raises: ValueError: If the model_type is not supported. """ # Placeholder input. images = array_ops.placeholder(dtypes.float32, shape=(1, None, None, 3), name=_INPUT_NODE) # Construct model. if model_type == 'inception_resnet_v2': _, end_points = inception.inception_resnet_v2_base(images) elif model_type == 'inception_resnet_v2-same': _, end_points = inception.inception_resnet_v2_base( images, align_feature_maps=True) elif model_type == 'inception_v2': _, end_points = inception.inception_v2_base(images) elif model_type == 'inception_v2-no-separable-conv': _, end_points = inception.inception_v2_base(images, use_separable_conv=False) elif model_type == 'inception_v3': _, end_points = inception.inception_v3_base(images) elif model_type == 'inception_v4': _, end_points = inception.inception_v4_base(images) elif model_type == 'alexnet_v2': _, end_points = alexnet.alexnet_v2(images) elif model_type == 'vgg_a': _, end_points = vgg.vgg_a(images) elif model_type == 'vgg_16': _, end_points = vgg.vgg_16(images) elif model_type == 'mobilenet_v1': _, end_points = mobilenet_v1.mobilenet_v1_base(images) elif model_type == 'mobilenet_v1_075': _, end_points = mobilenet_v1.mobilenet_v1_base(images, depth_multiplier=0.75) elif model_type == 'resnet_v1_50': _, end_points = resnet_v1.resnet_v1_50(images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v1_101': _, end_points = resnet_v1.resnet_v1_101(images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v1_152': _, end_points = resnet_v1.resnet_v1_152(images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v1_200': _, end_points = resnet_v1.resnet_v1_200(images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_50': _, end_points = resnet_v2.resnet_v2_50(images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_101': _, end_points = resnet_v2.resnet_v2_101(images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_152': _, end_points = resnet_v2.resnet_v2_152(images, num_classes=None, is_training=False, global_pool=False) elif model_type == 'resnet_v2_200': _, end_points = resnet_v2.resnet_v2_200(images, num_classes=None, is_training=False, global_pool=False) else: raise ValueError('Unsupported model_type %s.' % model_type) return end_points
else: with slim.arg_scope(arg_scope): slim_args = [slim.batch_norm, slim.dropout] with slim.arg_scope(slim_args, is_training=False): with tf.variable_scope(base_network, reuse=None) as scope: if base_network == 'InceptionV3': net, _ = inception.inception_v3_base( images, final_endpoint=endpoint, scope=scope) elif base_network == 'InceptionV3SE': net, _ = inception.inception_v3_se_base( images, final_endpoint=endpoint, scope=scope) elif base_network == 'InceptionV4': net, _ = inception.inception_v4_base( images, final_endpoint=endpoint, scope=scope) elif base_network == 'InceptionResnetV2': net, _ = inception.inception_resnet_v2_base( images, final_endpoint=endpoint, scope=scope) elif base_network == 'InceptionResnetV2SE': net, _ = inception.inception_resnet_v2_se_base( images, final_endpoint=endpoint, scope=scope) net = tf.reduce_mean(net, [0,1,2]) variable_averages = tf.train.ExponentialMovingAverage( moving_average_decay, tf_global_step) variables_to_restore = variable_averages.variables_to_restore() init_fn = slim.assign_from_checkpoint_fn( checkpoints_path, variables_to_restore) config_sess = tf.ConfigProto(allow_soft_placement=True) config_sess.gpu_options.allow_growth = True with tf.Session(config=config_sess) as sess: init_fn(sess)