1

我尝试制作一个卷积神经网络来对狗和猫进行分类。我在标题中提到了错误。

从我的搜索来看,有人说错误属于不同版本的tensorflow和keras库,也有人说这是语法错误。我会把我的代码留在这里,告诉我哪里出错了。

#IMPORTING LIBRARIES
import tensorflow as tf
import pandas as pd
import keras
from keras.preprocessing.image import ImageDataGenerator



#IMAGE DATA PREPROCESSING

#preprocessing the training set
train_datagen = ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)
training_set = train_datagen.flow_from_directory(
        directory = r"C:\Users\Cucu\Downloads\training_set",
        target_size=(64 , 64),
        batch_size=32,
        class_mode='binary')

#preprocessing the test set
test_datagen = ImageDataGenerator(rescale=1./255)
test_set = test_datagen.flow_from_directory(
        directory = r"C:\Users\Cucu\Downloads\test_set",
        target_size=(64 , 64),
        batch_size=32,
        class_mode='binary')


 
#BULDING THE CNN
#
#
#initialising the cnn
cnn = tf.keras.models.Sequential()


#convolution
cnn.add(tf.keras.Input(shape=(64, 64, 3)))
cnn.add(tf.keras.layers.Conv2D(filters = 32 , kernel_size = 3 , activation = 'relu' ))


#pooling
cnn.add(tf.keras.layers.MaxPool2D( pool_size = 2 , strides = 2))


#adding a SECOND CONVOLUTIONAL LAYER
cnn.add(tf.keras.layers.Conv2D(filters = 32 , kernel_size = 3 , activation = 'relu'))
cnn.add(tf.keras.layers.MaxPool2D( pool_size = 2 , strides = 2))


#flattening
cnn.add(tf.keras.layers.Flatten())


#full connection
cnn.add(tf.keras.layers.Dense(units = 128 , activation = 'relu'))


#adding the output layer
cnn.add(tf.keras.layers.Dense(units = 4 , activation = 'sigmoid'))




#TRAINING THE CNN
#
#
#compiling the cnn
cnn.compile(optimizer = 'adam' , loss = 'binary_crossentropy' , metrics = ['accuracy'] )


#training the cnn on the training_set & test_set
cnn.fit(x = training_set , validation_data = test_set , epochs = 25)



#MAKING A SINGLE PREDICTION
import numpy as np
test_image = image.load_img('dataset/single_predictioncat_or_dog_1.jpg' , test_size = (64 , 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image , axis = 0)
result = cnn.predic(test_image)
training_set.class_indices

错误是:

ValueError: logits and labels must have the same shape ((None, 4) vs (None, 1))
4

1 回答 1

6

更改最后一个密集层。而不是 4 使其成为 1。

#adding the output layer
cnn.add(tf.keras.layers.Dense(units = 1 , activation = 'sigmoid'))

这可能会发生,因为您的标签是每个输入的单个值。然而,您定义的最后一层有 4 个输出。损失函数无法比较两种不同的形状。

如果您仍想在最终密集层中添加多个单元,请将所有输出设为一个热向量,并在密集层中添加与数据集中标签一样多的单元。

于 2020-12-12T16:34:28.153 回答