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

John Jones :

I am trying to get my hands dirty with neural networks in practice, for such task i am trying to classify some images, where i'll have two classes basically. So, i took a CNN as an example using keras and tensorflow from tutorial on youtube.

I tried changing my output layer activation to sigmoid and when did it, i started getting the error:

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

Given specifically at the following line:

validation_steps = nb_validation_Samples // batch_size)

My neural network code:

Libraries

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
import numpy as np
from keras.preprocessing import image

Setup

img_width, img_height = 128, 160

train_data_dir = '/content/drive/My Drive/First-Group/Eyes/'
validation_data_dir = '/content/drive/My Drive/First-Validation-Group/'
nb_train_samples = 1300
nb_validation_Samples = 1300
epochs = 100
batch_size = 16


if K.image_data_format() == 'channels_first':
   input_shape = (3, img_width, img_height)
else:
   input_shape = (img_width, img_height, 3)

train_datagen = ImageDataGenerator(
    zoom_range=0.2,
)

test_datagen = ImageDataGenerator(rescale=1./255)


train_generator = train_datagen.flow_from_directory(
   train_data_dir,
   target_size=(img_width, img_height),
   batch_size=batch_size,
   class_mode='binary')


validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode="binary")



model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))


model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Dense(64))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.summary()

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


model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data = validation_generator,
error line -> **validation_steps = nb_validation_Samples // batch_size)**


model.save_weights('weights.npy')
Marco Cerliani :

the input of your network is 4d (batch_dim, height, width, channel), while your target is 2d (batch_dim, 1). you need something in your network to pass from 4d to 2d like flatten or global pooling. for example, you can add one of them after your last max-pooling layer.

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten()) #<========================
model.add(Dense(64))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))

the usage of binary_crossentropy as a loss with sigmoid and class_mode='binary' in generator seems ok if u are dealing with a binary classification problem

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Expected shape (None, 8) but got array with shape (8,1)

Tensorflow estimator ValueError: logits and labels must have the same shape ((?, 1) vs (?,))

Tensorflow Estimator logits and labels must have the same shape

ValueError: Error when checking input: expected gru_5_input to have shape (None, None, 10) but got array with shape (1, 4, 1)

Custom Loss Function: logits and targets must have the same shape ((?, 1) vs (45000,))

logits and labels must have the same first dimension, got logits shape [3662,5] and labels shape [18310]

"ValueError: Shapes (None, 1) and (None, 6) are incompatible"

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

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

ValueError: logits and labels must have the same shape ((1, 7, 7, 2) vs (1, 2))

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

"ValueError: ...incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 7, 169)"

ValueError: Error when checking target: expected activation_6 to have shape(None,2) but got array with shape (5760,1)

logits and labels must have the same first dimension, got logits shape [32,1] and labels shape [80000]

Keras ValueError: logits and labels must have the same shape ((None, 32, 17) vs (None, 17))

Model returning error - ValueError: logits and labels must have the same shape ((None, 18) vs (None, 1))

ValueError: logits and labels must have the same shape

LSTM error - 'logits and labels must have the same shape'

Keras Deep Learning ValueError: logits and labels must have the same shape ((None, 2) vs (None, 1))

text binary classification error:logits and labels must have the same shape

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

ValueError: expected axis -1 of input shape to have value 51948 but received input with shape (None, 52)

ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 6 but here input with shape (None, 8)

ValueError: logits and labels must have the same shape, but got shapes [2] and [2,1]

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

ValueError: `logits` and `labels` must have the same shape

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

Conv1D for classify non-image dataset show error ValueError : `logits` and `labels` must have the same shape

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

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive