keras: zca whitening gets stuck with train_datagen.fit()

南笙酒味 提交于 2020-02-04 05:35:06

问题


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))

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.

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


回答1:


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



来源:https://stackoverflow.com/questions/59848525/keras-zca-whitening-gets-stuck-with-train-datagen-fit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!