【问题标题】:expected activation_8 to have shape (2,) but got array with shape (1,)预期 activation_8 的形状为 (2,) 但得到的数组形状为 (1,)
【发布时间】:2020-05-14 11:49:10
【问题描述】:

我知道关于这个主题/错误还有其他帖子,但我不知道如何使它们适应我的代码。我可能只需要另一双眼睛来查看我的代码以找出哪里出错了。篇幅过长,我深表歉意。

我正在做一个图像识别项目。

首先我导入库:

from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from keras.models import Sequential
from keras.layers.core import Dense
from keras.optimizers import SGD
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation
from keras.layers.core import Flatten, Dropout
from keras import backend as K
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np
import argparse
import random
import pickle
import cv2
import os

接下来,我实例化我的 CNN 模型:

class SmallVGGNet:
    @staticmethod
    def build(width, height, depth, classes):
        model = Sequential()
        inputShape = (height, width, depth)
        chanDim = -1


        if K.image_data_format() == "channels_first":
            inputShape = (depth, height, width)
            chanDim = 1


        # CONV => RELU => POOL layer set
        model.add(Conv2D(32, (3, 3), padding="same",
            input_shape=inputShape))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(0.25))



        # (CONV => RELU) * 2 => POOL layer set
        model.add(Conv2D(64, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(64, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(0.25))


        # (CONV => RELU) * 3 => POOL layer set
        model.add(Conv2D(128, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(128, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(128, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(0.25))


        # first (and only) set of FC => RELU layers
        model.add(Flatten())
        model.add(Dense(512))
        model.add(Activation("relu"))
        model.add(BatchNormalization())
        model.add(Dropout(0.5))

        # softmax classifier
        model.add(Dense(classes))
        model.add(Activation("softmax"))

        # return the constructed network architecture
        return model

收集并处理我的图像:

data = []
labels = []

# grab the image paths and randomly shuffle them
shiba_path = sorted(list(paths.list_images('../downloads/shiba')))
fox_path = sorted(list(paths.list_images('../downloads/fox')))
combine_path = [shiba_path,fox_path]
random.seed(42)

# loop over the input data dictionaries
for path in combine_path:
    path = random.shuffle(path)

# loop over the input images
for imagePaths in combine_path:
    for imagePath in imagePaths:
        try:
            # load the image, resize the image to be 32x32 pixels (ignoring
            # aspect ratio), flatten the image into 32x32x3=3072 pixel image
            # into a list, and store the image in the data list
            image = cv2.imread(imagePath)
            image = cv2.resize(image, (64, 64))
            data.append(image)

            # extract the class label from the image path and update the
            # labels list
            label = imagePath.split(os.path.sep)[-2].split('/')[-1]
            labels.append(label)
        except:
            pass
# scale the raw pixel intensities to the range [0, 1]
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)

运行我的模型:

# partition the data into training and testing splits using 75% of
# the data for training and the remaining 25% for testing
(trainX, testX, trainY, testY) = train_test_split(data,
    labels, test_size=0.25, random_state=42)

# convert the labels from integers to vectors
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)

trainY 的形状为 (1429,1)trainX 的形状为 (1429, 64, 64, 3)

# construct the image generator for data augmentation
aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1,
    height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,
    horizontal_flip=True, fill_mode="nearest")

# initialize our VGG-like Convolutional Neural Network
model = SmallVGGNet.build(width=64, height=64, depth=3,
    classes=len(lb.classes_))

问题出现在下一步。

# initialize our initial learning rate, # of epochs to train for,
# and batch size
INIT_LR = 0.01
EPOCHS = 20
BS = 32

# initialize the model and optimizer
print("[INFO] training network...")
opt = SGD(lr=INIT_LR, decay=INIT_LR / EPOCHS)
model.compile(loss="categorical_crossentropy", optimizer=opt,
    metrics=["accuracy"])

# train the network
H = model.fit_generator(aug.flow(trainX, trainY, batch_size=BS),
    validation_data=(testX, testY), steps_per_epoch=len(trainX) // BS,
    epochs=EPOCHS)

这一步生成

检查目标时出错:预期 activation_8 的形状为 (2,),但得到的数组的形状为 (1,)

我哪里出错了?我在做什么期望我的 Y 形状为 2?它的形状应该是 1 还是应该是 2?

任何帮助将不胜感激。

【问题讨论】:

    标签: python tensorflow image-processing keras deep-learning


    【解决方案1】:

    我能够重新创建错误,问题出在最后一个激活层和损失函数中。

    由于您已经将目标变量转换为使用 LabelBinarizer 编码的 One-Hot,因此您的目标数据看起来像这样,其中包括这两个类。

    array([[1],
           [1],
           [1],
           [0],
           [0],
           [1],
           [0],
           [0]]) 
    

    在输出层,不给出类的数量作为输入,而是给出1,因为它只输出一个值,并将损失函数从categorical_crossentropy更改为binary_crossentropy

    下面是修改后的块。

    # softmax classifier
    model.add(Dense(1))
    model.add(Activation("softmax"))  
    

    将损失函数改为:

    model.compile(loss="binary_crossentropy", optimizer=opt,
        metrics=["accuracy"])
    

    这将解决您的问题,祝您学习愉快!

    编辑 1:

    提供模型摘要。

    Model: "sequential_1"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    conv2d_1 (Conv2D)            (None, 64, 64, 32)        896       
    _________________________________________________________________
    activation_1 (Activation)    (None, 64, 64, 32)        0         
    _________________________________________________________________
    batch_normalization_1 (Batch (None, 64, 64, 32)        128       
    _________________________________________________________________
    max_pooling2d_1 (MaxPooling2 (None, 32, 32, 32)        0         
    _________________________________________________________________
    dropout_1 (Dropout)          (None, 32, 32, 32)        0         
    _________________________________________________________________
    conv2d_2 (Conv2D)            (None, 32, 32, 64)        18496     
    _________________________________________________________________
    activation_2 (Activation)    (None, 32, 32, 64)        0         
    _________________________________________________________________
    batch_normalization_2 (Batch (None, 32, 32, 64)        256       
    _________________________________________________________________
    conv2d_3 (Conv2D)            (None, 32, 32, 64)        36928     
    _________________________________________________________________
    activation_3 (Activation)    (None, 32, 32, 64)        0         
    _________________________________________________________________
    batch_normalization_3 (Batch (None, 32, 32, 64)        256       
    _________________________________________________________________
    max_pooling2d_2 (MaxPooling2 (None, 16, 16, 64)        0         
    _________________________________________________________________
    dropout_2 (Dropout)          (None, 16, 16, 64)        0         
    _________________________________________________________________
    conv2d_4 (Conv2D)            (None, 16, 16, 128)       73856     
    _________________________________________________________________
    activation_4 (Activation)    (None, 16, 16, 128)       0         
    _________________________________________________________________
    batch_normalization_4 (Batch (None, 16, 16, 128)       512       
    _________________________________________________________________
    conv2d_5 (Conv2D)            (None, 16, 16, 128)       147584    
    _________________________________________________________________
    activation_5 (Activation)    (None, 16, 16, 128)       0         
    _________________________________________________________________
    batch_normalization_5 (Batch (None, 16, 16, 128)       512       
    _________________________________________________________________
    conv2d_6 (Conv2D)            (None, 16, 16, 128)       147584    
    _________________________________________________________________
    activation_6 (Activation)    (None, 16, 16, 128)       0         
    _________________________________________________________________
    batch_normalization_6 (Batch (None, 16, 16, 128)       512       
    _________________________________________________________________
    max_pooling2d_3 (MaxPooling2 (None, 8, 8, 128)         0         
    _________________________________________________________________
    dropout_3 (Dropout)          (None, 8, 8, 128)         0         
    _________________________________________________________________
    flatten_1 (Flatten)          (None, 8192)              0         
    _________________________________________________________________
    dense_1 (Dense)              (None, 512)               4194816   
    _________________________________________________________________
    activation_7 (Activation)    (None, 512)               0         
    _________________________________________________________________
    batch_normalization_7 (Batch (None, 512)               2048      
    _________________________________________________________________
    dropout_4 (Dropout)          (None, 512)               0         
    _________________________________________________________________
    dense_2 (Dense)              (None, 1)                 513       
    _________________________________________________________________
    activation_8 (Activation)    (None, 1)                 0         
    =================================================================
    Total params: 4,624,897
    Trainable params: 4,622,785
    Non-trainable params: 2,112
    

    【讨论】:

    • @Jerel - 如果您的问题使用上述解决方案得到解决,请接受并投票,谢谢。
    • 感谢您的帮助。我做了你推荐的更改,但我收到了一个新错误ValueError: Error when checking input: expected conv2d_1_input to have shape (64, 64, 2) but got array with shape (64, 64, 3) 它看起来像在我收集和预处理我的图像的步骤中它正在创建一个 3 by array 而不是 2 by。 trainX 的形状仍然是 (1429, 64, 64, 3)。我该如何解决?再次感谢您。
    • @Jerel - 我已将您的完整代码与我的数据和上述修改一起使用。我没有遇到任何错误。不过,我正在使用模型摘要编辑答案。您可以比较一下,如果有什么不同,请告诉我。
    • 当我对模型进行总结时,除了第一行之外,所有信息都与您的信息相同:conv2d_1 (Conv2D) (None, 64, 64, 32) 608 我的trainX 的形状应该是(1429, 64, 64, 3) 还是@987654334 @?
    • 你的trainX 形状应该是(1429, 64, 64, 3),你应该给model = SmallVGGNet.build(width=64, height=64, depth=3, classes=len(lb.classes_)),如果你给深度为2,你会得到你得到的错误。
    猜你喜欢
    • 2019-05-09
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 2019-08-10
    • 2018-08-09
    • 2019-07-03
    • 1970-01-01
    相关资源
    最近更新 更多