将图像数据从pylearn2中的npy文件转换为灰度

Convert image data to grayscale from npy file in pylearn2

我正在使用 pylearn2 训练一个简单的卷积神经网络。我将 RGB 图像数据存储在一个 npy 文件中。有没有办法直接从 npy 文件直接将该数据转换为灰度数据?

如果这是一个独立文件,则使用 numpy.load then convert the content using something like this:

加载文件
def rgb2gray(rgb):
    return np.dot(rgb[...,:3], [0.299, 0.587, 0.144])

如果文件是 pylearn2 数据集的一部分(来自 use_design_loc()),则加载数据集

from pylearn2.utils import serial
serial.load("file.pkl")

并将 rgb2gray() 函数应用于 X 成员(我假设是 DenseDesignMatrix)。