Keras Cropping2D改变了颜色通道

时间:2017-01-29 20:47:03

标签: keras keras-layer

我尝试使用以下代码片段来显示keras裁剪2D的效果:

from keras import backend as K
from keras.layers.convolutional import Cropping2D
from keras.models import Sequential
# with a Sequential model
model = Sequential()
model.add(Cropping2D(cropping=((22, 0), (0, 0)), input_shape=(160, 320, 3)))
cropping_output = K.function([model.layers[0].input],
                                  [model.layers[0].output])
cropped_image = cropping_output([image[None,...]])[0]
compare_images(image,
               cropped_image.reshape(cropped_image.shape[1:]))

这是绘图功能:

def compare_images(left_image, right_image):    
    print(image.shape)
    f, (ax1, ax2) = plt.subplots(1, 2, figsize=(24, 9))
    f.tight_layout()
    ax1.imshow(left_image)
    ax1.set_title('Shape '+ str(left_image.shape),
                  fontsize=50)
    ax2.imshow(right_image)
    ax2.set_title('Shape '+ str(right_image.shape)
                  , fontsize=50)
    plt.show()

结果是

enter image description here

显然,颜色通道已经改变。但为什么?我的代码中是否有错误或者可能是keras错误?

1 个答案:

答案 0 :(得分:1)

它不是Keras的错误。张量通常为float32类型,因此在评估输出时,它们也是float32类型。您需要在显示之前将图像数据转换为uint8类型。

ax2.imshow(np.uint8(right_image))
compare_images中的

应正确显示图像。