输入 0 与层 conv1d_3 不兼容:预期 ndim=3,发现 ndim=4

时间:2021-01-14 14:32:13

标签: keras

我能解决这个问题吗?

train_points.shape=(4096,3) train_labels.shape=(4096,2)

你好。我手上有 3D 屋顶模型,我将这些模型转换为点云数据。每个模型有 2048 个点(用于 x、y 和 z 坐标)。即(2048.3)。我还创建了一个标签矩阵。 (2048.1)。当我使用 conv1d 时,它给了我这个错误。我不明白问题出在哪里?

import numpy as np
import pandas as pd
import keras
import seaborn as sns
import matplotlib.pyplot as plt
import trimesh
import warnings
warnings.filterwarnings('ignore') #filter warnings

mesh=trimesh.load("m1.stl")
mesh.show()
point1 = mesh.sample(2048) #modelin üstündeki nokta sayısı

mesh=trimesh.load("m2.stl")
mesh.show()
point2 = mesh.sample(2048) #modelin üstündeki nokta sayısı



train_points=np.concatenate((point1,point2),axis=0)


fig = plt.figure(figsize=(15, 15))
ax = fig.add_subplot(222, projection="3d")
ax.scatter(point1[:, 0], point1[:, 1], point1[:, 2])
ax.set_axis_off()
plt.show()


fig = plt.figure(figsize=(15, 15))
ax = fig.add_subplot(222, projection="3d")
ax.scatter(point2[:, 0], point2[:, 1], point2[:, 2])
ax.set_axis_off()
plt.show()


labels=np.zeros((4096),dtype='int') #4096=örnek sayısı
labels[0:2048]=0
labels[2048:4096]=1

from keras.utils.np_utils import to_categorical # convert to one-hot-encoding
train_labels= to_categorical(labels,2) #2=örnek sınıf sayısı
np.save('etiket_matrisi.npy',train_labels)


from keras.models import Sequential
from keras.layers import Input, Dense, Activation, Dropout, Flatten
from keras.layers import Conv1D, MaxPool1D

#input_shape = keras.Input(shape=(4096, 3))
input_shape=(2,2048,3)

model = Sequential()

model.add(Conv1D(filters=8,kernel_size=3,input_shape=input_shape,padding="same"))
model.add(Activation("relu"))

model.add(Conv1D(filters=8,kernel_size=3,input_shape=input_shape,padding="same"))
model.add(Activation("relu"))

model.add(MaxPool1D(pool_size=2))

model.add(Dense(16))
model.add(Activation("relu"))
model.add(Dropout(0.25))

model.add(Dense(8))
model.add(Activation("relu"))
model.add(Dropout(0.25))


#fully connected
model.add(Flatten())
model.add(Dense(4, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(2, activation="softmax")) #çıktı katmanı(2 modelim var)

#Compile Model
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
model.summary


#fit the model

history=model.fit(train_points, train_labels, epochs=500, batch_size=250, verbose=1)

0 个答案:

没有答案