我想在tiff图像上重新训练初始模块。我已按照https://codelabs.developers.google.com/codelabs/tensorflow-for-poets/#0中的步骤进行操作。但是,初始模块似乎不支持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
有没有办法解决这个问题? 提前谢谢!
答案 0 :(得分:2)
你说TensorFlow不支持TIFF图像。
见这里:Tensorflow Machine Learning: No Decoder for TIFF Images?
如果您想使用TIFF图像,可以使用像PIL
或Pillow
这样的库,它可以读取TIFF图像并将它们转换为numpy数组以输入TensorFlow。
有关示例,请参阅Working with TIFFs (import, export) in Python using numpy。
如果您有大量的TIFF文件,上述情况会使培训变得缓慢,因为您将花费更多时间阅读和解码匮乏数据GPU的TIFF文件。
在这种情况下,请查看https://www.tensorflow.org/extend/new_data_formats有关如何支持自定义文件格式的信息。
答案 1 :(得分:1)
如果您想使用转换路线,那么我对Lipin Yang's website进行了一些修改后改编而成的这段代码就很好地将TIFF转换为JPEG,用于最近的TensorFlow项目。
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)
答案 2 :(得分:-1)
将 .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)