我致力于在我的数据集中实现CNN
这是我的代码,通过整形过程获得x列和y列
Y_train = train["Label"]
X_train = train.drop(labels = ["Label"],axis = 1)
X_train.shape -> /*(230, 67500)*/
X_train = np.pad(X_train, ((0,0), (0, (67600-X_train.shape[1]))), 'constant').reshape(-1, 260, 260)
Y_train = to_categorical(Y_train, num_classes = 10)
在我完成了一些程序和重塑过程后,我将X_列和Y_列分开。下面是显示的代码
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=42)
print("x_train shape",X_train.shape)
print("x_test shape",X_val.shape)
print("y_train shape",Y_train.shape)
print("y_test shape",Y_val.shape)
结果定义如下
x_train shape (207, 260, 260)
x_test shape (23, 260, 260)
y_train shape (207, 10)
y_test shape (23, 10)
然后我创建CNN模型
model = Sequential()
#
model.add(Conv2D(filters = 8, kernel_size = (5,5),padding = 'Same',
activation ='relu', input_shape = (260, 260)))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))
#
model.add(Conv2D(filters = 16, kernel_size = (3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
model.add(Dropout(0.25))
# fully connected
model.add(Flatten())
model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
然后我使用ImageGenerator来使用数据预测
datagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # dimesion reduction
rotation_range=0.5, # randomly rotate images in the range 5 degrees
zoom_range = 0.5, # Randomly zoom image 5%
width_shift_range=0.5, # randomly shift images horizontally 5%
height_shift_range=0.5, # randomly shift images vertically 5%
horizontal_flip=False, # randomly flip images
vertical_flip=False) # randomly flip images
X_train = np.pad(X_train, ((0,0), (0, (67600-X_train.shape[1]))), 'constant').reshape(-1, 260, 260, 1)
datagen.fit(X_train)
然后它抛出一个错误,如下所示
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,2) and requested shape (3,2)
我怎样才能修好它
我认为问题在于
ImageDataGenerator
期望图像具有宽度、高度和颜色通道(最常见的是红色、绿色和蓝色的3个通道)。因为还有一个批量大小,所以它期望的整体形状是(batch size, width, height, channels)
。你的张量是260x260,但没有颜色通道。它们是灰度图像吗每the documentation:
因此,我认为您只需要在最后添加一个额外的维度来重塑您的输入
相关问题 更多 >
编程相关推荐