【发布时间】:2017-03-03 02:23:34
【问题描述】:
我想制作 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实现的卷积神经网络。
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
如果有人能提供对此问题的任何见解,我将不胜感激。
【问题讨论】:
标签: python tensorflow