nets_factory.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # Copyright 2016 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 overfeat
  26. from nets import resnet_v1
  27. from nets import resnet_v2
  28. from nets import vgg
  29. slim = tf.contrib.slim
  30. networks_map = {'alexnet_v2': alexnet.alexnet_v2,
  31. 'cifarnet': cifarnet.cifarnet,
  32. 'overfeat': overfeat.overfeat,
  33. 'vgg_a': vgg.vgg_a,
  34. 'vgg_16': vgg.vgg_16,
  35. 'vgg_19': vgg.vgg_19,
  36. 'inception_v1': inception.inception_v1,
  37. 'inception_v2': inception.inception_v2,
  38. 'inception_v3': inception.inception_v3,
  39. 'inception_v4': inception.inception_v4,
  40. 'inception_resnet_v2': inception.inception_resnet_v2,
  41. 'lenet': lenet.lenet,
  42. 'resnet_v1_50': resnet_v1.resnet_v1_50,
  43. 'resnet_v1_101': resnet_v1.resnet_v1_101,
  44. 'resnet_v1_152': resnet_v1.resnet_v1_152,
  45. 'resnet_v1_200': resnet_v1.resnet_v1_200,
  46. 'resnet_v2_50': resnet_v2.resnet_v2_50,
  47. 'resnet_v2_101': resnet_v2.resnet_v2_101,
  48. 'resnet_v2_152': resnet_v2.resnet_v2_152,
  49. 'resnet_v2_200': resnet_v2.resnet_v2_200,
  50. }
  51. arg_scopes_map = {'alexnet_v2': alexnet.alexnet_v2_arg_scope,
  52. 'cifarnet': cifarnet.cifarnet_arg_scope,
  53. 'overfeat': overfeat.overfeat_arg_scope,
  54. 'vgg_a': vgg.vgg_arg_scope,
  55. 'vgg_16': vgg.vgg_arg_scope,
  56. 'vgg_19': vgg.vgg_arg_scope,
  57. 'inception_v1': inception.inception_v3_arg_scope,
  58. 'inception_v2': inception.inception_v3_arg_scope,
  59. 'inception_v3': inception.inception_v3_arg_scope,
  60. 'inception_v4': inception.inception_v4_arg_scope,
  61. 'inception_resnet_v2':
  62. inception.inception_resnet_v2_arg_scope,
  63. 'lenet': lenet.lenet_arg_scope,
  64. 'resnet_v1_50': resnet_v1.resnet_arg_scope,
  65. 'resnet_v1_101': resnet_v1.resnet_arg_scope,
  66. 'resnet_v1_152': resnet_v1.resnet_arg_scope,
  67. 'resnet_v1_200': resnet_v1.resnet_arg_scope,
  68. 'resnet_v2_50': resnet_v2.resnet_arg_scope,
  69. 'resnet_v2_101': resnet_v2.resnet_arg_scope,
  70. 'resnet_v2_152': resnet_v2.resnet_arg_scope,
  71. 'resnet_v2_200': resnet_v2.resnet_arg_scope,
  72. }
  73. def get_network_fn(name, num_classes, weight_decay=0.0, is_training=False):
  74. """Returns a network_fn such as `logits, end_points = network_fn(images)`.
  75. Args:
  76. name: The name of the network.
  77. num_classes: The number of classes to use for classification.
  78. weight_decay: The l2 coefficient for the model weights.
  79. is_training: `True` if the model is being used for training and `False`
  80. otherwise.
  81. Returns:
  82. network_fn: A function that applies the model to a batch of images. It has
  83. the following signature:
  84. logits, end_points = network_fn(images)
  85. Raises:
  86. ValueError: If network `name` is not recognized.
  87. """
  88. if name not in networks_map:
  89. raise ValueError('Name of network unknown %s' % name)
  90. arg_scope = arg_scopes_map[name](weight_decay=weight_decay)
  91. func = networks_map[name]
  92. @functools.wraps(func)
  93. def network_fn(images):
  94. with slim.arg_scope(arg_scope):
  95. return func(images, num_classes, is_training=is_training)
  96. if hasattr(func, 'default_image_size'):
  97. network_fn.default_image_size = func.default_image_size
  98. return network_fn