nets_factory.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # Copyright 2017 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Contains a factory for building various models."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import functools
  20. import tensorflow as tf
  21. from nets import alexnet
  22. from nets import cifarnet
  23. from nets import inception
  24. from nets import lenet
  25. from nets import mobilenet_v1
  26. from nets import overfeat
  27. from nets import resnet_v1
  28. from nets import resnet_v2
  29. from nets import vgg
  30. from nets.nasnet import nasnet
  31. slim = tf.contrib.slim
  32. networks_map = {'alexnet_v2': alexnet.alexnet_v2,
  33. 'cifarnet': cifarnet.cifarnet,
  34. 'overfeat': overfeat.overfeat,
  35. 'vgg_a': vgg.vgg_a,
  36. 'vgg_16': vgg.vgg_16,
  37. 'vgg_19': vgg.vgg_19,
  38. 'inception_v1': inception.inception_v1,
  39. 'inception_v2': inception.inception_v2,
  40. 'inception_v3': inception.inception_v3,
  41. 'inception_v4': inception.inception_v4,
  42. 'inception_resnet_v2': inception.inception_resnet_v2,
  43. 'lenet': lenet.lenet,
  44. 'resnet_v1_50': resnet_v1.resnet_v1_50,
  45. 'resnet_v1_101': resnet_v1.resnet_v1_101,
  46. 'resnet_v1_152': resnet_v1.resnet_v1_152,
  47. 'resnet_v1_200': resnet_v1.resnet_v1_200,
  48. 'resnet_v2_50': resnet_v2.resnet_v2_50,
  49. 'resnet_v2_101': resnet_v2.resnet_v2_101,
  50. 'resnet_v2_152': resnet_v2.resnet_v2_152,
  51. 'resnet_v2_200': resnet_v2.resnet_v2_200,
  52. 'mobilenet_v1': mobilenet_v1.mobilenet_v1,
  53. 'mobilenet_v1_075': mobilenet_v1.mobilenet_v1_075,
  54. 'mobilenet_v1_050': mobilenet_v1.mobilenet_v1_050,
  55. 'mobilenet_v1_025': mobilenet_v1.mobilenet_v1_025,
  56. 'nasnet_cifar': nasnet.build_nasnet_cifar,
  57. 'nasnet_mobile': nasnet.build_nasnet_mobile,
  58. 'nasnet_large': nasnet.build_nasnet_large,
  59. }
  60. arg_scopes_map = {'alexnet_v2': alexnet.alexnet_v2_arg_scope,
  61. 'cifarnet': cifarnet.cifarnet_arg_scope,
  62. 'overfeat': overfeat.overfeat_arg_scope,
  63. 'vgg_a': vgg.vgg_arg_scope,
  64. 'vgg_16': vgg.vgg_arg_scope,
  65. 'vgg_19': vgg.vgg_arg_scope,
  66. 'inception_v1': inception.inception_v3_arg_scope,
  67. 'inception_v2': inception.inception_v3_arg_scope,
  68. 'inception_v3': inception.inception_v3_arg_scope,
  69. 'inception_v4': inception.inception_v4_arg_scope,
  70. 'inception_resnet_v2':
  71. inception.inception_resnet_v2_arg_scope,
  72. 'lenet': lenet.lenet_arg_scope,
  73. 'resnet_v1_50': resnet_v1.resnet_arg_scope,
  74. 'resnet_v1_101': resnet_v1.resnet_arg_scope,
  75. 'resnet_v1_152': resnet_v1.resnet_arg_scope,
  76. 'resnet_v1_200': resnet_v1.resnet_arg_scope,
  77. 'resnet_v2_50': resnet_v2.resnet_arg_scope,
  78. 'resnet_v2_101': resnet_v2.resnet_arg_scope,
  79. 'resnet_v2_152': resnet_v2.resnet_arg_scope,
  80. 'resnet_v2_200': resnet_v2.resnet_arg_scope,
  81. 'mobilenet_v1': mobilenet_v1.mobilenet_v1_arg_scope,
  82. 'mobilenet_v1_075': mobilenet_v1.mobilenet_v1_arg_scope,
  83. 'mobilenet_v1_050': mobilenet_v1.mobilenet_v1_arg_scope,
  84. 'mobilenet_v1_025': mobilenet_v1.mobilenet_v1_arg_scope,
  85. 'nasnet_cifar': nasnet.nasnet_cifar_arg_scope,
  86. 'nasnet_mobile': nasnet.nasnet_mobile_arg_scope,
  87. 'nasnet_large': nasnet.nasnet_large_arg_scope,
  88. }
  89. def get_network_fn(name, num_classes, weight_decay=0.0, is_training=False):
  90. """Returns a network_fn such as `logits, end_points = network_fn(images)`.
  91. Args:
  92. name: The name of the network.
  93. num_classes: The number of classes to use for classification. If 0 or None,
  94. the logits layer is omitted and its input features are returned instead.
  95. weight_decay: The l2 coefficient for the model weights.
  96. is_training: `True` if the model is being used for training and `False`
  97. otherwise.
  98. Returns:
  99. network_fn: A function that applies the model to a batch of images. It has
  100. the following signature:
  101. net, end_points = network_fn(images)
  102. The `images` input is a tensor of shape [batch_size, height, width, 3]
  103. with height = width = network_fn.default_image_size. (The permissibility
  104. and treatment of other sizes depends on the network_fn.)
  105. The returned `end_points` are a dictionary of intermediate activations.
  106. The returned `net` is the topmost layer, depending on `num_classes`:
  107. If `num_classes` was a non-zero integer, `net` is a logits tensor
  108. of shape [batch_size, num_classes].
  109. If `num_classes` was 0 or `None`, `net` is a tensor with the input
  110. to the logits layer of shape [batch_size, 1, 1, num_features] or
  111. [batch_size, num_features]. Dropout has not been applied to this
  112. (even if the network's original classification does); it remains for
  113. the caller to do this or not.
  114. Raises:
  115. ValueError: If network `name` is not recognized.
  116. """
  117. if name not in networks_map:
  118. raise ValueError('Name of network unknown %s' % name)
  119. func = networks_map[name]
  120. @functools.wraps(func)
  121. def network_fn(images, **kwargs):
  122. arg_scope = arg_scopes_map[name](weight_decay=weight_decay)
  123. with slim.arg_scope(arg_scope):
  124. return func(images, num_classes, is_training=is_training, **kwargs)
  125. if hasattr(func, 'default_image_size'):
  126. network_fn.default_image_size = func.default_image_size
  127. return network_fn