谁能告诉我哪里出了问题?层 conv2d_21 的输入 0 与层不兼容:输入形状的预期轴 -1 的值为 1

Can anyone tell me what is wrong? Input 0 of layer conv2d_21 is incompatible with the layer: expected axis -1 of input shape to have value 1

WARNING:tensorflow:Model was constructed with shape (None, 48, 48, 1) for input Tensor("conv2d_21_input:0", shape=(None, 48, 48, 1), dtype=float32), but it was called on an input with incompatible shape (None, 48, 48, 3).

------------------------------------------ --------------------------

这是相关代码:

    from google.colab import drive

#drive.mount('/content/drive')

!ls "/content/drive/My Drive/Colab Notebooks/"

!cp "/content/drive/My Drive/Colab Notebooks/happy vs angry.zip" "happy vs angry.zip"



!ls

import tensorflow as tf

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator

import os
import numpy as np
import matplotlib.pyplot as plt
from zipfile import ZipFile

zf = ZipFile('/content/happy vs angry.zip', 'r')
zf.extractall('happy vs angry')
PATH = os.path.join(os.path.dirname('/content/happy vs angry'), 'happy vs angry')
train_dir = os.path.join(PATH, 'train')
test_dir = os.path.join(PATH, 'test')

train_happy_dir = os.path.join(train_dir, 'happy')  # directory with our training 
train_angry_dir = os.path.join(train_dir, 'angry')  # directory with our training 
test_happy_dir = os.path.join(test_dir, 'happy')  # directory with our validation 
test_angry_dir = os.path.join(test_dir, 'angry')  # directory with our validation 
batch_size=64
IMG_HEIGHT = 48
IMG_WIDTH = 48

train_image_generator = ImageDataGenerator(rescale=1./255) # Generator for our training data
test_image_generator = ImageDataGenerator(rescale=1./255) # Generator for our validation data

#train_image_generator = ImageDataGenerator(horizontal_flip=True)
#test_image_generator= ImageDataGenerator(horizontal_flip=True)
train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
                                                           directory=train_dir,
                                                           color_mode="grayscale",
                                                           shuffle=True,
                                                           target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                           class_mode='binary')

val_data_gen = test_image_generator.flow_from_directory(batch_size=batch_size,
                                                              directory=test_dir,
                                                              color_mode="grayscale",
                                                              target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                              class_mode='binary')
model = Sequential([
    Conv2D(64,3, activation='relu', input_shape=(48,48,1)),
    MaxPooling2D(),
    Dropout(0.2),
    Conv2D(32,3, activation='relu'),
    MaxPooling2D(),
    Dropout(0.2),
    Conv2D(64,3, activation='relu'),
    MaxPooling2D(),
    Dropout(0.2),
    Flatten(),
    Dense(512, activation='relu'),
    Dense(2,activation='softmax')
])
model.compile(optimizer='adam',
              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              metrics=['accuracy'])


model.summary()
import numpy as np
import random
from tensorflow.keras.preprocessing.image import img_to_array, load_img

# Let's define a new Model that will take an image as input, and will output
# intermediate representations for all layers in the previous model after
# the first.
successive_outputs = [layer.output for layer in model.layers[1:]]
#visualization_model = Model(img_input, successive_outputs)
visualization_model = tf.keras.models.Model(inputs = model.input, outputs = successive_outputs)
# Let's prepare a random input image from the training set.
happy_img_files = [os.path.join(train_happy_dir, f) for f in os.listdir(train_happy_dir)]
angry_img_files = [os.path.join(train_angry_dir, f) for f in os.listdir(train_angry_dir)]
img_path = random.choice(happy_img_files + angry_img_files)

img = load_img(img_path, target_size=(48, 48))  # this is a PIL image
x = img_to_array(img)  # Numpy array with shape (150, 150, 3)
x = x.reshape((1,) + x.shape)  # Numpy array with shape (1, 150, 150, 3)

# Rescale by 1/255
x /= 255

# Let's run our image through our network, thus obtaining all
# intermediate representations for this image.
successive_feature_maps = visualization_model.predict(x)

# These are the names of the layers, so can have them as part of our plot
layer_names = [layer.name for layer in model.layers]

# Now let's display our representations
for layer_name, feature_map in zip(layer_names, successive_feature_maps):
  if len(feature_map.shape) == 4:
    # Just do this for the conv / maxpool layers, not the fully-connected layers
    n_features = feature_map.shape[-1]  # number of features in feature map
    # The feature map has shape (1, size, size, n_features)
    size = feature_map.shape[1]
    # We will tile our images in this matrix
    display_grid = np.zeros((size, size * n_features))
    for i in range(n_features):
      # Postprocess the feature to make it visually palatable
      x = feature_map[0, :, :, i]
      x -= x.mean()
      x /= x.std()
      x *= 64
      x += 128
      x = np.clip(x, 0, 255).astype('uint8')
      # We'll tile each filter into this big horizontal grid
      display_grid[:, i * size : (i + 1) * size] = x
    # Display the grid
    scale = 20. / n_features
    plt.figure(figsize=(scale * n_features, scale))
    plt.title(layer_name)
    plt.grid(False)
    plt.imshow(display_grid, aspect='auto', cmap='viridis')
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-49-d8344f298369> in <module>()
     23 # Let's run our image through our network, thus obtaining all
     24 # intermediate representations for this image.
---> 25 successive_feature_maps = visualization_model.predict(x)
     26 
     27 # These are the names of the layers, so can have them as part of our plot

10 frames

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    966           except Exception as e:  # pylint:disable=broad-except
    967             if hasattr(e, "ag_error_metadata"):
--> 968               raise e.ag_error_metadata.to_exception(e)
    969             else:
    970               raise

ValueError: in user code:

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1147 predict_function  *
        outputs = self.distribute_strategy.run(
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:951 run  **
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1122 predict_step  **
        return self(x, training=False)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:927 __call__
        outputs = call_fn(cast_inputs, *args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py:719 call
        convert_kwargs_to_constants=base_layer_utils.call_context().saving)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py:888 _run_internal_graph

我一直收到这个错误,我不知道出了什么问题基本上我正在训练

一个面部表情模型,图像数据集是灰度的。图像为 (48,48 ) 像素。------------------------------------ --------------

 output_tensors = layer(computed_tensors, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:886 __call__
        self.name)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:216 assert_input_compatibility
        ' but received input with shape ' + str(shape))

    ValueError: Input 0 of layer conv2d_21 is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape [None, 48, 48, 3]

所以问题就在这里:

img = load_img(img_path, target_size=(48, 48))  # this is a PIL image

默认情况下,load_img 加载彩色图像(3 个通道),因此即使是灰度图像也会加载为彩色图像,其中每个通道的值都与灰度通道相同。要强制 load_image 生成单通道图像,您应该使用 color_mode 参数:

img = load_img(img_path, target_size=(48, 48), color_mode='grayscale')  # this is a PIL image

这将生成灰度单通道图像,预测应该有效。