コード例 #1
0
 def testVariablesSetDeviceMobileModel(self):
     batch_size = 5
     height, width = 224, 224
     num_classes = 1000
     inputs = tf.random_uniform((batch_size, height, width, 3))
     tf.train.create_global_step()
     # Force all Variables to reside on the device.
     with tf.variable_scope('on_cpu'), tf.device('/cpu:0'):
         with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
             nasnet.build_nasnet_mobile(inputs, num_classes)
     with tf.variable_scope('on_gpu'), tf.device('/gpu:0'):
         with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
             nasnet.build_nasnet_mobile(inputs, num_classes)
     for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                scope='on_cpu'):
         self.assertDeviceEqual(v.device, '/cpu:0')
     for v in tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                scope='on_gpu'):
         self.assertDeviceEqual(v.device, '/gpu:0')
コード例 #2
0
ファイル: model.py プロジェクト: huang-sien/test
    def MyNASNet(self, images, is_training):
        arg_scope = nasnet.nasnet_mobile_arg_scope()
        with slim.arg_scope(arg_scope):
            logits, end_points = nasnet.build_nasnet_mobile(
                images,
                num_classes=self.num_classes + 1,
                is_training=is_training)

        global_step = tf.train.get_or_create_global_step()

        return logits, end_points, global_step
コード例 #3
0
 def testBuildPreLogitsMobileModel(self):
     batch_size = 5
     height, width = 224, 224
     num_classes = None
     inputs = tf.random_uniform((batch_size, height, width, 3))
     tf.train.create_global_step()
     with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
         net, end_points = nasnet.build_nasnet_mobile(inputs, num_classes)
     self.assertFalse('AuxLogits' in end_points)
     self.assertFalse('Predictions' in end_points)
     self.assertTrue(net.op.name.startswith('final_layer/Mean'))
     self.assertListEqual(net.get_shape().as_list(), [batch_size, 1056])
コード例 #4
0
 def testOverrideHParamsMobileModel(self):
     batch_size = 5
     height, width = 224, 224
     num_classes = 1000
     inputs = tf.random_uniform((batch_size, height, width, 3))
     tf.train.create_global_step()
     config = nasnet.mobile_imagenet_config()
     config.set_hparam('data_format', 'NCHW')
     with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
         _, end_points = nasnet.build_nasnet_mobile(inputs,
                                                    num_classes,
                                                    config=config)
     self.assertListEqual(end_points['Stem'].shape.as_list(),
                          [batch_size, 88, 28, 28])
コード例 #5
0
 def testEvaluationMobileModel(self):
     batch_size = 2
     height, width = 224, 224
     num_classes = 1000
     with self.test_session() as sess:
         eval_inputs = tf.random_uniform((batch_size, height, width, 3))
         with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
             logits, _ = nasnet.build_nasnet_mobile(eval_inputs,
                                                    num_classes,
                                                    is_training=False)
         predictions = tf.argmax(logits, 1)
         sess.run(tf.global_variables_initializer())
         output = sess.run(predictions)
         self.assertEquals(output.shape, (batch_size, ))
コード例 #6
0
 def testUnknownBatchSizeMobileModel(self):
     batch_size = 1
     height, width = 224, 224
     num_classes = 1000
     with self.test_session() as sess:
         inputs = tf.placeholder(tf.float32, (None, height, width, 3))
         with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
             logits, _ = nasnet.build_nasnet_mobile(inputs, num_classes)
         self.assertListEqual(logits.get_shape().as_list(),
                              [None, num_classes])
         images = tf.random_uniform((batch_size, height, width, 3))
         sess.run(tf.global_variables_initializer())
         output = sess.run(logits, {inputs: images.eval()})
         self.assertEquals(output.shape, (batch_size, num_classes))
コード例 #7
0
 def testNoAuxHeadMobileModel(self):
     batch_size = 5
     height, width = 224, 224
     num_classes = 1000
     for use_aux_head in (True, False):
         tf.reset_default_graph()
         inputs = tf.random_uniform((batch_size, height, width, 3))
         tf.train.create_global_step()
         config = nasnet.mobile_imagenet_config()
         config.set_hparam('use_aux_head', int(use_aux_head))
         with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
             _, end_points = nasnet.build_nasnet_mobile(inputs,
                                                        num_classes,
                                                        config=config)
         self.assertEqual('AuxLogits' in end_points, use_aux_head)
コード例 #8
0
 def testBuildLogitsMobileModel(self):
     batch_size = 5
     height, width = 224, 224
     num_classes = 1000
     inputs = tf.random_uniform((batch_size, height, width, 3))
     tf.train.create_global_step()
     with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
         logits, end_points = nasnet.build_nasnet_mobile(
             inputs, num_classes)
     auxlogits = end_points['AuxLogits']
     predictions = end_points['Predictions']
     self.assertListEqual(auxlogits.get_shape().as_list(),
                          [batch_size, num_classes])
     self.assertListEqual(logits.get_shape().as_list(),
                          [batch_size, num_classes])
     self.assertListEqual(predictions.get_shape().as_list(),
                          [batch_size, num_classes])
コード例 #9
0
 def testAllEndPointsShapesMobileModel(self):
     batch_size = 5
     height, width = 224, 224
     num_classes = 1000
     inputs = tf.random_uniform((batch_size, height, width, 3))
     tf.train.create_global_step()
     with slim.arg_scope(nasnet.nasnet_mobile_arg_scope()):
         _, end_points = nasnet.build_nasnet_mobile(inputs, num_classes)
     endpoints_shapes = {
         'Stem': [batch_size, 28, 28, 88],
         'Cell_0': [batch_size, 28, 28, 264],
         'Cell_1': [batch_size, 28, 28, 264],
         'Cell_2': [batch_size, 28, 28, 264],
         'Cell_3': [batch_size, 28, 28, 264],
         'Cell_4': [batch_size, 14, 14, 528],
         'Cell_5': [batch_size, 14, 14, 528],
         'Cell_6': [batch_size, 14, 14, 528],
         'Cell_7': [batch_size, 14, 14, 528],
         'Cell_8': [batch_size, 7, 7, 1056],
         'Cell_9': [batch_size, 7, 7, 1056],
         'Cell_10': [batch_size, 7, 7, 1056],
         'Cell_11': [batch_size, 7, 7, 1056],
         'Reduction_Cell_0': [batch_size, 14, 14, 352],
         'Reduction_Cell_1': [batch_size, 7, 7, 704],
         'global_pool': [batch_size, 1056],
         # Logits and predictions
         'AuxLogits': [batch_size, num_classes],
         'Logits': [batch_size, num_classes],
         'Predictions': [batch_size, num_classes]
     }
     self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
     for endpoint_name in endpoints_shapes:
         tf.logging.info('Endpoint name: {}'.format(endpoint_name))
         expected_shape = endpoints_shapes[endpoint_name]
         self.assertTrue(endpoint_name in end_points)
         self.assertListEqual(
             end_points[endpoint_name].get_shape().as_list(),
             expected_shape)
コード例 #10
0
def pnasnet_mobile_arg_scope(weight_decay=4e-5,
                             batch_norm_decay=0.9997,
                             batch_norm_epsilon=0.001):
    """Default arg scope for the PNASNet Mobile ImageNet model."""
    return nasnet.nasnet_mobile_arg_scope(weight_decay, batch_norm_decay,
                                          batch_norm_epsilon)
コード例 #11
0
import numpy as np
from slim.nets.nasnet import nasnet  #轻量级 适合移动端
from slim.datasets import imagenet
slim = tf.contrib.slim

tf.reset_default_graph()

sample_images = ['hy.jpg', 'ps.jpg', 'filename3.jpg']  #导入测试样本

image_size = nasnet.build_nasnet_mobile.default_image_size  #获得模型图片尺寸
input_imgs = tf.placeholder(tf.float32,
                            [None, image_size, image_size, 3])  #为根据模型尺寸定义输入占位符

#定义输出
x1 = 2 * (input_imgs / 255) - 1  #归一化图片
arg_scope = nasnet.nasnet_mobile_arg_scope()  #获得模型的命名空间
with slim.arg_scope(arg_scope):  #将图片放入模型
    logits, end_points = nasnet.build_nasnet_mobile(x1,
                                                    num_classes=1001,
                                                    is_training=False)
    prob = end_points['Predictions']  #获得结果的节点
    y = tf.argmax(prob, axis=1)  #按概率获得分类结果

checkpoint_file = 'nasnet-a_mobile_04_10_2017/model.ckpt'  #定义模型地址
saver = tf.train.Saver()
with tf.Session() as sess:
    saver.restore(sess, checkpoint_file)  #载入模型

    def preimg(img):  #resize图片
        ch = 3
        if img.mode == 'RGBA':