【发布时间】:2017-03-13 04:40:44
【问题描述】:
我想调整输入到 keras 模型的输入图像的图像亮度。数据由模拟器提供并实时输入模型,因此我需要一种方法来调整模型本身的图像数据。我目前正在使用我自己的层和 openCV 来执行任务,但我收到以下错误。
文件“/usr/lib/python3/dist-packages/numpy/core/_methods.py”,第 70 行,_mean 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)
模型从模型中调用类
inputs = Input(shape=(160, 320, 3), dtype='int8')
x = Cropping2D(cropping=((50,0), (0,0)), input_shape=(160, 320, 3), dim_ordering='tf')(inputs)
x = ImageLayer()(x)
x = BatchNormalization(epsilon=0.001, mode=0, axis=2, momentum=0.99)(x)
有没有可能做我想做的事?
是否可以在 Keras 中执行 numpy 算术?我知道你可以在 Tensorflow 中使用 .eval()。
【问题讨论】: