我正在为 mnist 数据集尝试 CNN 模型。训练模型后,它通过 model.evaluate 给出 99% 的测试准确率。但是当我尝试预测一张图像的答案时,它总是在我调用 model.predict() 时返回相同的数组。
规范化数据:
train_images = mnist_train_images.reshape(mnist_train_images.shape[0], 28, 28, 1)
test_images = mnist_test_images.reshape(mnist_test_images.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
train_images = train_images.astype('float32')
test_images = test_images.astype('float32')
train_images /= 255
test_images /= 255
#converting labels to one hot encoded format
train_labels = tensorflow.keras.utils.to_categorical(mnist_train_labels, 10)
test_labels = tensorflow.keras.utils.to_categorical(mnist_test_labels, 10)
模型结构和模型训练:
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
# 64 3x3 kernels
model.add(Conv2D(64, (3, 3), activation='relu'))
# Reduce by taking the max of each 2x2 block
model.add(MaxPooling2D(pool_size=(2, 2)))
# Dropout to avoid overfitting
model.add(Dropout(0.25))
# Flatten the results to one dimension for passing into our final layer
model.add(Flatten())
# A hidden layer to learn with
model.add(Dense(128, activation='relu'))
# Another dropout
model.add(Dropout(0.5))
# Final categorization from 0-9 with softmax
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
with tensorflow.device('/device:GPU:0'):
model.fit(train_images, train_labels,
batch_size=128,
epochs=7,
verbose=2,
validation_data=(test_images, test_labels))
这每次总是给出相同的 pred_array,当然是错误的。我尝试了类似问题的答案。例如,尝试增加时代,也有一个答案说要做
尝试了一切,但似乎无济于事。也许,我对图像的规范化是错误的,或者我可能犯了一些愚蠢的错误,因为我是处理图像和使用 CNN 的新手。请帮忙
繁华开满天机
相关分类