2017-05-06 46 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格式。 –

相关问题