初始:如何处理与Inception一起使用的图像

时间:2017-03-03 02:23:35

标签: python tensorflow

我想让tensorflow的初始v3为图像提供标签。我的目标是将JPEG图像转换为初始神经网络接受的输入。我不知道如何首先处理图像,以便它可以与Google Inception的v3模型一起运行。原始的tensorflow项目在这里: https://github.com/tensorflow/models/tree/master/inception

最初,所有图像都在数据集中,整个数据集首先传递给ImageProcessing.py中的input()或distorted_inputs()。处理数据集中的图像并将其传递给train()或eval()方法(这两种方法都有效)。问题是我想要一个函数来打印一个特定图像(不是数据集)的标签。

以下是推理功能的代码,用于生成带谷歌启动的标签。 inceptionv4函数是在张量流中实现的卷积神经网络。

def inference(images, num_classes, for_training=False, restore_logits=True,
              scope=None):
  """Build Inception v3 model architecture.

  See here for reference: http://arxiv.org/abs/1512.00567

  Args:
    images: Images returned from inputs() or distorted_inputs().
    num_classes: number of classes
    for_training: If set to `True`, build the inference model for training.
      Kernels that operate differently for inference during training
      e.g. dropout, are appropriately configured.
    restore_logits: whether or not the logits layers should be restored.
      Useful for fine-tuning a model with different num_classes.
    scope: optional prefix string identifying the ImageNet tower.

  Returns:
    Logits. 2-D float Tensor.
    Auxiliary Logits. 2-D float Tensor of side-head. Used for training only.
  """
  # Parameters for BatchNorm.
  batch_norm_params = {
      # Decay for the moving averages.
      'decay': BATCHNORM_MOVING_AVERAGE_DECAY,
      # epsilon to prevent 0s in variance.
      'epsilon': 0.001,
  }
  # Set weight_decay for weights in Conv and FC layers.
  with slim.arg_scope([slim.ops.conv2d, slim.ops.fc], weight_decay=0.00004):
    with slim.arg_scope([slim.ops.conv2d],
                        stddev=0.1,
                        activation=tf.nn.relu,
                        batch_norm_params=batch_norm_params):
      logits, endpoints = inception_v4(
          images,
          dropout_keep_prob=0.8,
          num_classes=num_classes,
          is_training=for_training,
          scope=scope)

  # Add summaries for viewing model statistics on TensorBoard.
  _activation_summaries(endpoints)

  # Grab the logits associated with the side head. Employed during training.
  auxiliary_logits = endpoints['AuxLogits']

  return logits, auxiliary_logits

这是我尝试在将图像传递给推理函数之前处理它。

  def process_image(self, image_path):
    filename_queue = tf.train.string_input_producer(image_path)
    reader = tf.WholeFileReader()
    key, value = reader.read(filename_queue)

    img = tf.image.decode_jpeg(value)
    height = self.image_size
    width = self.image_size
    image_data = tf.cast(img, tf.float32)
    image_data = tf.reshape(image_data, shape=[1, height, width, 3])
    return image_data

我想简单地处理图像文件,以便将其传递给推理函数。并且该推断打印出标签。上面的代码没有工作和打印错误:

ValueError: Shape () must have rank at least 1

如果有人能够提供有关此问题的任何见解,我感谢您。

1 个答案:

答案 0 :(得分:0)

初始只需要(299,299,3)个输入缩放在-1和1之间的图像。请参阅下面的代码。我只是使用这个更改图像并将它们放入TFRecord(然后排队)来运行我的东西。

from PIL import Image
import PIL
import numpy as np
def load_image( self, image_path ):
    img = Image.open( image_path )
    newImg = img.resize((299,299), PIL.Image.BILINEAR).convert("RGB")
    data = np.array( newImg.getdata() )
    return 2*( data.reshape( (newImg.size[0], newImg.size[1], 3) ).astype( np.float32 )/255 ) - 1