1

我正在通过 Google Colab 使用我从 Wikiart 抓取并转换为 1024x1024 的照片数据集训练 GAN,但在创建 tfrecords 时不断出现此错误:

Traceback (most recent call last):
  File "dataset_tool.py", line 1249, in <module>
    execute_cmdline(sys.argv)
  File "dataset_tool.py", line 1244, in execute_cmdline
    func(**vars(args))
  File "dataset_tool.py", line 714, in create_from_images
    img = img.transpose([2, 0, 1]) # HWC => CHW
ValueError: axes don't match array

我将它设置为打印出它会停止的文件,并开始将这些文件从数据集中取出;但它的停滞似乎完全是随机的。它会在一次运行中完美地迭代文件,然后在从数据集中取出其他一些麻烦的照片后在下一次运行时失败。

我不确定不断删除停止它的照片的过程是否会结束/给我留下一个有意义的数据集,我应该尝试修复它吗?

4

2 回答 2

2

想出了解决方案,结果发现我抓取的一些图像是灰度的。为了解决这个问题,我使用了 imagemagick(也用于将照片调整为 1024x1024)来检查色彩空间。我将终端指向图像文件夹并运行:

magick identify *.jpg

从这里,我 ctrl+f'ed 查看哪些标记为“灰色”而不是“sRGB”。将这些从数据集中取出后,它就像一个魅力。

于 2021-04-13T21:34:42.483 回答
1

不久前我遇到了这个问题,在花了比我愿意承认从源集中寻找和提取数据的时间更多之后,我找到了你的问题。我什至做了 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。
它确实会稍微减慢准备过程,但如果您的训练数据来自不同的来源,它会很方便。

于 2021-05-26T01:14:33.283 回答