So I have been following a tutorial about Machine learning and I have come to this point in the code:
JavaScript
x
34
34
1
from tensorflow.keras.models import Sequential
2
from tensorflow.keras.layers import Dense,Dropout,Activation, Flatten, Conv2D, MaxPooling2D
3
import pickle
4
import numpy as np
5
6
pickle_in = open("X.pickle","rb")
7
X = pickle.load(pickle_in)
8
9
pickle_in = open("y.pickle","rb")
10
y = pickle.load(pickle_in)
11
12
X=np.array(X/255.0)
13
y=np.array(y)
14
15
model = Sequential()
16
model.add(Conv2D(64, (3,3), input_shape = X.shape[1:]))
17
model.add(Activation("relu"))
18
model.add(MaxPooling2D(pool_size=(2,2)))
19
20
model.add(Conv2D(64, (3,3)))
21
model.add(Activation("relu"))
22
model.add(MaxPooling2D(pool_size=(2,2)))
23
24
model.add(Flatten())
25
model.add(Dense(64))
26
27
model.add(Dense(1))
28
model.add(Activation("sigmoid"))
29
30
model.compile(loss="binary_crossentropy",
31
optimizer="adam",
32
metrics=["accuracy"])
33
model.fit(X,y, batch_size=32, validation_split=0.1)
34
When I execute this code it gives me the following Error:
ValueError: Input 0 of layer conv2d_10 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 100, 100]
I have seen multiple posts about this and none have really helped me! Can anyone help?? Thanks in advance!! :)
Advertisement
Answer
Add a reshape since a conv2D layer expects (batch, x, y, channels)
, (ndim=4) but you are only providing it (batch, x, y)
, (ndim=3). Just reshape it to (batch, x, y, 1)
.
Error reads Full shape received: [None, 100, 100]
. What it expects is a 4D array [None, 100, 100, 1]
–
JavaScript
1
23
23
1
model = Sequential()
2
model.add(Reshape((100,100,1),input_shape=X.shape[1:]))
3
model.add(Conv2D(64, (3,3)))
4
model.add(Activation("relu"))
5
model.add(MaxPooling2D(pool_size=(2,2)))
6
7
model.add(Conv2D(64, (3,3)))
8
model.add(Activation("relu"))
9
model.add(MaxPooling2D(pool_size=(2,2)))
10
11
model.add(Flatten())
12
model.add(Dense(64))
13
14
model.add(Dense(1))
15
model.add(Activation("sigmoid"))
16
17
model.compile(loss="binary_crossentropy",
18
optimizer="adam",
19
metrics=["accuracy"])
20
21
22
model.summary()
23
JavaScript
1
31
31
1
Model: "sequential_5"
2
_________________________________________________________________
3
Layer (type) Output Shape Param #
4
=================================================================
5
reshape_5 (Reshape) (None, 100, 100, 1) 0
6
_________________________________________________________________
7
conv2d_6 (Conv2D) (None, 98, 98, 64) 640
8
_________________________________________________________________
9
activation_9 (Activation) (None, 98, 98, 64) 0
10
_________________________________________________________________
11
max_pooling2d_6 (MaxPooling2 (None, 49, 49, 64) 0
12
_________________________________________________________________
13
conv2d_7 (Conv2D) (None, 47, 47, 64) 36928
14
_________________________________________________________________
15
activation_10 (Activation) (None, 47, 47, 64) 0
16
_________________________________________________________________
17
max_pooling2d_7 (MaxPooling2 (None, 23, 23, 64) 0
18
_________________________________________________________________
19
flatten_3 (Flatten) (None, 33856) 0
20
_________________________________________________________________
21
dense_6 (Dense) (None, 64) 2166848
22
_________________________________________________________________
23
dense_7 (Dense) (None, 1) 65
24
_________________________________________________________________
25
activation_11 (Activation) (None, 1) 0
26
=================================================================
27
Total params: 2,204,481
28
Trainable params: 2,204,481
29
Non-trainable params: 0
30
_________________________________________________________________
31