nn4.py 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # MIT License
  2. #
  3. # Copyright (c) 2016 David Sandberg
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in all
  13. # copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. # pylint: disable=missing-docstring
  23. from __future__ import absolute_import
  24. from __future__ import division
  25. from __future__ import print_function
  26. import tensorflow as tf
  27. import models.network as network
  28. def inference(images, keep_probability, phase_train=True, weight_decay=0.0):
  29. """ Define an inference network for face recognition based
  30. on inception modules using batch normalization
  31. Args:
  32. images: The images to run inference on, dimensions batch_size x height x width x channels
  33. phase_train: True if batch normalization should operate in training mode
  34. """
  35. endpoints = {}
  36. net = network.conv(images, 3, 64, 7, 7, 2, 2, 'SAME', 'conv1_7x7', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
  37. endpoints['conv1'] = net
  38. net = network.mpool(net, 3, 3, 2, 2, 'SAME', 'pool1')
  39. endpoints['pool1'] = net
  40. net = network.conv(net, 64, 64, 1, 1, 1, 1, 'SAME', 'conv2_1x1', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
  41. endpoints['conv2_1x1'] = net
  42. net = network.conv(net, 64, 192, 3, 3, 1, 1, 'SAME', 'conv3_3x3', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
  43. endpoints['conv3_3x3'] = net
  44. net = network.mpool(net, 3, 3, 2, 2, 'SAME', 'pool3')
  45. endpoints['pool3'] = net
  46. net = network.inception(net, 192, 1, 64, 96, 128, 16, 32, 3, 32, 1, 'MAX', 'incept3a', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
  47. endpoints['incept3a'] = net
  48. net = network.inception(net, 256, 1, 64, 96, 128, 32, 64, 3, 64, 1, 'MAX', 'incept3b', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
  49. endpoints['incept3b'] = net
  50. net = network.inception(net, 320, 2, 0, 128, 256, 32, 64, 3, 0, 2, 'MAX', 'incept3c', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
  51. endpoints['incept3c'] = net
  52. net = network.inception(net, 640, 1, 256, 96, 192, 32, 64, 3, 128, 1, 'MAX', 'incept4a', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
  53. endpoints['incept4a'] = net
  54. net = network.inception(net, 640, 1, 224, 112, 224, 32, 64, 3, 128, 1, 'MAX', 'incept4b', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
  55. endpoints['incept4b'] = net
  56. net = network.inception(net, 640, 1, 192, 128, 256, 32, 64, 3, 128, 1, 'MAX', 'incept4c', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
  57. endpoints['incept4c'] = net
  58. net = network.inception(net, 640, 1, 160, 144, 288, 32, 64, 3, 128, 1, 'MAX', 'incept4d', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
  59. endpoints['incept4d'] = net
  60. net = network.inception(net, 640, 2, 0, 160, 256, 64, 128, 3, 0, 2, 'MAX', 'incept4e', phase_train=phase_train, use_batch_norm=True)
  61. endpoints['incept4e'] = net
  62. net = network.inception(net, 1024, 1, 384, 192, 384, 0, 0, 3, 128, 1, 'MAX', 'incept5a', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
  63. endpoints['incept5a'] = net
  64. net = network.inception(net, 896, 1, 384, 192, 384, 0, 0, 3, 128, 1, 'MAX', 'incept5b', phase_train=phase_train, use_batch_norm=True, weight_decay=weight_decay)
  65. endpoints['incept5b'] = net
  66. net = network.apool(net, 3, 3, 1, 1, 'VALID', 'pool6')
  67. endpoints['pool6'] = net
  68. net = tf.reshape(net, [-1, 896])
  69. endpoints['prelogits'] = net
  70. net = tf.nn.dropout(net, keep_probability)
  71. endpoints['dropout'] = net
  72. return net, endpoints