代码之家  ›  专栏  ›  技术社区  ›  Ericccccc

当我使用tf时。nn。read_file()在vgg中测试图像,这是错误的。但当我用PIL打开它时,它是正确的。真奇怪

  •  0
  • Ericccccc  · 技术社区  · 8 年前

    我编写了一个vgg模型,并对其进行了训练。现在,我要测试一个新图像。当我使用code1时,它运行良好,但在code2中是错误的。 vgg()是我定义的模型。ckpt文件已保存在“D:\Demo\ckpt”中。

    代码1:它将预测打印为[1.77901700e-01 8.22093844e-01 4.42284863e-06]]

    def evaluate_one_image(path):
        with tf.Graph().as_default():
            image_plt = Image.open(path)
            image = image_plt.resize([224, 224])
            image_array = np.array(image)
            image = np.reshape(image_array, (1,224,224,3))
            x = tf.placeholder(tf.float32, shape=[1, 224, 224, 3])
            logit = vgg(x)
            logit = tf.nn.softmax(logit)
            logs_train_dir = 'D:\\Demo\\ckpt'
            saver = tf.train.Saver(tf.global_variables())
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(logs_train_dir)
                saver.restore(sess, ckpt.model_checkpoint_path)               
                prediction = sess.run(logit, feed_dict={x: image})
                print(prediction)
    

    ------------------------------------------------分界线--------------------------------------

    代码2:我在tensorflow中使用了一些函数。它将预测打印为[0.33333334 0.33333334 0.3333333 4]]。

    def test_one_image(path):
        with tf.Graph().as_default():
            image_plt = Image.open(path)
            image_tensor = tf.image.decode_jpeg(tf.read_file(path), channels=3)
            image_tensor = tf.image.resize_image_with_crop_or_pad(image_tensor, 224, 224)
            # image_tensor = tf.image.per_image_standardization(image_tensor)
            image_tensor = tf.reshape(image_tensor, [1, 224, 224, 3])
            x = tf.placeholder(tf.float32, shape=[1, 224, 224, 3])
            logit = vgg(x)
            logit = tf.nn.softmax(logit)
            logs_train_dir = 'D:\\Demo\\ckpt'
            saver = tf.train.Saver(tf.global_variables())
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(logs_train_dir)
                saver.restore(sess, ckpt.model_checkpoint_path)              
                prediction = sess.run(logit, feed_dict={x: image_tensor.eval()})
                print(prediction)
    

    我认为这两个代码的步骤几乎相同。但我不知道为什么这是错误的,以及如何处理它。帮帮我,非常感谢!

    2 回复  |  直到 8 年前
        1
  •  0
  •   Vladimir Bystricky    8 年前

    第一个代码 调整大小 形象第二个代码 庄稼 形象这是两个不同的动作,第一个动作将整个图像缩放到新的大小,第二个动作将图像的一部分剪切,使其大小相等 224 x 224

    更新:

    请尝试以下代码:

    def test_one_image(path):
        with tf.Graph().as_default():
            image_plt = Image.open(path)
            image_tensor = tf.image.decode_jpeg(tf.read_file(path), channels=3)
            image_tensor = tf.expand_dims(image_tensor, [0])
            image_tensor = tf.image.resize_images(image_tensor, [224, 224])
            logit = vgg(image_tensor)
            logit = tf.nn.softmax(logit)
            logs_train_dir = 'D:\\Demo\\ckpt'
            saver = tf.train.Saver(tf.global_variables())
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(logs_train_dir)
                saver.restore(sess, ckpt.model_checkpoint_path)              
                prediction = sess.run(logit)
                print(prediction)
    
        2
  •  0
  •   Ericccccc    8 年前

    当我使用此代码时,发生了错误。结果再次为[0.33333334 0.33333334 0.3333333 4]。

    def test_one_image(path):
        with tf.Graph().as_default():
            image_plt = Image.open(path)
            image_tensor = tf.image.decode_jpeg(tf.read_file(path), channels=3)
            image_tensor = tf.image.per_image_standardization(image_tensor)
            image_tensor = tf.reshape(image_tensor, [1, 224, 224, 3])
            x = tf.placeholder(tf.float32, shape=[1, 224, 224, 3])
            logit = vgg(x)
            logit = tf.nn.softmax(logit)
            logs_train_dir = 'D:\\Demo\\ckpt'
            saver = tf.train.Saver(tf.global_variables())
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(logs_train_dir)
                saver.restore(sess, ckpt.model_checkpoint_path)              
                prediction = sess.run(logit, feed_dict={x: image_tensor.eval()})
                print(prediction)