所以我一直在学习一个关于机器学习的教程,我在代码中已经达到了这一点:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Dropout,Activation, Flatten, Conv2D, MaxPooling2D
import pickle
import numpy as np
pickle_in = open("X.pickle","rb")
X = pickle.load(pickle_in)
pickle_in = open("y.pickle","rb")
y = pickle.load(pickle_in)
X=np.array(X/255.0)
y=np.array(y)
model = Sequential()
model.add(Conv2D(64, (3,3), input_shape = X.shape[1:]))
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(Dense(1))
model.add(Activation("sigmoid"))
model.compile(loss="binary_crossentropy",
optimizer="adam",
metrics=["accuracy"])
model.fit(X,y, batch_size=32, validation_split=0.1)
当我执行此代码时,它会给我以下错误:ValueError: Input 0 of layer conv2d_10 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 100, 100]
我看过很多关于这方面的帖子,但没有一个真正帮助过我!有人能帮忙吗??提前感谢!!:(
添加一个整形,因为conv2D层需要(batch, x, y, channels)
(ndim=4(,但您只提供了(batch, x, y)
(ndim=3(。只需将其重塑为(batch, x, y, 1)
即可。
读取Full shape received: [None, 100, 100]
时出错。它所期望的是一个4D阵列[None, 100, 100, 1]
-
model = Sequential()
model.add(Reshape((100,100,1),input_shape=X.shape[1:]))
model.add(Conv2D(64, (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(Dense(1))
model.add(Activation("sigmoid"))
model.compile(loss="binary_crossentropy",
optimizer="adam",
metrics=["accuracy"])
model.summary()
Model: "sequential_5"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
reshape_5 (Reshape) (None, 100, 100, 1) 0
_________________________________________________________________
conv2d_6 (Conv2D) (None, 98, 98, 64) 640
_________________________________________________________________
activation_9 (Activation) (None, 98, 98, 64) 0
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 49, 49, 64) 0
_________________________________________________________________
conv2d_7 (Conv2D) (None, 47, 47, 64) 36928
_________________________________________________________________
activation_10 (Activation) (None, 47, 47, 64) 0
_________________________________________________________________
max_pooling2d_7 (MaxPooling2 (None, 23, 23, 64) 0
_________________________________________________________________
flatten_3 (Flatten) (None, 33856) 0
_________________________________________________________________
dense_6 (Dense) (None, 64) 2166848
_________________________________________________________________
dense_7 (Dense) (None, 1) 65
_________________________________________________________________
activation_11 (Activation) (None, 1) 0
=================================================================
Total params: 2,204,481
Trainable params: 2,204,481
Non-trainable params: 0
_________________________________________________________________