不久前我遇到了这个问题,在花了比我愿意承认从源集中寻找和提取数据的时间更多之后,我找到了你的问题。我什至做了 ImageMagick 搜索来从数据集中清除灰度图像,但在我的情况下问题仍然存在。
我什至将我的数据集导出到我们的一台 Mac 上,以便使用 Preview 批量编辑颜色和分辨率并导出新的统一 jpeg。还是没修。这是我想出的解决方案。(更新运行时工作区中的 dataset_tool.py)
def create_from_images(tfrecord_dir, image_dir, shuffle):
print('Loading images from "%s"' % image_dir)
image_filenames = sorted(glob.glob(os.path.join(image_dir, '*')))
if len(image_filenames) == 0:
error('No input images found')
img = np.asarray(PIL.Image.open(image_filenames[0]))
resolution = img.shape[0]
channels = img.shape[2] if img.ndim == 3 else 1
if img.shape[1] != resolution:
error('Input images must have the same width and height')
if resolution != 2 ** int(np.floor(np.log2(resolution))):
error('Input image resolution must be a power-of-two')
if channels not in [1, 3]:
error('Input images must be stored as RGB or grayscale')
with TFRecordExporter(tfrecord_dir, len(image_filenames)) as tfr:
order = tfr.choose_shuffled_order() if shuffle else np.arange(len(image_filenames))
for idx in range(order.size):
pil_img = PIL.Image.open(image_filenames[order[idx]])
pil_img = pil_img.convert("RGB")
img = np.asarray(pil_img)
#print('\nimg: "%s" (%d)' % (image_filenames[order[idx]], channels))
if channels == 1:
img = img[np.newaxis, :, :] # HW => CHW
else:
img = img.transpose([2, 0, 1]) # HWC => CHW
tfr.add_image(img)
无论如何,基本上使用 PIL 将图像转换为 RGB。
它确实会稍微减慢准备过程,但如果您的训练数据来自不同的来源,它会很方便。