Python Keras层中的Numpy阵列

Python Keras层中的Numpy阵列,python,numpy,keras,Python,Numpy,Keras,我想调整输入到keras模型的输入图像的图像亮度。数据由模拟器提供并实时输入模型,因此我需要一种方法来调整模型本身中的图像数据。我目前正在使用我自己的openCV图层来执行该任务,但我得到以下错误 文件“/usr/lib/python3/dist packages/numpy/core/_methods.py”,第70行,单位为 ret=ret.dtype.type(ret/rcount) AttributeError:'DType'对象没有属性'type' 问题似乎在于“gamma=np.me

我想调整输入到keras模型的输入图像的图像亮度。数据由模拟器提供并实时输入模型,因此我需要一种方法来调整模型本身中的图像数据。我目前正在使用我自己的openCV图层来执行该任务,但我得到以下错误

文件“/usr/lib/python3/dist packages/numpy/core/_methods.py”,第70行,单位为 ret=ret.dtype.type(ret/rcount) AttributeError:'DType'对象没有属性'type'

问题似乎在于“gamma=np.median(img)/25”和试图在类“tensorflow.python.framework.ops.Tensor”上进行numpy数学运算的代码

我的班级代码是

class ImageLayer(Layer):

def __init__(self, **kwargs):
    super(ImageLayer, self).__init__(**kwargs)

def call(self, img, mask=None):

    print(type(img))
    # adjust the image brightness to help normalise dark and light images
    gamma = np.median(img) / 25
    if gamma > 5.:
        gamma = 5
    elif gamma < 0.5:
        gamma = 0.5
    # build a lookup table mapping the pixel values [0, 255] to
    # their adjusted gamma values
    # http://www.pyimagesearch.com/2015/10/05/opencv-gamma-correction/
    invGamma = 1.0 / gamma
    table = np.array([((i / 255.0) ** invGamma) * 255
                      for i in np.arange(0, 256)]).astype("uint8")

    # apply gamma correction using the lookup table
    return cv2.LUT(img, table)
有可能做我想做的事吗


可以在Keras中执行numpy算法吗?我知道你可以在Tensorflow中使用.eval()。

据我所知,你拍摄一张图像,然后1。修剪它2。改变亮度。然后将其输入到您的模型中。因此,与其定义形状的输入层
(160、320、3)
,不如定义一个在裁剪和更改亮度后得到的形状。然后像往常一样定义模型的其余部分。如果您这样做,那么您只需要编写自己的生成器,而不是编写层,在其中您可以使用普通的opencv/python/numpy更改亮度/裁剪等。例如,请参见,以了解如何定义能够使用多个辅助线程的多线程生成器

如果希望将亮度变化视为可学习的参数或将其包含在反向传播中,请不要执行此操作。换句话说,如果亮度变化是预处理操作,与学习方式无关,请使用上述技术

下面给出了一个简单的MNIST数据生成器(仅使用1个工作进程),它一次可以获取32个图像。您可以在读取图像后立即执行亮度更改操作。仅将此代码视为骨架。我还没有定义所有的变量,它不会开箱即用

def myGenerator():  # write the definition of your data generator
    while True:
        count = 0
        for i in range(len(allImgFilenames)):
            if count == 0:
                imgBatch = np.empty((batchSize, 3, 32, 32), dtype=float)
                labelsBatch = np.empty((batchSize,), dtype=int)
            img = cv2.imread(allImgFilenames[i])
            img = cv2.cvtcolor(img, cv2.COLOR_BGR2RGB)  # change the brightness
            img = np.float32(img)/255.
            imgBatch[count, :, :, :] = np.transpose(img, (2,0,1))
            labelsBatch[count] = np.random.randint(0,10,(1,1))
            count += 1
            if count == batchSize:
                count = 0
                yield (imgBatch, labelsBatch)
fit
功能中调用发电机,如下所示:

my_generator = myGenerator()
print("Built the generator")

model.fit_generator(my_generator, samples_per_epoch=60000, nb_epoch=10)
img, label = my_generator.next()  # this will give you `batchSize` number of samples.
model.predict(img)  # `img` should have 4 dimensions if RGB, img.shape = (1,3,nRows,nCols)
测试:

您希望从模拟器中实时获取数据。为此,您可以使用从模拟器获取数据的函数替换
cv2.imread()
。如果要在模拟图像后立即对其进行分类,还可以将“批次大小”更改为1。从生成器中获取图像,如下所示:

my_generator = myGenerator()
print("Built the generator")

model.fit_generator(my_generator, samples_per_epoch=60000, nb_epoch=10)
img, label = my_generator.next()  # this will give you `batchSize` number of samples.
model.predict(img)  # `img` should have 4 dimensions if RGB, img.shape = (1,3,nRows,nCols)

我希望这有帮助。

这有助于培训和验证模型。当我试图在模拟器中运行保存的模型时,它失败了。我知道如果你有测试数据,你可以使用“预测发生器”。有没有办法从模拟器中获取实时数据,然后通过生成器运行它?是的,你可以。您可以向生成器请求样本,然后使用
model.predict()
model.predict\u on\u batch()
。请参阅我的编辑。