简体   繁体   中英

Runtime Error with Keras AveragePooling2D

import tensorflow as tf
print("Tensorflow version: ", tf.__version__)
import logging
tf.get_logger().setLevel(logging.ERROR)

import numpy as np 
from keras.models import Sequential 
from keras.layers import AveragePooling2D 

# define input image 
image = np.array([  [2, 2, 7, 3], 
                    [9, 4, 6, 1], 
                    [8, 5, 2, 4], 
                    [3, 1, 2, 6]])

image = image.reshape(1, 4, 4, 1) 

# define model containing just a single average pooling layer 
# https://keras.io/api/layers/pooling_layers/average_pooling2d/
model = Sequential( 
            [AveragePooling2D(pool_size = 2, strides = 2)]
        ) 

# generate pooled output
output = model.predict(image) 

# print output image 
output = np.squeeze(output) 
print("Average Pooling using keras:",output)  

I got the next error:

  • ValueError: Input 0 of layer sequential_14 is incompatible with the layer: expected ndim=5, found ndim=4. Full shape received: [None, 4, 4, 1]

Do yo have any idea? Thanks.

Try to change type:

image = np.array([  [2, 2, 7, 3], 
                    [9, 4, 6, 1], 
                    [8, 5, 2, 4], 
                    [3, 1, 2, 6]], dtype="float64")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM