简体   繁体   English

Tensorflow Inception Resnet v2输入张量

[英]Tensorflow Inception resnet v2 input tensor

I'm trying to run this code 我正在尝试运行此代码

import os
import tensorflow as tf
from datasets import imagenet
from nets import inception_resnet_v2
from preprocessing import inception_preprocessing

checkpoints_dir = 'model'

slim = tf.contrib.slim

batch_size = 3
image_size = 299

with tf.Graph().as_default():

with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):
    logits, _ = inception_resnet_v2.inception_resnet_v2([1, 299, 299, 3], num_classes=1001, is_training=False)
    probabilities = tf.nn.softmax(logits)

    init_fn = slim.assign_from_checkpoint_fn(
    os.path.join(checkpoints_dir, 'inception_resnet_v2_2016_08_30.ckpt'),
    slim.get_model_variables('InceptionResnetV2'))

    with tf.Session() as sess:
        init_fn(sess)

        imgPath = '.../image_3.jpeg'
        testImage_string = tf.gfile.FastGFile(imgPath, 'rb').read()
        testImage = tf.image.decode_jpeg(testImage_string, channels=3)

        np_image, probabilities = sess.run([testImage, probabilities])
        probabilities = probabilities[0, 0:]
        sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])]

        names = imagenet.create_readable_names_for_imagenet_labels()
        for i in range(15):
            index = sorted_inds[i]
            print((probabilities[index], names[index]))

But TF displays an error: ValueError: rank of shape must be at least 4 not: 1 但是TF显示错误: ValueError: rank of shape must be at least 4 not: 1

I believe that problem is in input tensor shape [1, 299, 299, 3] . 我相信问题出在输入张量形状[1, 299, 299, 3] How to input tensor for 3 channel JPEG image??? 如何为3通道JPEG图像输入张量????

there is an also one similar question ( Using pre-trained inception_resnet_v2 with Tensorflow ). 还有一个类似的问题( 在Tensorflow中使用预训练的inception_resnet_v2 )。 I saw in code input_tensor - unfortunatelly there is explanation what is input_tensor . 我在代码input_tensor中看到了-不幸的是,有什么解释是input_tensor Maybe I'm asking something self-evident but i stuck! 也许我在问一些不言而喻的事情,但我坚持了! Thanks a lot in advance for any advice! 提前非常感谢您的任何建议!

You have to preprocess your image. 您必须预处理图像。 Here is a code: 这是一个代码:

import os
import tensorflow as tf
from datasets import imagenet
from nets import inception_resnet_v2
from preprocessing import inception_preprocessing

checkpoints_dir = 'model'

slim = tf.contrib.slim

batch_size = 3
image_size = 299

with tf.Graph().as_default():
    with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope()):

        imgPath = '.../cat.jpg'
        testImage_string = tf.gfile.FastGFile(imgPath, 'rb').read()
        testImage = tf.image.decode_jpeg(testImage_string, channels=3)
        processed_image = inception_preprocessing.preprocess_image(testImage, image_size, image_size, is_training=False)
        processed_images = tf.expand_dims(processed_image, 0)

        logits, _ = inception_resnet_v2.inception_resnet_v2(processed_images, num_classes=1001, is_training=False)
        probabilities = tf.nn.softmax(logits)

        init_fn = slim.assign_from_checkpoint_fn(
        os.path.join(checkpoints_dir, 'inception_resnet_v2_2016_08_30.ckpt'), slim.get_model_variables('InceptionResnetV2'))

        with tf.Session() as sess:
            init_fn(sess)

            np_image, probabilities = sess.run([processed_images, probabilities])
            probabilities = probabilities[0, 0:]
            sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x: x[1])]

            names = imagenet.create_readable_names_for_imagenet_labels()
            for i in range(15):
                index = sorted_inds[i]
                print((probabilities[index], names[index]))

The answer is: 答案是:

(0.1131034, 'tiger cat')
(0.079478227, 'tabby, tabby cat')
(0.052777905, 'Cardigan, Cardigan Welsh corgi')
(0.030195976, 'laptop, laptop computer')
(0.027841948, 'bathtub, bathing tub, bath, tub')
(0.026694898, 'television, television system')
(0.024981709, 'carton')
(0.024039172, 'Egyptian cat')
(0.018425584, 'tub, vat')
(0.018221909, 'Pembroke, Pembroke Welsh corgi')
(0.015066789, 'skunk, polecat, wood pussy')
(0.01377619, 'screen, CRT screen')
(0.012509955, 'monitor')
(0.012224807, 'mouse, computer mouse')
(0.012188354, 'refrigerator, icebox')

您可以使用tf.expand_dims(your_tensor_3channel, axis=0)将其扩展为批处理格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM