0

I'm getting a list of images to train my CNN.

model = Sequential()
model.add(Dense(32, activation='tanh', input_dim=100))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

data, labels = ReadImages(TRAIN_DIR) 

# Train the model, iterating on the data in batches of 32 samples
model.fit(np.array(data), np.array(labels), epochs=10, batch_size=32)

But I faced this error:

'with shape ' + str(data_shape))

ValueError: Error when checking input: expected dense_1_input to have 2 dimensions, but got array with shape (391, 605, 700, 3)

0nroth1
  • 201
  • 2
  • 11

3 Answers3

0

This is no CNN. A Convolutional Neural Network is defined by having Conv Layer. Those Layers work with imput shapes in 4D (Batchsize, ImageDimX, ImageDimY, ColorChannels). The Dense Layers(aka. Fully connected) you are using 2D input (Batchsize, DataAsAVector)

ben
  • 1,380
  • 9
  • 14
0

You are feeding images to the Dense Layer. Either flatten the images using .flatten() or use a model with CNN Layers. The shape (391,605,700,3) means you have 391 images of size 605x700 having 3 dimensions(rgb).

    model = Sequential()
    model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', input_shape=(605, 700, 3)))
    model.add(MaxPooling2D((2, 2)))
    model.add(Flatten())
    model.add(Dense(100, activation='relu', kernel_initializer='he_uniform'))
    model.add(Dense(1, activation='sigmoid'))
    model.compile(optimizer='rmsprop',
          loss='binary_crossentropy',
          metrics=['accuracy'])

This link has good explanations for basic CNN.

Sharan
  • 691
  • 1
  • 7
  • 16
  • Why do you pass (3,3) in a conv2d and (2,2) in maxpooling ? – 0nroth1 Aug 20 '19 at 13:06
  • The parameters are from machinelearningmastery.com. I didnt modify anything which is why I gave the link also in the answer. (3,3) is the kernel size in the convolutional layer for feature extraction. (2,2) is for pooling which is used to reduce the computation. – Sharan Aug 20 '19 at 13:16
0

You need to first flatten the image if you want to pass the image directly to dense layers as Dense layer takes input in 2 dimensions only and since you are passing whole image there is 4 dimensions in it i.e. Number of images X Height X Width X Number of channels (391, 605, 700, 3). You are not actually doing any convolutions on the image. To do convolutions you need to add CNN layers after initialising the model as sequential. To add dense layer :

model = Sequential()
model.add(Flatten())
model.add(Dense(32, activation='tanh', input_dim=100))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

To add CNN layer and then flatten it :

 model = Sequential()
    model.add(Conv2D(input_shape=(605,700,3), filters=64, kernel_size=(3,3), 
    padding="same",activation="relu"))
    model.add(Flatten())
    model.add(Dense(32, activation='tanh', input_dim=100))
    model.add(Dense(1, activation='sigmoid'))
    model.compile(optimizer='rmsprop',
                  loss='binary_crossentropy',
                  metrics=['accuracy'])
  • Can you explain me how did you put these 'filters=64' and a kernel_size=(3,3) ? – 0nroth1 Aug 20 '19 at 16:43
  • After test your code I receive a follow error : elif x.ndim == 1: AttributeError: 'str' object has no attribute 'ndim' – 0nroth1 Aug 20 '19 at 16:46
  • Filter and kernal size can be whatever you want like filter can be anything 64,128,256,512. And kernal size (3,3) can be 3x3 5x5 – Rohit Thakur Aug 21 '19 at 08:57
  • I already fixed the above error, but now, when I run my code, in epoch 1, my RAM goes to the limit and nothing happens – 0nroth1 Aug 21 '19 at 11:44
  • After waiting, the console shows "return array(a, dtype, copy=False, order=order) ValueError: could not convert string to float: 'nonPdr'" – 0nroth1 Aug 21 '19 at 12:02
  • NonPdr is one of my img classes – 0nroth1 Aug 21 '19 at 12:02
  • https://stackoverflow.com/questions/57578845/valueerror-could-not-convert-string-to-float-nonpdr – 0nroth1 Aug 21 '19 at 13:53