使用 tiff 图像重新训练 Inception
Retraining Inception with tiff images
我想在 tiff 图像上重新训练初始模块。我已按照 https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0 中的步骤操作。但是,inception 模块似乎不支持 tiff 图像,因为我收到以下错误
2017-06-22 16:52:56.712653: W tensorflow/core/platform /cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
Looking for images in 'Type 1'
No files found
Looking for images in 'Type 2'
No files found
No valid folders of images found at Myfolder
有什么办法可以解决这个问题吗?
你说的 TensorFlow 不支持 TIFF 图像是对的。
看这里:No Tensorflow decoder for TIFF images?
如果你想使用 TIFF 图像,你可以使用像 PIL
或 Pillow
这样的库,它可以读取 TIFF 图像并将它们转换成 numpy 数组以输入 TensorFlow。
有关示例,请参阅 Working with TIFFs (import, export) in Python using numpy。
如果您有大量 TIFF 文件,上述内容会使训练变慢,因为您将花费更多时间读取和解码 TIFF 文件,从而导致 GPU 数据不足。
在这种情况下,请查看 https://www.tensorflow.org/extend/new_data_formats 如何支持自定义文件格式。
如果您想使用转换路径,我对 Lipin Yang's website 稍作修改后改编的这段代码可以很好地为最近的 TensorFlow 项目将 TIFF 转换为 JPEG。
import os
from PIL import Image
current_path = os.getcwd()
for root, dirs, files in os.walk(current_path, topdown=False):
for name in files:
print(os.path.join(root, name))
#if os.path.splitext(os.path.join(root, name))[1].lower() == ".tiff":
if os.path.splitext(os.path.join(root, name))[1].lower() == ".tif":
if os.path.isfile(os.path.splitext(os.path.join(root, name))[0] + ".jpg"):
print ("A jpeg file already exists for %s" % name)
# If a jpeg with the name does *NOT* exist, convert one from the tif.
else:
outputfile = os.path.splitext(os.path.join(root, name))[0] + ".jpg"
try:
im = Image.open(os.path.join(root, name))
print ("Converting jpeg for %s" % name)
im.thumbnail(im.size)
im.save(outputfile, "JPEG", quality=100)
except Exception as e:
print(e)
将 .jpg 文件保存在另一个目录中(扩展 Beau Hilton 的回答)
main_path = "your/main/path"
data_folder = os.path.join(main_path, "Images_tiff")
data_folder_jpg = os.path.join(main_path, "Images_jpg")
if not os.path.isdir(data_folder_jpg):
os.mkdir(data_folder_jpg)
for root, dirs, files in os.walk(data_folder, topdown=False):
new_folder = os.path.join(data_folder_jpg,os.path.split(root)[1])
if (not os.path.exists(new_folder)) and files:
os.mkdir(new_folder)
for name in files:
print(os.path.join(root, name))
#if os.path.splitext(os.path.join(root, name))[1].lower() == ".tiff":
if os.path.splitext(os.path.join(root, name))[1].lower() == ".tif":
if os.path.isfile(os.path.splitext(os.path.join(new_folder, name))[0] + ".jpg"):
print ("A jpeg file already exists for %s" % name)
# If a jpeg with the name does *NOT* exist, convert one from the tif.
else:
outputfile = os.path.splitext(os.path.join(new_folder, name))[0] + ".jpg"
try:
im = Image.open(os.path.join(root, name))
print ("Converting jpeg for %s" % name)
im.thumbnail(im.size)
im.save(outputfile, "JPEG", quality=100)
except Exception as e:
print(e)
我想在 tiff 图像上重新训练初始模块。我已按照 https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0 中的步骤操作。但是,inception 模块似乎不支持 tiff 图像,因为我收到以下错误
2017-06-22 16:52:56.712653: W tensorflow/core/platform /cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
Looking for images in 'Type 1'
No files found
Looking for images in 'Type 2'
No files found
No valid folders of images found at Myfolder
有什么办法可以解决这个问题吗?
你说的 TensorFlow 不支持 TIFF 图像是对的。
看这里:No Tensorflow decoder for TIFF images?
如果你想使用 TIFF 图像,你可以使用像 PIL
或 Pillow
这样的库,它可以读取 TIFF 图像并将它们转换成 numpy 数组以输入 TensorFlow。
有关示例,请参阅 Working with TIFFs (import, export) in Python using numpy。
如果您有大量 TIFF 文件,上述内容会使训练变慢,因为您将花费更多时间读取和解码 TIFF 文件,从而导致 GPU 数据不足。
在这种情况下,请查看 https://www.tensorflow.org/extend/new_data_formats 如何支持自定义文件格式。
如果您想使用转换路径,我对 Lipin Yang's website 稍作修改后改编的这段代码可以很好地为最近的 TensorFlow 项目将 TIFF 转换为 JPEG。
import os
from PIL import Image
current_path = os.getcwd()
for root, dirs, files in os.walk(current_path, topdown=False):
for name in files:
print(os.path.join(root, name))
#if os.path.splitext(os.path.join(root, name))[1].lower() == ".tiff":
if os.path.splitext(os.path.join(root, name))[1].lower() == ".tif":
if os.path.isfile(os.path.splitext(os.path.join(root, name))[0] + ".jpg"):
print ("A jpeg file already exists for %s" % name)
# If a jpeg with the name does *NOT* exist, convert one from the tif.
else:
outputfile = os.path.splitext(os.path.join(root, name))[0] + ".jpg"
try:
im = Image.open(os.path.join(root, name))
print ("Converting jpeg for %s" % name)
im.thumbnail(im.size)
im.save(outputfile, "JPEG", quality=100)
except Exception as e:
print(e)
将 .jpg 文件保存在另一个目录中(扩展 Beau Hilton 的回答)
main_path = "your/main/path"
data_folder = os.path.join(main_path, "Images_tiff")
data_folder_jpg = os.path.join(main_path, "Images_jpg")
if not os.path.isdir(data_folder_jpg):
os.mkdir(data_folder_jpg)
for root, dirs, files in os.walk(data_folder, topdown=False):
new_folder = os.path.join(data_folder_jpg,os.path.split(root)[1])
if (not os.path.exists(new_folder)) and files:
os.mkdir(new_folder)
for name in files:
print(os.path.join(root, name))
#if os.path.splitext(os.path.join(root, name))[1].lower() == ".tiff":
if os.path.splitext(os.path.join(root, name))[1].lower() == ".tif":
if os.path.isfile(os.path.splitext(os.path.join(new_folder, name))[0] + ".jpg"):
print ("A jpeg file already exists for %s" % name)
# If a jpeg with the name does *NOT* exist, convert one from the tif.
else:
outputfile = os.path.splitext(os.path.join(new_folder, name))[0] + ".jpg"
try:
im = Image.open(os.path.join(root, name))
print ("Converting jpeg for %s" % name)
im.thumbnail(im.size)
im.save(outputfile, "JPEG", quality=100)
except Exception as e:
print(e)