vgg_test.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. """Tests for slim.nets.vgg."""
  16. from __future__ import absolute_import
  17. from __future__ import division
  18. from __future__ import print_function
  19. import tensorflow as tf
  20. from nets import vgg
  21. slim = tf.contrib.slim
  22. class VGGATest(tf.test.TestCase):
  23. def testBuild(self):
  24. batch_size = 5
  25. height, width = 224, 224
  26. num_classes = 1000
  27. with self.test_session():
  28. inputs = tf.random_uniform((batch_size, height, width, 3))
  29. logits, _ = vgg.vgg_a(inputs, num_classes)
  30. self.assertEquals(logits.op.name, 'vgg_a/fc8/squeezed')
  31. self.assertListEqual(logits.get_shape().as_list(),
  32. [batch_size, num_classes])
  33. def testFullyConvolutional(self):
  34. batch_size = 1
  35. height, width = 256, 256
  36. num_classes = 1000
  37. with self.test_session():
  38. inputs = tf.random_uniform((batch_size, height, width, 3))
  39. logits, _ = vgg.vgg_a(inputs, num_classes, spatial_squeeze=False)
  40. self.assertEquals(logits.op.name, 'vgg_a/fc8/BiasAdd')
  41. self.assertListEqual(logits.get_shape().as_list(),
  42. [batch_size, 2, 2, num_classes])
  43. def testEndPoints(self):
  44. batch_size = 5
  45. height, width = 224, 224
  46. num_classes = 1000
  47. with self.test_session():
  48. inputs = tf.random_uniform((batch_size, height, width, 3))
  49. _, end_points = vgg.vgg_a(inputs, num_classes)
  50. expected_names = ['vgg_a/conv1/conv1_1',
  51. 'vgg_a/pool1',
  52. 'vgg_a/conv2/conv2_1',
  53. 'vgg_a/pool2',
  54. 'vgg_a/conv3/conv3_1',
  55. 'vgg_a/conv3/conv3_2',
  56. 'vgg_a/pool3',
  57. 'vgg_a/conv4/conv4_1',
  58. 'vgg_a/conv4/conv4_2',
  59. 'vgg_a/pool4',
  60. 'vgg_a/conv5/conv5_1',
  61. 'vgg_a/conv5/conv5_2',
  62. 'vgg_a/pool5',
  63. 'vgg_a/fc6',
  64. 'vgg_a/fc7',
  65. 'vgg_a/fc8'
  66. ]
  67. self.assertSetEqual(set(end_points.keys()), set(expected_names))
  68. def testModelVariables(self):
  69. batch_size = 5
  70. height, width = 224, 224
  71. num_classes = 1000
  72. with self.test_session():
  73. inputs = tf.random_uniform((batch_size, height, width, 3))
  74. vgg.vgg_a(inputs, num_classes)
  75. expected_names = ['vgg_a/conv1/conv1_1/weights',
  76. 'vgg_a/conv1/conv1_1/biases',
  77. 'vgg_a/conv2/conv2_1/weights',
  78. 'vgg_a/conv2/conv2_1/biases',
  79. 'vgg_a/conv3/conv3_1/weights',
  80. 'vgg_a/conv3/conv3_1/biases',
  81. 'vgg_a/conv3/conv3_2/weights',
  82. 'vgg_a/conv3/conv3_2/biases',
  83. 'vgg_a/conv4/conv4_1/weights',
  84. 'vgg_a/conv4/conv4_1/biases',
  85. 'vgg_a/conv4/conv4_2/weights',
  86. 'vgg_a/conv4/conv4_2/biases',
  87. 'vgg_a/conv5/conv5_1/weights',
  88. 'vgg_a/conv5/conv5_1/biases',
  89. 'vgg_a/conv5/conv5_2/weights',
  90. 'vgg_a/conv5/conv5_2/biases',
  91. 'vgg_a/fc6/weights',
  92. 'vgg_a/fc6/biases',
  93. 'vgg_a/fc7/weights',
  94. 'vgg_a/fc7/biases',
  95. 'vgg_a/fc8/weights',
  96. 'vgg_a/fc8/biases',
  97. ]
  98. model_variables = [v.op.name for v in slim.get_model_variables()]
  99. self.assertSetEqual(set(model_variables), set(expected_names))
  100. def testEvaluation(self):
  101. batch_size = 2
  102. height, width = 224, 224
  103. num_classes = 1000
  104. with self.test_session():
  105. eval_inputs = tf.random_uniform((batch_size, height, width, 3))
  106. logits, _ = vgg.vgg_a(eval_inputs, is_training=False)
  107. self.assertListEqual(logits.get_shape().as_list(),
  108. [batch_size, num_classes])
  109. predictions = tf.argmax(logits, 1)
  110. self.assertListEqual(predictions.get_shape().as_list(), [batch_size])
  111. def testTrainEvalWithReuse(self):
  112. train_batch_size = 2
  113. eval_batch_size = 1
  114. train_height, train_width = 224, 224
  115. eval_height, eval_width = 256, 256
  116. num_classes = 1000
  117. with self.test_session():
  118. train_inputs = tf.random_uniform(
  119. (train_batch_size, train_height, train_width, 3))
  120. logits, _ = vgg.vgg_a(train_inputs)
  121. self.assertListEqual(logits.get_shape().as_list(),
  122. [train_batch_size, num_classes])
  123. tf.get_variable_scope().reuse_variables()
  124. eval_inputs = tf.random_uniform(
  125. (eval_batch_size, eval_height, eval_width, 3))
  126. logits, _ = vgg.vgg_a(eval_inputs, is_training=False,
  127. spatial_squeeze=False)
  128. self.assertListEqual(logits.get_shape().as_list(),
  129. [eval_batch_size, 2, 2, num_classes])
  130. logits = tf.reduce_mean(logits, [1, 2])
  131. predictions = tf.argmax(logits, 1)
  132. self.assertEquals(predictions.get_shape().as_list(), [eval_batch_size])
  133. def testForward(self):
  134. batch_size = 1
  135. height, width = 224, 224
  136. with self.test_session() as sess:
  137. inputs = tf.random_uniform((batch_size, height, width, 3))
  138. logits, _ = vgg.vgg_a(inputs)
  139. sess.run(tf.global_variables_initializer())
  140. output = sess.run(logits)
  141. self.assertTrue(output.any())
  142. class VGG16Test(tf.test.TestCase):
  143. def testBuild(self):
  144. batch_size = 5
  145. height, width = 224, 224
  146. num_classes = 1000
  147. with self.test_session():
  148. inputs = tf.random_uniform((batch_size, height, width, 3))
  149. logits, _ = vgg.vgg_16(inputs, num_classes)
  150. self.assertEquals(logits.op.name, 'vgg_16/fc8/squeezed')
  151. self.assertListEqual(logits.get_shape().as_list(),
  152. [batch_size, num_classes])
  153. def testFullyConvolutional(self):
  154. batch_size = 1
  155. height, width = 256, 256
  156. num_classes = 1000
  157. with self.test_session():
  158. inputs = tf.random_uniform((batch_size, height, width, 3))
  159. logits, _ = vgg.vgg_16(inputs, num_classes, spatial_squeeze=False)
  160. self.assertEquals(logits.op.name, 'vgg_16/fc8/BiasAdd')
  161. self.assertListEqual(logits.get_shape().as_list(),
  162. [batch_size, 2, 2, num_classes])
  163. def testEndPoints(self):
  164. batch_size = 5
  165. height, width = 224, 224
  166. num_classes = 1000
  167. with self.test_session():
  168. inputs = tf.random_uniform((batch_size, height, width, 3))
  169. _, end_points = vgg.vgg_16(inputs, num_classes)
  170. expected_names = ['vgg_16/conv1/conv1_1',
  171. 'vgg_16/conv1/conv1_2',
  172. 'vgg_16/pool1',
  173. 'vgg_16/conv2/conv2_1',
  174. 'vgg_16/conv2/conv2_2',
  175. 'vgg_16/pool2',
  176. 'vgg_16/conv3/conv3_1',
  177. 'vgg_16/conv3/conv3_2',
  178. 'vgg_16/conv3/conv3_3',
  179. 'vgg_16/pool3',
  180. 'vgg_16/conv4/conv4_1',
  181. 'vgg_16/conv4/conv4_2',
  182. 'vgg_16/conv4/conv4_3',
  183. 'vgg_16/pool4',
  184. 'vgg_16/conv5/conv5_1',
  185. 'vgg_16/conv5/conv5_2',
  186. 'vgg_16/conv5/conv5_3',
  187. 'vgg_16/pool5',
  188. 'vgg_16/fc6',
  189. 'vgg_16/fc7',
  190. 'vgg_16/fc8'
  191. ]
  192. self.assertSetEqual(set(end_points.keys()), set(expected_names))
  193. def testModelVariables(self):
  194. batch_size = 5
  195. height, width = 224, 224
  196. num_classes = 1000
  197. with self.test_session():
  198. inputs = tf.random_uniform((batch_size, height, width, 3))
  199. vgg.vgg_16(inputs, num_classes)
  200. expected_names = ['vgg_16/conv1/conv1_1/weights',
  201. 'vgg_16/conv1/conv1_1/biases',
  202. 'vgg_16/conv1/conv1_2/weights',
  203. 'vgg_16/conv1/conv1_2/biases',
  204. 'vgg_16/conv2/conv2_1/weights',
  205. 'vgg_16/conv2/conv2_1/biases',
  206. 'vgg_16/conv2/conv2_2/weights',
  207. 'vgg_16/conv2/conv2_2/biases',
  208. 'vgg_16/conv3/conv3_1/weights',
  209. 'vgg_16/conv3/conv3_1/biases',
  210. 'vgg_16/conv3/conv3_2/weights',
  211. 'vgg_16/conv3/conv3_2/biases',
  212. 'vgg_16/conv3/conv3_3/weights',
  213. 'vgg_16/conv3/conv3_3/biases',
  214. 'vgg_16/conv4/conv4_1/weights',
  215. 'vgg_16/conv4/conv4_1/biases',
  216. 'vgg_16/conv4/conv4_2/weights',
  217. 'vgg_16/conv4/conv4_2/biases',
  218. 'vgg_16/conv4/conv4_3/weights',
  219. 'vgg_16/conv4/conv4_3/biases',
  220. 'vgg_16/conv5/conv5_1/weights',
  221. 'vgg_16/conv5/conv5_1/biases',
  222. 'vgg_16/conv5/conv5_2/weights',
  223. 'vgg_16/conv5/conv5_2/biases',
  224. 'vgg_16/conv5/conv5_3/weights',
  225. 'vgg_16/conv5/conv5_3/biases',
  226. 'vgg_16/fc6/weights',
  227. 'vgg_16/fc6/biases',
  228. 'vgg_16/fc7/weights',
  229. 'vgg_16/fc7/biases',
  230. 'vgg_16/fc8/weights',
  231. 'vgg_16/fc8/biases',
  232. ]
  233. model_variables = [v.op.name for v in slim.get_model_variables()]
  234. self.assertSetEqual(set(model_variables), set(expected_names))
  235. def testEvaluation(self):
  236. batch_size = 2
  237. height, width = 224, 224
  238. num_classes = 1000
  239. with self.test_session():
  240. eval_inputs = tf.random_uniform((batch_size, height, width, 3))
  241. logits, _ = vgg.vgg_16(eval_inputs, is_training=False)
  242. self.assertListEqual(logits.get_shape().as_list(),
  243. [batch_size, num_classes])
  244. predictions = tf.argmax(logits, 1)
  245. self.assertListEqual(predictions.get_shape().as_list(), [batch_size])
  246. def testTrainEvalWithReuse(self):
  247. train_batch_size = 2
  248. eval_batch_size = 1
  249. train_height, train_width = 224, 224
  250. eval_height, eval_width = 256, 256
  251. num_classes = 1000
  252. with self.test_session():
  253. train_inputs = tf.random_uniform(
  254. (train_batch_size, train_height, train_width, 3))
  255. logits, _ = vgg.vgg_16(train_inputs)
  256. self.assertListEqual(logits.get_shape().as_list(),
  257. [train_batch_size, num_classes])
  258. tf.get_variable_scope().reuse_variables()
  259. eval_inputs = tf.random_uniform(
  260. (eval_batch_size, eval_height, eval_width, 3))
  261. logits, _ = vgg.vgg_16(eval_inputs, is_training=False,
  262. spatial_squeeze=False)
  263. self.assertListEqual(logits.get_shape().as_list(),
  264. [eval_batch_size, 2, 2, num_classes])
  265. logits = tf.reduce_mean(logits, [1, 2])
  266. predictions = tf.argmax(logits, 1)
  267. self.assertEquals(predictions.get_shape().as_list(), [eval_batch_size])
  268. def testForward(self):
  269. batch_size = 1
  270. height, width = 224, 224
  271. with self.test_session() as sess:
  272. inputs = tf.random_uniform((batch_size, height, width, 3))
  273. logits, _ = vgg.vgg_16(inputs)
  274. sess.run(tf.global_variables_initializer())
  275. output = sess.run(logits)
  276. self.assertTrue(output.any())
  277. class VGG19Test(tf.test.TestCase):
  278. def testBuild(self):
  279. batch_size = 5
  280. height, width = 224, 224
  281. num_classes = 1000
  282. with self.test_session():
  283. inputs = tf.random_uniform((batch_size, height, width, 3))
  284. logits, _ = vgg.vgg_19(inputs, num_classes)
  285. self.assertEquals(logits.op.name, 'vgg_19/fc8/squeezed')
  286. self.assertListEqual(logits.get_shape().as_list(),
  287. [batch_size, num_classes])
  288. def testFullyConvolutional(self):
  289. batch_size = 1
  290. height, width = 256, 256
  291. num_classes = 1000
  292. with self.test_session():
  293. inputs = tf.random_uniform((batch_size, height, width, 3))
  294. logits, _ = vgg.vgg_19(inputs, num_classes, spatial_squeeze=False)
  295. self.assertEquals(logits.op.name, 'vgg_19/fc8/BiasAdd')
  296. self.assertListEqual(logits.get_shape().as_list(),
  297. [batch_size, 2, 2, num_classes])
  298. def testEndPoints(self):
  299. batch_size = 5
  300. height, width = 224, 224
  301. num_classes = 1000
  302. with self.test_session():
  303. inputs = tf.random_uniform((batch_size, height, width, 3))
  304. _, end_points = vgg.vgg_19(inputs, num_classes)
  305. expected_names = [
  306. 'vgg_19/conv1/conv1_1',
  307. 'vgg_19/conv1/conv1_2',
  308. 'vgg_19/pool1',
  309. 'vgg_19/conv2/conv2_1',
  310. 'vgg_19/conv2/conv2_2',
  311. 'vgg_19/pool2',
  312. 'vgg_19/conv3/conv3_1',
  313. 'vgg_19/conv3/conv3_2',
  314. 'vgg_19/conv3/conv3_3',
  315. 'vgg_19/conv3/conv3_4',
  316. 'vgg_19/pool3',
  317. 'vgg_19/conv4/conv4_1',
  318. 'vgg_19/conv4/conv4_2',
  319. 'vgg_19/conv4/conv4_3',
  320. 'vgg_19/conv4/conv4_4',
  321. 'vgg_19/pool4',
  322. 'vgg_19/conv5/conv5_1',
  323. 'vgg_19/conv5/conv5_2',
  324. 'vgg_19/conv5/conv5_3',
  325. 'vgg_19/conv5/conv5_4',
  326. 'vgg_19/pool5',
  327. 'vgg_19/fc6',
  328. 'vgg_19/fc7',
  329. 'vgg_19/fc8'
  330. ]
  331. self.assertSetEqual(set(end_points.keys()), set(expected_names))
  332. def testModelVariables(self):
  333. batch_size = 5
  334. height, width = 224, 224
  335. num_classes = 1000
  336. with self.test_session():
  337. inputs = tf.random_uniform((batch_size, height, width, 3))
  338. vgg.vgg_19(inputs, num_classes)
  339. expected_names = [
  340. 'vgg_19/conv1/conv1_1/weights',
  341. 'vgg_19/conv1/conv1_1/biases',
  342. 'vgg_19/conv1/conv1_2/weights',
  343. 'vgg_19/conv1/conv1_2/biases',
  344. 'vgg_19/conv2/conv2_1/weights',
  345. 'vgg_19/conv2/conv2_1/biases',
  346. 'vgg_19/conv2/conv2_2/weights',
  347. 'vgg_19/conv2/conv2_2/biases',
  348. 'vgg_19/conv3/conv3_1/weights',
  349. 'vgg_19/conv3/conv3_1/biases',
  350. 'vgg_19/conv3/conv3_2/weights',
  351. 'vgg_19/conv3/conv3_2/biases',
  352. 'vgg_19/conv3/conv3_3/weights',
  353. 'vgg_19/conv3/conv3_3/biases',
  354. 'vgg_19/conv3/conv3_4/weights',
  355. 'vgg_19/conv3/conv3_4/biases',
  356. 'vgg_19/conv4/conv4_1/weights',
  357. 'vgg_19/conv4/conv4_1/biases',
  358. 'vgg_19/conv4/conv4_2/weights',
  359. 'vgg_19/conv4/conv4_2/biases',
  360. 'vgg_19/conv4/conv4_3/weights',
  361. 'vgg_19/conv4/conv4_3/biases',
  362. 'vgg_19/conv4/conv4_4/weights',
  363. 'vgg_19/conv4/conv4_4/biases',
  364. 'vgg_19/conv5/conv5_1/weights',
  365. 'vgg_19/conv5/conv5_1/biases',
  366. 'vgg_19/conv5/conv5_2/weights',
  367. 'vgg_19/conv5/conv5_2/biases',
  368. 'vgg_19/conv5/conv5_3/weights',
  369. 'vgg_19/conv5/conv5_3/biases',
  370. 'vgg_19/conv5/conv5_4/weights',
  371. 'vgg_19/conv5/conv5_4/biases',
  372. 'vgg_19/fc6/weights',
  373. 'vgg_19/fc6/biases',
  374. 'vgg_19/fc7/weights',
  375. 'vgg_19/fc7/biases',
  376. 'vgg_19/fc8/weights',
  377. 'vgg_19/fc8/biases',
  378. ]
  379. model_variables = [v.op.name for v in slim.get_model_variables()]
  380. self.assertSetEqual(set(model_variables), set(expected_names))
  381. def testEvaluation(self):
  382. batch_size = 2
  383. height, width = 224, 224
  384. num_classes = 1000
  385. with self.test_session():
  386. eval_inputs = tf.random_uniform((batch_size, height, width, 3))
  387. logits, _ = vgg.vgg_19(eval_inputs, is_training=False)
  388. self.assertListEqual(logits.get_shape().as_list(),
  389. [batch_size, num_classes])
  390. predictions = tf.argmax(logits, 1)
  391. self.assertListEqual(predictions.get_shape().as_list(), [batch_size])
  392. def testTrainEvalWithReuse(self):
  393. train_batch_size = 2
  394. eval_batch_size = 1
  395. train_height, train_width = 224, 224
  396. eval_height, eval_width = 256, 256
  397. num_classes = 1000
  398. with self.test_session():
  399. train_inputs = tf.random_uniform(
  400. (train_batch_size, train_height, train_width, 3))
  401. logits, _ = vgg.vgg_19(train_inputs)
  402. self.assertListEqual(logits.get_shape().as_list(),
  403. [train_batch_size, num_classes])
  404. tf.get_variable_scope().reuse_variables()
  405. eval_inputs = tf.random_uniform(
  406. (eval_batch_size, eval_height, eval_width, 3))
  407. logits, _ = vgg.vgg_19(eval_inputs, is_training=False,
  408. spatial_squeeze=False)
  409. self.assertListEqual(logits.get_shape().as_list(),
  410. [eval_batch_size, 2, 2, num_classes])
  411. logits = tf.reduce_mean(logits, [1, 2])
  412. predictions = tf.argmax(logits, 1)
  413. self.assertEquals(predictions.get_shape().as_list(), [eval_batch_size])
  414. def testForward(self):
  415. batch_size = 1
  416. height, width = 224, 224
  417. with self.test_session() as sess:
  418. inputs = tf.random_uniform((batch_size, height, width, 3))
  419. logits, _ = vgg.vgg_19(inputs)
  420. sess.run(tf.global_variables_initializer())
  421. output = sess.run(logits)
  422. self.assertTrue(output.any())
  423. if __name__ == '__main__':
  424. tf.test.main()