2017-05-06 42 views
0

我在TensorFlow中使用傳輸學習。我需要使用Inception V3模型來計算圖片的特徵向量。我的代碼JPG格式圖片的計算沒有問題,但是PNG格式的計算會出錯。如何使用Inception v3模型來計算傳輸學習中的PNG圖像的特徵向量

# read model 
with gfile.FastGFile(os.path.join(MODEL_DIR, MODEL_FILE), 'rb') as f: 
    graph_def = tf.GraphDef() 
    graph_def.ParseFromString(f.read()) 

bottleneck_tensor, jpeg_data_tensor = tf.import_graph_def(graph_def, return_elements=[BOTTLENECK_TENSOR_NAME, JPEG_DATA_TENSOR_NAME]) 

...... 

# get imagepath 
image_path = get_image_path(image_lists, INPUT_DATA, index, category) 
# read image 
image_data = gfile.FastGFile(image_path, 'rb').read() 

# calculate the feature vector 
# **This statement is wrong when png images** 
bottleneck_values = sess.run(bottleneck_tensor, {jpeg_data_tensor: image_data}) 

控制檯錯誤包括:

...... 

Not a JPEG file: starts with 0x89 0x50 

...... 

InvalidArgumentError (see above for traceback): Invalid JPEG data, size 19839 
    [[Node: import/DecodeJpeg = DecodeJpeg[acceptable_fraction=1, channels=3, dct_method="", fancy_upscaling=true, ratio=1, try_recover_truncated=false, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_import/DecodeJpeg/contents_0)]] 

我想錯了鑰匙是讀取代碼的圖片,但我不知道如何修改它支持PNG格式,可有人幫我?

謝謝

回答

0

我已經找到了解決辦法。 儘管我無法將其修改爲支持PNG格式,但我可以讀取PNG圖像並將其轉換爲JPEG格式。 添加代碼如下:

import io 

...... 

# get imagepath 
image_path = get_image_path(image_lists, INPUT_DATA, index, category) 
# read image 
data = open(image_path,'rb').read() 
ifile = io.BytesIO(data) 
im = Image.open(ifile).convert('RGB') 
ofile = io.BytesIO() 
im.save(ofile, 'JPEG') 
image_data = ofile.getvalue() 

# calculate the feature vector 
bottleneck_values = sess.run(bottleneck_tensor, {jpeg_data_tensor: image_data}) 

此方法不會產生磁盤新的圖像,這是OK!

0

你可以下載一些其他圖片,看看他們的工作?問題是真的是你的圖像加載我想......

否則,嘗試用pyplot讀取圖像:

import matplotlib.pyplot as plt 
image = plt.imread('image.jpg') 
+0

謝謝。我找到了一個解決方案。儘管我無法修改它以支持PNG格式,但我可以讀取PNG圖像並將其轉換爲JPEG格式。 –

相關問題