迁移学习中如何使用Inception v3模型计算PNG图片的特征向量

How to use the Inception v3 model to calculate the feature vector of PNG images in the transfer learning

我在使用迁移学习TensorFlow.I需要用Inception V3模型计算一个picture.My代码的特征向量在计算JPG格式图片没问题,但是计算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格式,谁能帮帮我?

谢谢

你能下载一些其他的图片看看能不能用?我认为问题确实是您的图片加载...

否则,尝试使用 pyplot 读取图像:

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

我找到了解决办法。 虽然我不能修改它以支持 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})

此方法不会在磁盘中生成新图像,没关系!