如何使层顺序的输入0兼容?

2024-04-25 20:23:37 发布

您现在位置:Python中文网/ 问答频道 /正文

显然,这是一个常见的问题,我已经检查了大约10个问题及其答案,如:q1q2q3等。我按照答案进行了操作,但问题没有解决

我有一个数据集,由99张大小不同的图像组成。它们已通过cv2.imread读取并存储在ims中。标签存储在labels中。我的部分代码如下:

labels = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
          3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7,
          7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9]

first_sift = cv2.xfeatures2d.SIFT_create(contrastThreshold=0.02, sigma=1.35)

dictionarySize = 30
bow = cv2.BOWKMeansTrainer(dictionarySize)

for i in range(len(ims)):
    gray = cv2.cvtColor(ims[i], cv2.COLOR_RGB2GRAY)
    kp, des = first_sift.detectAndCompute(gray, None)

    bow.add(des)

d = bow.cluster()

FLANN_INDEX_KDTREE = 0
index_params = d(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = d(checks=50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
second_sift = cv2.xfeatures2d.SIFT_create()

extractor = cv2.BOWImgDescriptorExtractor(second_sift, cv2.BFMatcher(cv2.NORM_L2))
extractor.setVocabulary(d)

feats = []
for i in range(len(ims)):

    gray = cv2.cvtColor(ims[i], cv2.COLOR_RGB2GRAY)
    result = extractor.compute(gray, first_sift.detect(gray))
    feats.append(result)

num = len(ims)

X = np.array(feats).reshape(num, dictionarySize)
y = np.array(labels)

labels_num = np.max(y) + 1

X, y = shuffle(X, y)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)

X_train = (X_train - np.min(X)) / (np.max(X) - np.min(X))
X_test = (X_test - np.min(X)) / (np.max(X) - np.min(X))

y_train_encode = keras.utils.to_categorical(y_train, labels_num)
y_test_encode = keras.utils.to_categorical(y_test, labels_num)

model = Sequential()

model.add(Conv2D(32, (3, 3), padding="same", activation="relu", input_shape=(None, None, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.summary()

model.compile(loss='categorical_crossentropy', optimizer=Adam(), metrics=['accuracy'])

hist = model.fit(X_train, y_train_encode, batch_size=32, epochs=50, verbose=1,
                 validation_data=(X_test, y_test_encode))

以下是模型摘要:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, None, None, 32)    320       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, None, None, 32)    0         
=================================================================
Total params: 320
Trainable params: 320
Non-trainable params: 0
_________________________________________________________________

运行代码时,出现以下错误:

ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 30]

我还要补充:

X形为(99,30)

y形为(99,)

标签数量为10个

X_列车形状为(66,30)

X_测试。形状为(33,30)