EG version upgrade to 1.3
[ealt-edge.git] / example-apps / PDD / pcb-defect-detection / libs / networks / slim_nets / nets_factory_test.py
1 # Copyright 2016 Google Inc. 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
16 """Tests for slim.inception."""
17
18 from __future__ import absolute_import
19 from __future__ import division
20 from __future__ import print_function
21
22 import tensorflow as tf
23
24 from nets import nets_factory
25
26 slim = tf.contrib.slim
27
28
29 class NetworksTest(tf.test.TestCase):
30
31   def testGetNetworkFn(self):
32     batch_size = 5
33     num_classes = 1000
34     for net in nets_factory.networks_map:
35       with self.test_session():
36         net_fn = nets_factory.get_network_fn(net, num_classes)
37         # Most networks use 224 as their default_image_size
38         image_size = getattr(net_fn, 'default_image_size', 224)
39         inputs = tf.random_uniform((batch_size, image_size, image_size, 3))
40         logits, end_points = net_fn(inputs)
41         self.assertTrue(isinstance(logits, tf.Tensor))
42         self.assertTrue(isinstance(end_points, dict))
43         self.assertEqual(logits.get_shape().as_list()[0], batch_size)
44         self.assertEqual(logits.get_shape().as_list()[-1], num_classes)
45
46   def testGetNetworkFnArgScope(self):
47     batch_size = 5
48     num_classes = 10
49     net = 'cifarnet'
50     with self.test_session(use_gpu=True):
51       net_fn = nets_factory.get_network_fn(net, num_classes)
52       image_size = getattr(net_fn, 'default_image_size', 224)
53       with slim.arg_scope([slim.model_variable, slim.variable],
54                           device='/CPU:0'):
55         inputs = tf.random_uniform((batch_size, image_size, image_size, 3))
56         net_fn(inputs)
57       weights = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, 'CifarNet/conv1')[0]
58       self.assertDeviceEqual('/CPU:0', weights.device)
59
60 if __name__ == '__main__':
61   tf.test.main()