首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Inception:如何处理与Inception一起使用的图像

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

Stack Overflow用户
提问于 2017-03-03 10:23:35
回答 1查看 1.4K关注 0票数 2

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

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

下面是推理函数的代码,该函数用于生成带有google inception的标签。inceptionv4函数是在tensorflow中实现的卷积神经网络。

代码语言:javascript
复制
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

这是我在将图像传递给推理函数之前对其进行处理的尝试。

代码语言:javascript
复制
  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

如果有人能对这个问题提供任何见解,我将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2017-04-05 08:45:42

Inception只需要输入在-1和1之间缩放的(299,299,3)个图像。请参阅下面的代码。我只需使用它更改图像,并将它们放入TFRecord (然后将其放入队列)以运行我的程序。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42569513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档