mobilenet_v1_test.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. """Tests for MobileNet v1."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import numpy as np
  20. import tensorflow as tf
  21. from nets import mobilenet_v1
  22. slim = tf.contrib.slim
  23. class MobilenetV1Test(tf.test.TestCase):
  24. def testBuildClassificationNetwork(self):
  25. batch_size = 5
  26. height, width = 224, 224
  27. num_classes = 1000
  28. inputs = tf.random_uniform((batch_size, height, width, 3))
  29. logits, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)
  30. self.assertTrue(logits.op.name.startswith(
  31. 'MobilenetV1/Logits/SpatialSqueeze'))
  32. self.assertListEqual(logits.get_shape().as_list(),
  33. [batch_size, num_classes])
  34. self.assertTrue('Predictions' in end_points)
  35. self.assertListEqual(end_points['Predictions'].get_shape().as_list(),
  36. [batch_size, num_classes])
  37. def testBuildPreLogitsNetwork(self):
  38. batch_size = 5
  39. height, width = 224, 224
  40. num_classes = None
  41. inputs = tf.random_uniform((batch_size, height, width, 3))
  42. net, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)
  43. self.assertTrue(net.op.name.startswith('MobilenetV1/Logits/AvgPool'))
  44. self.assertListEqual(net.get_shape().as_list(), [batch_size, 1, 1, 1024])
  45. self.assertFalse('Logits' in end_points)
  46. self.assertFalse('Predictions' in end_points)
  47. def testBuildBaseNetwork(self):
  48. batch_size = 5
  49. height, width = 224, 224
  50. inputs = tf.random_uniform((batch_size, height, width, 3))
  51. net, end_points = mobilenet_v1.mobilenet_v1_base(inputs)
  52. self.assertTrue(net.op.name.startswith('MobilenetV1/Conv2d_13'))
  53. self.assertListEqual(net.get_shape().as_list(),
  54. [batch_size, 7, 7, 1024])
  55. expected_endpoints = ['Conv2d_0',
  56. 'Conv2d_1_depthwise', 'Conv2d_1_pointwise',
  57. 'Conv2d_2_depthwise', 'Conv2d_2_pointwise',
  58. 'Conv2d_3_depthwise', 'Conv2d_3_pointwise',
  59. 'Conv2d_4_depthwise', 'Conv2d_4_pointwise',
  60. 'Conv2d_5_depthwise', 'Conv2d_5_pointwise',
  61. 'Conv2d_6_depthwise', 'Conv2d_6_pointwise',
  62. 'Conv2d_7_depthwise', 'Conv2d_7_pointwise',
  63. 'Conv2d_8_depthwise', 'Conv2d_8_pointwise',
  64. 'Conv2d_9_depthwise', 'Conv2d_9_pointwise',
  65. 'Conv2d_10_depthwise', 'Conv2d_10_pointwise',
  66. 'Conv2d_11_depthwise', 'Conv2d_11_pointwise',
  67. 'Conv2d_12_depthwise', 'Conv2d_12_pointwise',
  68. 'Conv2d_13_depthwise', 'Conv2d_13_pointwise']
  69. self.assertItemsEqual(end_points.keys(), expected_endpoints)
  70. def testBuildOnlyUptoFinalEndpoint(self):
  71. batch_size = 5
  72. height, width = 224, 224
  73. endpoints = ['Conv2d_0',
  74. 'Conv2d_1_depthwise', 'Conv2d_1_pointwise',
  75. 'Conv2d_2_depthwise', 'Conv2d_2_pointwise',
  76. 'Conv2d_3_depthwise', 'Conv2d_3_pointwise',
  77. 'Conv2d_4_depthwise', 'Conv2d_4_pointwise',
  78. 'Conv2d_5_depthwise', 'Conv2d_5_pointwise',
  79. 'Conv2d_6_depthwise', 'Conv2d_6_pointwise',
  80. 'Conv2d_7_depthwise', 'Conv2d_7_pointwise',
  81. 'Conv2d_8_depthwise', 'Conv2d_8_pointwise',
  82. 'Conv2d_9_depthwise', 'Conv2d_9_pointwise',
  83. 'Conv2d_10_depthwise', 'Conv2d_10_pointwise',
  84. 'Conv2d_11_depthwise', 'Conv2d_11_pointwise',
  85. 'Conv2d_12_depthwise', 'Conv2d_12_pointwise',
  86. 'Conv2d_13_depthwise', 'Conv2d_13_pointwise']
  87. for index, endpoint in enumerate(endpoints):
  88. with tf.Graph().as_default():
  89. inputs = tf.random_uniform((batch_size, height, width, 3))
  90. out_tensor, end_points = mobilenet_v1.mobilenet_v1_base(
  91. inputs, final_endpoint=endpoint)
  92. self.assertTrue(out_tensor.op.name.startswith(
  93. 'MobilenetV1/' + endpoint))
  94. self.assertItemsEqual(endpoints[:index+1], end_points)
  95. def testBuildCustomNetworkUsingConvDefs(self):
  96. batch_size = 5
  97. height, width = 224, 224
  98. conv_defs = [
  99. mobilenet_v1.Conv(kernel=[3, 3], stride=2, depth=32),
  100. mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=1, depth=64),
  101. mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=2, depth=128),
  102. mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=1, depth=512)
  103. ]
  104. inputs = tf.random_uniform((batch_size, height, width, 3))
  105. net, end_points = mobilenet_v1.mobilenet_v1_base(
  106. inputs, final_endpoint='Conv2d_3_pointwise', conv_defs=conv_defs)
  107. self.assertTrue(net.op.name.startswith('MobilenetV1/Conv2d_3'))
  108. self.assertListEqual(net.get_shape().as_list(),
  109. [batch_size, 56, 56, 512])
  110. expected_endpoints = ['Conv2d_0',
  111. 'Conv2d_1_depthwise', 'Conv2d_1_pointwise',
  112. 'Conv2d_2_depthwise', 'Conv2d_2_pointwise',
  113. 'Conv2d_3_depthwise', 'Conv2d_3_pointwise']
  114. self.assertItemsEqual(end_points.keys(), expected_endpoints)
  115. def testBuildAndCheckAllEndPointsUptoConv2d_13(self):
  116. batch_size = 5
  117. height, width = 224, 224
  118. inputs = tf.random_uniform((batch_size, height, width, 3))
  119. with slim.arg_scope([slim.conv2d, slim.separable_conv2d],
  120. normalizer_fn=slim.batch_norm):
  121. _, end_points = mobilenet_v1.mobilenet_v1_base(
  122. inputs, final_endpoint='Conv2d_13_pointwise')
  123. endpoints_shapes = {'Conv2d_0': [batch_size, 112, 112, 32],
  124. 'Conv2d_1_depthwise': [batch_size, 112, 112, 32],
  125. 'Conv2d_1_pointwise': [batch_size, 112, 112, 64],
  126. 'Conv2d_2_depthwise': [batch_size, 56, 56, 64],
  127. 'Conv2d_2_pointwise': [batch_size, 56, 56, 128],
  128. 'Conv2d_3_depthwise': [batch_size, 56, 56, 128],
  129. 'Conv2d_3_pointwise': [batch_size, 56, 56, 128],
  130. 'Conv2d_4_depthwise': [batch_size, 28, 28, 128],
  131. 'Conv2d_4_pointwise': [batch_size, 28, 28, 256],
  132. 'Conv2d_5_depthwise': [batch_size, 28, 28, 256],
  133. 'Conv2d_5_pointwise': [batch_size, 28, 28, 256],
  134. 'Conv2d_6_depthwise': [batch_size, 14, 14, 256],
  135. 'Conv2d_6_pointwise': [batch_size, 14, 14, 512],
  136. 'Conv2d_7_depthwise': [batch_size, 14, 14, 512],
  137. 'Conv2d_7_pointwise': [batch_size, 14, 14, 512],
  138. 'Conv2d_8_depthwise': [batch_size, 14, 14, 512],
  139. 'Conv2d_8_pointwise': [batch_size, 14, 14, 512],
  140. 'Conv2d_9_depthwise': [batch_size, 14, 14, 512],
  141. 'Conv2d_9_pointwise': [batch_size, 14, 14, 512],
  142. 'Conv2d_10_depthwise': [batch_size, 14, 14, 512],
  143. 'Conv2d_10_pointwise': [batch_size, 14, 14, 512],
  144. 'Conv2d_11_depthwise': [batch_size, 14, 14, 512],
  145. 'Conv2d_11_pointwise': [batch_size, 14, 14, 512],
  146. 'Conv2d_12_depthwise': [batch_size, 7, 7, 512],
  147. 'Conv2d_12_pointwise': [batch_size, 7, 7, 1024],
  148. 'Conv2d_13_depthwise': [batch_size, 7, 7, 1024],
  149. 'Conv2d_13_pointwise': [batch_size, 7, 7, 1024]}
  150. self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
  151. for endpoint_name, expected_shape in endpoints_shapes.items():
  152. self.assertTrue(endpoint_name in end_points)
  153. self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
  154. expected_shape)
  155. def testOutputStride16BuildAndCheckAllEndPointsUptoConv2d_13(self):
  156. batch_size = 5
  157. height, width = 224, 224
  158. output_stride = 16
  159. inputs = tf.random_uniform((batch_size, height, width, 3))
  160. with slim.arg_scope([slim.conv2d, slim.separable_conv2d],
  161. normalizer_fn=slim.batch_norm):
  162. _, end_points = mobilenet_v1.mobilenet_v1_base(
  163. inputs, output_stride=output_stride,
  164. final_endpoint='Conv2d_13_pointwise')
  165. endpoints_shapes = {'Conv2d_0': [batch_size, 112, 112, 32],
  166. 'Conv2d_1_depthwise': [batch_size, 112, 112, 32],
  167. 'Conv2d_1_pointwise': [batch_size, 112, 112, 64],
  168. 'Conv2d_2_depthwise': [batch_size, 56, 56, 64],
  169. 'Conv2d_2_pointwise': [batch_size, 56, 56, 128],
  170. 'Conv2d_3_depthwise': [batch_size, 56, 56, 128],
  171. 'Conv2d_3_pointwise': [batch_size, 56, 56, 128],
  172. 'Conv2d_4_depthwise': [batch_size, 28, 28, 128],
  173. 'Conv2d_4_pointwise': [batch_size, 28, 28, 256],
  174. 'Conv2d_5_depthwise': [batch_size, 28, 28, 256],
  175. 'Conv2d_5_pointwise': [batch_size, 28, 28, 256],
  176. 'Conv2d_6_depthwise': [batch_size, 14, 14, 256],
  177. 'Conv2d_6_pointwise': [batch_size, 14, 14, 512],
  178. 'Conv2d_7_depthwise': [batch_size, 14, 14, 512],
  179. 'Conv2d_7_pointwise': [batch_size, 14, 14, 512],
  180. 'Conv2d_8_depthwise': [batch_size, 14, 14, 512],
  181. 'Conv2d_8_pointwise': [batch_size, 14, 14, 512],
  182. 'Conv2d_9_depthwise': [batch_size, 14, 14, 512],
  183. 'Conv2d_9_pointwise': [batch_size, 14, 14, 512],
  184. 'Conv2d_10_depthwise': [batch_size, 14, 14, 512],
  185. 'Conv2d_10_pointwise': [batch_size, 14, 14, 512],
  186. 'Conv2d_11_depthwise': [batch_size, 14, 14, 512],
  187. 'Conv2d_11_pointwise': [batch_size, 14, 14, 512],
  188. 'Conv2d_12_depthwise': [batch_size, 14, 14, 512],
  189. 'Conv2d_12_pointwise': [batch_size, 14, 14, 1024],
  190. 'Conv2d_13_depthwise': [batch_size, 14, 14, 1024],
  191. 'Conv2d_13_pointwise': [batch_size, 14, 14, 1024]}
  192. self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
  193. for endpoint_name, expected_shape in endpoints_shapes.items():
  194. self.assertTrue(endpoint_name in end_points)
  195. self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
  196. expected_shape)
  197. def testOutputStride8BuildAndCheckAllEndPointsUptoConv2d_13(self):
  198. batch_size = 5
  199. height, width = 224, 224
  200. output_stride = 8
  201. inputs = tf.random_uniform((batch_size, height, width, 3))
  202. with slim.arg_scope([slim.conv2d, slim.separable_conv2d],
  203. normalizer_fn=slim.batch_norm):
  204. _, end_points = mobilenet_v1.mobilenet_v1_base(
  205. inputs, output_stride=output_stride,
  206. final_endpoint='Conv2d_13_pointwise')
  207. endpoints_shapes = {'Conv2d_0': [batch_size, 112, 112, 32],
  208. 'Conv2d_1_depthwise': [batch_size, 112, 112, 32],
  209. 'Conv2d_1_pointwise': [batch_size, 112, 112, 64],
  210. 'Conv2d_2_depthwise': [batch_size, 56, 56, 64],
  211. 'Conv2d_2_pointwise': [batch_size, 56, 56, 128],
  212. 'Conv2d_3_depthwise': [batch_size, 56, 56, 128],
  213. 'Conv2d_3_pointwise': [batch_size, 56, 56, 128],
  214. 'Conv2d_4_depthwise': [batch_size, 28, 28, 128],
  215. 'Conv2d_4_pointwise': [batch_size, 28, 28, 256],
  216. 'Conv2d_5_depthwise': [batch_size, 28, 28, 256],
  217. 'Conv2d_5_pointwise': [batch_size, 28, 28, 256],
  218. 'Conv2d_6_depthwise': [batch_size, 28, 28, 256],
  219. 'Conv2d_6_pointwise': [batch_size, 28, 28, 512],
  220. 'Conv2d_7_depthwise': [batch_size, 28, 28, 512],
  221. 'Conv2d_7_pointwise': [batch_size, 28, 28, 512],
  222. 'Conv2d_8_depthwise': [batch_size, 28, 28, 512],
  223. 'Conv2d_8_pointwise': [batch_size, 28, 28, 512],
  224. 'Conv2d_9_depthwise': [batch_size, 28, 28, 512],
  225. 'Conv2d_9_pointwise': [batch_size, 28, 28, 512],
  226. 'Conv2d_10_depthwise': [batch_size, 28, 28, 512],
  227. 'Conv2d_10_pointwise': [batch_size, 28, 28, 512],
  228. 'Conv2d_11_depthwise': [batch_size, 28, 28, 512],
  229. 'Conv2d_11_pointwise': [batch_size, 28, 28, 512],
  230. 'Conv2d_12_depthwise': [batch_size, 28, 28, 512],
  231. 'Conv2d_12_pointwise': [batch_size, 28, 28, 1024],
  232. 'Conv2d_13_depthwise': [batch_size, 28, 28, 1024],
  233. 'Conv2d_13_pointwise': [batch_size, 28, 28, 1024]}
  234. self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
  235. for endpoint_name, expected_shape in endpoints_shapes.items():
  236. self.assertTrue(endpoint_name in end_points)
  237. self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
  238. expected_shape)
  239. def testBuildAndCheckAllEndPointsApproximateFaceNet(self):
  240. batch_size = 5
  241. height, width = 128, 128
  242. inputs = tf.random_uniform((batch_size, height, width, 3))
  243. with slim.arg_scope([slim.conv2d, slim.separable_conv2d],
  244. normalizer_fn=slim.batch_norm):
  245. _, end_points = mobilenet_v1.mobilenet_v1_base(
  246. inputs, final_endpoint='Conv2d_13_pointwise', depth_multiplier=0.75)
  247. # For the Conv2d_0 layer FaceNet has depth=16
  248. endpoints_shapes = {'Conv2d_0': [batch_size, 64, 64, 24],
  249. 'Conv2d_1_depthwise': [batch_size, 64, 64, 24],
  250. 'Conv2d_1_pointwise': [batch_size, 64, 64, 48],
  251. 'Conv2d_2_depthwise': [batch_size, 32, 32, 48],
  252. 'Conv2d_2_pointwise': [batch_size, 32, 32, 96],
  253. 'Conv2d_3_depthwise': [batch_size, 32, 32, 96],
  254. 'Conv2d_3_pointwise': [batch_size, 32, 32, 96],
  255. 'Conv2d_4_depthwise': [batch_size, 16, 16, 96],
  256. 'Conv2d_4_pointwise': [batch_size, 16, 16, 192],
  257. 'Conv2d_5_depthwise': [batch_size, 16, 16, 192],
  258. 'Conv2d_5_pointwise': [batch_size, 16, 16, 192],
  259. 'Conv2d_6_depthwise': [batch_size, 8, 8, 192],
  260. 'Conv2d_6_pointwise': [batch_size, 8, 8, 384],
  261. 'Conv2d_7_depthwise': [batch_size, 8, 8, 384],
  262. 'Conv2d_7_pointwise': [batch_size, 8, 8, 384],
  263. 'Conv2d_8_depthwise': [batch_size, 8, 8, 384],
  264. 'Conv2d_8_pointwise': [batch_size, 8, 8, 384],
  265. 'Conv2d_9_depthwise': [batch_size, 8, 8, 384],
  266. 'Conv2d_9_pointwise': [batch_size, 8, 8, 384],
  267. 'Conv2d_10_depthwise': [batch_size, 8, 8, 384],
  268. 'Conv2d_10_pointwise': [batch_size, 8, 8, 384],
  269. 'Conv2d_11_depthwise': [batch_size, 8, 8, 384],
  270. 'Conv2d_11_pointwise': [batch_size, 8, 8, 384],
  271. 'Conv2d_12_depthwise': [batch_size, 4, 4, 384],
  272. 'Conv2d_12_pointwise': [batch_size, 4, 4, 768],
  273. 'Conv2d_13_depthwise': [batch_size, 4, 4, 768],
  274. 'Conv2d_13_pointwise': [batch_size, 4, 4, 768]}
  275. self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
  276. for endpoint_name, expected_shape in endpoints_shapes.items():
  277. self.assertTrue(endpoint_name in end_points)
  278. self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
  279. expected_shape)
  280. def testModelHasExpectedNumberOfParameters(self):
  281. batch_size = 5
  282. height, width = 224, 224
  283. inputs = tf.random_uniform((batch_size, height, width, 3))
  284. with slim.arg_scope([slim.conv2d, slim.separable_conv2d],
  285. normalizer_fn=slim.batch_norm):
  286. mobilenet_v1.mobilenet_v1_base(inputs)
  287. total_params, _ = slim.model_analyzer.analyze_vars(
  288. slim.get_model_variables())
  289. self.assertAlmostEqual(3217920, total_params)
  290. def testBuildEndPointsWithDepthMultiplierLessThanOne(self):
  291. batch_size = 5
  292. height, width = 224, 224
  293. num_classes = 1000
  294. inputs = tf.random_uniform((batch_size, height, width, 3))
  295. _, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)
  296. endpoint_keys = [key for key in end_points.keys() if key.startswith('Conv')]
  297. _, end_points_with_multiplier = mobilenet_v1.mobilenet_v1(
  298. inputs, num_classes, scope='depth_multiplied_net',
  299. depth_multiplier=0.5)
  300. for key in endpoint_keys:
  301. original_depth = end_points[key].get_shape().as_list()[3]
  302. new_depth = end_points_with_multiplier[key].get_shape().as_list()[3]
  303. self.assertEqual(0.5 * original_depth, new_depth)
  304. def testBuildEndPointsWithDepthMultiplierGreaterThanOne(self):
  305. batch_size = 5
  306. height, width = 224, 224
  307. num_classes = 1000
  308. inputs = tf.random_uniform((batch_size, height, width, 3))
  309. _, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)
  310. endpoint_keys = [key for key in end_points.keys()
  311. if key.startswith('Mixed') or key.startswith('Conv')]
  312. _, end_points_with_multiplier = mobilenet_v1.mobilenet_v1(
  313. inputs, num_classes, scope='depth_multiplied_net',
  314. depth_multiplier=2.0)
  315. for key in endpoint_keys:
  316. original_depth = end_points[key].get_shape().as_list()[3]
  317. new_depth = end_points_with_multiplier[key].get_shape().as_list()[3]
  318. self.assertEqual(2.0 * original_depth, new_depth)
  319. def testRaiseValueErrorWithInvalidDepthMultiplier(self):
  320. batch_size = 5
  321. height, width = 224, 224
  322. num_classes = 1000
  323. inputs = tf.random_uniform((batch_size, height, width, 3))
  324. with self.assertRaises(ValueError):
  325. _ = mobilenet_v1.mobilenet_v1(
  326. inputs, num_classes, depth_multiplier=-0.1)
  327. with self.assertRaises(ValueError):
  328. _ = mobilenet_v1.mobilenet_v1(
  329. inputs, num_classes, depth_multiplier=0.0)
  330. def testHalfSizeImages(self):
  331. batch_size = 5
  332. height, width = 112, 112
  333. num_classes = 1000
  334. inputs = tf.random_uniform((batch_size, height, width, 3))
  335. logits, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)
  336. self.assertTrue(logits.op.name.startswith('MobilenetV1/Logits'))
  337. self.assertListEqual(logits.get_shape().as_list(),
  338. [batch_size, num_classes])
  339. pre_pool = end_points['Conv2d_13_pointwise']
  340. self.assertListEqual(pre_pool.get_shape().as_list(),
  341. [batch_size, 4, 4, 1024])
  342. def testUnknownImageShape(self):
  343. tf.reset_default_graph()
  344. batch_size = 2
  345. height, width = 224, 224
  346. num_classes = 1000
  347. input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
  348. with self.test_session() as sess:
  349. inputs = tf.placeholder(tf.float32, shape=(batch_size, None, None, 3))
  350. logits, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes)
  351. self.assertTrue(logits.op.name.startswith('MobilenetV1/Logits'))
  352. self.assertListEqual(logits.get_shape().as_list(),
  353. [batch_size, num_classes])
  354. pre_pool = end_points['Conv2d_13_pointwise']
  355. feed_dict = {inputs: input_np}
  356. tf.global_variables_initializer().run()
  357. pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
  358. self.assertListEqual(list(pre_pool_out.shape), [batch_size, 7, 7, 1024])
  359. def testGlobalPoolUnknownImageShape(self):
  360. tf.reset_default_graph()
  361. batch_size = 2
  362. height, width = 300, 400
  363. num_classes = 1000
  364. input_np = np.random.uniform(0, 1, (batch_size, height, width, 3))
  365. with self.test_session() as sess:
  366. inputs = tf.placeholder(tf.float32, shape=(batch_size, None, None, 3))
  367. logits, end_points = mobilenet_v1.mobilenet_v1(inputs, num_classes,
  368. global_pool=True)
  369. self.assertTrue(logits.op.name.startswith('MobilenetV1/Logits'))
  370. self.assertListEqual(logits.get_shape().as_list(),
  371. [batch_size, num_classes])
  372. pre_pool = end_points['Conv2d_13_pointwise']
  373. feed_dict = {inputs: input_np}
  374. tf.global_variables_initializer().run()
  375. pre_pool_out = sess.run(pre_pool, feed_dict=feed_dict)
  376. self.assertListEqual(list(pre_pool_out.shape), [batch_size, 10, 13, 1024])
  377. def testUnknowBatchSize(self):
  378. batch_size = 1
  379. height, width = 224, 224
  380. num_classes = 1000
  381. inputs = tf.placeholder(tf.float32, (None, height, width, 3))
  382. logits, _ = mobilenet_v1.mobilenet_v1(inputs, num_classes)
  383. self.assertTrue(logits.op.name.startswith('MobilenetV1/Logits'))
  384. self.assertListEqual(logits.get_shape().as_list(),
  385. [None, num_classes])
  386. images = tf.random_uniform((batch_size, height, width, 3))
  387. with self.test_session() as sess:
  388. sess.run(tf.global_variables_initializer())
  389. output = sess.run(logits, {inputs: images.eval()})
  390. self.assertEquals(output.shape, (batch_size, num_classes))
  391. def testEvaluation(self):
  392. batch_size = 2
  393. height, width = 224, 224
  394. num_classes = 1000
  395. eval_inputs = tf.random_uniform((batch_size, height, width, 3))
  396. logits, _ = mobilenet_v1.mobilenet_v1(eval_inputs, num_classes,
  397. is_training=False)
  398. predictions = tf.argmax(logits, 1)
  399. with self.test_session() as sess:
  400. sess.run(tf.global_variables_initializer())
  401. output = sess.run(predictions)
  402. self.assertEquals(output.shape, (batch_size,))
  403. def testTrainEvalWithReuse(self):
  404. train_batch_size = 5
  405. eval_batch_size = 2
  406. height, width = 150, 150
  407. num_classes = 1000
  408. train_inputs = tf.random_uniform((train_batch_size, height, width, 3))
  409. mobilenet_v1.mobilenet_v1(train_inputs, num_classes)
  410. eval_inputs = tf.random_uniform((eval_batch_size, height, width, 3))
  411. logits, _ = mobilenet_v1.mobilenet_v1(eval_inputs, num_classes,
  412. reuse=True)
  413. predictions = tf.argmax(logits, 1)
  414. with self.test_session() as sess:
  415. sess.run(tf.global_variables_initializer())
  416. output = sess.run(predictions)
  417. self.assertEquals(output.shape, (eval_batch_size,))
  418. def testLogitsNotSqueezed(self):
  419. num_classes = 25
  420. images = tf.random_uniform([1, 224, 224, 3])
  421. logits, _ = mobilenet_v1.mobilenet_v1(images,
  422. num_classes=num_classes,
  423. spatial_squeeze=False)
  424. with self.test_session() as sess:
  425. tf.global_variables_initializer().run()
  426. logits_out = sess.run(logits)
  427. self.assertListEqual(list(logits_out.shape), [1, 1, 1, num_classes])
  428. if __name__ == '__main__':
  429. tf.test.main()