keras:zca美白卡在train_datagen.fit()中 [英] keras: zca whitening gets stuck with train_datagen.fit()

查看:110
本文介绍了keras:zca美白卡在train_datagen.fit()中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将zca_whitening与keras图像处理选项一起使用,但是计算陷入僵局,并且永远不会结束.我引起问题的代码部分如下所示:

I am trying to use zca_whitening with the keras image processing option, but the calculation gets stuck and never ends. My part of the code causing the problem looks like this:

train_datagen = ImageDataGenerator(rotation_range=30, zca_whitening=True)

def read_pil_image(img_path, height, width):
        with open(img_path, 'rb') as f:
            return np.array(Image.open(f).convert('RGB').resize((width, height)))

def load_all_images(dataset_path, height, width, img_ext='jpg'):
    return np.array([read_pil_image(str(p), height, width) for p in 
                                    Path(dataset_path).rglob("*."+img_ext)])

zca_dir = 'some/path/to_jpg_images/'
train_datagen.fit(load_all_images(zca_dir, height, width))

当我执行上面的代码时,它卡在train_datagen.fit()上.没有显示错误信息.我发现如果我不推荐zca_whitening并将其替换为samplewise_center=True, samplewise_std_normalization=True,代码就可以正常工作.

When I execute the above it gets stuck at train_datagen.fit(). There is no error message displayed. I found out if I just outcomment the zca_whitening and substitute it with samplewise_center=True, samplewise_std_normalization=True the code works just fine.

我首先认为目录zca_dir可能包含太多图片,所以我将它们减少到30张.仍然是同样的问题. 该问题出现在具有2种不同keras版本的2台不同机器上.在此帖子中,建议反转我的图像从[80,80,3]到[3,80,80],但这并没有改变.

I thought first that maybe the directory zca_dir contains too many pictures, so I reduced them to 30. Still, same problem. The problem appears on 2 different machines with 2 different keras versions. In this post there is the suggestion to reverse the dimension of my images from [80,80,3] to [3,80,80], but this doesn't change anything.

您知道我该如何解决吗?谢谢

Do you know how I can solve this? Thanks

推荐答案

这是一个常见问题,并不是由于形状错误,如在stackoverflow上的其他帖子中所建议的.喀拉斯语主要贡献者确认,问题在于zca-计算需要非常非常长的时间,因为它使用 numpy.linalg.svd(),即使在矩阵(n * m * 3),n〜m〜100上,计算量也很大. 此处是一篇简短的文章介绍了另一种计算zca的方法,它对我来说快了几个数量级-(1000,80,80,3)矩阵的zca计算时间缩短了1分钟.虽然,我无法确定文章中的数学是正确的,但计算得出的图像与原始论文中的图像非常相似.根据本文实现自己的keras预处理功能应该不太困难

This is a common issue and it is NOT due to a wrong shape, as suggested in other posts on stackoverflow. As confirmed by the keras main contributors the problem is that the zca-calculation takes very, very long, because it uses numpy.linalg.svd() which is computationally heavy even on matrices (n*m*3), n~m~100. Here is a brief article of another way to calculate zca and it is orders of magnitude faster for me - the zca calculation time for a (1000,80,80,3) matrix improves to 1 min. Although, I can't confirm that the math in the article is correct the calculations produce images very similar to the ones in the original paper. It shouldn't be too hard to implement one's own keras preprocessing function based on this article

这篇关于keras:zca美白卡在train_datagen.fit()中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆