在我的Keras CNN中,我像这样添加Input层:
model.add(Conv2D(32, (3, 3), input_shape=(img_width, img_height, nb_channel)))
与nb_channel = 3 RGB输入和= 1灰度输入和flow_from_directory
和ImageDataGenerator
但是,我想指定一组要输入到CNN的颜色,例如,只允许绿色和红色通道,我该怎么做?
我正在将Keras与tensorflow后端一起使用
除了@ Minh-Tuan Nguyen的巧妙解决方案之外,我们还可以按照以下方式进行切片
#custom filter
def filter_layer(x):
red_x = x[:,:,:,0]
blue_x = x[:,:,:,2]
green_x = x[:,:,:,1]
red_x = tf.expand_dims(red_x, axis=3)
blue_x = tf.expand_dims(blue_x, axis=3)
green_x = tf.expand_dims(green_x, axis=3)
output = tf.concat([red_x, blue_x], axis=3)
return output
#model
input = Input(shape=(img_height, img_width, img_channels))
在合并步骤中,我们可以选择所需的切片。
我认为在这里更“天真”地处理切片会更容易,因为据我所知,Keras不支持使用python和numpy等索引列表对张量进行切片。以下是我针对此问题的代码示例。尝试查看它是否符合您的要求。
indices = [0,2]
def filter_layer(input, indices=indices):
for i in range(len(indices)):
index = indices[i]
x_temp = Lambda(lambda x: x[:,:,:,index][...,None])(input)
if i==0:
x = x_temp
else:
x = Concatenate(-1)([x, x_temp])
return x
input = Input(shape=(img_height, img_width, img_channels))
x = Lambda(filter_layer)(input)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句