我尝试使用以下代码片段可视化 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 错误?