train_test.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. import unittest
  23. import tempfile
  24. import numpy as np
  25. import cv2
  26. import os
  27. import shutil
  28. import download_and_extract # @UnresolvedImport
  29. import subprocess
  30. def memory_usage_psutil():
  31. # return the memory usage in MB
  32. import psutil
  33. process = psutil.Process(os.getpid())
  34. mem = process.memory_info()[0] / float(2 ** 20)
  35. return mem
  36. def align_dataset_if_needed(self):
  37. if not os.path.exists('data/lfw_aligned'):
  38. argv = ['python',
  39. 'src/align/align_dataset_mtcnn.py',
  40. 'data/lfw',
  41. 'data/lfw_aligned',
  42. '--image_size', '160',
  43. '--margin', '32' ]
  44. subprocess.call(argv)
  45. class TrainTest(unittest.TestCase):
  46. @classmethod
  47. def setUpClass(self):
  48. self.tmp_dir = tempfile.mkdtemp()
  49. self.dataset_dir = os.path.join(self.tmp_dir, 'dataset')
  50. create_mock_dataset(self.dataset_dir, 160)
  51. self.lfw_pairs_file = create_mock_lfw_pairs(self.tmp_dir)
  52. print(self.lfw_pairs_file)
  53. self.pretrained_model_name = '20180402-114759'
  54. download_and_extract.download_and_extract_file(self.pretrained_model_name, 'data/')
  55. download_and_extract.download_and_extract_file('lfw-subset', 'data/')
  56. self.model_file = os.path.join('data', self.pretrained_model_name, 'model-%s.ckpt-275' % self.pretrained_model_name)
  57. self.pretrained_model = os.path.join('data', self.pretrained_model_name)
  58. self.frozen_graph_filename = os.path.join('data', self.pretrained_model_name+'.pb')
  59. print('Memory utilization (SetUpClass): %.3f MB' % memory_usage_psutil())
  60. @classmethod
  61. def tearDownClass(self):
  62. # Recursively remove the temporary directory
  63. shutil.rmtree(self.tmp_dir)
  64. def tearDown(self):
  65. print('Memory utilization (TearDown): %.3f MB' % memory_usage_psutil())
  66. def test_training_classifier_inception_resnet_v1(self):
  67. print('test_training_classifier_inception_resnet_v1')
  68. argv = ['python',
  69. 'src/train_softmax.py',
  70. '--logs_base_dir', self.tmp_dir,
  71. '--models_base_dir', self.tmp_dir,
  72. '--data_dir', self.dataset_dir,
  73. '--model_def', 'models.inception_resnet_v1',
  74. '--epoch_size', '1',
  75. '--max_nrof_epochs', '1',
  76. '--batch_size', '1',
  77. '--lfw_pairs', self.lfw_pairs_file,
  78. '--lfw_dir', self.dataset_dir,
  79. '--lfw_nrof_folds', '2',
  80. '--lfw_batch_size', '1',
  81. '--nrof_preprocess_threads', '1' ]
  82. subprocess.call(argv)
  83. def test_training_classifier_inception_resnet_v2(self):
  84. print('test_training_classifier_inception_resnet_v2')
  85. argv = ['python',
  86. 'src/train_softmax.py',
  87. '--logs_base_dir', self.tmp_dir,
  88. '--models_base_dir', self.tmp_dir,
  89. '--data_dir', self.dataset_dir,
  90. '--model_def', 'models.inception_resnet_v2',
  91. '--epoch_size', '1',
  92. '--max_nrof_epochs', '1',
  93. '--batch_size', '1',
  94. '--lfw_pairs', self.lfw_pairs_file,
  95. '--lfw_dir', self.dataset_dir,
  96. '--lfw_nrof_folds', '2',
  97. '--lfw_batch_size', '1' ]
  98. subprocess.call(argv)
  99. def test_training_classifier_squeezenet(self):
  100. print('test_training_classifier_squeezenet')
  101. argv = ['python',
  102. 'src/train_softmax.py',
  103. '--logs_base_dir', self.tmp_dir,
  104. '--models_base_dir', self.tmp_dir,
  105. '--data_dir', self.dataset_dir,
  106. '--model_def', 'models.squeezenet',
  107. '--epoch_size', '1',
  108. '--max_nrof_epochs', '1',
  109. '--batch_size', '1',
  110. '--lfw_pairs', self.lfw_pairs_file,
  111. '--lfw_dir', self.dataset_dir,
  112. '--lfw_nrof_folds', '2',
  113. '--lfw_batch_size', '1',
  114. '--nrof_preprocess_threads', '1' ]
  115. subprocess.call(argv)
  116. def test_train_tripletloss_inception_resnet_v1(self):
  117. print('test_train_tripletloss_inception_resnet_v1')
  118. argv = ['python',
  119. 'src/train_tripletloss.py',
  120. '--logs_base_dir', self.tmp_dir,
  121. '--models_base_dir', self.tmp_dir,
  122. '--data_dir', self.dataset_dir,
  123. '--model_def', 'models.inception_resnet_v1',
  124. '--epoch_size', '1',
  125. '--max_nrof_epochs', '1',
  126. '--batch_size', '6',
  127. '--people_per_batch', '2',
  128. '--images_per_person', '3',
  129. '--lfw_pairs', self.lfw_pairs_file,
  130. '--lfw_dir', self.dataset_dir,
  131. '--lfw_nrof_folds', '2' ]
  132. subprocess.call(argv)
  133. def test_finetune_tripletloss_inception_resnet_v1(self):
  134. print('test_finetune_tripletloss_inception_resnet_v1')
  135. argv = ['python',
  136. 'src/train_tripletloss.py',
  137. '--logs_base_dir', self.tmp_dir,
  138. '--models_base_dir', self.tmp_dir,
  139. '--data_dir', self.dataset_dir,
  140. '--model_def', 'models.inception_resnet_v1',
  141. '--pretrained_model', self.model_file,
  142. '--embedding_size', '512',
  143. '--epoch_size', '1',
  144. '--max_nrof_epochs', '1',
  145. '--batch_size', '6',
  146. '--people_per_batch', '2',
  147. '--images_per_person', '3',
  148. '--lfw_pairs', self.lfw_pairs_file,
  149. '--lfw_dir', self.dataset_dir,
  150. '--lfw_nrof_folds', '2' ]
  151. subprocess.call(argv)
  152. def test_compare(self):
  153. print('test_compare')
  154. argv = ['python',
  155. 'src/compare.py',
  156. os.path.join('data/', self.pretrained_model_name),
  157. 'data/images/Anthony_Hopkins_0001.jpg',
  158. 'data/images/Anthony_Hopkins_0002.jpg' ]
  159. subprocess.call(argv)
  160. def test_validate_on_lfw(self):
  161. print('test_validate_on_lfw')
  162. align_dataset_if_needed(self)
  163. argv = ['python',
  164. 'src/validate_on_lfw.py',
  165. 'data/lfw_aligned',
  166. self.pretrained_model,
  167. '--lfw_pairs', 'data/lfw/pairs_small.txt',
  168. '--lfw_nrof_folds', '2',
  169. '--lfw_batch_size', '6']
  170. subprocess.call(argv)
  171. def test_validate_on_lfw_frozen_graph(self):
  172. print('test_validate_on_lfw_frozen_graph')
  173. self.pretrained_model = os.path.join('data', self.pretrained_model_name)
  174. frozen_model = os.path.join(self.pretrained_model, self.pretrained_model_name+'.pb')
  175. argv = ['python',
  176. 'src/validate_on_lfw.py',
  177. self.dataset_dir,
  178. frozen_model,
  179. '--lfw_pairs', self.lfw_pairs_file,
  180. '--lfw_nrof_folds', '2',
  181. '--lfw_batch_size', '6']
  182. subprocess.call(argv)
  183. def test_freeze_graph(self):
  184. print('test_freeze_graph')
  185. argv = ['python',
  186. 'src/freeze_graph.py',
  187. self.pretrained_model,
  188. self.frozen_graph_filename ]
  189. subprocess.call(argv)
  190. # Create a mock dataset with random pixel images
  191. def create_mock_dataset(dataset_dir, image_size):
  192. nrof_persons = 3
  193. nrof_images_per_person = 2
  194. np.random.seed(seed=666)
  195. os.mkdir(dataset_dir)
  196. for i in range(nrof_persons):
  197. class_name = '%04d' % (i+1)
  198. class_dir = os.path.join(dataset_dir, class_name)
  199. os.mkdir(class_dir)
  200. for j in range(nrof_images_per_person):
  201. img_name = '%04d' % (j+1)
  202. img_path = os.path.join(class_dir, class_name+'_'+img_name + '.png')
  203. img = np.random.uniform(low=0.0, high=255.0, size=(image_size,image_size,3))
  204. cv2.imwrite(img_path, img) #@UndefinedVariable
  205. # Create a mock LFW pairs file
  206. def create_mock_lfw_pairs(tmp_dir):
  207. pairs_filename = os.path.join(tmp_dir, 'pairs_mock.txt')
  208. with open(pairs_filename, 'w') as f:
  209. f.write('10 300\n')
  210. f.write('0001 1 2\n')
  211. f.write('0001 1 0002 1\n')
  212. f.write('0002 1 0003 1\n')
  213. f.write('0001 1 0003 1\n')
  214. f.write('0002 1 2\n')
  215. f.write('0001 2 0002 2\n')
  216. f.write('0002 2 0003 2\n')
  217. f.write('0001 2 0003 2\n')
  218. f.write('0003 1 2\n')
  219. f.write('0001 1 0002 2\n')
  220. f.write('0002 1 0003 2\n')
  221. f.write('0001 1 0003 2\n')
  222. return pairs_filename
  223. if __name__ == "__main__":
  224. unittest.main()