Reputation: 9645
I tried to visualize the effect of keras cropping 2D using the following code snippet:
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:]))
Here is the plotting function:
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()
The result is
Obviously, the color channel has been changed. But why? Is there an error in my code or could that be a keras bug?
Upvotes: 1
Views: 1640
Reputation: 9099
Its not a Keras bug. Tensors are usually of float32
type so when the output is evaluated they are also of float32
type. You need to convert the image data to uint8
type before displaying.
ax2.imshow(np.uint8(right_image))
in compare_images
should display the image correctly.
Upvotes: 1