Keras裁剪2D更改颜色通道

Keras裁剪2D更改颜色通道,keras,keras-layer,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_sha

我尝试使用以下代码片段可视化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()
结果是


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

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

ax2.imshow(np.uint8(right_image))

compare\u图像中
应正确显示图像。

您是如何绘制这些图像的?出于某种原因,我将其编辑到原始问题中,matplotlib有时会拍摄照片的负片,而不是原始照片。你能比较两张图片中的实际值并检查它们是否不同吗?