简体   繁体   中英

How can I solve this error during running CNN model with Keras?

When running my CNN model, below are my code with Keras

import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.preprocessing.image import ImageDataGenerator,array_to_img, img_to_array, load_img
from tensorflow.keras.preprocessing import image
import matplotlib.pyplot as plt

train = ImageDataGenerator(
        rotation_range=90,    
        width_shift_range=0.2,  
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.3, 
        horizontal_flip = True, 
        vertical_flip = True,
        zca_whitening = True, 
        brightness_range=[0.2,1.2], 
        fill_mode='wrap')

test = ImageDataGenerator(rescale=1./255)

train_dataset = train.flow_from_directory("/content/drive/MyDrive/dataset",
                                          target_size=(256,256),
                                          batch_size = 32,
                                          class_mode = 'binary')
                                         
test_dataset = test.flow_from_directory("/content/drive/MyDrive/dataset",
                                          target_size=(256,256),
                                          batch_size = 32,
                                          class_mode = 'binary')

model = keras.Sequential()

# Convolutional layer and maxpool layer 1
model.add(keras.layers.Conv2D(64,(3,3),activation='relu',input_shape=(256,256,3)))
model.add(keras.layers.MaxPool2D(2,2))

# Convolutional layer and maxpool layer 2
model.add(keras.layers.Conv2D(128,(3,3),activation='relu'))
model.add(keras.layers.MaxPool2D(2,2))

# Convolutional layer and maxpool layer 3
model.add(keras.layers.Conv2D(256,(3,3),activation='relu'))
model.add(keras.layers.MaxPool2D(2,2))

# Flattening Operation
model.add(keras.layers.Flatten())

# Fully Connected layer
model.add(keras.layers.Dense(1024,activation='relu'))

## Output layer
model.add(keras.layers.Dense(10,activation='softmax'))  

model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])

train_imagesize = 327
batch_size = 32
epochs = 10 
steps_per_epoch = train_imagesize//batch_size

model.fit_generator(
         train_dataset,
         steps_per_epoch = steps_per_epoch,
         epochs = epochs,
         validation_data = test_dataset)

I get such errors below, which show the error from my validation_data = test_dataset line, but I do not really understand the meaning of logits and labels must have the same shape.


ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss
        y, y_pred, sample_weight, regularization_losses=self.losses)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
        losses = call_fn(y_true, y_pred)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call  **
        return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1932, in binary_crossentropy
        backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
    File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5247, in binary_crossentropy
        return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)

    ValueError: `logits` and `labels` must have the same shape, received ((None, 10) vs (None, 1)).

I have no idea how to solve this. Any help is appreciated.

Because this is a binary classification problem, the output layer should have 1 neuron(unit) and a sigmoid activation function. To overcome the error, replace the following line of code:

# Output layer
model.add(keras.layers.Dense(10,activation='softmax'))

with the following code:

# Output layer
model.add(keras.layers.Dense(1,activation='sigmoid'))

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