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

2024-03-28 08:38:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想制作tensorflow的inception v3,为图像提供标签。我的目标是将JPEG图像转换为初始神经网络所接受的输入。我不知道如何先处理这些图像,这样它才能与googleinception的v3模型一起运行。最初的tensorflow项目如下: https://github.com/tensorflow/models/tree/master/inception

最初,所有的图像都在一个数据集中,整个数据集首先被传递到ImageProcessing.py中的input()或扭曲的_inputs()。数据集中的图像被处理并传递给train()或eval()方法(这两种方法都起作用)。问题是我需要一个函数来打印一个特定图像(而不是数据集)的标记。在

下面是用于使用googleinception生成标记的推理函数的代码。inceptionv4函数是一种用tensorflow实现的卷积神经网络。在

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

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

^{pr2}$

我想简单地处理一个图像文件,以便将其传递给推理函数。这个推论会打印出标签。以上代码不工作,打印错误:

ValueError: Shape () must have rank at least 1

如果有人能深入了解这个问题,我将不胜感激。在


Tags: the数据函数图像formodeltensorflowwith
1条回答
网友
1楼 · 发布于 2024-03-28 08:38:55

《盗梦空间》只需要(299299,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

相关问题 更多 >