Exemplo n.º 1
0
        def _network_define(num_classes):
            with tf.variable_scope("input"):
                paser = ImageTFRecordBuilder(299, 299, 3, None, None, None,
                                             None, None, None).get_parser()
                # files = tf.train.match_filenames_once(train_files)
                tfrecord_paths = tf.placeholder(tf.string, shape=[])
                files = tf.train.match_filenames_once(tfrecord_paths)
                ds = tf.data.TFRecordDataset(files)
                ds = ds.map(paser).repeat(10).shuffle(
                    buffer_size=100).batch(32)
                iterator = ds.make_initializable_iterator()
                images, labels = iterator.get_next()

            # with tf.variable_scope("input"):
            #     images = tf.placeholder(dtype=tf.float32, shape=[None, 299, 299, 3], name="input-image")
            #     labels = tf.placeholder(dtype=tf.int32, shape=[None], name="input-label")
            arg_scope = inception_v3.inception_v3_arg_scope()
            print("arg_scope:")
            for k, v in arg_scope.items():
                print(k)
                for k2, v2 in v.items():
                    print("  ", k2, v2)

            with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
                logits, _ = inception_v3.inception_v3(images,
                                                      num_classes=num_classes)
            return images, labels, logits, iterator, tfrecord_paths
Exemplo n.º 2
0
 def _build_arch(self, net_in):
     with slim.arg_scope(inception_v3_arg_scope()):
         network = SlimNetsLayer(layer=net_in,
                                 slim_layer=inception_v3,
                                 slim_args=self.slim_args,
                                 name='InceptionV3')
     return network
    def test_inception(self):

        num_classes = 1001
        batch_size = 1

        # Read input
        self.image = tf.read_file('./images/python.jpg')
        image_decoded = tf.image.decode_jpeg(self.image, channels=3)

        image_prep = preprocess_for_eval(image_decoded, 299, 299)
        image_exp = tf.expand_dims(image_prep, 0)

        # Restore trained variables from checkpoint file
        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            net, end_points = inception_v3.inception_v3_base(image_exp)
        # Output : 1 x 8 x 8 x 2048
        shape = net.get_shape()
        net = slim.avg_pool2d(net, shape[1:3], padding="VALID")
        # Output : 1 x 1 x 1 x 2048
        net = slim.dropout(net)
        # Output : 1 x 8 x 8 x 2048
        net = slim.flatten(net)
        # Output : 1 x 2048

        saver = tf.train.Saver(max_to_keep=None)

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            saver.restore(sess, self.inception_checkpoint_file)
            output = sess.run(net)
            print(output)
Exemplo n.º 4
0
def _image_to_vec(image_str_tensor):
    def _decode_and_resize(image_str_tensor):
        """Decodes jpeg string, resizes it and returns a uint8 tensor."""

        # These constants are set by Inception v3's expectations.
        height = 299
        width = 299
        channels = 3

        image = tf.read_file(image_str_tensor)
        image = tf.image.decode_jpeg(image, channels=channels)
        image = tf.expand_dims(image, 0)
        image = tf.image.resize_bilinear(image, [height, width],
                                         align_corners=False)
        image = tf.squeeze(image, squeeze_dims=[0])
        image = tf.cast(image, dtype=tf.uint8)
        return image

    image = tf.map_fn(_decode_and_resize,
                      image_str_tensor,
                      back_prop=False,
                      dtype=tf.uint8)
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)
    image = tf.subtract(image, 0.5)
    inception_input = tf.multiply(image, 2.0)

    # Build Inception layers, which expect a tensor of type float from [-1, 1)
    # and shape [batch_size, height, width, channels].
    with tf.contrib.slim.arg_scope(inception_v3_arg_scope()):
        _, end_points = inception_v3(inception_input, is_training=False)

    embeddings = end_points['PreLogits']
    inception_embeddings = tf.squeeze(embeddings, [1, 2],
                                      name='SpatialSqueeze')
    return inception_embeddings
Exemplo n.º 5
0
    def inception_v3(self):
        print(
            '**********************\nBuild inception_v3\n**********************\n'
        )
        config = self.config

        images = tf.placeholder(dtype=tf.float32,
                                shape=[
                                    config.batch_size, self.image_shape[0],
                                    self.image_shape[1], self.image_shape[2]
                                ])
        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            with slim.arg_scope([layers_lib.batch_norm, layers_lib.dropout],
                                is_training=self.is_train):
                logits, end_points = inception_v3.inception_v3_base(
                    images, final_endpoint="Mixed_7c")
        downsample_feat = end_points['Mixed_7c']
        shapetemp = downsample_feat.get_shape().as_list()

        res10_gap = tf.nn.avg_pool(downsample_feat,
                                   ksize=[1, shapetemp[1], shapetemp[2], 1],
                                   strides=[1, shapetemp[1], shapetemp[2], 1],
                                   padding='VALID')
        shapetemp = res10_gap.get_shape().as_list()

        res10_gap = tf.reshape(
            res10_gap, [-1, shapetemp[1] * shapetemp[2] * shapetemp[3]])
        print('shape of res10_gap: ', res10_gap.get_shape().as_list())
        fc1 = self.nn.dense(res10_gap, config.num_lstm_units)
        fc1 = self.nn.dropout(fc1)
        print('shape of fc1: ', fc1.get_shape().as_list())
        self.conv_feats = fc1
        self.images = images
Exemplo n.º 6
0
def init():
    global g_tf_sess, probabilities, label_dict, input_images

    parser = argparse.ArgumentParser(description="Start a tensorflow model serving")
    parser.add_argument('--model_name', dest="model_name", required=True)
    parser.add_argument('--labels_dir', dest="labels_dir", required=True)
    args, _ = parser.parse_known_args()

    label_dict = get_class_label_dict(args.labels_dir)
    classes_num = len(label_dict)

    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        input_images = tf.placeholder(tf.float32, [1, image_size, image_size, num_channel])
        logits, _ = inception_v3.inception_v3(input_images,
                                              num_classes=classes_num,
                                              is_training=False)
        probabilities = tf.argmax(logits, 1)

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    g_tf_sess = tf.Session(config=config)
    g_tf_sess.run(tf.global_variables_initializer())
    g_tf_sess.run(tf.local_variables_initializer())

    model_path = Model.get_model_path(args.model_name)
    saver = tf.train.Saver()
    saver.restore(g_tf_sess, model_path)
Exemplo n.º 7
0
def Build_Image_Embeddings(mode, images, train_inception):
    """Builds the image model subgraph and generates image embeddings.

    Inputs:
      self.images

    Outputs:
      self.image_embeddings
    """
    print("tl : Build Image Embeddings = InceptionV3 + Dense Layer / uses SlimNetsLayer and DenseLayer instead")

    with slim.arg_scope(inception_v3_arg_scope()):
        net_img_in = tl.layers.InputLayer(images, name='input_image_layer')
        network = tl.layers.SlimNetsLayer(layer=net_img_in, slim_layer=inception_v3,
                                          slim_args= {
                                                 'trainable' : train_inception,
                                                 'is_training' : mode == 'train',
                                                },
                                            name='',
                                            )
    network = tl.layers.DenseLayer(network,
                                    n_units = embedding_size,
                                    act = tf.identity,
                                    W_init = initializer,
                                    b_init = None,          # no biases
                                    name='image_embedding')
    return network
def main(): 
    # 加 载 预 处 理 好 的 数 据 
    processed_data= np.load(INPUT_DATA, allow_pickle=True) 
    training_images= processed_data[0] 
    n_training_example= len(training_images) 
    training_labels= processed_data[1] 
    validation_images= processed_data[2] 
    validation_labels= processed_data[3] 
    testing_images= processed_data[4] 
    testing_labels= processed_data[5] 
    print("%d training examples, %d validation examples and %d testing examples." % (n_training_example, len(validation_labels), len(testing_labels)))
    # 定 义 inception-v3 的 输 入 , images 为 输 入 图 片 , labels 为 每 一 张 图 片 对 应 的 标 签 
    images = tf.placeholder(tf.float32, [None, 299, 299, 3], name='Input_images')
    labels = tf.placeholder(tf.int64, [None], name='labels')
    
    # 定 义 inception-v3 模 型 
    with slim.arg_scope(inception_v3.inception_v3_arg_scope()): 
        logits, _ = inception_v3.inception_v3(images, num_classes=N_CLASSES)
    # 获 取 需 要 训 练 的 变 量 
    trainable_variables= get_trainable_variables() 
    # 定 义 交 叉 熵 损 失 
    tf.losses.softmax_cross_entropy(tf.one_hot(labels, N_CLASSES), logits, weights=1.0) 
    # 定 义 训 练 过 程 
    train_step= tf.train.RMSPropOptimizer(LEARNING_RATE).minimize(tf.losses.get_total_loss())
    # 计 算 正 确 率 
    with tf.name_scope('evaluation'): 
        correct_prediction= tf.equal(tf.argmax(logits, 1), labels) 
        evaluation_step= tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    # 定 义 加 载 模 型 的 函 数 
    load_fn= slim.assign_from_checkpoint_fn(CKPT_FILE, get_tuned_variables(), ignore_missing_vars=True)

    #定义保存新的训练好的模型的函数 
    saver = tf.train.Saver() 
    with tf.Session() as sess: 
        # 初 始 化 没 有 加 载 进 来 的 变 量 
        init= tf.global_variables_initializer() 
        sess.run(init)
        #加 载 谷 歌 已 经 训 练 好 的 模 型 
        print('Loading tuned variables from%s' % CKPT_FILE) 
        load_fn(sess) 
        start= 0 
        end= BATCH 
        for i in range(STEPS): 
            # 运 行 训 练 过 程 , 这 里 不 会 更 新 全 部 的 参 数 , 只 会 更 新 指 定 的 部 分 参 数 
            sess.run(train_step, feed_dict={ images: training_images[start:end], labels: training_labels[start:end] })
            # 输 出 日 志 
            if i% 2 == 0 or i+ 1 == STEPS: 
                #saver.save(sess, TRAIN_FILE, global_step = i)
                validation_accuracy= sess.run(evaluation_step, feed_dict={ images: validation_images, labels: validation_labels }) 
                print('Step %d: Validation accuracy = %.lf%%' % (i, validation_accuracy* 100.0))
            # 因 为 在 数 据 预 处 理 的 时 候 已 经 做 过 了 打 乱 数 据 的 操 作 , 所 以 这 里 只 需 要 顺 序 使 用 训 练 数 据 
            start = end 
            if start == n_training_example: 
                start = 0 
            end = start + BATCH 
            if end > n_training_example: 
                end = n_training_example
        # 在 后 的 测 试 数 据 上 测 试 正 确 率 
        test_accuracy= sess.run(evaluation_step, feed_dict={ images: testing_images, labels: testing_labels }) 
        print('Final test accuracy = %.lf%%' % (test_accuracy* 100))
Exemplo n.º 9
0
    def _build_net(self):

        batch_norm_params = {
            "is_training": self._cnn_trainable,
            "trainable": self._cnn_trainable,
            # Decay for the moving averages.
            "decay": 0.9997,
            # Epsilon to prevent 0s in variance.
            "epsilon": 0.001,
            # Collection containing the moving mean and moving variance.
            "variables_collections": {
                "beta": None,
                "gamma": None,
                "moving_mean": ["moving_vars"],
                "moving_variance": ["moving_vars"],
            }
        }
        with slim.arg_scope(inception_v3.inception_v3_arg_scope(weight_decay=self._weight_decay, stddev=self._stddev)):

            with tf.variable_scope(self.scope_name, 'InceptionV3', [self._inputs]) as scope:
                with slim.arg_scope(
                        [slim.conv2d],
                        normalizer_fn=slim.batch_norm,
                        normalizer_params=batch_norm_params):
                    with slim.arg_scope([slim.conv2d, slim.fully_connected], trainable=self._cnn_trainable):
                        net, end_points = inception_v3.inception_v3_base(self._inputs, scope=scope)
                    net = slim.avg_pool2d(net, net.get_shape()[1:3], padding='VALID', scope='AvgPool_1a')
        return net, end_points
Exemplo n.º 10
0
    def _image_to_vec(image_str_tensor):
        def _decode_and_resize(image_tensor):
            """Decodes jpeg string, resizes it and returns a uint8 tensor."""

            # These constants are set by Inception v3's expectations.
            height = 299
            width = 299
            channels = 3

            image_tensor = tf.where(tf.equal(image_tensor, ''),
                                    IMAGE_DEFAULT_STRING, image_tensor)

            # Fork by whether image_tensor value is a file path, or a base64 encoded string.
            slash_positions = tf.equal(
                tf.string_split([image_tensor], delimiter="").values, '/')
            is_file_path = tf.cast(tf.count_nonzero(slash_positions), tf.bool)

            # The following two functions are required for tf.cond. Note that we can not replace them
            # with lambda. According to TF docs, if using inline lambda, both branches of condition
            # will be executed. The workaround is to use a function call.
            def _read_file():
                return tf.read_file(image_tensor)

            def _decode_base64():
                return tf.decode_base64(image_tensor)

            image = tf.cond(is_file_path, lambda: _read_file(),
                            lambda: _decode_base64())
            image = tf.image.decode_jpeg(image, channels=channels)
            image = tf.expand_dims(image, 0)
            image = tf.image.resize_bilinear(image, [height, width],
                                             align_corners=False)
            image = tf.squeeze(image, squeeze_dims=[0])
            image = tf.cast(image, dtype=tf.uint8)
            return image

        # The CloudML Prediction API always "feeds" the Tensorflow graph with
        # dynamic batch sizes e.g. (?,).  decode_jpeg only processes scalar
        # strings because it cannot guarantee a batch of images would have
        # the same output size.  We use tf.map_fn to give decode_jpeg a scalar
        # string from dynamic batches.
        image = tf.map_fn(_decode_and_resize,
                          image_str_tensor,
                          back_prop=False,
                          dtype=tf.uint8)
        image = tf.image.convert_image_dtype(image, dtype=tf.float32)
        # "gradients_[feature_name]" will be used for computing integrated gradients.
        image = tf.identity(image, name='gradients_' + feature_name)
        image = tf.subtract(image, 0.5)
        inception_input = tf.multiply(image, 2.0)

        # Build Inception layers, which expect a tensor of type float from [-1, 1)
        # and shape [batch_size, height, width, channels].
        with tf.contrib.slim.arg_scope(inception_v3_arg_scope()):
            _, end_points = inception_v3(inception_input, is_training=False)

        embeddings = end_points['PreLogits']
        inception_embeddings = tf.squeeze(embeddings, [1, 2],
                                          name='SpatialSqueeze')
        return inception_embeddings
Exemplo n.º 11
0
    def build_inception_graph(self):
        """Builds an inception graph and add the necessary input & output tensors.

      To use other Inception models modify this file. Also preprocessing must be
      modified accordingly.

      See tensorflow/contrib/slim/python/slim/nets/inception_v3.py for
      details about InceptionV3.

    Returns:
      input_jpeg: A placeholder for jpeg string batch that allows feeding the
                  Inception layer with image bytes for prediction.
      inception_embeddings: The embeddings tensor.
    """

        # These constants are set by Inception v3's expectations.
        height = 299
        width = 299
        channels = 3

        image_str_tensor = tf.placeholder(tf.string, shape=[None])

        # The CloudML Prediction API always "feeds" the Tensorflow graph with
        # dynamic batch sizes e.g. (?,).  decode_jpeg only processes scalar
        # strings because it cannot guarantee a batch of images would have
        # the same output size.  We use tf.map_fn to give decode_jpeg a scalar
        # string from dynamic batches.
        def decode_and_resize(image_str_tensor):
            """Decodes jpeg string, resizes it and returns a uint8 tensor."""
            image = tf.image.decode_jpeg(image_str_tensor, channels=channels)
            # Note resize expects a batch_size, but tf_map supresses that index,
            # thus we have to expand then squeeze.  Resize returns float32 in the
            # range [0, uint8_max]
            image = tf.expand_dims(image, 0)
            image = tf.image.resize_bilinear(image, [height, width],
                                             align_corners=False)
            image = tf.squeeze(image, squeeze_dims=[0])
            image = tf.cast(image, dtype=tf.uint8)
            return image

        image = tf.map_fn(decode_and_resize,
                          image_str_tensor,
                          back_prop=False,
                          dtype=tf.uint8)
        # convert_image_dtype, also scales [0, uint8_max] -> [0 ,1).
        image = tf.image.convert_image_dtype(image, dtype=tf.float32)

        # Then shift images to [-1, 1) for Inception.
        image = tf.sub(image, 0.5)
        image = tf.mul(image, 2.0)

        # Build Inception layers, which expect A tensor of type float from [-1, 1)
        # and shape [batch_size, height, width, channels].
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            _, end_points = inception.inception_v3(image, is_training=False)

        inception_embeddings = end_points['PreLogits']
        inception_embeddings = tf.squeeze(inception_embeddings, [1, 2],
                                          name='SpatialSqueeze')
        return image_str_tensor, inception_embeddings
Exemplo n.º 12
0
  def build_inception_graph(self):
    """Builds an inception graph and add the necessary input & output tensors.

      To use other Inception models modify this file. Also preprocessing must be
      modified accordingly.

      See tensorflow/contrib/slim/python/slim/nets/inception_v3.py for
      details about InceptionV3.

    Returns:
      input_jpeg: A placeholder for jpeg string batch that allows feeding the
                  Inception layer with image bytes for prediction.
      inception_embeddings: The embeddings tensor.
    """

    # These constants are set by Inception v3's expectations.
    height = 299
    width = 299
    channels = 3

    image_str_tensor = tf.placeholder(tf.string, shape=[None])

    # The CloudML Prediction API always "feeds" the Tensorflow graph with
    # dynamic batch sizes e.g. (?,).  decode_jpeg only processes scalar
    # strings because it cannot guarantee a batch of images would have
    # the same output size.  We use tf.map_fn to give decode_jpeg a scalar
    # string from dynamic batches.
    def decode_and_resize(image_str_tensor):
      """Decodes jpeg string, resizes it and returns a uint8 tensor."""
      image = tf.image.decode_jpeg(image_str_tensor, channels=channels)
      # Note resize expects a batch_size, but tf_map supresses that index,
      # thus we have to expand then squeeze.  Resize returns float32 in the
      # range [0, uint8_max]
      image = tf.expand_dims(image, 0)
      image = tf.image.resize_bilinear(
          image, [height, width], align_corners=False)
      image = tf.squeeze(image, squeeze_dims=[0])
      image = tf.cast(image, dtype=tf.uint8)
      return image

    image = tf.map_fn(
        decode_and_resize, image_str_tensor, back_prop=False, dtype=tf.uint8)
    # convert_image_dtype, also scales [0, uint8_max] -> [0 ,1).
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)

    # Then shift images to [-1, 1) for Inception.
    image = tf.subtract(image, 0.5)
    image = tf.multiply(image, 2.0)

    # Build Inception layers, which expect A tensor of type float from [-1, 1)
    # and shape [batch_size, height, width, channels].
    with slim.arg_scope(inception.inception_v3_arg_scope()):
      _, end_points = inception.inception_v3(image, is_training=False)

    inception_embeddings = end_points['PreLogits']
    inception_embeddings = tf.squeeze(
        inception_embeddings, [1, 2], name='SpatialSqueeze')
    return image_str_tensor, inception_embeddings
Exemplo n.º 13
0
 def _network_define(num_classes):
     with tf.variable_scope("input"):
         images = tf.placeholder(dtype=tf.float32,
                                 shape=[None, 299, 299, 3],
                                 name="input-image")
         labels = tf.placeholder(dtype=tf.int32,
                                 shape=[None],
                                 name="input-label")
     arg_scope = inception_v3.inception_v3_arg_scope()
     print("arg_scope:")
     for k, v in arg_scope.items():
         print(k)
         for k2, v2 in v.items():
             print("  ", k2, v2)
     with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
         logits, _ = inception_v3.inception_v3(images,
                                               num_classes=num_classes)
     return images, labels, logits
    def build_graph(self):
        """Forms the core by building a wrapper around the inception graph.

      Here we add the necessary input & output tensors, to decode jpegs,
      serialize embeddings, restore from checkpoint etc.

      To use other Inception models modify this file. Note that to use other
      models beside Inception, you should make sure input_shape matches
      their input. Resizing or other modifications may be necessary as well.
      See tensorflow/contrib/slim/python/slim/nets/inception_v3.py for
      details about InceptionV3.

    Returns:
      input_jpeg: A tensor containing raw image bytes as the input layer.
      embedding: The embeddings tensor, that will be materialized later.
    """

        input_jpeg = tf.placeholder(tf.string, shape=None)
        image = tf.image.decode_jpeg(input_jpeg, channels=self.CHANNELS)

        # Augmentation
        print("Function Definition")

        def color(x):
            x = tf.image.random_hue(x, 0.08)
            x = tf.image.random_saturation(x, 0.6, 1.6)
            x = tf.image.random_brightness(x, 0.05)
            x = tf.image.random_contrast(x, 0.7, 1.3)
            return x

        print("Done")

        image = color(image)

        # Note resize expects a batch_size, but we are feeding a single image.
        # So we have to expand then squeeze.  Resize returns float32 in the
        # range [0, uint8_max]
        image = tf.expand_dims(image, 0)

        # convert_image_dtype also scales [0, uint8_max] -> [0 ,1).
        image = color(image)
        image = tf.image.convert_image_dtype(image, dtype=tf.float32)
        image = tf.image.resize_bilinear(image, [self.HEIGHT, self.WIDTH],
                                         align_corners=False)

        # Then rescale range to [-1, 1) for Inception.
        image = tf.subtract(image, 0.5)
        inception_input = tf.multiply(image, 2.0)

        # Build Inception layers, which expect a tensor of type float from [-1, 1)
        # and shape [batch_size, height, width, channels].
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            _, end_points = inception.inception_v3(inception_input,
                                                   is_training=False)

        embedding = end_points['PreLogits']
        return input_jpeg, embedding
Exemplo n.º 15
0
 def testModelHasExpectedNumberOfParameters(self):
   batch_size = 5
   height, width = 299, 299
   inputs = random_ops.random_uniform((batch_size, height, width, 3))
   with arg_scope(inception_v3.inception_v3_arg_scope()):
     inception_v3.inception_v3_base(inputs)
   total_params, _ = model_analyzer.analyze_vars(
       variables_lib.get_model_variables())
   self.assertAlmostEqual(21802784, total_params)
Exemplo n.º 16
0
 def testModelHasExpectedNumberOfParameters(self):
     batch_size = 5
     height, width = 299, 299
     inputs = random_ops.random_uniform((batch_size, height, width, 3))
     with arg_scope(inception_v3.inception_v3_arg_scope()):
         inception_v3.inception_v3_base(inputs)
     total_params, _ = model_analyzer.analyze_vars(
         variables_lib.get_model_variables())
     self.assertAlmostEqual(21802784, total_params)
Exemplo n.º 17
0
 def fine_tune_inception(self):
     if self.dataset == 'DRIMDB':
         self.images = tf.placeholder(tf.float32, [None, 299, 299, self.c],
                                      name='input_image')
         self.labels = tf.placeholder(tf.int64, [None], name='labels')
         with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
             self.logits, _ = inception_v3.inception_v3(
                 self.images, num_classes=self.class_num, is_training=False)
         #trainable_variables = get_trainable_variables('InceptionV3/Logits,InceptionV3/AuxLogits')
         print('Loading tuned variables from %s' % self.ckpt)
def build_network(image_input, num_classes=1001, is_training=False):
    net_in = tl.layers.InputLayer(image_input, name='input_layer')
    with slim.arg_scope(inception_v3_arg_scope()):
        network = tl.layers.SlimNetsLayer(
            prev_layer=net_in, slim_layer=inception_v3, slim_args={
                'num_classes': num_classes,
                'is_training': is_training
            }, name='InceptionV3')

    predictions = tf.nn.sigmoid(network.outputs, name='Predictions')
    return network, predictions
Exemplo n.º 19
0
def main(self):
    process_data=np.load(INPUT_DATA)
    training_images=process_data[0]
    n_training_example=len(training_images)
    training_labels=process_data[1]
    validation_images=process_data[2]
    validation_labels=process_data[3]
    testing_images=process_data[4]
    testing_labels=process_data[5]

    print("%d training examples,%d validation examples and %d testing examples."%(n_training_example,len(validation_labels),len(testing_labels)))
    images=tf.placeholder(tf.float32,[None,299,299,3],name='input_images')
    labels=tf.placeholder(tf.int64,[None],name='labels')
    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
         logits,_=inception_v3.inception_v3(images,num_classes=N_CLASS)
         trainable_variables=get_trainable_variables()
    tf.losses.softmax_cross_entropy(tf.one_hot(labels,N_CLASS),logits,weights=1.0)
    train_step=tf.train.RMSPropOptimizer(LEARNING_RTAE).minimize(tf.losses.get_total_loss())

    with tf.name_scope('evaluation'):
        correct_prediction=tf.equal(tf.argmax(logits,1),labels)
        evaluation_step=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    load_fn=slim.assign_from_checkpoint_fn(CKPT_FILE,get_tuned_variables(),ignore_missing_vars=True)
    saver =tf.train.saver()
    with tf.Session() as sess:
        init=tf.global_variables_initializer()
        sess.run(init)
        print('Loading tuned variables from %s'%CKPT_FILE)
        load_fn(sess)

        start =0
        end =BATCH
        for i in range(STEPS):
            sess.run(train_step,feed_dict={
                images:training_images[start:end],
                labels:training_labels[start:end]
            })
            if i%30==0 or i+1==STEPS:
                saver.save(sess,TRAIN_FILE,global_step=i)
                validation_acuracy=sess.run(evaluation_step,feed_dict={
                    images:validation_images,
                    labels:validation_labels
                })
                print('Step %d:Validation accuracy=%.lf%'%(i,validation_acuracy*100))
                start =end
                if start==n_training_example:
                    start=0
                    end=start+BATCH
                if end >n_training_example:
                     end=n_training_example
            test_accuary=sess.run(evaluation_step,feed_dict={
                images:testing_images,labels:testing_labels
             })
            print('Final test accuracy%.lf%' % (test_accuary * 100))
Exemplo n.º 20
0
def build_network(image_input, num_classes=1001, is_training=False):
    net_in = tl.layers.InputLayer(image_input, name='input_layer')
    with slim.arg_scope(inception_v3_arg_scope()):
        network = tl.layers.SlimNetsLayer(
            layer=net_in, slim_layer=inception_v3, slim_args={
                'num_classes': num_classes,
                'is_training': is_training
            }, name='InceptionV3')

    predictions = tf.nn.sigmoid(network.outputs, name='Predictions')
    return network, predictions
Exemplo n.º 21
0
def main(argv=None):
    processed_data = np.load(input_data)
    training_images = processed_data[0]
    training_labels = processed_data[1]
    n_training_example = len(training_labels)
    validation_images = processed_data[2]
    validation_labels = processed_data[3]
    testing_images = processed_data[4]
    testing_labels = processed_data[5]
    print('%d training examples, %d validation examples and %d testing examples.'
          % (n_training_example, len(validation_labels), len(testing_labels)))

    images = tf.placeholder(tf.float32, [None, 299, 299, 3], name='input_images')
    labels = tf.placeholder(tf.int64, [None], 'labels')

    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        logits, _ = inception_v3.inception_v3(images, num_classes=n_classes)

    training_variables = get_trainable_variables()
    tf.losses.softmax_cross_entropy(tf.one_hot(labels, n_classes), logits, weights=1.0)
    train_step = tf.train.RMSPropOptimizer(learning_rate).minimize(tf.losses.get_total_loss())

    with tf.name_scope('evaluation'):
        correct_prediction = tf.equal(tf.argmax(logits, 1), labels)
        evaluation_step = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    load_fn = slim.assign_from_checkpoint_fn(ckpt_file, get_tuned_variables(), ignore_missing_vars=True)

    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        print('loading tuned variables from %s' % ckpt_file)
        load_fn(sess)

        start = 0
        end = batch
        for i in range(steps):
            sess.run(train_step, feed_dict={
                images: training_images[start:end],
                labels: training_labels[start:end]})

            if i % 30 == 0 or i+1 == steps:
                saver.save(sess, train_file, global_step=i)
                validation_accuracy = sess.run(evaluation_step, feed_dict={
                    images: validation_images, labels: validation_labels})
                print('step %d: Vaildation accuracy = %.1f%%' % (i, validation_accuracy*100.0))
            if start == n_training_example:
                start = 0
            end = start + batch
            if end > n_training_example:
                end = n_training_example

        test_accuary = sess.run(evaluation_step, feed_dict={
            images: testing_images, labels: testing_labels})
        print('Final test accuracy = %.1f%%' % (test_accuary*100.0))
Exemplo n.º 22
0
def inception_v3_model(image, is_training):
    saver = tf.train.Saver()
    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        net, end_points = inception_v3.inception_v3(image, num_classes=None)
        with tf.variable_scope('Logits'):
            net = slim.flatten(net, scopre='flatten')
            predictions = slim.fully_connected(net,
                                               1,
                                               activation_fn=None,
                                               scope='output')
            return predictions
def inception(image, is_train=True, reuse=tf.AUTO_REUSE):
    preprocessed = tf.multiply(tf.subtract(image / 255, 0.5), 2.0)
    arg_scope = inception_v3_arg_scope()
    with slim.arg_scope(arg_scope):
        logits, end_point = inception_v3(preprocessed,
                                         1001,
                                         is_training=is_train,
                                         reuse=reuse)
        logits = logits[:, 1:]  # ignore background class
        probs = tf.nn.softmax(logits)  # probabilities
    return logits, probs, end_point
Exemplo n.º 24
0
  def _image_to_vec(image_str_tensor):

    def _decode_and_resize(image_tensor):
      """Decodes jpeg string, resizes it and returns a uint8 tensor."""

      # These constants are set by Inception v3's expectations.
      height = 299
      width = 299
      channels = 3

      image_tensor = tf.where(tf.equal(image_tensor, ''), IMAGE_DEFAULT_STRING, image_tensor)

      # Fork by whether image_tensor value is a file path, or a base64 encoded string.
      slash_positions = tf.equal(tf.string_split([image_tensor], delimiter="").values, '/')
      is_file_path = tf.cast(tf.count_nonzero(slash_positions), tf.bool)

      # The following two functions are required for tf.cond. Note that we can not replace them
      # with lambda. According to TF docs, if using inline lambda, both branches of condition
      # will be executed. The workaround is to use a function call.
      def _read_file():
        return tf.read_file(image_tensor)

      def _decode_base64():
        return tf.decode_base64(image_tensor)

      image = tf.cond(is_file_path, lambda: _read_file(), lambda: _decode_base64())
      image = tf.image.decode_jpeg(image, channels=channels)
      image = tf.expand_dims(image, 0)
      image = tf.image.resize_bilinear(image, [height, width], align_corners=False)
      image = tf.squeeze(image, squeeze_dims=[0])
      image = tf.cast(image, dtype=tf.uint8)
      return image

    # The CloudML Prediction API always "feeds" the Tensorflow graph with
    # dynamic batch sizes e.g. (?,).  decode_jpeg only processes scalar
    # strings because it cannot guarantee a batch of images would have
    # the same output size.  We use tf.map_fn to give decode_jpeg a scalar
    # string from dynamic batches.
    image = tf.map_fn(_decode_and_resize, image_str_tensor, back_prop=False, dtype=tf.uint8)
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)
    # "gradients_[feature_name]" will be used for computing integrated gradients.
    image = tf.identity(image, name='gradients_' + feature_name)
    image = tf.subtract(image, 0.5)
    inception_input = tf.multiply(image, 2.0)

    # Build Inception layers, which expect a tensor of type float from [-1, 1)
    # and shape [batch_size, height, width, channels].
    with tf.contrib.slim.arg_scope(inception_v3_arg_scope()):
      _, end_points = inception_v3(inception_input, is_training=False)

    embeddings = end_points['PreLogits']
    inception_embeddings = tf.squeeze(embeddings, [1, 2], name='SpatialSqueeze')
    return inception_embeddings
Exemplo n.º 25
0
    def inference(self):

        x = tf.reshape(self.x,
                       shape=[
                           -1, self.input_shape[0], self.input_shape[1],
                           self.input_shape[2]
                       ])
        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            logits, end_points = inception_v3.inception_v3(
                x,
                num_classes=self.nclasses,
                is_training=self.is_training,
                spatial_squeeze=True)
        return logits
Exemplo n.º 26
0
    def build_model(self, is_training=True, dropout_keep_prob=0.5):
        self.inputs = tf.placeholder(real_type(self.FLAGS),
                                     [self.FLAGS.batch_size, 299, 299, 3])
        self.targets = tf.placeholder(tf.int32, [self.FLAGS.batch_size])

        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            logits, endpoints = inception_v3.inception_v3(
                self.inputs, self.FLAGS.num_classes)
        loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
            logits=logits, labels=self.targets)
        self.cost = tf.reduce_sum(loss)
        self.global_step = tf.contrib.framework.get_or_create_global_step()
        self.train_op = tf.train.AdagradOptimizer(0.01).minimize(
            loss, global_step=self.global_step)
Exemplo n.º 27
0
    def output_logits(self, num_labels, net=None, scope='Logits', reuse=tf.AUTO_REUSE):

        net = net if net is not None else self.net

        with slim.arg_scope(inception_v3.inception_v3_arg_scope(weight_decay=self._weight_decay, stddev=self._stddev)):
            with tf.variable_scope(self.scope_name, 'InceptionV3', reuse=reuse):
                with tf.variable_scope(scope):

                    logits = slim.dropout(net, keep_prob=self._keep_prob, is_training=self._is_training,
                                          scope='Dropout_1b')
                    logits = slim.conv2d(logits, num_labels, [1, 1], activation_fn=None,
                                         normalizer_fn=None, scope='Conv2d_1c_1x1')
                    logits = slim.flatten(logits, scope="flatten")
        return logits
def main(_):
    # start_time = datetime.datetime.now()
    label_file_name = os.path.join(args.label_dir, "labels.txt")
    label_dict = get_class_label_dict(label_file_name)
    classes_num = len(label_dict)
    test_feeder = DataIterator(data_dir=args.dataset_path)
    total_size = len(test_feeder.labels)
    count = 0
    # get model from model registry
    model_path = Model.get_model_path(args.model_name)
    with tf.Session() as sess:
        test_images = test_feeder.input_pipeline(batch_size=args.batch_size)
        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            input_images = tf.placeholder(
                tf.float32,
                [args.batch_size, image_size, image_size, num_channel])
            logits, _ = inception_v3.inception_v3(input_images,
                                                  num_classes=classes_num,
                                                  is_training=False)
            probabilities = tf.argmax(logits, 1)

        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        saver = tf.train.Saver()
        saver.restore(sess, model_path)
        out_filename = os.path.join(args.output_dir, "result-labels.txt")
        with open(out_filename, "w") as result_file:
            i = 0
            while count < total_size and not coord.should_stop():
                test_images_batch = sess.run(test_images)
                file_names_batch = test_feeder.file_paths[
                    i * args.batch_size:min(test_feeder.size, (i + 1) *
                                            args.batch_size)]
                results = sess.run(probabilities,
                                   feed_dict={input_images: test_images_batch})
                new_add = min(args.batch_size, total_size - count)
                count += new_add
                i += 1
                for j in range(new_add):
                    result_file.write(
                        os.path.basename(file_names_batch[j]) + ": " +
                        label_dict[results[j]] + "\n")
                result_file.flush()
            coord.request_stop()
            coord.join(threads)

        # copy the file to artifacts
        shutil.copy(out_filename, "./outputs/")
Exemplo n.º 29
0
    def build_graph(self):
        """Forms the core by building a wrapper around the inception graph.

      Here we add the necessary input & output tensors, to decode jpegs,
      serialize embeddings, restore from checkpoint etc.

      To use other Inception models modify this file. Note that to use other
      models beside Inception, you should make sure input_shape matches
      their input. Resizing or other modifications may be necessary as well.
      See tensorflow/contrib/slim/python/slim/nets/inception_v3.py for
      details about InceptionV3.

    Returns:
      input_jpeg: A tensor containing raw image bytes as the input layer.
      embedding: The embeddings tensor, that will be materialized later.
    """

        distorted_version = tf.placeholder(tf.int32, name='distorted_version')
        input_jpeg = tf.placeholder(tf.string, shape=None)
        image = tf.image.decode_jpeg(input_jpeg, channels=self.CHANNELS)

        # convert_image_dtype also scales [0, uint8_max] -> [0 ,1).
        image = tf.image.convert_image_dtype(image, dtype=tf.float32)

        # distort based on distorted_version
        case_arms = {}
        for x in range(DISTORT_IMAGE_COUNT):
            guard, value = self.case_arm(x, distorted_version, image)
            case_arms[guard] = value
        image = tf.case(case_arms, default=lambda: image, exclusive=True)
        image.set_shape([self.HEIGHT, self.WIDTH, self.CHANNELS])

        # Then rescale range to [-1, 1) for Inception.
        image = tf.subtract(image, 0.5)
        inception_input = tf.multiply(image, 2.0)

        # Note resize expects a batch_size, but we are feeding a single image.
        # So we have to expand then squeeze.  Resize returns float32 in the
        # range [0, uint8_max]
        inception_input = tf.expand_dims(inception_input, 0)

        # Build Inception layers, which expect a tensor of type float from [-1, 1)
        # and shape [batch_size, height, width, channels].
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            _, end_points = inception.inception_v3(inception_input,
                                                   is_training=False)

        embedding = end_points['PreLogits']
        return input_jpeg, distorted_version, embedding
Exemplo n.º 30
0
def training_start():  #主要函数

    #下面创建inception_v3模型的结构和其所有变量。
    with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
        logits, _ = inception_v3.inception_v3(images, number_of_calsses)

    for var in tf.trainable_variables():  #区分可训练变量
        if var.op.name.startswith(
                trainable_scopes[0]) or var.op.name.startswith(
                    trainable_scopes[1]):
            tf.add_to_collection("trainable_variables_for_now", var)
        else:
            non_trainable_variables.append(var)

    tf.GraphKeys.TRAINABLE_VARIABLES = "trainable_variables_for_now"  #改变之前的可训练变量集合  ②

    load_fn = slim.assign_from_checkpoint_fn(ckpt_file,
                                             non_trainable_variables,
                                             True)  #读取不可训练变量

    tf.losses.softmax_cross_entropy(tf.one_hot(labels, number_of_calsses),
                                    logits)  #添加损失。原模型里已经有正则项。
    training_step = tf.train.RMSPropOptimizer(learning_rate).minimize(
        tf.losses.get_total_loss())  #定义优化训练步骤
    #print(tf.get_collection("losses"))  #③
    correct_prediction = tf.equal(tf.argmax(logits, 1), labels)
    evaluation_step = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    saver = tf.train.Saver()

    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    load_fn(sess)

    for i in range(steps):
        segmented_batch_training(sess, training_step, batch,
                                 training_file_pointer)
        if (i + 1) % 30 == 0:
            #saver.save(sess, output_file, global_step=i)  #保存checkpoint模型结构与数据
            print(
                "%d-%dth iteration is passed with validation accuracy of %f" %
                (i - 28, i + 1,
                 segmented_evaluation(sess, evaluation_step, True)))

    print("Final test accuracy is %f" %
          (segmented_evaluation(sess, evaluation_step, False)))
    def __call__(self, inputs):

        # Input Layers
        net_in = tl.layers.InputLayer(inputs, name='input_layer')

        with slim.arg_scope(inception_v3_arg_scope()):

            network = tl.layers.SlimNetsLayer(prev_layer=net_in,
                                              slim_layer=inception_v3,
                                              slim_args={
                                                  'num_classes': 1001,
                                                  'is_training': False,
                                              },
                                              name='InceptionV3')

            self.network = network
            self.network.print_params(False)

            conv_layers = [
                "InceptionV3/InceptionV3/Conv2d_1a_3x3/Relu",
                "InceptionV3/InceptionV3/Conv2d_2a_3x3/Relu",
                "InceptionV3/InceptionV3/Conv2d_2b_3x3/Relu",
                "InceptionV3/InceptionV3/Conv2d_3b_1x1/Relu",
                "InceptionV3/InceptionV3/Conv2d_4a_3x3/Relu",
                "InceptionV3/InceptionV3/Mixed_5b/concat",
                "InceptionV3/InceptionV3/Mixed_5c/concat",
                "InceptionV3/InceptionV3/Mixed_5d/concat",
                "InceptionV3/InceptionV3/Mixed_6a/concat",
                "InceptionV3/InceptionV3/Mixed_6b/concat",
                "InceptionV3/InceptionV3/Mixed_6c/concat",
                "InceptionV3/InceptionV3/Mixed_6d/concat",
                "InceptionV3/InceptionV3/Mixed_6e/concat",
                "InceptionV3/InceptionV3/Mixed_7a/concat",
                "InceptionV3/InceptionV3/Mixed_7b/concat",
                "InceptionV3/InceptionV3/Mixed_7c/concat",
            ]

            conv_outs = [
                tl.layers.get_layers_with_name(self.network,
                                               name=layer_name,
                                               printable=False)[0]
                for layer_name in conv_layers
            ]

            return network, conv_outs
Exemplo n.º 32
0
    def __init__(self,
                 tensor,
                 keep_prob=1.0,
                 num_classes=1001,
                 retrain_layer=[],
                 weights_path='./weights/inception_v3.ckpt'):
        # Call the parent class
        Model.__init__(self, tensor, keep_prob, num_classes, retrain_layer,
                       weights_path)

        # TODO This implementation has a problem while validation (is still set to training)
        is_training = True if retrain_layer else False
        with slim.arg_scope(inception_v3_arg_scope()):
            self.final, self.endpoints = inception_v3(
                self.tensor,
                num_classes=num_classes,
                is_training=is_training,
                dropout_keep_prob=keep_prob)
def main(_):
    # start_time = datetime.datetime.now()
    label_file_name = os.path.join(args.label_dir, "labels.txt")
    label_dict = get_class_label_dict(label_file_name)
    classes_num = len(label_dict)
    test_feeder = DataIterator(data_dir=args.dataset_path)
    total_size = len(test_feeder.labels)
    count = 0
    # get model from model registry
    model_path = Model.get_model_path(args.model_name)
    with tf.Session() as sess:
        test_images = test_feeder.input_pipeline(batch_size=args.batch_size)
        with slim.arg_scope(inception_v3.inception_v3_arg_scope()):
            input_images = tf.placeholder(tf.float32, [args.batch_size, image_size, image_size, num_channel])
            logits, _ = inception_v3.inception_v3(input_images,
                                                  num_classes=classes_num,
                                                  is_training=False)
            probabilities = tf.argmax(logits, 1)

        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        saver = tf.train.Saver()
        saver.restore(sess, model_path)
        out_filename = os.path.join(args.output_dir, "result-labels.txt")
        with open(out_filename, "w") as result_file:
            i = 0
            while count < total_size and not coord.should_stop():
                test_images_batch = sess.run(test_images)
                file_names_batch = test_feeder.file_paths[i * args.batch_size:
                                                          min(test_feeder.size, (i + 1) * args.batch_size)]
                results = sess.run(probabilities, feed_dict={input_images: test_images_batch})
                new_add = min(args.batch_size, total_size - count)
                count += new_add
                i += 1
                for j in range(new_add):
                    result_file.write(os.path.basename(file_names_batch[j]) + ": " + label_dict[results[j]] + "\n")
                result_file.flush()
            coord.request_stop()
            coord.join(threads)

        # copy the file to artifacts
        shutil.copy(out_filename, "./outputs/")
Exemplo n.º 34
0
    def _image_to_vec(image_str_tensor):
        def _decode_and_resize(image_str_tensor):
            """Decodes jpeg string, resizes it and returns a uint8 tensor."""

            # These constants are set by Inception v3's expectations.
            height = 299
            width = 299
            channels = 3

            image = tf.where(tf.equal(image_str_tensor, ''),
                             IMAGE_DEFAULT_STRING, image_str_tensor)
            image = tf.decode_base64(image)
            image = tf.image.decode_jpeg(image, channels=channels)
            image = tf.expand_dims(image, 0)
            image = tf.image.resize_bilinear(image, [height, width],
                                             align_corners=False)
            image = tf.squeeze(image, squeeze_dims=[0])
            image = tf.cast(image, dtype=tf.uint8)
            return image

        # The CloudML Prediction API always "feeds" the Tensorflow graph with
        # dynamic batch sizes e.g. (?,).  decode_jpeg only processes scalar
        # strings because it cannot guarantee a batch of images would have
        # the same output size.  We use tf.map_fn to give decode_jpeg a scalar
        # string from dynamic batches.
        image = tf.map_fn(_decode_and_resize,
                          image_str_tensor,
                          back_prop=False,
                          dtype=tf.uint8)
        image = tf.image.convert_image_dtype(image, dtype=tf.float32)
        image = tf.subtract(image, 0.5)
        inception_input = tf.multiply(image, 2.0)

        # Build Inception layers, which expect a tensor of type float from [-1, 1)
        # and shape [batch_size, height, width, channels].
        with tf.contrib.slim.arg_scope(inception_v3_arg_scope()):
            _, end_points = inception_v3(inception_input, is_training=False)

        embeddings = end_points['PreLogits']
        inception_embeddings = tf.squeeze(embeddings, [1, 2],
                                          name='SpatialSqueeze')
        return inception_embeddings
Exemplo n.º 35
0
def inception_layer(net_in, reuse=None):
    with slim.arg_scope(inception_v3_arg_scope()):
        ## Alternatively, you should implement inception_v3 without TensorLayer as follow.
        # logits, end_points = inception_v3(X, num_classes=1001,
        #                                   is_training=False)
        network = tl.layers.SlimNetsLayer(layer=net_in, slim_layer=inception_v3,
                                          slim_args= {
                                              'num_classes' : 1001,
                                              'is_training' : False,
                                              #  'dropout_keep_prob' : 0.8,       # for training
                                              #  'min_depth' : 16,
                                              #  'depth_multiplier' : 1.0,
                                              #  'prediction_fn' : slim.softmax,
                                              #  'spatial_squeeze' : True,
                                              'reuse' : reuse,
                                              #  'scope' : 'InceptionV3'
                                          },
                                          name='InceptionV3'  # <-- the name should be the same with the ckpt model
                                          )
    return network
Exemplo n.º 36
0
  def build_graph(self):
    """Forms the core by building a wrapper around the inception graph.

      Here we add the necessary input & output tensors, to decode jpegs,
      serialize embeddings, restore from checkpoint etc.

      To use other Inception models modify this file. Note that to use other
      models beside Inception, you should make sure input_shape matches
      their input. Resizing or other modifications may be necessary as well.
      See tensorflow/contrib/slim/python/slim/nets/inception_v3.py for
      details about InceptionV3.

    Returns:
      input_jpeg: A tensor containing raw image bytes as the input layer.
      embedding: The embeddings tensor, that will be materialized later.
    """

    input_jpeg = tf.placeholder(tf.string, shape=None)
    image = tf.image.decode_jpeg(input_jpeg, channels=self.CHANNELS)

    # Note resize expects a batch_size, but we are feeding a single image.
    # So we have to expand then squeeze.  Resize returns float32 in the
    # range [0, uint8_max]
    image = tf.expand_dims(image, 0)

    # convert_image_dtype also scales [0, uint8_max] -> [0 ,1).
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)
    image = tf.image.resize_bilinear(
        image, [self.HEIGHT, self.WIDTH], align_corners=False)

    # Then rescale range to [-1, 1) for Inception.
    image = tf.subtract(image, 0.5)
    inception_input = tf.multiply(image, 2.0)

    # Build Inception layers, which expect a tensor of type float from [-1, 1)
    # and shape [batch_size, height, width, channels].
    with slim.arg_scope(inception.inception_v3_arg_scope()):
      _, end_points = inception.inception_v3(inception_input, is_training=False)

    embedding = end_points['PreLogits']
    return input_jpeg, embedding
Exemplo n.º 37
0
    def test_slim_layer(self):

        with self.assertNotRaises(Exception):
            with slim.arg_scope(inception_v3_arg_scope()):
                # Alternatively, you should implement inception_v3 without TensorLayer as follow.
                # logits, end_points = inception_v3(X, num_classes=1001,
                #                                   is_training=False)
                tl.layers.SlimNetsLayer(
                    self.net_in["slim"],
                    slim_layer=inception_v3,
                    slim_args={
                        'num_classes': 1001,
                        'is_training': False,
                        #  'dropout_keep_prob' : 0.8,       # for training
                        #  'min_depth' : 16,
                        #  'depth_multiplier' : 1.0,
                        #  'prediction_fn' : slim.softmax,
                        #  'spatial_squeeze' : True,
                        #  'reuse' : None,
                        #  'scope' : 'InceptionV3'
                    },
                    name='InceptionV3'  # <-- the name should be the same with the ckpt model
                )
Exemplo n.º 38
0
  def _image_to_vec(image_str_tensor):

    def _decode_and_resize(image_str_tensor):
      """Decodes jpeg string, resizes it and returns a uint8 tensor."""

      # These constants are set by Inception v3's expectations.
      height = 299
      width = 299
      channels = 3

      image = tf.where(tf.equal(image_str_tensor, ''), IMAGE_DEFAULT_STRING, image_str_tensor)
      image = tf.decode_base64(image)
      image = tf.image.decode_jpeg(image, channels=channels)
      image = tf.expand_dims(image, 0)
      image = tf.image.resize_bilinear(image, [height, width], align_corners=False)
      image = tf.squeeze(image, squeeze_dims=[0])
      image = tf.cast(image, dtype=tf.uint8)
      return image

    # The CloudML Prediction API always "feeds" the Tensorflow graph with
    # dynamic batch sizes e.g. (?,).  decode_jpeg only processes scalar
    # strings because it cannot guarantee a batch of images would have
    # the same output size.  We use tf.map_fn to give decode_jpeg a scalar
    # string from dynamic batches.
    image = tf.map_fn(_decode_and_resize, image_str_tensor, back_prop=False, dtype=tf.uint8)
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)
    image = tf.subtract(image, 0.5)
    inception_input = tf.multiply(image, 2.0)

    # Build Inception layers, which expect a tensor of type float from [-1, 1)
    # and shape [batch_size, height, width, channels].
    with tf.contrib.slim.arg_scope(inception_v3_arg_scope()):
      _, end_points = inception_v3(inception_input, is_training=False)

    embeddings = end_points['PreLogits']
    inception_embeddings = tf.squeeze(embeddings, [1, 2], name='SpatialSqueeze')
    return inception_embeddings
#                                        'dropout_keep_prob' : 0.5,
#                                        'spatial_squeeze' : True,
#                                        'scope' : 'alexnet_v2'
#                                         },
#                                     name='alexnet_v2'  # <-- the name should be the same with the ckpt model
#                                     )
# sess = tf.InteractiveSession()
# # sess.run(tf.initialize_all_variables())
# tl.layers.initialize_global_variables(sess)
# network.print_params()


## InceptionV3 / All TF-Slim nets can be merged into TensorLayer
x = tf.placeholder(tf.float32, shape=[None, 299, 299, 3])
net_in = tl.layers.InputLayer(x, name='input_layer')
with slim.arg_scope(inception_v3_arg_scope()):
    ## Alternatively, you should implement inception_v3 without TensorLayer as follow.
    # logits, end_points = inception_v3(X, num_classes=1001,
    #                                   is_training=False)
    network = tl.layers.SlimNetsLayer(layer=net_in, slim_layer=inception_v3,
                                    slim_args= {
                                             'num_classes' : 1001,
                                             'is_training' : False,
                                            #  'dropout_keep_prob' : 0.8,       # for training
                                            #  'min_depth' : 16,
                                            #  'depth_multiplier' : 1.0,
                                            #  'prediction_fn' : slim.softmax,
                                            #  'spatial_squeeze' : True,
                                            #  'reuse' : None,
                                            #  'scope' : 'InceptionV3'
                                            },