2017-07-19 84 views
0

tf.image.decode_jpeg()對於jpg圖像,Tensorflow的功能與scipy.misc.imread()不同。雖然圖像看起來相似,但像素值不同。tensorflow爲什麼解碼jpeg圖像與scipy imread不同?

import numpy as np 
import scipy 
import tensorflow as tf 
import matplotlib.pyplot as plt 
def minimal_example(): 
    def _bytes_feature(value): 
     return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) 

    tffilename = 'astronaut.tfrecords' 
    writer = tf.python_io.TFRecordWriter(tffilename) 
    #image_source = 'https://upload.wikimedia.org/wikipedia/commons/8/88/Astronaut-EVA.jpg' 
    image_path = 'astronaut.jpg' 
    image_file = open(image_path,'rb') 
    image = image_file.read() 
    image_scipy = scipy.misc.imread(image_path) 
    example = tf.train.Example(features=tf.train.Features(feature={'image':_bytes_feature(image)})) 
    writer.write(example.SerializeToString()) 
    writer.close() 

    record_iterator = tf.python_io.tf_record_iterator(path=tffilename) 
    example = tf.train.Example() 
    example.ParseFromString(next(record_iterator)) 
    image = example.features.feature['image'].bytes_list.value[0] 
    image_tf = tf.image.decode_jpeg(image).eval(session=tf.Session()) 
    fig = plt.figure() 
    ax1 = fig.add_subplot(121) 
    ax2 = fig.add_subplot(122) 
    ax1.imshow(image_scipy) 
    ax2.imshow(image_tf) 
    print('Reconstruction Error', np.sum(np.abs(image_tf - image_scipy))) 
    plt.show() 

結果:

Reconstruction Error 3420883624 

這是一個錯誤還是我做錯了什麼?

回答

2

的差異產生的,因爲不準確的,但使用Tensorflow

快,默認離散餘弦變換分析按照Source code

//爲JPEG解碼的TensorFlow-選擇缺省是IFAST,犧牲

//圖像質量的速度。

flags_.dct_method = JDCT_IFAST;

爲了得到準確的解碼可以例如在設置屬性dct_method = 'INTEGER_ACCURATE'如示於下

def minimal_example(): 
    #image_source = 'https://upload.wikimedia.org/wikipedia/commons/8/88/Astronaut-EVA.jpg' 
    image_path = 'astronaut.jpg' 
    image_file = open(image_path,'rb') 
    image_raw = image_file.read() 
    image_scipy = scipy.misc.imread(image_path) 
    image_tf = tf.image.decode_jpeg(image_raw).eval(session=tf.Session()) 
    image_tf_accurate = tf.image.decode_jpeg(image_raw,dct_method="INTEGER_ACCURATE").eval(session=tf.Session()) 
    print('Error For Default: ', np.sum(np.abs(image_tf - image_scipy))) 
    print('Error For Accurate: ', np.sum(np.abs(image_tf_accurate - image_scipy))) 
    #Error For Default: 3420883624 
    #Error For Accurate: 0 
1

The JPEG standard does not require bit-to-bit identical decoding。所以,不同實現之間會有一些變化。

但是,它仍然需要

最大1個針對每個像素分量差值的比特。

所以兩個輸出不應該分開一個。對?

print('max diff: ', np.max(np.abs(image_tf.astype(float) - image_scipy.astype(float)))) 
# max diff: 17.0 

哎喲,至少一個實現不遵循標準...

+0

第一次聽到。很有意思! scipy似乎使用libjpeg-dev(通過PIL/Pillow),而tensorflow可能使用[libjpeg-turbo](https://github.com/tensorflow/tensorflow/issues/4807)。 – sascha

相關問題